From 8239ca122c495cc9b21b770a841c5f41c1532c21 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 5 Apr 2018 19:01:40 -0700 Subject: [PATCH] Skip matcher when there is only one candidate --- README.md | 3 +++ src/InteractiveCodeSearch.jl | 8 +++++++- test/runtests.jl | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d07fd85..11bb8f4 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/src/InteractiveCodeSearch.jl b/src/InteractiveCodeSearch.jl index d962251..6d2b4ae 100644 --- a/src/InteractiveCodeSearch.jl +++ b/src/InteractiveCodeSearch.jl @@ -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)) @@ -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 @@ -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)) @@ -82,6 +87,7 @@ end const CONFIG = SearchConfig( edit, # open `peco`, # interactive_matcher + true, # auto_open ) isline(::Any) = false diff --git a/test/runtests.jl b/test/runtests.jl index ea56691..747bbd4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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") @@ -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