Skip to content

Commit

Permalink
Merge pull request #789 from julia-vscode/avi/fixmoredoc
Browse files Browse the repository at this point in the history
show document for `const` variables, follow up #744
  • Loading branch information
davidanthoff authored Jul 5, 2020
2 parents 0198aee + 80b90b5 commit c1ed200
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/requests/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,6 @@ function get_module_of(s::StaticLint.Scope, ms = [])
end
end

using Base.Docs, Markdown

function julia_getDocAt_request(params::VersionedTextDocumentPositionParams, server::LanguageServerInstance, conn)
uri = URI2(params.textDocument.uri)
hasdocument(server, uri) || return nodocuemnt_error(uri)
Expand All @@ -391,6 +389,7 @@ function julia_getDocAt_request(params::VersionedTextDocumentPositionParams, ser
x = get_expr1(getcst(doc), get_offset(doc, params.position))
x isa EXPR && typof(x) === CSTParser.OPERATOR && resolve_op_ref(x, server)
documentation = get_hover(x, "", server)

md = Markdown.parse(documentation)
return webview_html(md)
end
Expand Down
47 changes: 38 additions & 9 deletions src/requests/hover.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ function get_hover(x, documentation::String, server) documentation end

function get_hover(x::EXPR, documentation::String, server)
if (CSTParser.isidentifier(x) || CSTParser.isoperator(x)) && StaticLint.hasref(x)
if refof(x) isa StaticLint.Binding
documentation = get_hover(refof(x), documentation, server)
elseif refof(x) isa SymbolServer.SymStore
documentation = get_hover(refof(x), documentation, server)
r = refof(x)
documentation = if r isa StaticLint.Binding
get_hover(r, documentation, server)
elseif r isa SymbolServer.SymStore
get_hover(r, documentation, server)
else
documentation
end
end
return documentation
Expand All @@ -29,8 +32,12 @@ function get_hover(b::StaticLint.Binding, documentation::String, server)
documentation = get_func_hover(b, documentation, server)
else
try
if binding_has_preceding_docs(b)
documentation = string(documentation, Expr(parentof(b.val).args[2]))
documentation = if binding_has_preceding_docs(b)
string(documentation, Expr(parentof(b.val).args[2]))
elseif const_binding_has_preceding_docs(b)
string(documentation, Expr(parentof(parentof(b.val)).args[2]))
else
documentation
end
documentation = string(documentation, "```julia\n", Expr(b.val), "\n```\n")
catch err
Expand Down Expand Up @@ -96,9 +103,13 @@ function get_func_hover(b::StaticLint.Binding, documentation, server, visited =
push!(visited, b) # TODO: remove
end # TODO: remove
if b.val isa EXPR
if binding_has_preceding_docs(b)
documentation = if binding_has_preceding_docs(b)
# Binding has preceding docs so use them..
documentation = string(documentation, Expr(parentof(b.val).args[2]))
string(documentation, Expr(parentof(b.val).args[2]))
elseif const_binding_has_preceding_docs(b)
string(documentation, Expr(parentof(parentof(b.val)).args[2]))
else
documentation
end
if CSTParser.defines_function(b.val)
documentation = string(documentation, "```julia\n", Expr(CSTParser.get_sig(b.val)), "\n```\n")
Expand All @@ -114,8 +125,26 @@ function get_func_hover(b::StaticLint.Binding, documentation, server, visited =
return documentation
end

binding_has_preceding_docs(b::StaticLint.Binding) = parentof(b.val) isa EXPR && typof(parentof(b.val)) === CSTParser.MacroCall && length(parentof(b.val).args) == 3 && typof(parentof(b.val).args[1]) === CSTParser.GlobalRefDoc && CSTParser.isstring(parentof(b.val).args[2])
binding_has_preceding_docs(b::StaticLint.Binding) = expr_has_preceding_docs(b.val)

function const_binding_has_preceding_docs(b::StaticLint.Binding)
p = parentof(b.val)
is_const_expr(p) && expr_has_preceding_docs(p)
end

expr_has_preceding_docs(x) = false
expr_has_preceding_docs(x::EXPR) = is_doc_expr(parentof(x))

is_const_expr(x) = false
is_const_expr(x::EXPR) = length(x.args) == 2 && kindof(x.args[1]) === CSTParser.Tokens.CONST

is_doc_expr(x) = false
function is_doc_expr(x::EXPR)
return typof(x) === CSTParser.MacroCall &&
length(x.args) == 3 &&
typof(x.args[1]) === CSTParser.GlobalRefDoc &&
CSTParser.isstring(x.args[2])
end

get_fcall_position(x, documentation, visited = nothing) = documentation

Expand Down
2 changes: 1 addition & 1 deletion src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function mismatched_version_error(uri, doc, params, msg, data = nothing)
return JSONRPC.JSONRPCError(
-32099,
"version mismatch in $(msg) request for $(uri): JLS $(doc._version), client: $(params.version)",
nothing
data
)
end

Expand Down

0 comments on commit c1ed200

Please sign in to comment.