diff --git a/Project.toml b/Project.toml index 2eca3ca..1a91c29 100644 --- a/Project.toml +++ b/Project.toml @@ -6,6 +6,7 @@ version = "0.3.3-DEV" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" +peco_jll = "b89f85f3-45f8-5ea8-8223-9dfa39faf294" [compat] julia = "= 0.7, 1.0" diff --git a/src/InteractiveCodeSearch.jl b/src/InteractiveCodeSearch.jl index 72fc063..60d3311 100644 --- a/src/InteractiveCodeSearch.jl +++ b/src/InteractiveCodeSearch.jl @@ -47,12 +47,6 @@ using Base using Base: IOError using InteractiveUtils: edit, gen_call_with_extracted_types, methodswith -function _readandwrite(cmds) - processes = open(cmds, "r+") - return (processes.out, processes.in, processes) -end - - abstract type SearchPolicy end struct Shallow <: SearchPolicy end struct Recursive <: SearchPolicy end @@ -60,10 +54,27 @@ struct Recursive <: SearchPolicy end mutable struct SearchConfig # CONFIG open - interactive_matcher::Cmd + interactive_matcher::Function auto_open::Bool end +function setmatcher!(cmd::Function, obj::SearchConfig) + obj.interactive_matcher = cmd + return +end + +function setmatcher!(cmd::Cmd, obj::SearchConfig) + setmatcher!(convertCmd(cmd), obj) +end + +function convertCmd(cmd) + maybe_warn_matcher(cmd) + return function (f) + maybe_warn_matcher(cmd) + f(cmd) + end +end + maybe_identifier(s) = !startswith(string(s), "#") is_locatable(::Any) = false @@ -147,6 +158,12 @@ function find_source_file(file) return path end +function _readandwrite(matcher) + proc = matcher() do cmd + open(`$cmd`, "r+") + end + return (proc.out, proc.in, proc) +end """ read_stdout(input::AbstractString, cmd) @@ -182,7 +199,6 @@ function parse_loc(line) end function run_matcher(input) - maybe_warn_matcher() return String(read_stdout(input, CONFIG.interactive_matcher)) end @@ -265,8 +281,7 @@ InteractiveCodeSearch.CONFIG.auto_open = false # open matcher even when there ## Using InteractiveCodeSearch.jl by default -Put the following code in your `~/.julia/config/startup.jl` (≥ Julia 0.7) -or `~/.juliarc.jl` (Julia 0.6): +Put the following code in your `~/.julia/config/startup.jl`: ```julia using InteractiveCodeSearch @@ -275,7 +290,7 @@ using InteractiveCodeSearch """ const CONFIG = SearchConfig( edit, # open - `peco`, # interactive_matcher + x->error("uninitialized"), # interactive_matcher true, # auto_open ) @@ -437,10 +452,23 @@ macro searchmethods(x) end end -const preferred_terminal = Cmd[ - `peco`, - `percol`, -] +################################################################################ +# matcher binaries # +################################################################################ + +@static if VERSION write(io, "egg"), `cat`))) == "egg" + @test strip(String(read_stdout("spam", convertCmd(`cat`)))) == "spam" + @test strip(String(read_stdout(io -> write(io, "egg"), convertCmd(`cat`)))) == "egg" end @testset "parse_loc" begin @@ -70,7 +70,7 @@ end with_config( open = (_...) -> error("must not be called"), - interactive_matcher = `echo " at test.jl:249"`, + interactive_matcher = convertCmd(`echo " at test.jl:249"`), auto_open = true, ) do # when function has only one method, `auto_open` has to kick-in: @@ -91,7 +91,7 @@ end with_config( open = (_...) -> error("must not be called"), - interactive_matcher = `echo " at test.jl:249"`, + interactive_matcher = convertCmd(`echo " at test.jl:249"`), auto_open = false, ) do # When `auto_open = false`, the matcher has to be called @@ -127,7 +127,7 @@ end dummy_openline(args...) = push!(open_args, args) with_config( - interactive_matcher = `echo " at test.jl:249"`, + interactive_matcher = convertCmd(`echo " at test.jl:249"`), open = dummy_openline, auto_open = false, ) do @@ -149,7 +149,7 @@ end end with_config( - interactive_matcher = `true`, + interactive_matcher = convertCmd(`true`), open = (args...) -> error("open must not be called"), auto_open = false, ) do