From 9bcee64980a3335deddc95ee323a0e766d5129c6 Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Sun, 27 Nov 2022 11:40:20 +0800 Subject: [PATCH 1/3] add howtotype hover --- src/languageserverinstance.jl | 36 ++++++++++++++++++++++++++--------- src/requests/hover.jl | 14 +++++++++++--- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/languageserverinstance.jl b/src/languageserverinstance.jl index 751e9e38..a755e843 100644 --- a/src/languageserverinstance.jl +++ b/src/languageserverinstance.jl @@ -66,14 +66,16 @@ mutable struct LanguageServerInstance workspace::JuliaWorkspace - function LanguageServerInstance(pipe_in, pipe_out, env_path="", depot_path="", err_handler=nothing, symserver_store_path=nothing, download=true, symbolcache_upstream = nothing) + howtotype_cache::Dict{String,String} + + function LanguageServerInstance(pipe_in, pipe_out, env_path="", depot_path="", err_handler=nothing, symserver_store_path=nothing, download=true, symbolcache_upstream=nothing) new( JSONRPC.JSONRPCEndpoint(pipe_in, pipe_out, err_handler), Set{String}(), Dict{URI,Document}(), env_path, depot_path, - SymbolServer.SymbolServerInstance(depot_path, symserver_store_path; symbolcache_upstream = symbolcache_upstream), + SymbolServer.SymbolServerInstance(depot_path, symserver_store_path; symbolcache_upstream=symbolcache_upstream), Channel(Inf), StaticLint.ExternalEnv(deepcopy(SymbolServer.stdlibs), SymbolServer.collect_extended_methods(SymbolServer.stdlibs), collect(keys(SymbolServer.stdlibs))), Dict(), @@ -95,7 +97,8 @@ mutable struct LanguageServerInstance missing, missing, false, - JuliaWorkspace() + JuliaWorkspace(), + howtotypeCache(), ) end end @@ -184,7 +187,7 @@ function trigger_symbolstore_reload(server::LanguageServerInstance) ssi_ret, payload = SymbolServer.getstore( server.symbol_server, server.env_path, - function (msg, percentage = missing) + function (msg, percentage=missing) if server.clientcapability_window_workdoneprogress && server.current_symserver_progress_token !== nothing msg = ismissing(percentage) ? msg : string(msg, " ($percentage%)") JSONRPC.send( @@ -198,7 +201,7 @@ function trigger_symbolstore_reload(server::LanguageServerInstance) end end, server.err_handler, - download = server.symserver_use_download + download=server.symserver_use_download ) server.number_of_outstanding_symserver_requests -= 1 @@ -286,7 +289,7 @@ function Base.run(server::LanguageServerInstance) @debug "LS: Starting client listener task." while true msg = JSONRPC.get_next_message(server.jr_endpoint) - put!(server.combined_msg_queue, (type = :clientmsg, msg = msg)) + put!(server.combined_msg_queue, (type=:clientmsg, msg=msg)) end catch err bt = catch_backtrace() @@ -299,7 +302,7 @@ function Base.run(server::LanguageServerInstance) end finally if isopen(server.combined_msg_queue) - put!(server.combined_msg_queue, (type = :close,)) + put!(server.combined_msg_queue, (type=:close,)) close(server.combined_msg_queue) end @debug "LS: Client listener task done." @@ -309,7 +312,7 @@ function Base.run(server::LanguageServerInstance) @debug "LS: Starting symbol server listener task." while true msg = take!(server.symbol_results_channel) - put!(server.combined_msg_queue, (type = :symservmsg, msg = msg)) + put!(server.combined_msg_queue, (type=:symservmsg, msg=msg)) end catch err bt = catch_backtrace() @@ -322,7 +325,7 @@ function Base.run(server::LanguageServerInstance) end finally if isopen(server.combined_msg_queue) - put!(server.combined_msg_queue, (type = :close,)) + put!(server.combined_msg_queue, (type=:close,)) close(server.combined_msg_queue) end @debug "LS: Symbol server listener task done." @@ -430,3 +433,18 @@ function relintserver(server) lint!(doc, server) end end + +function howtotypeCache() + tcache = Dict{String,String}() + for (k, v) in REPL.REPLCompletions.latex_symbols + tcache[v] = k + end + for (k, v) in REPL.REPLCompletions.emoji_symbols + tcache[v] = k + end + + for (k, v) in REPL.REPLCompletions.symbols_latex_canonical + tcache[k] = v + end + tcache +end \ No newline at end of file diff --git a/src/requests/hover.jl b/src/requests/hover.jl index ac73626a..a61ae0e2 100644 --- a/src/requests/hover.jl +++ b/src/requests/hover.jl @@ -23,11 +23,19 @@ function get_hover(x::EXPR, documentation::String, server) else documentation end + if !isnothing(x.val) + estr = string(x.val) + if length(estr) == 1 + if haskey(server.howtotype_cache, estr) + documentation *= "\nHow to type $estr: $(server.howtotype_cache[estr])" + end + end + end end return documentation end -function get_tooltip(b::StaticLint.Binding, documentation::String, server; show_definition = false) +function get_tooltip(b::StaticLint.Binding, documentation::String, server; show_definition=false) if b.val isa StaticLint.Binding documentation = get_hover(b.val, documentation, server) elseif b.val isa EXPR @@ -75,7 +83,7 @@ function get_tooltip(b::StaticLint.Binding, documentation::String, server; show_ return documentation end -get_hover(b::StaticLint.Binding, documentation::String, server) = get_tooltip(b, documentation, server; show_definition = true) +get_hover(b::StaticLint.Binding, documentation::String, server) = get_tooltip(b, documentation, server; show_definition=true) get_typed_definition(b) = _completion_type(b) get_typed_definition(b::StaticLint.Binding) = @@ -194,7 +202,7 @@ function get_preceding_docs(expr::EXPR, documentation) end end -ensure_ends_with(s, c = "\n") = endswith(s, c) ? s : string(s, c) +ensure_ends_with(s, c="\n") = endswith(s, c) ? s : string(s, c) binding_has_preceding_docs(b::StaticLint.Binding) = expr_has_preceding_docs(b.val) From 6a4475d4a2728d844873752344b47cf764e9ea9f Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Sun, 27 Nov 2022 11:55:15 +0800 Subject: [PATCH 2/3] lazy loading cache --- src/languageserverinstance.jl | 15 +++++++++++++-- src/requests/hover.jl | 5 +++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/languageserverinstance.jl b/src/languageserverinstance.jl index a755e843..072ddfa7 100644 --- a/src/languageserverinstance.jl +++ b/src/languageserverinstance.jl @@ -66,7 +66,7 @@ mutable struct LanguageServerInstance workspace::JuliaWorkspace - howtotype_cache::Dict{String,String} + howtotype_cache::Union{Nothing,Dict{String,String}} function LanguageServerInstance(pipe_in, pipe_out, env_path="", depot_path="", err_handler=nothing, symserver_store_path=nothing, download=true, symbolcache_upstream=nothing) new( @@ -98,7 +98,7 @@ mutable struct LanguageServerInstance missing, false, JuliaWorkspace(), - howtotypeCache(), + nothing, ) end end @@ -447,4 +447,15 @@ function howtotypeCache() tcache[k] = v end tcache +end + +function findHowtotype(server::LanguageServerInstance, estr::String) + if isnothing(server.howtotype_cache) + server.howtotype_cache = howtotypeCache() + end + if haskey(server.howtotype_cache, estr) + return server.howtotype_cache[estr] + else + return nothing + end end \ No newline at end of file diff --git a/src/requests/hover.jl b/src/requests/hover.jl index a61ae0e2..807e6fc2 100644 --- a/src/requests/hover.jl +++ b/src/requests/hover.jl @@ -26,8 +26,9 @@ function get_hover(x::EXPR, documentation::String, server) if !isnothing(x.val) estr = string(x.val) if length(estr) == 1 - if haskey(server.howtotype_cache, estr) - documentation *= "\nHow to type $estr: $(server.howtotype_cache[estr])" + howtotype = findHowtotype(server, estr) + if !isnothing(howtotype) + documentation *= "\nHow to type $estr: $howtotype" end end end From 402909f65d6ab0167c1f98660f492dc67234dfb1 Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Sun, 27 Nov 2022 12:26:48 +0800 Subject: [PATCH 3/3] :bug: check def --- src/languageserverinstance.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/languageserverinstance.jl b/src/languageserverinstance.jl index 072ddfa7..9d12d37c 100644 --- a/src/languageserverinstance.jl +++ b/src/languageserverinstance.jl @@ -442,9 +442,10 @@ function howtotypeCache() for (k, v) in REPL.REPLCompletions.emoji_symbols tcache[v] = k end - - for (k, v) in REPL.REPLCompletions.symbols_latex_canonical - tcache[k] = v + if isdefined(REPL.REPLCompletions, :symbols_latex_canonical) + for (k, v) in REPL.REPLCompletions.symbols_latex_canonical + tcache[k] = v + end end tcache end