Skip to content

Commit 2633b63

Browse files
authored
Merge pull request #1338 from julia-vscode/add-diag-testitem-marking-logic
Move all marking logic for testitems and diag here
2 parents 236009f + 5c26bbc commit 2633b63

File tree

7 files changed

+165
-121
lines changed

7 files changed

+165
-121
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ JSONRPC = "1.1"
2727
JuliaFormatter = "0.20.0, 0.21, 0.22, 0.23, 1"
2828
PrecompileTools = "1"
2929
StaticLint = "8.0"
30-
JuliaWorkspaces = "4.7"
30+
JuliaWorkspaces = "5"
3131
SymbolServer = "8"
3232
Tokenize = "0.5.10"
3333
URIs = "1.3"

src/LanguageServer.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ include("languageserverinstance.jl")
2828
include("multienv.jl")
2929
include("runserver.jl")
3030
include("staticlint.jl")
31+
include("testitem_diagnostic_marking.jl")
3132

3233
include("requests/misc.jl")
3334
include("requests/textdocument.jl")

src/languageserverinstance.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,7 @@ function Base.run(server::LanguageServerInstance; timings = [])
490490
end
491491

492492
function relintserver(server)
493-
JuliaWorkspaces.mark_current_diagnostics(server.workspace)
494-
JuliaWorkspaces.mark_current_testitems(server.workspace)
493+
marked_versions = mark_current_diagnostics_testitems(server.workspace)
495494

496495
roots = Set{Document}()
497496
documents = collect(getdocuments_value(server))
@@ -514,6 +513,5 @@ function relintserver(server)
514513
lint!(doc, server)
515514
end
516515
end
517-
publish_diagnostics(get_uri.(documents), server, server.jr_endpoint, "relintserver")
518-
publish_tests(server)
516+
publish_diagnostics_testitems(server, marked_versions, get_uri.(documents))
519517
end

