Skip to content

Commit a1166d8

Browse files
committed
fuzzy completions
1 parent 37406da commit a1166d8

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "1.23.0"
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
77
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
88
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
9+
FuzzyCompletions = "fb4132e2-a121-4a70-b8a1-d5b831dcdcc2"
910
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1011
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1112
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"

deps/kspec.jl

+8-2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ end
4949
installkernel(name::AbstractString, options::AbstractString...;
5050
julia::Cmd,
5151
specname::AbstractString,
52-
env=Dict())
52+
fuzzy_completion_mode::Bool=false
53+
env::Dict{<:AbstractString}=Dict())
5354
5455
Install a new Julia kernel, where the given `options` are passed to the `julia`
5556
executable, the user-visible kernel name is given by `name` followed by the
5657
Julia version, and the `env` dictionary is added to the environment.
58+
If `fuzzy_completion_mode` is `true`, then <kbd>shift-tab</kbd> completions will be done fuzzily.
5759
5860
The new kernel name is returned by `installkernel`. For example:
5961
```
@@ -85,7 +87,9 @@ installkernel(
8587
function installkernel(name::AbstractString, julia_options::AbstractString...;
8688
julia::Cmd = `$(joinpath(Sys.BINDIR,exe("julia")))`,
8789
specname::AbstractString = replace(lowercase(name), " "=>"-"),
88-
env::Dict{<:AbstractString}=Dict{String,Any}())
90+
fuzzy_completion_mode::Bool = false,
91+
env::Dict{<:AbstractString} = Dict{String,Any}(),
92+
)
8993
# Is IJulia being built from a debug build? If so, add "debug" to the description.
9094
debugdesc = ccall(:jl_is_debugbuild,Cint,())==1 ? "-debug" : ""
9195

@@ -100,6 +104,8 @@ function installkernel(name::AbstractString, julia_options::AbstractString...;
100104
ijulia_dir = get(ENV, "IJULIA_DIR", dirname(@__DIR__)) # support non-Pkg IJulia installs
101105
append!(kernelcmd_array, [joinpath(ijulia_dir,"src","kernel.jl"), "{connection_file}"])
102106

107+
push!(env, "IJULIA_COMPLETION_MODE" => fuzzy_completion_mode ? "fuzzy" : "repl")
108+
103109
ks = Dict(
104110
"argv" => kernelcmd_array,
105111
"display_name" => name * " " * Base.VERSION_STRING * debugdesc,

src/handlers.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ind2chr(m::Msg, str::String, i::Integer) = i == 0 ? 0 :
5050

5151
import REPL: REPLCompletions
5252
import REPL.REPLCompletions: sorted_keywords, emoji_symbols, latex_symbols
53+
import FuzzyCompletions
5354

5455
complete_type(::Type{<:Function}) = "function"
5556
complete_type(::Type{<:Type}) = "type"
@@ -123,8 +124,12 @@ function complete_request(socket, msg)
123124
end
124125

125126
codestart = find_parsestart(code, cursorpos)
126-
comps_, positions = REPLCompletions.completions(code[codestart:end], cursorpos-codestart+1, current_module[])
127-
comps = unique!(REPLCompletions.completion_text.(comps_)) # julia#26930
127+
completion_mode = get(ENV, "IJULIA_COMPLETION_MODE", "repl")
128+
completions, completion_text = completion_mode == "fuzzy" ? (FuzzyCompletions.completions, FuzzyCompletions.completion_text) :
129+
(REPLCompletions.completions, REPLCompletions.completion_text)
130+
comps_, positions = completions(code[codestart:end], cursorpos-codestart+1, current_module[])
131+
completion_mode == "fuzzy" && filter!((0)FuzzyCompletions.score, comps_) # cut off too many candidates, they would be useless in most jupyter frontends
132+
comps = unique!(completion_text.(comps_)) # julia#26930
128133
# positions = positions .+ (codestart - 1) on Julia 0.7
129134
positions = (first(positions) + codestart - 1):(last(positions) + codestart - 1)
130135
metadata = Dict()

0 commit comments

Comments
 (0)