Path: blob/master/modules/gdscript/language_server/gdscript_text_document.cpp
20831 views
/**************************************************************************/1/* gdscript_text_document.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gdscript_text_document.h"3132#include "../gdscript.h"33#include "gdscript_extend_parser.h"34#include "gdscript_language_protocol.h"3536#include "editor/script/script_text_editor.h"37#include "editor/settings/editor_settings.h"38#include "servers/display/display_server.h"3940void GDScriptTextDocument::_bind_methods() {41ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen);42ClassDB::bind_method(D_METHOD("didClose"), &GDScriptTextDocument::didClose);43ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange);44ClassDB::bind_method(D_METHOD("willSaveWaitUntil"), &GDScriptTextDocument::willSaveWaitUntil);45ClassDB::bind_method(D_METHOD("didSave"), &GDScriptTextDocument::didSave);46ClassDB::bind_method(D_METHOD("nativeSymbol"), &GDScriptTextDocument::nativeSymbol);47ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol);48ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);49ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve);50ClassDB::bind_method(D_METHOD("rename"), &GDScriptTextDocument::rename);51ClassDB::bind_method(D_METHOD("prepareRename"), &GDScriptTextDocument::prepareRename);52ClassDB::bind_method(D_METHOD("references"), &GDScriptTextDocument::references);53ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange);54ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);55ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);56ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);57ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);58ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition);59ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration);60ClassDB::bind_method(D_METHOD("signatureHelp"), &GDScriptTextDocument::signatureHelp);61ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor);62}6364void GDScriptTextDocument::didOpen(const Variant &p_param) {65GDScriptLanguageProtocol::get_singleton()->lsp_did_open(p_param);66}6768void GDScriptTextDocument::didChange(const Variant &p_param) {69GDScriptLanguageProtocol::get_singleton()->lsp_did_change(p_param);70}7172void GDScriptTextDocument::didClose(const Variant &p_param) {73GDScriptLanguageProtocol::get_singleton()->lsp_did_close(p_param);74}7576void GDScriptTextDocument::willSaveWaitUntil(const Variant &p_param) {77Dictionary dict = p_param;78LSP::TextDocumentIdentifier doc;79doc.load(dict["textDocument"]);8081String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);82Ref<Script> scr = ResourceLoader::load(path);83if (scr.is_valid()) {84ScriptEditor::get_singleton()->clear_docs_from_script(scr);85}86}8788void GDScriptTextDocument::didSave(const Variant &p_param) {89Dictionary dict = p_param;90LSP::TextDocumentIdentifier doc;91doc.load(dict["textDocument"]);92String text = dict["text"];9394String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);95Ref<GDScript> scr = ResourceLoader::load(path);96if (scr.is_valid() && (scr->load_source_code(path) == OK)) {97if (scr->is_tool()) {98scr->get_language()->reload_tool_script(scr, true);99} else {100scr->reload(true);101}102103scr->update_exports();104105if (!Thread::is_main_thread()) {106callable_mp(this, &GDScriptTextDocument::reload_script).call_deferred(scr);107} else {108reload_script(scr);109}110}111}112113void GDScriptTextDocument::reload_script(Ref<GDScript> p_to_reload_script) {114ScriptEditor::get_singleton()->reload_scripts(true);115ScriptEditor::get_singleton()->update_docs_from_script(p_to_reload_script);116ScriptEditor::get_singleton()->trigger_live_script_reload(p_to_reload_script->get_path());117}118119void GDScriptTextDocument::notify_client_show_symbol(const LSP::DocumentSymbol *symbol) {120ERR_FAIL_NULL(symbol);121GDScriptLanguageProtocol::get_singleton()->notify_client("gdscript/show_native_symbol", symbol->to_json(true));122}123124Variant GDScriptTextDocument::nativeSymbol(const Dictionary &p_params) {125Variant ret;126127LSP::NativeSymbolInspectParams params;128params.load(p_params);129130if (const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_native_symbol(params)) {131ret = symbol->to_json(true);132notify_client_show_symbol(symbol);133}134135return ret;136}137138Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {139Dictionary params = p_params["textDocument"];140String uri = params["uri"];141String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri);142Array arr;143144ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_parse_result(path);145if (parser) {146LSP::DocumentSymbol symbol = parser->get_symbols();147arr.push_back(symbol.to_json(true));148}149return arr;150}151152Array GDScriptTextDocument::documentHighlight(const Dictionary &p_params) {153Array arr;154LSP::TextDocumentPositionParams params;155params.load(p_params);156157const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);158if (symbol) {159String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(params.textDocument.uri);160Vector<LSP::Location> usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_usages_in_file(*symbol, path);161162for (const LSP::Location &usage : usages) {163LSP::DocumentHighlight highlight;164highlight.range = usage.range;165arr.push_back(highlight.to_json());166}167}168return arr;169}170171Array GDScriptTextDocument::completion(const Dictionary &p_params) {172Array arr;173174LSP::CompletionParams params;175params.load(p_params);176Dictionary request_data = params.to_json();177178List<ScriptLanguage::CodeCompletionOption> options;179GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options);180181if (!options.is_empty()) {182int i = 0;183arr.resize(options.size());184185for (const ScriptLanguage::CodeCompletionOption &option : options) {186LSP::CompletionItem item;187item.label = option.display;188item.data = request_data;189item.insertText = option.insert_text;190191switch (option.kind) {192case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:193item.kind = LSP::CompletionItemKind::Enum;194break;195case ScriptLanguage::CODE_COMPLETION_KIND_CLASS:196item.kind = LSP::CompletionItemKind::Class;197break;198case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:199item.kind = LSP::CompletionItemKind::Property;200break;201case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:202item.kind = LSP::CompletionItemKind::Method;203break;204case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:205item.kind = LSP::CompletionItemKind::Event;206break;207case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:208item.kind = LSP::CompletionItemKind::Constant;209break;210case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:211item.kind = LSP::CompletionItemKind::Variable;212break;213case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:214item.kind = LSP::CompletionItemKind::File;215break;216case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:217item.kind = LSP::CompletionItemKind::Snippet;218break;219case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:220item.kind = LSP::CompletionItemKind::Text;221break;222default: {223}224}225226arr[i] = item.to_json();227i++;228}229}230return arr;231}232233Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {234LSP::TextDocumentPositionParams params;235params.load(p_params);236String new_name = p_params["newName"];237238return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);239}240241Variant GDScriptTextDocument::prepareRename(const Dictionary &p_params) {242LSP::TextDocumentPositionParams params;243params.load(p_params);244245LSP::DocumentSymbol symbol;246LSP::Range range;247if (GDScriptLanguageProtocol::get_singleton()->get_workspace()->can_rename(params, symbol, range)) {248return Variant(range.to_json());249}250251// `null` -> rename not valid at current location.252return Variant();253}254255Array GDScriptTextDocument::references(const Dictionary &p_params) {256Array res;257258LSP::ReferenceParams params;259params.load(p_params);260261const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);262if (symbol) {263Vector<LSP::Location> usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_all_usages(*symbol);264res.resize(usages.size());265int declaration_adjustment = 0;266for (int i = 0; i < usages.size(); i++) {267LSP::Location usage = usages[i];268if (!params.context.includeDeclaration && usage.range == symbol->range) {269declaration_adjustment++;270continue;271}272res[i - declaration_adjustment] = usages[i].to_json();273}274275if (declaration_adjustment > 0) {276res.resize(res.size() - declaration_adjustment);277}278}279280return res;281}282283Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {284LSP::CompletionItem item;285item.load(p_params);286287LSP::CompletionParams params;288Variant data = p_params["data"];289290const LSP::DocumentSymbol *symbol = nullptr;291292if (data.get_type() == Variant::DICTIONARY) {293params.load(p_params["data"]);294symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == LSP::CompletionItemKind::Method || item.kind == LSP::CompletionItemKind::Function);295296} else if (data.is_string()) {297String query = data;298299Vector<String> param_symbols = query.split(SYMBOL_SEPARATOR, false);300301if (param_symbols.size() >= 2) {302StringName class_name = param_symbols[0];303const String &member_name = param_symbols[param_symbols.size() - 1];304String inner_class_name;305if (param_symbols.size() >= 3) {306inner_class_name = param_symbols[1];307}308309if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) {310if (const LSP::DocumentSymbol *const *member = members->getptr(member_name)) {311symbol = *member;312}313}314315if (!symbol) {316ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_parse_result(class_name);317if (parser) {318symbol = parser->get_member_symbol(member_name, inner_class_name);319}320}321}322}323324if (symbol) {325item.documentation = symbol->render();326}327328if (item.kind == LSP::CompletionItemKind::Event) {329if (params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {330const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";331item.insertText = item.label.quote(quote_style);332}333}334335if (item.kind == LSP::CompletionItemKind::Method) {336bool is_trigger_character = params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter;337bool is_quote_character = params.context.triggerCharacter == "\"" || params.context.triggerCharacter == "'";338339if (is_trigger_character && is_quote_character && item.insertText.is_quoted()) {340item.insertText = item.insertText.unquote();341}342}343344return item.to_json(true);345}346347Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {348return Array();349}350351Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {352return Array();353}354355Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {356Array ret;357358LSP::DocumentLinkParams params;359params.load(p_params);360361List<LSP::DocumentLink> links;362GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);363for (const LSP::DocumentLink &E : links) {364ret.push_back(E.to_json());365}366return ret;367}368369Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {370return Array();371}372373Variant GDScriptTextDocument::hover(const Dictionary &p_params) {374LSP::TextDocumentPositionParams params;375params.load(p_params);376377const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);378if (symbol) {379LSP::Hover hover;380hover.contents = symbol->render();381hover.range.start = params.position;382hover.range.end = params.position;383return hover.to_json();384385} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {386Dictionary ret;387Array contents;388List<const LSP::DocumentSymbol *> list;389GDScriptLanguageProtocol::get_singleton()->resolve_related_symbols(params, list);390for (const LSP::DocumentSymbol *&E : list) {391if (const LSP::DocumentSymbol *s = E) {392contents.push_back(s->render().value);393}394}395ret["contents"] = contents;396return ret;397}398399return Variant();400}401402Array GDScriptTextDocument::definition(const Dictionary &p_params) {403LSP::TextDocumentPositionParams params;404params.load(p_params);405List<const LSP::DocumentSymbol *> symbols;406return find_symbols(params, symbols);407}408409Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {410LSP::TextDocumentPositionParams params;411params.load(p_params);412List<const LSP::DocumentSymbol *> symbols;413Array arr = find_symbols(params, symbols);414if (arr.is_empty() && !symbols.is_empty() && !symbols.front()->get()->native_class.is_empty()) { // Find a native symbol415const LSP::DocumentSymbol *symbol = symbols.front()->get();416if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {417String id;418switch (symbol->kind) {419case LSP::SymbolKind::Class:420id = "class_name:" + symbol->name;421break;422case LSP::SymbolKind::Constant:423id = "class_constant:" + symbol->native_class + ":" + symbol->name;424break;425case LSP::SymbolKind::Property:426case LSP::SymbolKind::Variable:427id = "class_property:" + symbol->native_class + ":" + symbol->name;428break;429case LSP::SymbolKind::Enum:430id = "class_enum:" + symbol->native_class + ":" + symbol->name;431break;432case LSP::SymbolKind::Method:433case LSP::SymbolKind::Function:434id = "class_method:" + symbol->native_class + ":" + symbol->name;435break;436default:437id = "class_global:" + symbol->native_class + ":" + symbol->name;438break;439}440callable_mp(this, &GDScriptTextDocument::show_native_symbol_in_editor).call_deferred(id);441} else {442notify_client_show_symbol(symbol);443}444}445return arr;446}447448Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {449Variant ret;450451LSP::TextDocumentPositionParams params;452params.load(p_params);453454LSP::SignatureHelp s;455if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {456ret = s.to_json();457}458459return ret;460}461462GDScriptTextDocument::GDScriptTextDocument() {463file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);464}465466void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {467callable_mp(ScriptEditor::get_singleton(), &ScriptEditor::goto_help).call_deferred(p_symbol_id);468469DisplayServer::get_singleton()->window_move_to_foreground();470}471472Array GDScriptTextDocument::find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list) {473Array arr;474const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);475if (symbol) {476LSP::Location location;477location.uri = symbol->uri;478if (!location.uri.is_empty()) {479location.range = symbol->selectionRange;480const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);481if (file_checker->file_exists(path)) {482arr.push_back(location.to_json());483}484}485r_list.push_back(symbol);486} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {487List<const LSP::DocumentSymbol *> list;488GDScriptLanguageProtocol::get_singleton()->resolve_related_symbols(p_location, list);489for (const LSP::DocumentSymbol *&E : list) {490if (const LSP::DocumentSymbol *s = E) {491if (!s->uri.is_empty()) {492LSP::Location location;493location.uri = s->uri;494location.range = s->selectionRange;495arr.push_back(location.to_json());496r_list.push_back(s);497}498}499}500}501return arr;502}503504505