Skip to content

Commit c9fed96

Browse files
authored
Merge pull request #17 from JuliaPluto/feature-flags
Kwargs `enable_` for feature flags
2 parents 7888685 + 463fbeb commit c9fed96

File tree

1 file changed

+63
-46
lines changed

1 file changed

+63
-46
lines changed

src/FuzzyCompletions.jl

+63-46
Original file line numberDiff line numberDiff line change
@@ -601,60 +601,74 @@ end
601601

602602
end # @static if isdefined(Base, :TOML)
603603

604-
function completions(string::String, pos::Int, context_module::Module = Main, shift::Bool=true)
604+
function completions(string::String, pos::Int, context_module::Module = Main, shift::Bool=true;
605+
enable_questionmark_methods::Bool = true,
606+
enable_dict_keys::Bool = true,
607+
enable_expanduser::Bool = true,
608+
enable_path::Bool = true,
609+
enable_bslash::Bool = true,
610+
enable_methods::Bool = true,
611+
enable_keyword_argument::Bool = true,
612+
enable_packages::Bool = true,
613+
)
605614
# First parse everything up to the current position
606615
partial = string[1:pos]
607616
inc_tag = Base.incomplete_tag(Meta.parse(partial, raise=false, depwarn=false))
608617

609-
# ?(x, y)TAB lists methods you can call with these objects
610-
# ?(x, y TAB lists methods that take these objects as the first two arguments
611-
# MyModule.?(x, y)TAB restricts the search to names in MyModule
612-
rexm = match(r"(\w+\.|)\?\((.*)$", partial)
613-
if rexm !== nothing
614-
# Get the module scope
615-
if isempty(rexm.captures[1])
616-
callee_module = context_module
617-
else
618-
modname = Symbol(rexm.captures[1][1:end-1])
619-
if isdefined(context_module, modname)
620-
callee_module = getfield(context_module, modname)
621-
if !isa(callee_module, Module)
618+
if enable_questionmark_methods
619+
# ?(x, y)TAB lists methods you can call with these objects
620+
# ?(x, y TAB lists methods that take these objects as the first two arguments
621+
# MyModule.?(x, y)TAB restricts the search to names in MyModule
622+
rexm = match(r"(\w+\.|)\?\((.*)$", partial)
623+
if rexm !== nothing
624+
# Get the module scope
625+
if isempty(rexm.captures[1])
626+
callee_module = context_module
627+
else
628+
modname = Symbol(rexm.captures[1][1:end-1])
629+
if isdefined(context_module, modname)
630+
callee_module = getfield(context_module, modname)
631+
if !isa(callee_module, Module)
632+
callee_module = context_module
633+
end
634+
else
622635
callee_module = context_module
623636
end
624-
else
625-
callee_module = context_module
626637
end
627-
end
628-
moreargs = !endswith(rexm.captures[2], ')')
629-
callstr = "_(" * rexm.captures[2]
630-
if moreargs
631-
callstr *= ')'
632-
end
633-
ex_org = Meta.parse(callstr, raise=false, depwarn=false)
634-
if isa(ex_org, Expr)
635-
return complete_any_methods(ex_org, callee_module::Module, context_module, moreargs, shift), (0:length(rexm.captures[1])+1) .+ rexm.offset, false
638+
moreargs = !endswith(rexm.captures[2], ')')
639+
callstr = "_(" * rexm.captures[2]
640+
if moreargs
641+
callstr *= ')'
642+
end
643+
ex_org = Meta.parse(callstr, raise=false, depwarn=false)
644+
if isa(ex_org, Expr)
645+
return complete_any_methods(ex_org, callee_module::Module, context_module, moreargs, shift), (0:length(rexm.captures[1])+1) .+ rexm.offset, false
646+
end
636647
end
637648
end
638649

639-
# if completing a key in a Dict
640-
identifier, partial_key, loc = dict_identifier_key(partial,inc_tag, context_module)
641-
if identifier !== nothing
642-
matches = find_dict_matches(identifier)::Vector{String}
643-
length(matches)==1 && (lastindex(string) <= pos || string[nextind(string,pos)] != ']') && (matches[1]*=']')
644-
if length(matches)>0
645-
suggestions = Completion[DictCompletion(identifier, match, partial_key) for match in matches]
646-
return sort_suggestions!(suggestions), loc:pos, false
650+
if enable_dict_keys
651+
identifier, partial_key, loc = dict_identifier_key(partial,inc_tag, context_module)
652+
if identifier !== nothing
653+
matches = find_dict_matches(identifier)::Vector{String}
654+
length(matches)==1 && (lastindex(string) <= pos || string[nextind(string,pos)] != ']') && (matches[1]*=']')
655+
if length(matches)>0
656+
suggestions = Completion[DictCompletion(identifier, match, partial_key) for match in matches]
657+
return sort_suggestions!(suggestions), loc:pos, false
658+
end
647659
end
648660
end
649661