src/requests/init.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ function initialized_notification(params::InitializedParams, server::LanguageSer
224224

225225
end
226226

227-
JuliaWorkspaces.mark_current_diagnostics(server.workspace)
228-
JuliaWorkspaces.mark_current_testitems(server.workspace)
227+
marked_versions = mark_current_diagnostics_testitems(server.workspace)
229228
added_docs = Document[]
230229

231230
if server.workspaceFolders !== nothing
@@ -289,8 +288,7 @@ function initialized_notification(params::InitializedParams, server::LanguageSer
289288
end
290289
end
291290

292-
publish_diagnostics(get_uri.(added_docs), server, conn, "initialized_notification")
293-
publish_tests(server)
291+
publish_diagnostics_testitems(server, marked_versions, get_uri.(added_docs))
294292

295293
request_julia_config(server, conn)
296294

src/requests/textdocument.jl

Lines changed: 6 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
function textDocument_didOpen_notification(params::DidOpenTextDocumentParams, server::LanguageServerInstance, conn)
2-
JuliaWorkspaces.mark_current_diagnostics(server.workspace)
3-
JuliaWorkspaces.mark_current_testitems(server.workspace)
2+
marked_versions = mark_current_diagnostics_testitems(server.workspace)
43

54
uri = params.textDocument.uri
65
if hasdocument(server, uri)
@@ -33,16 +32,15 @@ function textDocument_didOpen_notification(params::DidOpenTextDocumentParams, se
3332

3433
parse_all(doc, server)
3534
lint!(doc, server)
36-
publish_diagnostics([get_uri(doc)], server, conn, "textDocument_didOpen_notification")
37-
publish_tests(server)
35+
publish_diagnostics_testitems(server, marked_versions, [get_uri(doc)])
3836
end
3937

4038

4139
function textDocument_didClose_notification(params::DidCloseTextDocumentParams, server::LanguageServerInstance, conn)
4240
uri = params.textDocument.uri
4341
doc = getdocument(server, uri)
4442

45-
JuliaWorkspaces.mark_current_testitems(server.workspace)
43+
marked_versions = mark_current_diagnostics_testitems(server.workspace)
4644

4745
if is_workspace_file(doc)
4846
set_open_in_editor(doc, false)
@@ -74,7 +72,7 @@ function textDocument_didClose_notification(params::DidCloseTextDocumentParams,
7472
JuliaWorkspaces.remove_file!(server.workspace, uri)
7573
end
7674

77-
publish_tests(server)
75+
publish_diagnostics_testitems(server, marked_versions, JuliaWorkspaces.URIs2.URI[])
7876
end
7977

8078
function textDocument_didSave_notification(params::DidSaveTextDocumentParams, server::LanguageServerInstance, conn)
@@ -137,8 +135,7 @@ function measure_sub_operation(f, request_name, server)
137135
end
138136

139137
function textDocument_didChange_notification(params::DidChangeTextDocumentParams, server::LanguageServerInstance, conn)
140-
JuliaWorkspaces.mark_current_diagnostics(server.workspace)
141-
JuliaWorkspaces.mark_current_testitems(server.workspace)
138+
marked_versions = mark_current_diagnostics_testitems(server.workspace)
142139

143140
uri = params.textDocument.uri
144141

@@ -186,8 +183,7 @@ function textDocument_didChange_notification(params::DidChangeTextDocumentParams
186183
lint!(doc, server)
187184
end
188185

189-
publish_diagnostics([get_uri(doc)], server, conn, "textDocument_didChange_notification")
190-
publish_tests(server)
186+
publish_diagnostics_testitems(server, marked_versions, [get_uri(doc)])
191187
end
192188

193189
function parse_all(doc::Document, server::LanguageServerInstance)
@@ -398,97 +394,3 @@ function try_to_load_parents(child_path, server)
398394
end
399395
end
400396
end
401-
402-
function publish_diagnostics(uris::Vector{URI}, server, conn, source)
403-
jw_diagnostics_updated, jw_diagnostics_deleted = JuliaWorkspaces.get_files_with_updated_diagnostics(server.workspace)
404-
405-
all_uris_with_updates = Set{URI}()
406-
407-
for uri in uris
408-
push!(all_uris_with_updates, uri)
409-
end
410-
411-
for uri in jw_diagnostics_updated
412-
push!(all_uris_with_updates, uri)
413-
end
414-
415-
diagnostics = Dict{URI,Vector{Diagnostic}}()
416-
417-
for uri in all_uris_with_updates
418-
diags = Diagnostic[]
419-
diagnostics[uri] = diags
420-
421-
if hasdocument(server, uri)
422-
doc = getdocument(server, uri)
423-
424-
if server.runlinter && (is_workspace_file(doc) || isunsavedfile(doc))
425-
pkgpath = getpath(doc)
426-
if any(is_in_target_dir_of_package.(Ref(pkgpath), server.lint_disableddirs))
427-
filter!(!is_diag_dependent_on_env, doc.diagnostics)
428-
end
429-
append!(diags, doc.diagnostics)
430-
end
431-
end
432-
433-
if JuliaWorkspaces.has_file(server.workspace, uri)
434-
st = JuliaWorkspaces.get_text_file(server.workspace, uri).content
435-
436-
new_diags = JuliaWorkspaces.get_diagnostic(server.workspace, uri)
437-
438-
append!(diags, Diagnostic(
439-
Range(st, i.range),
440-
if i.severity==:error
441-
DiagnosticSeverities.Error
442-
elseif i.severity==:warning
443-
DiagnosticSeverities.Warning
444-
elseif i.severity==:info
445-
DiagnosticSeverities.Information
446-
else
447-
error("Unknown severity $(i.severity)")
448-
end,
449-
missing,
450-
missing,
451-
i.source,
452-
i.message,
453-
missing,
454-
missing
455-
) for i in new_diags)
456-
end
457-
end
458-
459-
for (uri,diags) in diagnostics
460-
version = get(server._open_file_versions, uri, missing)
461-
params = PublishDiagnosticsParams(uri, version, diags)
462-
JSONRPC.send(conn, textDocument_publishDiagnostics_notification_type, params)
463-
end
464-
end
465-
466-
function publish_tests(server::LanguageServerInstance)
467-
if !ismissing(server.initialization_options) && get(server.initialization_options, "julialangTestItemIdentification", false)
468-
updated_files, deleted_files = JuliaWorkspaces.get_files_with_updated_testitems(server.workspace)
469-
470-
for uri in updated_files
471-
testitems_results = JuliaWorkspaces.get_test_items(server.workspace, uri)
472-
st = JuliaWorkspaces.get_text_file(server.workspace, uri).content
473-
474-
testitems = TestItemDetail[TestItemDetail(i.id, i.name, Range(st, i.range), i.code, Range(st, i.code_range), i.option_default_imports, string.(i.option_tags), string.(i.option_setup)) for i in testitems_results.testitems]
475-
testsetups= TestSetupDetail[TestSetupDetail(string(i.name), string(i.kind), Range(st, i.range), i.code, Range(st, i.code_range), ) for i in testitems_results.testsetups]
476-
testerrors = TestErrorDetail[TestErrorDetail(te.id, te.name, Range(st, te.range), te.message) for te in testitems_results.testerrors]
477-
478-
version = get(server._open_file_versions, uri, missing)
479-
480-
params = PublishTestsParams(
481-
uri,
482-
version,
483-
testitems,
484-
testsetups,
485-
testerrors
486-
)
487-
JSONRPC.send(server.jr_endpoint, textDocument_publishTests_notification_type, params)
488-
end
489-
490-
for uri in deleted_files
491-
JSONRPC.send(server.jr_endpoint, textDocument_publishTests_notification_type, PublishTestsParams(uri, missing, TestItemDetail[], TestSetupDetail[], TestErrorDetail[]))
492-
end
493-
end
494-
end

src/requests/workspace.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
function workspace_didChangeWatchedFiles_notification(params::DidChangeWatchedFilesParams, server::LanguageServerInstance, conn)
2-
JuliaWorkspaces.mark_current_diagnostics(server.workspace)
3-
JuliaWorkspaces.mark_current_testitems(server.workspace)
2+
marked_versions = mark_current_diagnostics_testitems(server.workspace)
43

54
docs_to_lint = Document[]
65

@@ -82,8 +81,8 @@ function workspace_didChangeWatchedFiles_notification(params::DidChangeWatchedFi
8281
for lint_doc in docs_to_lint
8382
lint!(lint_doc, server)
8483
end
85-
publish_diagnostics(get_uri.(docs_to_lint), server, conn, "workspace_didChangeWatchedFiles_notification")
86-
publish_tests(server)
84+
85+
publish_diagnostics_testitems(server, marked_versions, get_uri.(docs_to_lint))
8786
end
8887

8988
function workspace_didChangeConfiguration_notification(params::DidChangeConfigurationParams, server::LanguageServerInstance, conn)
@@ -174,8 +173,7 @@ function gc_files_from_workspace(server::LanguageServerInstance)
174173
end
175174

176175
function workspace_didChangeWorkspaceFolders_notification(params::DidChangeWorkspaceFoldersParams, server::LanguageServerInstance, conn)
177-
JuliaWorkspaces.mark_current_diagnostics(server.workspace)
178-
JuliaWorkspaces.mark_current_testitems(server.workspace)
176+
marked_versions = mark_current_diagnostics_testitems(server.workspace)
179177

180178
added_docs = Document[]
181179

@@ -209,8 +207,8 @@ function workspace_didChangeWorkspaceFolders_notification(params::DidChangeWorks
209207
for doc in added_docs
210208
lint!(doc, server)
211209
end
212-
publish_diagnostics(get_uri.(added_docs), server, conn, "workspace_didChangeWorkspaceFolders_notification")
213-
publish_tests(server)
210+
211+
publish_diagnostics_testitems(server, marked_versions, get_uri.(added_docs))
214212
end
215213

216214
function workspace_symbol_request(params::WorkspaceSymbolParams, server::LanguageServerInstance, conn)

src/testitem_diagnostic_marking.jl

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
function mark_current_diagnostics_testitems(jw::JuliaWorkspace)
2+
ti_results = Dict{URI,UInt}(k => hash(v) for (k,v) in JuliaWorkspaces.get_test_items(jw))
3+
4+
diag_results = Dict{URI,UInt}(k => hash(v) for (k,v) in JuliaWorkspaces.get_diagnostics(jw))
5+
6+
return (testitems=ti_results, diagnostics=diag_results)
7+
end
8+
9+
function get_files_with_updated_diagnostics_testitems(jw::JuliaWorkspace, old_marked_versions::@NamedTuple{testitems::Dict{URI,UInt},diagnostics::Dict{URI,UInt}})
10+
# Testitems
11+
new_marked_versions_ti = Dict{URI,UInt}(k => hash(v) for (k,v) in JuliaWorkspaces.get_test_items(jw))
12+
13+
old_text_files_ti = Set{URI}(keys(old_marked_versions.testitems))
14+
new_text_files_ti = Set{URI}(keys(new_marked_versions_ti))
15+
16+
deleted_files_ti = setdiff(old_text_files_ti, new_text_files_ti)
17+
updated_files_ti = Set{URI}()
18+
19+
for (uri,hash_value) in new_marked_versions_ti
20+
if !(uri in old_text_files_ti)
21+
push!(updated_files_ti, uri)
22+
else
23+
if hash_value != old_marked_versions.testitems[uri]
24+
push!(updated_files_ti, uri)
25+
end
26+
end
27+
end
28+
29+
# Diagnostics
30+
new_marked_versions_diag = Dict{URI,UInt}(k => hash(v) for (k,v) in JuliaWorkspaces.get_diagnostics(jw))
31+
32+
old_text_files_diag = Set{URI}(keys(old_marked_versions.diagnostics))
33+
new_text_files_diag = Set{URI}(keys(new_marked_versions_diag))
34+
35+
deleted_files_diag = setdiff(old_text_files_diag, new_text_files_diag)
36+
updated_files_diag = Set{URI}()
37+
38+
for (uri,hash_value) in new_marked_versions_diag
39+
if !(uri in old_text_files_diag)
40+
push!(updated_files_diag, uri)
41+
else
42+
if hash_value != old_marked_versions.diagnostics[uri]
43+
push!(updated_files_diag, uri)
44+
end
45+
end
46+
end
47+
48+
return (;updated_files_ti, deleted_files_ti, updated_files_diag, deleted_files_diag)
49+
end
50+
51+
function publish_diagnostics(server, jw_diagnostics_updated, jw_diagnostics_deleted, uris::Vector{URI})
52+
all_uris_with_updates = Set{URI}()
53+
54+
for uri in uris
55+
push!(all_uris_with_updates, uri)
56+
end
57+
58+
for uri in jw_diagnostics_updated
59+
push!(all_uris_with_updates, uri)
60+
end
61+
62+
diagnostics = Dict{URI,Vector{Diagnostic}}()
63+
64+
for uri in all_uris_with_updates
65+
diags = Diagnostic[]
66+
diagnostics[uri] = diags
67+
68+
if hasdocument(server, uri)
69+
doc = getdocument(server, uri)
70+
71+
if server.runlinter && (is_workspace_file(doc) || isunsavedfile(doc))
72+
pkgpath = getpath(doc)
73+
if any(is_in_target_dir_of_package.(Ref(pkgpath), server.lint_disableddirs))
74+
filter!(!is_diag_dependent_on_env, doc.diagnostics)
75+
end
76+
append!(diags, doc.diagnostics)
77+
end
78+
end
79+
80+
if JuliaWorkspaces.has_file(server.workspace, uri)
81+
st = JuliaWorkspaces.get_text_file(server.workspace, uri).content
82+
83+
new_diags = JuliaWorkspaces.get_diagnostic(server.workspace, uri)
84+
85+
append!(diags, Diagnostic(
86+
Range(st, i.range),
87+
if i.severity==:error
88+
DiagnosticSeverities.Error
89+
elseif i.severity==:warning
90+
DiagnosticSeverities.Warning
91+
elseif i.severity==:info
92+
DiagnosticSeverities.Information
93+
else
94+
error("Unknown severity $(i.severity)")
95+
end,
96+
missing,
97+
missing,
98+
i.source,
99+
i.message,
100+
missing,
101+
missing
102+
) for i in new_diags)
103+
end
104+
end
105+
106+
for (uri,diags) in diagnostics
107+
version = get(server._open_file_versions, uri, missing)
108+
params = PublishDiagnosticsParams(uri, version, diags)
109+
JSONRPC.send(server.jr_endpoint, textDocument_publishDiagnostics_notification_type, params)
110+
end
111+
end
112+
113+
function publish_tests(server::LanguageServerInstance, updated_files, deleted_files)
114+
if !ismissing(server.initialization_options) && get(server.initialization_options, "julialangTestItemIdentification", false)
115+
for uri in updated_files
116+
testitems_results = JuliaWorkspaces.get_test_items(server.workspace, uri)
117+
st = JuliaWorkspaces.get_text_file(server.workspace, uri).content
118+
119+
testitems = TestItemDetail[TestItemDetail(i.id, i.name, Range(st, i.range), i.code, Range(st, i.code_range), i.option_default_imports, string.(i.option_tags), string.(i.option_setup)) for i in testitems_results.testitems]
120+
testsetups= TestSetupDetail[TestSetupDetail(string(i.name), string(i.kind), Range(st, i.range), i.code, Range(st, i.code_range), ) for i in testitems_results.testsetups]
121+
testerrors = TestErrorDetail[TestErrorDetail(te.id, te.name, Range(st, te.range), te.message) for te in testitems_results.testerrors]
122+
123+
version = get(server._open_file_versions, uri, missing)
124+
125+
params = PublishTestsParams(
126+
uri,
127+
version,
128+
testitems,
129+
testsetups,
130+
testerrors
131+
)
132+
JSONRPC.send(server.jr_endpoint, textDocument_publishTests_notification_type, params)
133+
end
134+
135+
for uri in deleted_files
136+
JSONRPC.send(server.jr_endpoint, textDocument_publishTests_notification_type, PublishTestsParams(uri, missing, TestItemDetail[], TestSetupDetail[], TestErrorDetail[]))
137+
end
138+
end
139+
end
140+
141+
142+
function publish_diagnostics_testitems(server, marked_versions, uris::Vector{URI})
143+
updated_files = get_files_with_updated_diagnostics_testitems(server.workspace, marked_versions)
144+
145+
publish_diagnostics(server, updated_files.updated_files_diag, updated_files.deleted_files_diag, uris)
146+
publish_tests(server, updated_files.updated_files_ti, updated_files.deleted_files_ti)
147+
end

0 commit comments

Comments
 (0)