Skip to content

Commit

Permalink
Skip matcher when there is only one candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Apr 6, 2018
1 parent 5393ef4 commit 8239ca1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ InteractiveCodeSearch.CONFIG.interactive_matcher = `peco` # default
InteractiveCodeSearch.CONFIG.interactive_matcher = `percol`
InteractiveCodeSearch.CONFIG.open = edit # default
InteractiveCodeSearch.CONFIG.open = less # use Base.less to read code
InteractiveCodeSearch.CONFIG.auto_open = true # default
InteractiveCodeSearch.CONFIG.auto_open = false # open matcher even when there
# are only one candidate
```


Expand Down
8 changes: 7 additions & 1 deletion src/InteractiveCodeSearch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Base: find_source_file
mutable struct SearchConfig # CONFIG
open
interactive_matcher
auto_open
end

is_identifier(s) = ismatch(r"^@?[a-z_]+$"i, string(s))
Expand Down Expand Up @@ -52,6 +53,10 @@ end
run_matcher(input) = String(read_stdout(CONFIG.interactive_matcher, input))

function choose_method(methods)
if CONFIG.auto_open && length(methods) == 1
m = first(methods)
return string(m.file), m.line
end
out = run_matcher(join(map(string, methods), "\n"))
if isempty(out)
return
Expand All @@ -65,7 +70,7 @@ function run_open(path, lineno)
end

maybe_open(::Void) = nothing
maybe_open(x::Tuple{String, Int}) = run_open(x...)
maybe_open(x::Tuple{String, Integer}) = run_open(x...)

search_methods(methods) = maybe_open(choose_method(methods))

Expand All @@ -82,6 +87,7 @@ end
const CONFIG = SearchConfig(
edit, # open
`peco`, # interactive_matcher
true, # auto_open
)

isline(::Any) = false
Expand Down
34 changes: 33 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestInteractiveCodeSearch

using InteractiveCodeSearch
using InteractiveCodeSearch: list_locatables, module_methods,
using InteractiveCodeSearch: list_locatables, module_methods, choose_method,
read_stdout, parse_loc, single_macrocall
using Base: find_source_file
using Base.Test
Expand Down Expand Up @@ -56,6 +56,37 @@ end
@test_nothrow module_methods(Base.Filesystem)
end

@testset "choose_method" begin
single_method = module_methods # has only one method

with_config(
open = (_...) -> error("must not be called"),
interactive_matcher = `echo " at test.jl:249"`,
auto_open = true,
) do
# when function has only one method, `auto_open` has to kick-in:
path, line = choose_method(methods(single_method))
@test path isa String
@test path != "test.jl"
@test line isa Integer
@test line != 249

# `code_search` has several methods, so the matcher has to be called:
path, line = choose_method(methods(InteractiveCodeSearch.code_search))
@test (path, line) == ("test.jl", 249)
end

with_config(
open = (_...) -> error("must not be called"),
interactive_matcher = `echo " at test.jl:249"`,
auto_open = false,
) do
# When `auto_open = false`, the matcher has to be called
path, line = choose_method(methods(single_method))
@test (path, line) == ("test.jl", 249)
end
end

@testset "single_macrocall" begin
@test single_macrocall(:(@search)) == Symbol("@search")
@test single_macrocall(quote @search end) == Symbol("@search")
Expand All @@ -70,6 +101,7 @@ end
with_config(
interactive_matcher = `echo " at test.jl:249"`,
open = dummy_openline,
auto_open = false,
) do
@test_nothrow @eval @search read_stdout
@test_nothrow @eval @search @search
Expand Down

0 comments on commit 8239ca1

Please sign in to comment.