/
textdocument.jl
354 lines (311 loc) · 13.9 KB
/
textdocument.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
function textDocument_didOpen_notification(params::DidOpenTextDocumentParams, server::LanguageServerInstance, conn)
uri = params.textDocument.uri
if hasdocument(server, uri)
doc = getdocument(server, uri)
set_text_document!(doc, TextDocument(uri, params.textDocument.text, params.textDocument.version))
set_open_in_editor(doc, true)
else
doc = Document(TextDocument(uri, params.textDocument.text, params.textDocument.version), false, server)
setdocument!(server, uri, doc)
doc._workspace_file = any(i -> startswith(string(uri), string(filepath2uri(i))), server.workspaceFolders)
set_open_in_editor(doc, true)
fpath = getpath(doc)
!isempty(fpath) && try_to_load_parents(fpath, server)
end
parse_all(doc, server)
end
function textDocument_didClose_notification(params::DidCloseTextDocumentParams, server::LanguageServerInstance, conn)
uri = params.textDocument.uri
doc = getdocument(server, uri)
if is_workspace_file(doc)
set_open_in_editor(doc, false)
else
if any(getroot(d) == getroot(doc) && (d._open_in_editor || is_workspace_file(d)) for (uri, d::Document) in getdocuments_pair(server) if d != doc)
# If any other open document shares doc's root we just mark it as closed...
set_open_in_editor(doc, false)
else
# ...otherwise we delete all documents that share root with doc.
for (u, d) in getdocuments_pair(server)
if getroot(d) == getroot(doc)
deletedocument!(server, u)
empty!(doc.diagnostics)
publish_diagnostics(doc, server, conn)
end
end
end
end
end
function textDocument_didSave_notification(params::DidSaveTextDocumentParams, server::LanguageServerInstance, conn)
uri = params.textDocument.uri
doc = getdocument(server, uri)
if params.text isa String
if get_text(doc) != params.text
JSONRPC.send(conn, window_showMessage_notification_type, ShowMessageParams(MessageTypes.Error, "Julia Extension: Please contact us! Your extension just crashed with a bug that we have been trying to replicate for a long time. You could help the development team a lot by contacting us at https://github.com/julia-vscode/julia-vscode so that we can work together to fix this issue."))
throw(LSSyncMismatch("Mismatch between server and client text for $(get_uri(doc)). _open_in_editor is $(doc._open_in_editor). _workspace_file is $(doc._workspace_file). _version is $(get_version(doc))."))
end
end
parse_all(doc, server)
end
function textDocument_willSave_notification(params::WillSaveTextDocumentParams, server::LanguageServerInstance, conn)
end
function textDocument_willSaveWaitUntil_request(params::WillSaveTextDocumentParams, server::LanguageServerInstance, conn)
return TextEdit[]
end
comp(x, y) = x == y
function comp(x::CSTParser.EXPR, y::CSTParser.EXPR)
comp(x.head, y.head) &&
x.span == y.span &&
x.fullspan == y.fullspan &&
x.val == y.val &&
length(x) == length(y) &&
all(comp(x[i], y[i]) for i = 1:length(x))
end
function textDocument_didChange_notification(params::DidChangeTextDocumentParams, server::LanguageServerInstance, conn)
doc = getdocument(server, params.textDocument.uri)
s0 = get_text(doc)
if params.textDocument.version < get_version(doc)
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).")
end
new_text_document = apply_text_edits(get_text_document(doc), params.contentChanges, params.textDocument.version)
set_text_document!(doc, new_text_document)
if endswith(get_uri(doc).path, ".jmd")
parse_all(doc, server)
else
cst0, cst1 = getcst(doc), CSTParser.parse(get_text(doc), true)
r1, r2, r3 = CSTParser.minimal_reparse(s0, get_text(doc), cst0, cst1, inds = true)
for i in setdiff(1:length(cst0.args), r1 , r3) # clean meta from deleted expr
StaticLint.clear_meta(cst0[i])
end
setcst(doc, EXPR(cst0.head, EXPR[cst0.args[r1]; cst1.args[r2]; cst0.args[r3]], nothing))
sizeof(get_text(doc)) == getcst(doc).fullspan || @error "CST does not match input string length."
headof(doc.cst) === :file ? set_doc(doc.cst, doc) : @info "headof(doc) isn't :file for $(doc._path)"
target_exprs = getcst(doc).args[last(r1) .+ (1:length(r2))]
semantic_pass(getroot(doc), target_exprs)
lint!(doc, server)
end
end
function parse_all(doc::Document, server::LanguageServerInstance)
ps = CSTParser.ParseState(get_text(doc))
StaticLint.clear_meta(getcst(doc))
if endswith(get_uri(get_text_document(doc)).path, ".jmd")
doc.cst, ps = parse_jmd(ps, get_text(doc))
else
doc.cst, ps = CSTParser.parse(ps, true)
end
sizeof(get_text(doc)) == getcst(doc).fullspan || @error "CST does not match input string length."
if headof(doc.cst) === :file
set_doc(doc.cst, doc)
end
semantic_pass(getroot(doc))
lint!(doc, server)
end
function mark_errors(doc, out=Diagnostic[])
line_offsets = get_line_offsets(get_text_document(doc))
errs = StaticLint.collect_hints(getcst(doc), getenv(doc), doc.server.lint_missingrefs)
n = length(errs)
n == 0 && return out
i = 1
start = true
offset = errs[i][1]
r = Int[0, 0]
nlines = length(line_offsets)
if offset > last(line_offsets)
line = nlines
else
line = 1
io = IOBuffer(get_text(doc))
while line < nlines
seek(io, line_offsets[line])
char = 0
while line_offsets[line] <= offset < line_offsets[line + 1]
while offset > position(io)
c = read(io, Char)
if UInt32(c) >= 0x010000
char += 1
end
char += 1
end
if start
r[1] = line
r[2] = char
offset += errs[i][2].span
else
if headof(errs[i][2]) === :errortoken
push!(out, Diagnostic(Range(r[1] - 1, r[2], line - 1, char), DiagnosticSeverities.Error, missing, "Julia", "Parsing error", missing, missing))
elseif CSTParser.isidentifier(errs[i][2]) && !StaticLint.haserror(errs[i][2])
push!(out, Diagnostic(Range(r[1] - 1, r[2], line - 1, char), DiagnosticSeverities.Warning, missing, "Julia", "Missing reference: $(errs[i][2].val)", missing, missing))
elseif StaticLint.haserror(errs[i][2]) && StaticLint.errorof(errs[i][2]) isa StaticLint.LintCodes
if StaticLint.errorof(errs[i][2]) in (StaticLint.UnusedFunctionArgument, StaticLint.UnusedBinding, StaticLint.UnusedTypeParameter)
push!(out, Diagnostic(Range(r[1] - 1, r[2], line - 1, char), DiagnosticSeverities.Hint, missing, "Julia", get(StaticLint.LintCodeDescriptions, StaticLint.errorof(errs[i][2]), ""), [DiagnosticTags.Unnecessary], missing))
else
push!(out, Diagnostic(Range(r[1] - 1, r[2], line - 1, char), DiagnosticSeverities.Information, missing, "Julia", get(StaticLint.LintCodeDescriptions, StaticLint.errorof(errs[i][2]), ""), missing, missing))
end
end
i += 1
i > n && break
offset = errs[i][1]
end
start = !start
offset = start ? errs[i][1] : errs[i][1] + errs[i][2].span
end
line += 1
end
close(io)
end
return out
end
isunsavedfile(doc::Document) = get_uri(doc).scheme == "untitled" # Not clear if this is consistent across editors.
"""
is_diag_dependent_on_env(diag::Diagnostic)::Bool
Is this diagnostic reliant on the current environment being accurately represented?
"""
function is_diag_dependent_on_env(diag::Diagnostic)
startswith(diag.message, "Missing reference: ") ||
startswith(diag.message, "Possible method call error") ||
startswith(diag.message, "An imported")
end
function publish_diagnostics(doc::Document, server, conn)
diagnostics = if server.runlinter && server.symbol_store_ready && (is_workspace_file(doc) || isunsavedfile(doc))
pkgpath = getpath(doc)
if any(is_in_target_dir_of_package.(Ref(pkgpath), server.lint_disableddirs))
filter!(!is_diag_dependent_on_env, doc.diagnostics)
end
doc.diagnostics
else
Diagnostic[]
end
text_document = get_text_document(doc)
params = PublishDiagnosticsParams(get_uri(text_document), get_version(text_document), diagnostics)
JSONRPC.send(conn, textDocument_publishDiagnostics_notification_type, params)
end
function clear_diagnostics(uri::URI, server, conn)
doc = getdocument(server, uri)
empty!(doc.diagnostics)
publishDiagnosticsParams = PublishDiagnosticsParams(get_uri(doc), get_version(doc), Diagnostic[])
JSONRPC.send(conn, textDocument_publishDiagnostics_notification_type, publishDiagnosticsParams)
end
function clear_diagnostics(server, conn)
for uri in getdocuments_key(server)
clear_diagnostics(uri, server, conn)
end
end
function parse_jmd(ps, str)
currentbyte = 1
blocks = []
while ps.nt.kind != Tokens.ENDMARKER
CSTParser.next(ps)
if ps.t.kind == Tokens.CMD || ps.t.kind == Tokens.TRIPLE_CMD
push!(blocks, (ps.t.startbyte, CSTParser.INSTANCE(ps)))
end
end
top = EXPR(:file, EXPR[], nothing)
if isempty(blocks)
return top, ps
end
for (startbyte, b) in blocks
if CSTParser.ismacrocall(b) && headof(b.args[1]) === :globalrefcmd && headof(b.args[3]) === :TRIPLESTRING && (startswith(b.args[3].val, "julia") || startswith(b.args[3].val, "{julia"))
blockstr = b.args[3].val
ps = CSTParser.ParseState(blockstr)
# skip first line
while ps.nt.startpos[1] == 1
CSTParser.next(ps)
end
prec_str_size = currentbyte:startbyte + ps.nt.startbyte + 3
push!(top, EXPR(:STRING, length(prec_str_size), length(prec_str_size)))
args, ps = CSTParser.parse(ps, true)
for a in args.args
push!(top, a)
end
CSTParser.update_span!(top)
currentbyte = top.fullspan + 1
elseif CSTParser.ismacrocall(b) && headof(b.args[1]) === :globalrefcmd && headof(b.args[3]) === :STRING && b.val !== nothing && startswith(b.val, "j ")
blockstr = b.args[3].val
ps = CSTParser.ParseState(blockstr)
CSTParser.next(ps)
prec_str_size = currentbyte:startbyte + ps.nt.startbyte + 1
push!(top, EXPR(:STRING, length(prec_str_size), length(prec_str_size)))
args, ps = CSTParser.parse(ps, true)
for a in args.args
push!(top, a)
end
CSTParser.update_span!(top)
currentbyte = top.fullspan + 1
end
end
prec_str_size = currentbyte:sizeof(str) # OK
push!(top, EXPR(:STRING, length(prec_str_size), length(prec_str_size)))
CSTParser.update_span!(top)
return top, ps
end
function search_for_parent(dir::String, file::String, drop=3, parents=String[])
drop < 1 && return parents
try
!isdir(dir) && return parents
!hasreadperm(dir) && return parents
for f in readdir(dir)
filename = joinpath(dir, f)
if isvalidjlfile(filename)
# Could be sped up?
content = try
s = read(filename, String)
isvalid(s) || continue
s
catch err
isa(err, Base.IOError) || isa(err, Base.SystemError) || rethrow()
continue
end
occursin(file, content) && push!(parents, joinpath(dir, f))
end
end
search_for_parent(splitdir(dir)[1], file, drop - 1, parents)
catch err
isa(err, Base.IOError) || isa(err, Base.SystemError) || rethrow()
return parents
end
return parents
end
function is_parentof(parent_path, child_path, server)
!isvalidjlfile(parent_path) && return false
previous_server_docs = collect(getdocuments_key(server)) # additions to this to be removed at end
# load parent file
puri = filepath2uri(parent_path)
if !hasdocument(server, puri)
content = try
s = read(parent_path, String)
isvalid(s) || return false
s
catch err
isa(err, Base.IOError) || isa(err, Base.SystemError) || rethrow()
return false
end
pdoc = Document(TextDocument(puri, content, 0), false, server)
setdocument!(server, puri, pdoc)
CSTParser.parse(get_text(pdoc), true)
if headof(pdoc.cst) === :file
set_doc(pdoc.cst, pdoc)
end
else
pdoc = getdocument(server, puri)
end
semantic_pass(getroot(pdoc))
# check whether child has been included automatically
if any(getpath(d) == child_path for (k, d) in getdocuments_pair(server) if !(k in previous_server_docs))
cdoc = getdocument(server, filepath2uri(child_path))
parse_all(cdoc, server)
semantic_pass(getroot(cdoc))
return true, "", CSTParser.Tokens.STRING
else
# clean up
foreach(k -> !(k in previous_server_docs) && deletedocument!(server, k), getdocuments_key(server))
return false
end
end
function try_to_load_parents(child_path, server)
for p in search_for_parent(splitdir(child_path)...)
p == child_path && continue
success = is_parentof(p, child_path, server)
if success
return try_to_load_parents(p, server)
end
end
end