650662
# otherwise...
651-
if inc_tag in [:cmd, :string]
663+
if enable_path && inc_tag in [:cmd, :string]
652664
m = match(r"[\t\n\r\"`><=*?|]| (?!\\)", reverse(partial))
653665
startpos = nextind(partial, reverseind(partial, m.offset))
654666
r = startpos:pos
655667

656-
expanded = complete_expanduser(replace(string[r], r"\\ " => " "), r)
657-
expanded[3] && return expanded # If user expansion available, return it
668+
if enable_expanduser
669+
expanded = complete_expanduser(replace(string[r], r"\\ " => " "), r)
670+
expanded[3] && return expanded # If user expansion available, return it
671+
end
658672

659673
paths, r, success = complete_path(replace(string[r], r"\\ " => " "), pos)
660674

@@ -674,12 +688,15 @@ function completions(string::String, pos::Int, context_module::Module = Main, sh
674688
(success || inc_tag === :cmd) && return paths, r, success
675689
end
676690

677-
ok, ret = bslash_completions(string, pos)
678-
ok && return ret
691+
if enable_bslash
692+
ok, ret = bslash_completions(string, pos)
693+
ok && return ret
694+
end
679695

680696
# Make sure that only bslash_completions is working on strings
681-
inc_tag==:string && return Completion[], 0:-1, false
682-
if @static VERSION v"1.9.0-DEV.1034" && inc_tag === :other
697+
inc_tag === :string && return Completion[], 0:-1, false
698+
699+
if @static VERSION v"1.9.0-DEV.1034" && enable_methods && inc_tag === :other
683700
frange, ex, wordrange, method_name_end = identify_possible_method_completion(partial, pos)
684701
if last(frange) != -1 && all(isspace, @view partial[wordrange]) # no last argument to complete
685702
if ex.head === :call
@@ -688,7 +705,7 @@ function completions(string::String, pos::Int, context_module::Module = Main, sh
688705
return complete_methods(ex, context_module, shift), first(frange):(method_name_end - 1), false
689706
end
690707
end
691-
elseif @static VERSION < v"1.9.0-DEV.1034" && inc_tag === :other && should_method_complete(partial)
708+
elseif @static VERSION < v"1.9.0-DEV.1034" && enable_methods && inc_tag === :other && should_method_complete(partial)
692709
frange, method_name_end = find_start_brace(partial)
693710
# strip preceding ! operator
694711
s = replace(partial[frange], r"\!+([^=\(]+)" => s"\1")
@@ -708,10 +725,10 @@ function completions(string::String, pos::Int, context_module::Module = Main, sh
708725
return Completion[], 0:-1, false
709726
end
710727

711-
@static if VERSION v"1.9.0-DEV.1034"
712-
# Check whether we can complete a keyword argument in a function call
713-
kwarg_completion, wordrange = complete_keyword_argument(partial, pos, context_module)
714-
isempty(wordrange) || return kwarg_completion, wordrange, !isempty(kwarg_completion)
728+
enable_keyword_argument && @static if VERSION v"1.9.0-DEV.1034"
729+
# Check whether we can complete a keyword argument in a function call
730+
kwarg_completion, wordrange = complete_keyword_argument(partial, pos, context_module)
731+
isempty(wordrange) || return kwarg_completion, wordrange, !isempty(kwarg_completion)
715732
end
716733

717734
dotpos = something(findprev(isequal('.'), string, pos), 0)
@@ -724,7 +741,7 @@ function completions(string::String, pos::Int, context_module::Module = Main, sh
724741
ffunc = (mod,x)->true
725742
suggestions = Completion[]
726743
comp_keywords = true
727-
if afterusing(string, startpos)
744+
if enable_packages && afterusing(string, startpos)
728745
# We're right after using or import. Let's look only for packages
729746
# and modules we can reach from here
730747

0 commit comments

Comments
 (0)