Path: blob/master/modules/gdscript/language_server/gdscript_text_document.cpp
11353 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) {65LSP::TextDocumentItem doc = load_document_item(p_param);66sync_script_content(doc.uri, doc.text);67}6869void GDScriptTextDocument::didClose(const Variant &p_param) {70// Left empty on purpose. Godot does nothing special on closing a document,71// but it satisfies LSP clients that require didClose be implemented.72}7374void GDScriptTextDocument::didChange(const Variant &p_param) {75LSP::TextDocumentItem doc = load_document_item(p_param);76Dictionary dict = p_param;77Array contentChanges = dict["contentChanges"];78for (int i = 0; i < contentChanges.size(); ++i) {79LSP::TextDocumentContentChangeEvent evt;80evt.load(contentChanges[i]);81doc.text = evt.text;82}83sync_script_content(doc.uri, doc.text);84}8586void GDScriptTextDocument::willSaveWaitUntil(const Variant &p_param) {87LSP::TextDocumentItem doc = load_document_item(p_param);8889String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);90Ref<Script> scr = ResourceLoader::load(path);91if (scr.is_valid()) {92ScriptEditor::get_singleton()->clear_docs_from_script(scr);93}94}9596void GDScriptTextDocument::didSave(const Variant &p_param) {97LSP::TextDocumentItem doc = load_document_item(p_param);98Dictionary dict = p_param;99String text = dict["text"];100101sync_script_content(doc.uri, text);102103String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);104Ref<GDScript> scr = ResourceLoader::load(path);105if (scr.is_valid() && (scr->load_source_code(path) == OK)) {106if (scr->is_tool()) {107scr->get_language()->reload_tool_script(scr, true);108} else {109scr->reload(true);110}111112scr->update_exports();113114if (!Thread::is_main_thread()) {115callable_mp(this, &GDScriptTextDocument::reload_script).call_deferred(scr);116} else {117reload_script(scr);118}119}120}121122void GDScriptTextDocument::reload_script(Ref<GDScript> p_to_reload_script) {123ScriptEditor::get_singleton()->reload_scripts(true);124ScriptEditor::get_singleton()->update_docs_from_script(p_to_reload_script);125ScriptEditor::get_singleton()->trigger_live_script_reload(p_to_reload_script->get_path());126}127128LSP::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) {129LSP::TextDocumentItem doc;130Dictionary params = p_param;131doc.load(params["textDocument"]);132return doc;133}134135void GDScriptTextDocument::notify_client_show_symbol(const LSP::DocumentSymbol *symbol) {136ERR_FAIL_NULL(symbol);137GDScriptLanguageProtocol::get_singleton()->notify_client("gdscript/show_native_symbol", symbol->to_json(true));138}139140void GDScriptTextDocument::initialize() {141if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {142for (const KeyValue<StringName, ClassMembers> &E : GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members) {143const ClassMembers &members = E.value;144145for (const KeyValue<String, const LSP::DocumentSymbol *> &F : members) {146const LSP::DocumentSymbol *symbol = members.get(F.key);147LSP::CompletionItem item = symbol->make_completion_item();148item.data = JOIN_SYMBOLS(String(E.key), F.key);149native_member_completions.push_back(item.to_json());150}151}152}153}154155Variant GDScriptTextDocument::nativeSymbol(const Dictionary &p_params) {156Variant ret;157158LSP::NativeSymbolInspectParams params;159params.load(p_params);160161if (const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_native_symbol(params)) {162ret = symbol->to_json(true);163notify_client_show_symbol(symbol);164}165166return ret;167}168169Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {170Dictionary params = p_params["textDocument"];171String uri = params["uri"];172String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri);173Array arr;174if (HashMap<String, ExtendGDScriptParser *>::ConstIterator parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) {175LSP::DocumentSymbol symbol = parser->value->get_symbols();176arr.push_back(symbol.to_json(true));177}178return arr;179}180181Array GDScriptTextDocument::completion(const Dictionary &p_params) {182Array arr;183184LSP::CompletionParams params;185params.load(p_params);186Dictionary request_data = params.to_json();187188List<ScriptLanguage::CodeCompletionOption> options;189GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options);190191if (!options.is_empty()) {192int i = 0;193arr.resize(options.size());194195for (const ScriptLanguage::CodeCompletionOption &option : options) {196LSP::CompletionItem item;197item.label = option.display;198item.data = request_data;199item.insertText = option.insert_text;200201switch (option.kind) {202case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:203item.kind = LSP::CompletionItemKind::Enum;204break;205case ScriptLanguage::CODE_COMPLETION_KIND_CLASS:206item.kind = LSP::CompletionItemKind::Class;207break;208case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:209item.kind = LSP::CompletionItemKind::Property;210break;211case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:212item.kind = LSP::CompletionItemKind::Method;213break;214case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:215item.kind = LSP::CompletionItemKind::Event;216break;217case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:218item.kind = LSP::CompletionItemKind::Constant;219break;220case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:221item.kind = LSP::CompletionItemKind::Variable;222break;223case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:224item.kind = LSP::CompletionItemKind::File;225break;226case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:227item.kind = LSP::CompletionItemKind::Snippet;228break;229case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:230item.kind = LSP::CompletionItemKind::Text;231break;232default: {233}234}235236arr[i] = item.to_json();237i++;238}239}240return arr;241}242243Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {244LSP::TextDocumentPositionParams params;245params.load(p_params);246String new_name = p_params["newName"];247248return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);249}250251Variant GDScriptTextDocument::prepareRename(const Dictionary &p_params) {252LSP::TextDocumentPositionParams params;253params.load(p_params);254255LSP::DocumentSymbol symbol;256LSP::Range range;257if (GDScriptLanguageProtocol::get_singleton()->get_workspace()->can_rename(params, symbol, range)) {258return Variant(range.to_json());259}260261// `null` -> rename not valid at current location.262return Variant();263}264265Array GDScriptTextDocument::references(const Dictionary &p_params) {266Array res;267268LSP::ReferenceParams params;269params.load(p_params);270271const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);272if (symbol) {273Vector<LSP::Location> usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_all_usages(*symbol);274res.resize(usages.size());275int declaration_adjustment = 0;276for (int i = 0; i < usages.size(); i++) {277LSP::Location usage = usages[i];278if (!params.context.includeDeclaration && usage.range == symbol->range) {279declaration_adjustment++;280continue;281}282res[i - declaration_adjustment] = usages[i].to_json();283}284285if (declaration_adjustment > 0) {286res.resize(res.size() - declaration_adjustment);287}288}289290return res;291}292293Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {294LSP::CompletionItem item;295item.load(p_params);296297LSP::CompletionParams params;298Variant data = p_params["data"];299300const LSP::DocumentSymbol *symbol = nullptr;301302if (data.get_type() == Variant::DICTIONARY) {303params.load(p_params["data"]);304symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == LSP::CompletionItemKind::Method || item.kind == LSP::CompletionItemKind::Function);305306} else if (data.is_string()) {307String query = data;308309Vector<String> param_symbols = query.split(SYMBOL_SEPARATOR, false);310311if (param_symbols.size() >= 2) {312StringName class_name = param_symbols[0];313const String &member_name = param_symbols[param_symbols.size() - 1];314String inner_class_name;315if (param_symbols.size() >= 3) {316inner_class_name = param_symbols[1];317}318319if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) {320if (const LSP::DocumentSymbol *const *member = members->getptr(member_name)) {321symbol = *member;322}323}324325if (!symbol) {326if (HashMap<String, ExtendGDScriptParser *>::ConstIterator E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) {327symbol = E->value->get_member_symbol(member_name, inner_class_name);328}329}330}331}332333if (symbol) {334item.documentation = symbol->render();335}336337if (item.kind == LSP::CompletionItemKind::Event) {338if (params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {339const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";340item.insertText = item.label.quote(quote_style);341}342}343344if (item.kind == LSP::CompletionItemKind::Method) {345bool is_trigger_character = params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter;346bool is_quote_character = params.context.triggerCharacter == "\"" || params.context.triggerCharacter == "'";347348if (is_trigger_character && is_quote_character && item.insertText.is_quoted()) {349item.insertText = item.insertText.unquote();350}351}352353return item.to_json(true);354}355356Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {357return Array();358}359360Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {361return Array();362}363364Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {365Array ret;366367LSP::DocumentLinkParams params;368params.load(p_params);369370List<LSP::DocumentLink> links;371GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);372for (const LSP::DocumentLink &E : links) {373ret.push_back(E.to_json());374}375return ret;376}377378Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {379return Array();380}381382Variant GDScriptTextDocument::hover(const Dictionary &p_params) {383LSP::TextDocumentPositionParams params;384params.load(p_params);385386const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);387if (symbol) {388LSP::Hover hover;389hover.contents = symbol->render();390hover.range.start = params.position;391hover.range.end = params.position;392return hover.to_json();393394} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {395Dictionary ret;396Array contents;397List<const LSP::DocumentSymbol *> list;398GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list);399for (const LSP::DocumentSymbol *&E : list) {400if (const LSP::DocumentSymbol *s = E) {401contents.push_back(s->render().value);402}403}404ret["contents"] = contents;405return ret;406}407408return Variant();409}410411Array GDScriptTextDocument::definition(const Dictionary &p_params) {412LSP::TextDocumentPositionParams params;413params.load(p_params);414List<const LSP::DocumentSymbol *> symbols;415return find_symbols(params, symbols);416}417418Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {419LSP::TextDocumentPositionParams params;420params.load(p_params);421List<const LSP::DocumentSymbol *> symbols;422Array arr = find_symbols(params, symbols);423if (arr.is_empty() && !symbols.is_empty() && !symbols.front()->get()->native_class.is_empty()) { // Find a native symbol424const LSP::DocumentSymbol *symbol = symbols.front()->get();425if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {426String id;427switch (symbol->kind) {428case LSP::SymbolKind::Class:429id = "class_name:" + symbol->name;430break;431case LSP::SymbolKind::Constant:432id = "class_constant:" + symbol->native_class + ":" + symbol->name;433break;434case LSP::SymbolKind::Property:435case LSP::SymbolKind::Variable:436id = "class_property:" + symbol->native_class + ":" + symbol->name;437break;438case LSP::SymbolKind::Enum:439id = "class_enum:" + symbol->native_class + ":" + symbol->name;440break;441case LSP::SymbolKind::Method:442case LSP::SymbolKind::Function:443id = "class_method:" + symbol->native_class + ":" + symbol->name;444break;445default:446id = "class_global:" + symbol->native_class + ":" + symbol->name;447break;448}449callable_mp(this, &GDScriptTextDocument::show_native_symbol_in_editor).call_deferred(id);450} else {451notify_client_show_symbol(symbol);452}453}454return arr;455}456457Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {458Variant ret;459460LSP::TextDocumentPositionParams params;461params.load(p_params);462463LSP::SignatureHelp s;464if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {465ret = s.to_json();466}467468return ret;469}470471GDScriptTextDocument::GDScriptTextDocument() {472file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);473}474475void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {476String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);477GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);478}479480void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {481callable_mp(ScriptEditor::get_singleton(), &ScriptEditor::goto_help).call_deferred(p_symbol_id);482483DisplayServer::get_singleton()->window_move_to_foreground();484}485486Array GDScriptTextDocument::find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list) {487Array arr;488const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);489if (symbol) {490LSP::Location location;491location.uri = symbol->uri;492if (!location.uri.is_empty()) {493location.range = symbol->selectionRange;494const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);495if (file_checker->file_exists(path)) {496arr.push_back(location.to_json());497}498r_list.push_back(symbol);499}500} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {501List<const LSP::DocumentSymbol *> list;502GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list);503for (const LSP::DocumentSymbol *&E : list) {504if (const LSP::DocumentSymbol *s = E) {505if (!s->uri.is_empty()) {506LSP::Location location;507location.uri = s->uri;508location.range = s->selectionRange;509arr.push_back(location.to_json());510r_list.push_back(s);511}512}513}514}515return arr;516}517518519