ANN: InteractiveCodeSearch.jl --- Interactively search Julia code


#1

Let me introduce my package InteractiveCodeSearch.jl. It helps you searching Julia code.

gif animation

Julia has @edit, @less, etc. which are very handy for reading the implementation of functions. However, you need to specify a “good enough” set of (type) parameters for them to find the location of the code.

Instead, InteractiveCodeSearch provides a few macros to interactively choose the code you want to read. Features:

  • Interactively choose a method signature before opening the code location in your editor.
  • Various ways to search methods, such as: by function name @search show, function call expression @search show(stdout, "hello"), function call signature @search show(::IO, ::String), module name @search Base, argument value @searchmethods 1, argument type @searchmethods ::Int, and return type @searchreturn Int.
  • Interactively search history. It works in IJulia as well.

It’s already registered so you can install it by:

]add InteractiveCodeSearch         # Julia ≥ 0.7
Pkg.add("InteractiveCodeSearch")   # Julia 0.6

EDIT: I forgot to mention that the most of the cool things are happening in the interactive matching program. I use peco (written in Go) and InteractiveCodeSearch is useful (e.g., it can handle @search Base) because peco is highly efficient.


My mental load using Julia is much higher than, e.g., in Python. How to reduce it?
#2

Wow, this works really well!


#3

I’m glad that you find it useful!


#4

I wasted some time thinking I need peco.jl to get this working, but now it works.

You think it would be possible to have a special key in the REPL that would trigger @search? For example ‘)’ .


#5

Ah, I guess README is not very clear about installation steps.

Yeah, that’s possible. I’ve done it in IPython.jl. Maybe it would be nice to have something context-aware like alt-e in Rebugger.jl.


#6

Putting this in ~/.julia/config/startup.jl seems to work:

using REPL
using REPL: LineEdit

@async begin
    # This is why we need https://github.com/JuliaLang/julia/pull/29896...
    for _ in 1:20
        try
            Base.active_repl.interface.modes[1].keymap_dict
            break
        catch
        end
        sleep(0.05)
    end

    repl = Base.active_repl
    repl isa REPL.LineEditREPL || return
    insert_search = function(s, _...)
        if isempty(s) || position(LineEdit.buffer(s)) == 0
            LineEdit.edit_insert(s, "@search ")
        else
            LineEdit.edit_insert(s, ')')
        end
    end
    new_keymap = Dict{Any,Any}(')' => insert_search)

    main_mode = repl.interface.modes[1]
    main_mode.keymap_dict = LineEdit.keymap_merge(main_mode.keymap_dict,
                                                  new_keymap)
end

#7

Hello,

Thanks for this great tool! It will be nice to have it integrated to REPL out of the box.

I’m using Visual Studio Code as my default editor but I get:

julia> @search show
[ Info: Opening options.jl:54
Unknown editor: no line number information passed.
The method is defined at line 54.

Is there a way to go directly to line number?

If Visual Studio Code can’t be supported for now maybe @davidanthoff can help or give some tips / advices to improve its support.

Maybe doc should provide information to set InteractiveCodeSearch.jl with an other editor (with go to line number supported).

Thanks again @tkf that’s a great contribution

Kind regards


#8

You might need to set the JULIA_EDITOR environment variable (e.g. add JULIA_EDITOR=code to your ~/.bashrc on Linux).


#9

As @tkoolen mentioned, setting environment variable JULIA_EDITOR, VISUAL or EDITOR should work. This should also make stdlib @edit work.

Behind the scene, as briefly noted in the README, default configuration for opening an editor is InteractiveCodeSearch.CONFIG.open = edit where edit is from the standard library InteractiveUtils (see: https://docs.julialang.org/en/v1/stdlib/InteractiveUtils/#InteractiveUtils.edit-Tuple{AbstractString,Integer}). If, for some reasons, you want to bypass stdlib edit, you can define your own function of the form function(file, line::Integer) and set it to InteractiveCodeSearch.CONFIG.open.