Skip to content

Commit ce837d9

Browse files
committed
Add more diagnostics
1 parent 2ce7b9a commit ce837d9

File tree

4 files changed

+103
-43
lines changed

4 files changed

+103
-43
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ JuliaWorkspaces = "e554591c-7f10-434f-9f27-2097f62a04fd"
1818
Tokenize = "0796e94c-ce3b-5d07-9a54-7f471281c624"
1919
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
2020
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
21+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
2122

2223
[compat]
2324
CSTParser = "3.3"

src/LanguageServer.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ import JuliaWorkspaces
1111
using JuliaWorkspaces: JuliaWorkspace, URIs2
1212
using JuliaWorkspaces.URIs2: URI, uri2filepath, filepath2uri
1313
using PrecompileTools
14+
import Dates
1415

1516
export LanguageServerInstance, runserver
1617

18+
@static if VERSION >= v"1.11-"
19+
using Base.ScopedValues
20+
21+
const g_operationId = ScopedValue(string(uuid4()))
22+
end
23+
1724
JSON.lower(uri::URI) = string(uri)
1825

1926
include("exception_types.jl")

src/languageserverinstance.jl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -413,20 +413,28 @@ function Base.run(server::LanguageServerInstance; timings = [])
413413

414414
add_timer_message!(did_show_timer, timings, msg)
415415

416-
tic = time_ns()
417-
JSONRPC.dispatch_msg(server.jr_endpoint, msg_dispatcher, msg)
418-
toc = time_ns()
416+
@static if VERSION>=v"1.11-"
417+
with(g_operationId => string(uuid4())) do
418+
start_time = string(Dates.unix2datetime(time()), "Z")
419+
tic = time_ns()
420+
JSONRPC.dispatch_msg(server.jr_endpoint, msg_dispatcher, msg)
421+
toc = time_ns()
422+
duration = (toc - tic) / 1e+6
419423

420-
duration = (toc - tic) / 1e+6
421-
422-
JSONRPC.send(
423-
server.jr_endpoint,
424-
telemetry_event_notification_type,
425-
Dict(
426-
"command" => "request_metric",
427-
"name" => msg["method"],
428-
"duration" => duration)
429-
)
424+
JSONRPC.send(
425+
server.jr_endpoint,
426+
telemetry_event_notification_type,
427+
Dict(
428+
"command" => "request_metric",
429+
"operationId" => g_operationId[],
430+
"name" => msg["method"],
431+
"time" => start_time,
432+
"duration" => duration)
433+
)
434+
end
435+
else
436+
JSONRPC.dispatch_msg(server.jr_endpoint, msg_dispatcher, msg)
437+
end
430438
elseif message.type == :symservmsg
431439
@debug "Received new data from Julia Symbol Server."
432440

src/requests/textdocument.jl

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,36 @@ function comp(x::CSTParser.EXPR, y::CSTParser.EXPR)
113113
all(comp(x[i], y[i]) for i = 1:length(x))
114114
end
115115

116+
function measure_sub_operation(f, request_name, server)
117+
start_time = string(Dates.unix2datetime(time()), "Z")
118+
tic = time_ns()
119+
f()
120+
toc = time_ns()
121+
duration = (toc - tic) / 1e+6
122+
123+
@static if VERSION >= v"1.11-"
124+
JSONRPC.send(
125+
server.jr_endpoint,
126+
telemetry_event_notification_type,
127+
Dict(
128+
"command" => "request_metric",
129+
"operationId" => string(uuid4()),
130+
"operationParentId" => g_operationId[],
131+
"name" => request_name,
132+
"duration" => duration,
133+
"time" => start_time
134+
)
135+
)
136+
end
137+
end
138+
116139
function textDocument_didChange_notification(params::DidChangeTextDocumentParams, server::LanguageServerInstance, conn)
117-
JuliaWorkspaces.mark_current_diagnostics(server.workspace)
118-
JuliaWorkspaces.mark_current_testitems(server.workspace)
140+
measure_sub_operation("JuliaWorkspaces.mark_current_diagnostics", server) do
141+
JuliaWorkspaces.mark_current_diagnostics(server.workspace)
142+
end
143+
measure_sub_operation("JuliaWorkspaces.mark_current_testitems", server) do
144+
JuliaWorkspaces.mark_current_testitems(server.workspace)
145+
end
119146

120147
uri = params.textDocument.uri
121148

