Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/language_server/gdscript_text_document.cpp
10278 views
1
/**************************************************************************/
2
/* gdscript_text_document.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "gdscript_text_document.h"
32
33
#include "../gdscript.h"
34
#include "gdscript_extend_parser.h"
35
#include "gdscript_language_protocol.h"
36
37
#include "editor/script/script_text_editor.h"
38
#include "editor/settings/editor_settings.h"
39
#include "servers/display_server.h"
40
41
void GDScriptTextDocument::_bind_methods() {
42
ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen);
43
ClassDB::bind_method(D_METHOD("didClose"), &GDScriptTextDocument::didClose);
44
ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange);
45
ClassDB::bind_method(D_METHOD("willSaveWaitUntil"), &GDScriptTextDocument::willSaveWaitUntil);
46
ClassDB::bind_method(D_METHOD("didSave"), &GDScriptTextDocument::didSave);
47
ClassDB::bind_method(D_METHOD("nativeSymbol"), &GDScriptTextDocument::nativeSymbol);
48
ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol);
49
ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);
50
ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve);
51
ClassDB::bind_method(D_METHOD("rename"), &GDScriptTextDocument::rename);
52
ClassDB::bind_method(D_METHOD("prepareRename"), &GDScriptTextDocument::prepareRename);
53
ClassDB::bind_method(D_METHOD("references"), &GDScriptTextDocument::references);
54
ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange);
55
ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);
56
ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);
57
ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);
58
ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);
59
ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition);
60
ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration);
61
ClassDB::bind_method(D_METHOD("signatureHelp"), &GDScriptTextDocument::signatureHelp);
62
ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor);
63
}
64
65
void GDScriptTextDocument::didOpen(const Variant &p_param) {
66
LSP::TextDocumentItem doc = load_document_item(p_param);
67
sync_script_content(doc.uri, doc.text);
68
}
69
70
void GDScriptTextDocument::didClose(const Variant &p_param) {
71
// Left empty on purpose. Godot does nothing special on closing a document,
72
// but it satisfies LSP clients that require didClose be implemented.
73
}
74
75
void GDScriptTextDocument::didChange(const Variant &p_param) {
76
LSP::TextDocumentItem doc = load_document_item(p_param);
77
Dictionary dict = p_param;
78
Array contentChanges = dict["contentChanges"];
79
for (int i = 0; i < contentChanges.size(); ++i) {
80
LSP::TextDocumentContentChangeEvent evt;
81
evt.load(contentChanges[i]);
82
doc.text = evt.text;
83
}
84
sync_script_content(doc.uri, doc.text);
85
}
86
87
void GDScriptTextDocument::willSaveWaitUntil(const Variant &p_param) {
88
LSP::TextDocumentItem doc = load_document_item(p_param);
89
90
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
91
Ref<Script> scr = ResourceLoader::load(path);
92
if (scr.is_valid()) {
93
ScriptEditor::get_singleton()->clear_docs_from_script(scr);
94
}
95
}
96
97
void GDScriptTextDocument::didSave(const Variant &p_param) {
98
LSP::TextDocumentItem doc = load_document_item(p_param);
99
Dictionary dict = p_param;
100
String text = dict["text"];
101
102
sync_script_content(doc.uri, text);
103
104
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
105
Ref<GDScript> scr = ResourceLoader::load(path);
106
if (scr.is_valid() && (scr->load_source_code(path) == OK)) {
107
if (scr->is_tool()) {
108
scr->get_language()->reload_tool_script(scr, true);
109
} else {
110
scr->reload(true);
111
}
112
113
scr->update_exports();
114
115
if (!Thread::is_main_thread()) {
116
callable_mp(this, &GDScriptTextDocument::reload_script).call_deferred(scr);
117
} else {
118
reload_script(scr);
119
}
120
}
121
}
122
123
void GDScriptTextDocument::reload_script(Ref<GDScript> p_to_reload_script) {
124
ScriptEditor::get_singleton()->reload_scripts(true);
125
ScriptEditor::get_singleton()->update_docs_from_script(p_to_reload_script);
126
ScriptEditor::get_singleton()->trigger_live_script_reload(p_to_reload_script->get_path());
127
}
128
129
LSP::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) {
130
LSP::TextDocumentItem doc;
131
Dictionary params = p_param;
132
doc.load(params["textDocument"]);
133
return doc;
134
}
135
136
void GDScriptTextDocument::notify_client_show_symbol(const LSP::DocumentSymbol *symbol) {
137
ERR_FAIL_NULL(symbol);
138
GDScriptLanguageProtocol::get_singleton()->notify_client("gdscript/show_native_symbol", symbol->to_json(true));
139
}
140
141
void GDScriptTextDocument::initialize() {
142
if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
143
for (const KeyValue<StringName, ClassMembers> &E : GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members) {
144
const ClassMembers &members = E.value;
145
146
for (const KeyValue<String, const LSP::DocumentSymbol *> &F : members) {
147
const LSP::DocumentSymbol *symbol = members.get(F.key);
148
LSP::CompletionItem item = symbol->make_completion_item();
149
item.data = JOIN_SYMBOLS(String(E.key), F.key);
150
native_member_completions.push_back(item.to_json());
151
}
152
}
153
}
154
}
155
156
Variant GDScriptTextDocument::nativeSymbol(const Dictionary &p_params) {
157
Variant ret;
158
159
LSP::NativeSymbolInspectParams params;
160
params.load(p_params);
161
162
if (const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_native_symbol(params)) {
163
ret = symbol->to_json(true);
164
notify_client_show_symbol(symbol);
165
}
166
167
return ret;
168
}
169
170
Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
171
Dictionary params = p_params["textDocument"];
172
String uri = params["uri"];
173
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri);
174
Array arr;
175
if (HashMap<String, ExtendGDScriptParser *>::ConstIterator parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) {
176
LSP::DocumentSymbol symbol = parser->value->get_symbols();
177
arr.push_back(symbol.to_json(true));
178
}
179
return arr;
180
}
181
182
Array GDScriptTextDocument::completion(const Dictionary &p_params) {
183
Array arr;
184
185
LSP::CompletionParams params;
186
params.load(p_params);
187
Dictionary request_data = params.to_json();
188
189
List<ScriptLanguage::CodeCompletionOption> options;
190
GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options);
191
192
if (!options.is_empty()) {
193
int i = 0;
194
arr.resize(options.size());
195
196
for (const ScriptLanguage::CodeCompletionOption &option : options) {
197
LSP::CompletionItem item;
198
item.label = option.display;
199
item.data = request_data;
200
item.insertText = option.insert_text;
201
202
switch (option.kind) {
203
case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
204
item.kind = LSP::CompletionItemKind::Enum;
205
break;
206
case ScriptLanguage::CODE_COMPLETION_KIND_CLASS:
207
item.kind = LSP::CompletionItemKind::Class;
208
break;
209
case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
210
item.kind = LSP::CompletionItemKind::Property;
211
break;
212
case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
213
item.kind = LSP::CompletionItemKind::Method;
214
break;
215
case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
216
item.kind = LSP::CompletionItemKind::Event;
217
break;
218
case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
219
item.kind = LSP::CompletionItemKind::Constant;
220
break;
221
case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
222
item.kind = LSP::CompletionItemKind::Variable;
223
break;
224
case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
225
item.kind = LSP::CompletionItemKind::File;
226
break;
227
case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
228
item.kind = LSP::CompletionItemKind::Snippet;
229
break;
230
case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
231
item.kind = LSP::CompletionItemKind::Text;
232
break;
233
default: {
234
}
235
}
236
237
arr[i] = item.to_json();
238
i++;
239
}
240
}
241
return arr;
242
}
243
244
Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {
245
LSP::TextDocumentPositionParams params;
246
params.load(p_params);
247
String new_name = p_params["newName"];
248
249
return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);
250
}
251
252
Variant GDScriptTextDocument::prepareRename(const Dictionary &p_params) {
253
LSP::TextDocumentPositionParams params;
254
params.load(p_params);
255
256
LSP::DocumentSymbol symbol;
257
LSP::Range range;
258
if (GDScriptLanguageProtocol::get_singleton()->get_workspace()->can_rename(params, symbol, range)) {
259
return Variant(range.to_json());
260
}
261
262
// `null` -> rename not valid at current location.
263
return Variant();
264
}
265
266
Array GDScriptTextDocument::references(const Dictionary &p_params) {
267
Array res;
268
269
LSP::ReferenceParams params;
270
params.load(p_params);
271
272
const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
273
if (symbol) {
274
Vector<LSP::Location> usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_all_usages(*symbol);
275
res.resize(usages.size());
276
int declaration_adjustment = 0;
277
for (int i = 0; i < usages.size(); i++) {
278
LSP::Location usage = usages[i];
279
if (!params.context.includeDeclaration && usage.range == symbol->range) {
280
declaration_adjustment++;
281
continue;
282
}
283
res[i - declaration_adjustment] = usages[i].to_json();
284
}
285
286
if (declaration_adjustment > 0) {
287
res.resize(res.size() - declaration_adjustment);
288
}
289
}
290
291
return res;
292
}
293
294
Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
295
LSP::CompletionItem item;
296
item.load(p_params);
297
298
LSP::CompletionParams params;
299
Variant data = p_params["data"];
300
301
const LSP::DocumentSymbol *symbol = nullptr;
302
303
if (data.get_type() == Variant::DICTIONARY) {
304
params.load(p_params["data"]);
305
symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == LSP::CompletionItemKind::Method || item.kind == LSP::CompletionItemKind::Function);
306
307
} else if (data.is_string()) {
308
String query = data;
309
310
Vector<String> param_symbols = query.split(SYMBOL_SEPARATOR, false);
311
312
if (param_symbols.size() >= 2) {
313
StringName class_name = param_symbols[0];
314
const String &member_name = param_symbols[param_symbols.size() - 1];
315
String inner_class_name;
316
if (param_symbols.size() >= 3) {
317
inner_class_name = param_symbols[1];
318
}
319
320
if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) {
321
if (const LSP::DocumentSymbol *const *member = members->getptr(member_name)) {
322
symbol = *member;
323
}
324
}
325
326
if (!symbol) {
327
if (HashMap<String, ExtendGDScriptParser *>::ConstIterator E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) {
328
symbol = E->value->get_member_symbol(member_name, inner_class_name);
329
}
330
}
331
}
332
}
333
334
if (symbol) {
335
item.documentation = symbol->render();
336
}
337
338
if (item.kind == LSP::CompletionItemKind::Event) {
339
if (params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {
340
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
341
item.insertText = item.label.quote(quote_style);
342
}
343
}
344
345
if (item.kind == LSP::CompletionItemKind::Method) {
346
bool is_trigger_character = params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter;
347
bool is_quote_character = params.context.triggerCharacter == "\"" || params.context.triggerCharacter == "'";
348
349
if (is_trigger_character && is_quote_character && item.insertText.is_quoted()) {
350
item.insertText = item.insertText.unquote();
351
}
352
}
353
354
return item.to_json(true);
355
}
356
357
Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
358
Array arr;
359
return arr;
360
}
361
362
Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
363
Array arr;
364
return arr;
365
}
366
367
Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {
368
Array ret;
369
370
LSP::DocumentLinkParams params;
371
params.load(p_params);
372
373
List<LSP::DocumentLink> links;
374
GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);
375
for (const LSP::DocumentLink &E : links) {
376
ret.push_back(E.to_json());
377
}
378
return ret;
379
}
380
381
Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
382
Array arr;
383
return arr;
384
}
385
386
Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
387
LSP::TextDocumentPositionParams params;
388
params.load(p_params);
389
390
const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
391
if (symbol) {
392
LSP::Hover hover;
393
hover.contents = symbol->render();
394
hover.range.start = params.position;
395
hover.range.end = params.position;
396
return hover.to_json();
397
398
} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
399
Dictionary ret;
400
Array contents;
401
List<const LSP::DocumentSymbol *> list;
402
GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list);
403
for (const LSP::DocumentSymbol *&E : list) {
404
if (const LSP::DocumentSymbol *s = E) {
405
contents.push_back(s->render().value);
406
}
407
}
408
ret["contents"] = contents;
409
return ret;
410
}
411
412
return Variant();
413
}
414
415
Array GDScriptTextDocument::definition(const Dictionary &p_params) {
416
LSP::TextDocumentPositionParams params;
417
params.load(p_params);
418
List<const LSP::DocumentSymbol *> symbols;
419
Array arr = find_symbols(params, symbols);
420
return arr;
421
}
422
423
Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {
424
LSP::TextDocumentPositionParams params;
425
params.load(p_params);
426
List<const LSP::DocumentSymbol *> symbols;
427
Array arr = find_symbols(params, symbols);
428
if (arr.is_empty() && !symbols.is_empty() && !symbols.front()->get()->native_class.is_empty()) { // Find a native symbol
429
const LSP::DocumentSymbol *symbol = symbols.front()->get();
430
if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {
431
String id;
432
switch (symbol->kind) {
433
case LSP::SymbolKind::Class:
434
id = "class_name:" + symbol->name;
435
break;
436
case LSP::SymbolKind::Constant:
437
id = "class_constant:" + symbol->native_class + ":" + symbol->name;
438
break;
439
case LSP::SymbolKind::Property:
440
case LSP::SymbolKind::Variable:
441
id = "class_property:" + symbol->native_class + ":" + symbol->name;
442
break;
443
case LSP::SymbolKind::Enum:
444
id = "class_enum:" + symbol->native_class + ":" + symbol->name;
445
break;
446
case LSP::SymbolKind::Method:
447
case LSP::SymbolKind::Function:
448
id = "class_method:" + symbol->native_class + ":" + symbol->name;
449
break;
450
default:
451
id = "class_global:" + symbol->native_class + ":" + symbol->name;
452
break;
453
}
454
callable_mp(this, &GDScriptTextDocument::show_native_symbol_in_editor).call_deferred(id);
455
} else {
456
notify_client_show_symbol(symbol);
457
}
458
}
459
return arr;
460
}
461
462
Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {
463
Variant ret;
464
465
LSP::TextDocumentPositionParams params;
466
params.load(p_params);
467
468
LSP::SignatureHelp s;
469
if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {
470
ret = s.to_json();
471
}
472
473
return ret;
474
}
475
476
GDScriptTextDocument::GDScriptTextDocument() {
477
file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
478
}
479
480
void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {
481
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);
482
GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);
483
}
484
485
void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
486
callable_mp(ScriptEditor::get_singleton(), &ScriptEditor::goto_help).call_deferred(p_symbol_id);
487
488
DisplayServer::get_singleton()->window_move_to_foreground();
489
}
490
491
Array GDScriptTextDocument::find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list) {
492
Array arr;
493
const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);
494
if (symbol) {
495
LSP::Location location;
496
location.uri = symbol->uri;
497
if (!location.uri.is_empty()) {
498
location.range = symbol->selectionRange;
499
const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);
500
if (file_checker->file_exists(path)) {
501
arr.push_back(location.to_json());
502
}
503
r_list.push_back(symbol);
504
}
505
} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
506
List<const LSP::DocumentSymbol *> list;
507
GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list);
508
for (const LSP::DocumentSymbol *&E : list) {
509
if (const LSP::DocumentSymbol *s = E) {
510
if (!s->uri.is_empty()) {
511
LSP::Location location;
512
location.uri = s->uri;
513
location.range = s->selectionRange;
514
arr.push_back(location.to_json());
515
r_list.push_back(s);
516
}
517
}
518
}
519
}
520
return arr;
521
}
522
523