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

julia/getDocAt handler #744

Merged
merged 9 commits into from
Jul 1, 2020
Merged
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Tokenize = "0796e94c-ce3b-5d07-9a54-7f471281c624"
JSONRPC = "b9b8584e-8fd3-41f9-ad0c-7255d428e418"
SymbolServer = "cf896787-08d5-524d-9de7-132aaa0cb996"
URIParser = "30578b45-9adc-5946-b283-645ec420af67"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"

[extras]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
1 change: 1 addition & 0 deletions src/LanguageServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using JSON, REPL, CSTParser, DocumentFormat, SymbolServer, StaticLint
using CSTParser: EXPR, Tokenize.Tokens, typof, kindof, parentof, valof
using StaticLint: refof, scopeof, bindingof
using UUIDs
using Base.Docs, Markdown
import JSONRPC
using JSONRPC: Outbound, @dict_readable

Expand Down
3 changes: 1 addition & 2 deletions src/languageserverinstance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ function Base.run(server::LanguageServerInstance)
msg_dispatcher[textDocument_rename_request_type] = (conn, params)->textDocument_rename_request(params, server, conn)
msg_dispatcher[textDocument_documentSymbol_request_type] = (conn, params)->textDocument_documentSymbol_request(params, server, conn)
msg_dispatcher[julia_getModuleAt_request_type] = (conn, params)->julia_getModuleAt_request(params, server, conn)
msg_dispatcher[julia_getDocAt_request_type] = (conn, params)->julia_getDocAt_request(params, server, conn)
msg_dispatcher[textDocument_hover_request_type] = (conn, params)->textDocument_hover_request(params, server, conn)
msg_dispatcher[initialize_request_type] = (conn, params)->initialize_request(params, server, conn)
msg_dispatcher[initialized_notification_type] = (conn, params)->initialized_notification(params, server, conn)
Expand Down Expand Up @@ -323,5 +324,3 @@ function Base.run(server::LanguageServerInstance)
end
end
end


2 changes: 1 addition & 1 deletion src/protocol/messagedefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const workspace_didChangeConfiguration_notification_type = JSONRPC.NotificationT
const workspace_didChangeWorkspaceFolders_notification_type = JSONRPC.NotificationType("workspace/didChangeWorkspaceFolders", DidChangeWorkspaceFoldersParams)
const workspace_applyEdit_request_type = JSONRPC.RequestType("workspace/applyEdit", ApplyWorkspaceEditParams, ApplyWorkspaceEditResponse)
const workspace_configuration_request_type = JSONRPC.RequestType("workspace/configuration", ConfigurationParams, Vector{Any})

const julia_getModuleAt_request_type = JSONRPC.RequestType("julia/getModuleAt", VersionedTextDocumentPositionParams, String)
const julia_getCurrentBlockRange_request_type = JSONRPC.RequestType("julia/getCurrentBlockRange", VersionedTextDocumentPositionParams, Position)
const julia_getDocAt_request_type = JSONRPC.RequestType("julia/getDocAt", VersionedTextDocumentPositionParams, String)
const julia_activateenvironment_notification_type = JSONRPC.NotificationType("julia/activateenvironment", String)

const initialize_request_type = JSONRPC.RequestType("initialize", InitializeParams, InitializeResult)
Expand Down
45 changes: 43 additions & 2 deletions src/requests/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ function julia_getModuleAt_request(params::VersionedTextDocumentPositionParams,
end
end
else
return JSONRPC.JSONRPCError(-32099, "version mismatch in getModuleAt for $(uri): JLS $(doc._version), client: $(params.version)", nothing)
return mismatched_version_error(uri, doc, params, "getModuleAt")
end
else
return JSONRPC.JSONRPCError(-32099, "document $(uri) requested but not present in the JLS", nothing)
return nodocuemnt_error(uri)
end
return "Main"
end
Expand All @@ -376,3 +376,44 @@ function get_module_of(s::StaticLint.Scope, ms = [])
return isempty(ms) ? "Main" : join(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)

doc = getdocument(server, uri)
if doc._version !== params.version
return mismatched_version_error(uri, doc, params, "getDocAt")
end

x = get_expr1(getcst(doc), get_offset(doc, params.position))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be get_expr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, why do you think so ?
I adapted code from this line, and not so sure about the difference between get_expr and get_expr1...
Maybe @ZacLN could you explain this to me ?

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

const CODE_LANG_REGEX = r"\<code class\=\"language-(?<lang>(?!\>).+)\"\>"

function webview_html(md)
# HACK goes on ...
s = html(md)
if haskey(md.meta, :module)
mod = md.meta[:module]
newhref = string("julia-vscode", '/', mod)
s = replace(s, "<a href=\"@ref\"" => "<a href=\"$newhref\"")
end
s = replace(s, CODE_LANG_REGEX => annotate_highlight_js)
return s
end

function annotate_highlight_js(s)
m::RegexMatch = match(CODE_LANG_REGEX, s)
lang = m[:lang]
if lang == "jldoctest"
lang = "julia-repl"
end
return "<code class=\"language-$(lang) hljs\">"
end
4 changes: 2 additions & 2 deletions src/requests/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ function julia_getCurrentBlockRange_request(tdpp::VersionedTextDocumentPositionP
fallback = (Position(0, 0), Position(0, 0), tdpp.position)
uri = URI2(tdpp.textDocument.uri)

hasdocument(server, uri) || return JSONRPC.JSONRPCError(-32099, "document $(uri) requested but not present in the JLS", nothing)
hasdocument(server, uri) || return nodocuemnt_error(uri)

doc = getdocument(server, uri)

if doc._version !== tdpp.version
return JSONRPC.JSONRPCError(-32099, "version mismatch in getCurrentBlockRange for $(uri): JLS $(doc._version), client: $(tdpp.version)", nothing)
return mismatched_version_error(uri, doc, tdpp, "getCurrentBlockRange")
end

offset = get_offset(doc, tdpp.position)
Expand Down
11 changes: 11 additions & 0 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
nodocuemnt_error(uri, data = nothing) =
return JSONRPC.JSONRPCError(-32099, "document $(uri) requested but not present in the JLS", data)

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
)
end

function uri2filepath(uri::AbstractString)
parsed_uri = try
URIParser.URI(uri)
Expand Down