Skip to content

Commit 9822394

Browse files
authored
Merge pull request #1064 from julia-vscode/sp/document-link-include
Use DocumentLinkProvider for include
2 parents 7801686 + 99feef0 commit 9822394

File tree

7 files changed

+78
-45
lines changed

7 files changed

+78
-45
lines changed

src/languageserverinstance.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ function Base.run(server::LanguageServerInstance)
363363
msg_dispatcher[julia_refreshLanguageServer_notification_type] = request_wrapper(julia_refreshLanguageServer_notification, server)
364364
msg_dispatcher[julia_getDocFromWord_request_type] = request_wrapper(julia_getDocFromWord_request, server)
365365
msg_dispatcher[textDocument_selectionRange_request_type] = request_wrapper(textDocument_selectionRange_request, server)
366+
msg_dispatcher[textDocument_documentLink_request_type] = request_wrapper(textDocument_documentLink_request, server)
366367

367368
# The exit notification message should not be wrapped in request_wrapper (which checks
368369
# if the server have been requested to be shut down). Instead, this message needs to be

src/protocol/features.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ end
114114

115115
@dict_readable struct DocumentLinkParams
116116
textDocument::TextDocumentIdentifier
117+
workDoneToken::Union{Int,String,Missing}
118+
partialResultToken::Union{Int,String,Missing}
117119
end
118120

119121
struct DocumentLink <: Outbound

