Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add howtotype hover #1194

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions src/languageserverinstance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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::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(
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(),
Expand All @@ -95,7 +97,8 @@ mutable struct LanguageServerInstance
missing,
missing,
false,
JuliaWorkspace()
JuliaWorkspace(),
nothing,
)
end
end
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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."
Expand All @@ -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()
Expand All @@ -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."
Expand Down Expand Up @@ -430,3 +433,30 @@ 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
if isdefined(REPL.REPLCompletions, :symbols_latex_canonical)
for (k, v) in REPL.REPLCompletions.symbols_latex_canonical
tcache[k] = v
end
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
15 changes: 12 additions & 3 deletions src/requests/hover.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ function get_hover(x::EXPR, documentation::String, server)
else
documentation
end
if !isnothing(x.val)
estr = string(x.val)
if length(estr) == 1
howtotype = findHowtotype(server, estr)
if !isnothing(howtotype)
documentation *= "\nHow to type $estr: $howtotype"
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
Expand Down Expand Up @@ -75,7 +84,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) =
Expand Down Expand Up @@ -194,7 +203,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)

Expand Down