Debian Codegraph @

modules/gdscript/language_server/gdscript_text_document.cpp

raw symbols SCIP
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/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 GDScriptLanguageProtocol::get_singleton()->lsp_did_open(p_param);
67 }
68
69 void GDScriptTextDocument::didChange(const Variant &p_param) {
70 GDScriptLanguageProtocol::get_singleton()->lsp_did_change(p_param);
71 }
72
73 void GDScriptTextDocument::didClose(const Variant &p_param) {
74 GDScriptLanguageProtocol::get_singleton()->lsp_did_close(p_param);
75 }
76
77 void GDScriptTextDocument::willSaveWaitUntil(const Variant &p_param) {
78 Dictionary dict = p_param;
79 LSP::TextDocumentIdentifier doc;
80 doc.load(dict["textDocument"]);
81
82 String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
83 Ref<Script> scr = ResourceLoader::load(path);
84 if (scr.is_valid()) {
85 ScriptEditor::get_singleton()->clear_docs_from_script(scr);
86 }
87 }
88
89 void GDScriptTextDocument::didSave(const Variant &p_param) {
90 Dictionary dict = p_param;
91 LSP::TextDocumentIdentifier doc;
92 doc.load(dict["textDocument"]);
93 String text = dict["text"];
94
95 String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
96 Ref<GDScript> scr = ResourceLoader::load(path);
97 if (scr.is_valid() && (scr->load_source_code(path) == OK)) {
98 if (scr->is_tool()) {
99 scr->get_language()->reload_tool_script(scr, true);
100 } else {
101 scr->reload(true);
102 }
103
104 scr->update_exports();
105
106 if (!Thread::is_main_thread()) {
107 callable_mp(this, &GDScriptTextDocument::reload_script).call_deferred(scr);
108 } else {
109 reload_script(scr);
110 }
111 }
112 }
113
114 void GDScriptTextDocument::reload_script(Ref<GDScript> p_to_reload_script) {
115 ScriptEditor::get_singleton()->reload_scripts(true);
116 ScriptEditor::get_singleton()->update_docs_from_script(p_to_reload_script);
117 ScriptEditor::get_singleton()->trigger_live_script_reload(p_to_reload_script->get_path());
118 }
119
120 void GDScriptTextDocument::notify_client_show_symbol(const LSP::DocumentSymbol *symbol) {
121 ERR_FAIL_NULL(symbol);
122 GDScriptLanguageProtocol::get_singleton()->notify_client("gdscript/show_native_symbol", symbol->to_json(true));
123 }
124
125 void GDScriptTextDocument::initialize() {
126 if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
127 for (const KeyValue<StringName, ClassMembers> &E : GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members) {
128 const ClassMembers &members = E.value;
129
130 for (const KeyValue<String, const LSP::DocumentSymbol *> &F : members) {
131 const LSP::DocumentSymbol *symbol = members.get(F.key);
132 LSP::CompletionItem item = symbol->make_completion_item();
133 item.data = JOIN_SYMBOLS(String(E.key), F.key);
134 native_member_completions.push_back(item.to_json());
135 }
136 }
137 }
138 }
139
140 Variant GDScriptTextDocument::nativeSymbol(const Dictionary &p_params) {
141 Variant ret;
142
143 LSP::NativeSymbolInspectParams params;
144 params.load(p_params);
145
146 if (const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_native_symbol(params)) {
147 ret = symbol->to_json(true);
148 notify_client_show_symbol(symbol);
149 }
150
151 return ret;
152 }
153
154 Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
155 Dictionary params = p_params["textDocument"];
156 String uri = params["uri"];
157 String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri);
158 Array arr;
159
160 ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_parse_result(path);
161 if (parser) {
162 LSP::DocumentSymbol symbol = parser->get_symbols();
163 arr.push_back(symbol.to_json(true));
164 }
165 return arr;
166 }
167
168 Array GDScriptTextDocument::completion(const Dictionary &p_params) {
169 Array arr;
170
171 LSP::CompletionParams params;
172 params.load(p_params);
173 Dictionary request_data = params.to_json();
174
175 List<ScriptLanguage::CodeCompletionOption> options;
176 GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options);
177
178 if (!options.is_empty()) {
179 int i = 0;
180 arr.resize(options.size());
181
182 for (const ScriptLanguage::CodeCompletionOption &option : options) {
183 LSP::CompletionItem item;
184 item.label = option.display;
185 item.data = request_data;
186 item.insertText = option.insert_text;
187
188 switch (option.kind) {
189 case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
190 item.kind = LSP::CompletionItemKind::Enum;
191 break;
192 case ScriptLanguage::CODE_COMPLETION_KIND_CLASS:
193 item.kind = LSP::CompletionItemKind::Class;
194 break;
195 case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
196 item.kind = LSP::CompletionItemKind::Property;
197 break;
198 case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
199 item.kind = LSP::CompletionItemKind::Method;
200 break;
201 case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
202 item.kind = LSP::CompletionItemKind::Event;
203 break;
204 case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
205 item.kind = LSP::CompletionItemKind::Constant;
206 break;
207 case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
208 item.kind = LSP::CompletionItemKind::Variable;
209 break;
210 case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
211 item.kind = LSP::CompletionItemKind::File;
212 break;
213 case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
214 item.kind = LSP::CompletionItemKind::Snippet;
215 break;
216 case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
217 item.kind = LSP::CompletionItemKind::Text;
218 break;
219 default: {
220 }
221 }
222
223 arr[i] = item.to_json();
224 i++;
225 }
226 }
227 return arr;
228 }
229
230 Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {
231 LSP::TextDocumentPositionParams params;
232 params.load(p_params);
233 String new_name = p_params["newName"];
234
235 return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);
236 }
237
238 Variant GDScriptTextDocument::prepareRename(const Dictionary &p_params) {
239 LSP::TextDocumentPositionParams params;
240 params.load(p_params);
241
242 LSP::DocumentSymbol symbol;
243 LSP::Range range;
244 if (GDScriptLanguageProtocol::get_singleton()->get_workspace()->can_rename(params, symbol, range)) {
245 return Variant(range.to_json());
246 }
247
248 // `null` -> rename not valid at current location.
249 return Variant();
250 }
251
252 Array GDScriptTextDocument::references(const Dictionary &p_params) {
253 Array res;
254
255 LSP::ReferenceParams params;
256 params.load(p_params);
257
258 const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
259 if (symbol) {
260 Vector<LSP::Location> usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_all_usages(*symbol);
261 res.resize(usages.size());
262 int declaration_adjustment = 0;
263 for (int i = 0; i < usages.size(); i++) {
264 LSP::Location usage = usages[i];
265 if (!params.context.includeDeclaration && usage.range == symbol->range) {
266 declaration_adjustment++;
267 continue;
268 }
269 res[i - declaration_adjustment] = usages[i].to_json();
270 }
271
272 if (declaration_adjustment > 0) {
273 res.resize(res.size() - declaration_adjustment);
274 }
275 }
276
277 return res;
278 }
279
280 Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
281 LSP::CompletionItem item;
282 item.load(p_params);
283
284 LSP::CompletionParams params;
285 Variant data = p_params["data"];
286
287 const LSP::DocumentSymbol *symbol = nullptr;
288
289 if (data.get_type() == Variant::DICTIONARY) {
290 params.load(p_params["data"]);
291 symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == LSP::CompletionItemKind::Method || item.kind == LSP::CompletionItemKind::Function);
292
293 } else if (data.is_string()) {
294 String query = data;
295
296 Vector<String> param_symbols = query.split(SYMBOL_SEPARATOR, false);
297
298 if (param_symbols.size() >= 2) {
299 StringName class_name = param_symbols[0];
300 const String &member_name = param_symbols[param_symbols.size() - 1];
301 String inner_class_name;
302 if (param_symbols.size() >= 3) {
303 inner_class_name = param_symbols[1];
304 }
305
306 if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) {
307 if (const LSP::DocumentSymbol *const *member = members->getptr(member_name)) {
308 symbol = *member;
309 }
310 }
311
312 if (!symbol) {
313 ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_parse_result(class_name);
314 if (parser) {
315 symbol = parser->get_member_symbol(member_name, inner_class_name);
316 }
317 }
318 }
319 }
320
321 if (symbol) {
322 item.documentation = symbol->render();
323 }
324
325 if (item.kind == LSP::CompletionItemKind::Event) {
326 if (params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {
327 const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
328 item.insertText = item.label.quote(quote_style);
329 }
330 }
331
332 if (item.kind == LSP::CompletionItemKind::Method) {
333 bool is_trigger_character = params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter;
334 bool is_quote_character = params.context.triggerCharacter == "\"" || params.context.triggerCharacter == "'";
335
336 if (is_trigger_character && is_quote_character && item.insertText.is_quoted()) {
337 item.insertText = item.insertText.unquote();
338 }
339 }
340
341 return item.to_json(true);
342 }
343
344 Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
345 return Array();
346 }
347
348 Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
349 return Array();
350 }
351
352 Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {
353 Array ret;
354
355 LSP::DocumentLinkParams params;
356 params.load(p_params);
357
358 List<LSP::DocumentLink> links;
359 GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);
360 for (const LSP::DocumentLink &E : links) {
361 ret.push_back(E.to_json());
362 }
363 return ret;
364 }
365
366 Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
367 return Array();
368 }
369
370 Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
371 LSP::TextDocumentPositionParams params;
372 params.load(p_params);
373
374 const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
375 if (symbol) {
376 LSP::Hover hover;
377 hover.contents = symbol->render();
378 hover.range.start = params.position;
379 hover.range.end = params.position;
380 return hover.to_json();
381
382 } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
383 Dictionary ret;
384 Array contents;
385 List<const LSP::DocumentSymbol *> list;
386 GDScriptLanguageProtocol::get_singleton()->resolve_related_symbols(params, list);
387 for (const LSP::DocumentSymbol *&E : list) {
388 if (const LSP::DocumentSymbol *s = E) {
389 contents.push_back(s->render().value);
390 }
391 }
392 ret["contents"] = contents;
393 return ret;
394 }
395
396 return Variant();
397 }
398
399 Array GDScriptTextDocument::definition(const Dictionary &p_params) {
400 LSP::TextDocumentPositionParams params;
401 params.load(p_params);
402 List<const LSP::DocumentSymbol *> symbols;
403 return find_symbols(params, symbols);
404 }
405
406 Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {
407 LSP::TextDocumentPositionParams params;
408 params.load(p_params);
409 List<const LSP::DocumentSymbol *> symbols;
410 Array arr = find_symbols(params, symbols);
411 if (arr.is_empty() && !symbols.is_empty() && !symbols.front()->get()->native_class.is_empty()) { // Find a native symbol
412 const LSP::DocumentSymbol *symbol = symbols.front()->get();
413 if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {
414 String id;
415 switch (symbol->kind) {
416 case LSP::SymbolKind::Class:
417 id = "class_name:" + symbol->name;
418 break;
419 case LSP::SymbolKind::Constant:
420 id = "class_constant:" + symbol->native_class + ":" + symbol->name;
421 break;
422 case LSP::SymbolKind::Property:
423 case LSP::SymbolKind::Variable:
424 id = "class_property:" + symbol->native_class + ":" + symbol->name;
425 break;
426 case LSP::SymbolKind::Enum:
427 id = "class_enum:" + symbol->native_class + ":" + symbol->name;
428 break;
429 case LSP::SymbolKind::Method:
430 case LSP::SymbolKind::Function:
431 id = "class_method:" + symbol->native_class + ":" + symbol->name;
432 break;
433 default:
434 id = "class_global:" + symbol->native_class + ":" + symbol->name;
435 break;
436 }
437 callable_mp(this, &GDScriptTextDocument::show_native_symbol_in_editor).call_deferred(id);
438 } else {
439 notify_client_show_symbol(symbol);
440 }
441 }
442 return arr;
443 }
444
445 Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {
446 Variant ret;
447
448 LSP::TextDocumentPositionParams params;
449 params.load(p_params);
450
451 LSP::SignatureHelp s;
452 if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {
453 ret = s.to_json();
454 }
455
456 return ret;
457 }
458
459 GDScriptTextDocument::GDScriptTextDocument() {
460 file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
461 }
462
463 void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
464 callable_mp(ScriptEditor::get_singleton(), &ScriptEditor::goto_help).call_deferred(p_symbol_id);
465
466 DisplayServer::get_singleton()->window_move_to_foreground();
467 }
468
469 Array GDScriptTextDocument::find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list) {
470 Array arr;
471 const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);
472 if (symbol) {
473 LSP::Location location;
474 location.uri = symbol->uri;
475 if (!location.uri.is_empty()) {
476 location.range = symbol->selectionRange;
477 const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);
478 if (file_checker->file_exists(path)) {
479 arr.push_back(location.to_json());
480 }
481 }
482 r_list.push_back(symbol);
483 } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
484 List<const LSP::DocumentSymbol *> list;
485 GDScriptLanguageProtocol::get_singleton()->resolve_related_symbols(p_location, list);
486 for (const LSP::DocumentSymbol *&E : list) {
487 if (const LSP::DocumentSymbol *s = E) {
488 if (!s->uri.is_empty()) {
489 LSP::Location location;
490 location.uri = s->uri;
491 location.range = s->selectionRange;
492 arr.push_back(location.to_json());
493 r_list.push_back(s);
494 }
495 }
496 }
497 }
498 return arr;
499 }