src/protocol/messagedefs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const textDocument_willSave_notification_type = JSONRPC.NotificationType("textDo
1818
const textDocument_willSaveWaitUntil_request_type = JSONRPC.RequestType("textDocument/willSaveWaitUntil", WillSaveTextDocumentParams, Union{Vector{TextEdit}, Nothing})
1919
const textDocument_publishDiagnostics_notification_type = JSONRPC.NotificationType("textDocument/publishDiagnostics", PublishDiagnosticsParams)
2020
const textDocument_selectionRange_request_type = JSONRPC.RequestType("textDocument/selectionRange", SelectionRangeParams, Union{Vector{SelectionRange}, Nothing})
21+
const textDocument_documentLink_request_type = JSONRPC.RequestType("textDocument/documentLink", DocumentLinkParams, Union{Vector{DocumentLink}, Nothing})
2122

2223
const workspace_executeCommand_request_type = JSONRPC.RequestType("workspace/executeCommand", ExecuteCommandParams, Any)
2324
const workspace_symbol_request_type = JSONRPC.RequestType("workspace/symbol", WorkspaceSymbolParams, Union{Vector{SymbolInformation}, Nothing})

src/requests/features.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,6 @@ function textDocument_definition_request(params::TextDocumentPositionParams, ser
7777
b = resolve_shadow_binding(b)
7878
(tls = StaticLint.retrieve_toplevel_scope(x)) === nothing && return locations
7979
get_definitions(b, tls, getenv(doc, server), locations)
80-
elseif x isa EXPR && CSTParser.isstringliteral(x)
81-
# TODO: move to its own function
82-
if valof(x) isa String && sizeof(valof(x)) < 256 # AUDIT: OK
83-
try
84-
if isabspath(valof(x)) && safe_isfile(valof(x))
85-
push!(locations, Location(filepath2uri(valof(x)), Range(0, 0, 0, 0)))
86-
elseif !isempty(getpath(doc)) && safe_isfile(joinpath(_dirname(getpath(doc)), valof(x)))
87-
push!(locations, Location(filepath2uri(joinpath(_dirname(getpath(doc)), valof(x))), Range(0, 0, 0, 0)))
88-
end
89-
catch err
90-
isa(err, Base.IOError) ||
91-
isa(err, Base.SystemError) ||
92-
(VERSION == v"1.2.0" && isa(err, ErrorException) && err.msg == "type Nothing has no field captures ") ||
93-
rethrow()
94-
end
95-
end
9680
end
9781

9882
return locations

src/requests/init.jl

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,37 @@ function ServerCapabilities(client::ClientCapabilities)
22
prepareSupport = !ismissing(client.textDocument) && !ismissing(client.textDocument.rename) && client.textDocument.rename.prepareSupport === true
33

44
ServerCapabilities(
5-
TextDocumentSyncOptions(true,
6-
TextDocumentSyncKinds.Full,
7-
false,
8-
false,
9-
SaveOptions(true)),
10-
CompletionOptions(false, [".", "@", "\"", "^"], missing),
11-
true,
12-
SignatureHelpOptions(["(", ","], missing),
13-
false,
14-
true,
15-
false,
16-
false,
17-
true,
18-
true,
19-
true,
20-
true,
21-
missing,
22-
missing,
23-
false,
24-
true,
25-
true,
26-
missing,
27-
RenameOptions(missing, prepareSupport),
28-
false,
29-
ExecuteCommandOptions(missing, collect(keys(LSActions))),
30-
true,
31-
true,
32-
WorkspaceOptions(WorkspaceFoldersOptions(true, true)),
33-
missing)
5+
TextDocumentSyncOptions(true,
6+
TextDocumentSyncKinds.Full,
7+
false,
8+
false,
9+
SaveOptions(true)
10+
),
11+
CompletionOptions(false, [".", "@", "\"", "^"], missing),
12+
true,
13+
SignatureHelpOptions(["(", ","], missing),
14+
false,
15+
true,
16+
false,
17+
false,
18+
true,
19+
true,
20+
true,
21+
true,
22+
missing,
23+
DocumentLinkOptions(false, missing),
24+
false,
25+
true,
26+
true,
27+
missing,
28+
RenameOptions(missing, prepareSupport),
29+
false,
30+
ExecuteCommandOptions(missing, collect(keys(LSActions))),
31+
true,
32+
true,
33+
WorkspaceOptions(WorkspaceFoldersOptions(true, true)),
34+
missing
35+
)
3436

3537
end
3638

src/requests/misc.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,34 @@ end
8484

8585
julia_refreshLanguageServer_notification(_, server::LanguageServerInstance, conn) =
8686
trigger_symbolstore_reload(server)
87+
88+
function textDocument_documentLink_request(params::DocumentLinkParams, server::LanguageServerInstance, conn)
89+
doc = getdocument(server, URI2(params.textDocument.uri))
90+
links = DocumentLink[]
91+
find_document_links(getcst(doc), doc, 0, links)
92+
return links
93+
end
94+
95+
function find_document_links(x, doc, offset, links)
96+
if x isa EXPR && CSTParser.isstringliteral(x)
97+
if valof(x) isa String && sizeof(valof(x)) < 256 # AUDIT: OK
98+
try
99+
if isabspath(valof(x)) && safe_isfile(valof(x))
100+
path = valof(x)
101+
push!(links, DocumentLink(Range(doc, offset .+ (0:x.span)), filepath2uri(path), missing, missing))
102+
elseif !isempty(getpath(doc)) && safe_isfile(joinpath(_dirname(getpath(doc)), valof(x)))
103+
path = joinpath(_dirname(getpath(doc)), valof(x))
104+
push!(links, DocumentLink(Range(doc, offset .+ (0:x.span)), filepath2uri(path), missing, missing))
105+
end
106+
catch err
107+
isa(err, Base.IOError) || isa(err, Base.SystemError) || rethrow()
108+
end
109+
end
110+
end
111+
if x.args !== nothing
112+
for arg in x
113+
find_document_links(arg, doc, offset, links)
114+
offset += arg.fullspan
115+
end
116+
end
117+
end

test/test_document.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,15 @@ end
124124
@test LanguageServer.get_offset(doc, 0, 6) == 9
125125
@test LanguageServer.get_position_at(doc, 9) == (0, 6)
126126
end
127+
128+
@testset "document link provider" begin
129+
doc = LS.Document(LS.filepath2uri(@__FILE__), """
130+
include("test_document.jl")
131+
include("runtests_does_not_exist.jl")
132+
""", false)
133+
links = LS.DocumentLink[]
134+
LS.find_document_links(LS.getcst(doc), doc, 0, links)
135+
@test length(links) == 1
136+
# lowercase because windows drives can apparently be inconsistently capitalized
137+
@test lowercase(links[1].target) == lowercase(LS.filepath2uri(@__FILE__))
138+
end

0 commit comments

Comments
 (0)