@@ -127,43 +154,60 @@ function textDocument_didChange_notification(params::DidChangeTextDocumentParams
127154
error("The client and server have different textDocument versions for $(get_uri(doc)). LS version is $(get_version(doc)), request version is $(params.textDocument.version).")
128155
end
129156

130-
new_text_document = apply_text_edits(get_text_document(doc), params.contentChanges, params.textDocument.version)
131-
set_text_document!(doc, new_text_document)
157+
measure_sub_operation("File edits", server) do
158+
new_text_document = apply_text_edits(get_text_document(doc), params.contentChanges, params.textDocument.version)
159+
set_text_document!(doc, new_text_document)
132160

133-
if !haskey(server._open_file_versions, uri)
134-
error("This should not happen")
135-
end
161+
if !haskey(server._open_file_versions, uri)
162+
error("This should not happen")
163+
end
136164

137-
if server._open_file_versions[uri]>params.textDocument.version
138-
error("Outdated version: server $(server._open_file_versions[uri]) params $(params.textDocument.version)")
139-
end
165+
if server._open_file_versions[uri]>params.textDocument.version
166+
error("Outdated version: server $(server._open_file_versions[uri]) params $(params.textDocument.version)")
167+
end
140168

141-
# We originally applied each text edit individually, but that doesn't work because
142-
# we need to convert the LS positions to Julia indices after each text edit update
143-
# For now we just use the new text that we already created for the legacy TextDocument
144-
new_text_file = JuliaWorkspaces.TextFile(uri, JuliaWorkspaces.SourceText(get_text(new_text_document), get_language_id(doc)))
145-
JuliaWorkspaces.update_file!(server.workspace, new_text_file)
169+
# We originally applied each text edit individually, but that doesn't work because
170+
# we need to convert the LS positions to Julia indices after each text edit update
171+
# For now we just use the new text that we already created for the legacy TextDocument
172+
new_text_file = JuliaWorkspaces.TextFile(uri, JuliaWorkspaces.SourceText(get_text(new_text_document), get_language_id(doc)))
173+
JuliaWorkspaces.update_file!(server.workspace, new_text_file)
174+
end
146175

147176
if get_language_id(doc) in ("markdown", "juliamarkdown")
148-
parse_all(doc, server)
149-
lint!(doc, server)
150-
elseif get_language_id(doc) == "julia"
151-
cst0, cst1 = getcst(doc), CSTParser.parse(get_text(doc), true)
152-
r1, r2, r3 = CSTParser.minimal_reparse(s0, get_text(doc), cst0, cst1, inds = true)
153-
for i in setdiff(1:length(cst0.args), r1 , r3) # clean meta from deleted expr
154-
StaticLint.clear_meta(cst0[i])
177+
measure_sub_operation("parse_all md", server) do
178+
parse_all(doc, server)
179+
end
180+
measure_sub_operation("lint! md", server) do
181+
lint!(doc, server)
155182
end
156-
setcst(doc, EXPR(cst0.head, EXPR[cst0.args[r1]; cst1.args[r2]; cst0.args[r3]], nothing))
157-
sizeof(get_text(doc)) == getcst(doc).fullspan || @error "CST does not match input string length."
158-
headof(doc.cst) === :file ? set_doc(doc.cst, doc) : @info "headof(doc) isn't :file for $(doc._path)"
183+
elseif get_language_id(doc) == "julia"
184+
measure_sub_operation("overall tree diff", server) do
185+
cst0, cst1 = getcst(doc), CSTParser.parse(get_text(doc), true)
186+
r1, r2, r3 = CSTParser.minimal_reparse(s0, get_text(doc), cst0, cst1, inds = true)
187+
for i in setdiff(1:length(cst0.args), r1 , r3) # clean meta from deleted expr
188+
StaticLint.clear_meta(cst0[i])
189+
end
190+
setcst(doc, EXPR(cst0.head, EXPR[cst0.args[r1]; cst1.args[r2]; cst0.args[r3]], nothing))
191+
sizeof(get_text(doc)) == getcst(doc).fullspan || @error "CST does not match input string length."
192+
headof(doc.cst) === :file ? set_doc(doc.cst, doc) : @info "headof(doc) isn't :file for $(doc._path)"
193+
194+
target_exprs = getcst(doc).args[last(r1) .+ (1:length(r2))]
159195

160-
target_exprs = getcst(doc).args[last(r1) .+ (1:length(r2))]
161-
semantic_pass(getroot(doc), target_exprs)
162-
lint!(doc, server)
196+
measure_sub_operation("semantic_pass", server) do
197+
semantic_pass(getroot(doc), target_exprs)
198+
end
199+
measure_sub_operation("lint!", server) do
200+
lint!(doc, server)
201+
end
202+
end
163203
end
164204

165-
publish_diagnostics([get_uri(doc)], server, conn, "textDocument_didChange_notification")
166-
publish_tests(server)
205+
measure_sub_operation("publish_diagnostics", server) do
206+
publish_diagnostics([get_uri(doc)], server, conn, "textDocument_didChange_notification")
207+
end
208+
measure_sub_operation("publish_tests", server) do
209+
publish_tests(server)
210+
end
167211
end
168212

169213
function parse_all(doc::Document, server::LanguageServerInstance)

0 commit comments

Comments
 (0)