Path: blob/master/modules/gdscript/language_server/gdscript_text_document.cpp
10278 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_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) {357Array arr;358return arr;359}360361Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {362Array arr;363return arr;364}365366Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {367Array ret;368369LSP::DocumentLinkParams params;370params.load(p_params);371372List<LSP::DocumentLink> links;373GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);374for (const LSP::DocumentLink &E : links) {375ret.push_back(E.to_json());376}377return ret;378}379380Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {381Array arr;382return arr;383}384385Variant GDScriptTextDocument::hover(const Dictionary &p_params) {386LSP::TextDocumentPositionParams params;387params.load(p_params);388389const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);390if (symbol) {391LSP::Hover hover;392hover.contents = symbol->render();393hover.range.start = params.position;394hover.range.end = params.position;395return hover.to_json();396397} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {398Dictionary ret;399Array contents;400List<const LSP::DocumentSymbol *> list;401GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list);402for (const LSP::DocumentSymbol *&E : list) {403if (const LSP::DocumentSymbol *s = E) {404contents.push_back(s->render().value);405}406}407ret["contents"] = contents;408return ret;409}410411return Variant();412}413414Array GDScriptTextDocument::definition(const Dictionary &p_params) {415LSP::TextDocumentPositionParams params;416params.load(p_params);417List<const LSP::DocumentSymbol *> symbols;418Array arr = find_symbols(params, symbols);419return arr;420}421422Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {423LSP::TextDocumentPositionParams params;424params.load(p_params);425List<const LSP::DocumentSymbol *> symbols;426Array arr = find_symbols(params, symbols);427if (arr.is_empty() && !symbols.is_empty() && !symbols.front()->get()->native_class.is_empty()) { // Find a native symbol428const LSP::DocumentSymbol *symbol = symbols.front()->get();429if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {430String id;431switch (symbol->kind) {432case LSP::SymbolKind::Class:433id = "class_name:" + symbol->name;434break;435case LSP::SymbolKind::Constant:436id = "class_constant:" + symbol->native_class + ":" + symbol->name;437break;438case LSP::SymbolKind::Property:439case LSP::SymbolKind::Variable:440id = "class_property:" + symbol->native_class + ":" + symbol->name;441break;442case LSP::SymbolKind::Enum:443id = "class_enum:" + symbol->native_class + ":" + symbol->name;444break;445case LSP::SymbolKind::Method:446case LSP::SymbolKind::Function:447id = "class_method:" + symbol->native_class + ":" + symbol->name;448break;449default:450id = "class_global:" + symbol->native_class + ":" + symbol->name;451break;452}453callable_mp(this, &GDScriptTextDocument::show_native_symbol_in_editor).call_deferred(id);454} else {455notify_client_show_symbol(symbol);456}457}458return arr;459}460461Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {462Variant ret;463464LSP::TextDocumentPositionParams params;465params.load(p_params);466467LSP::SignatureHelp s;468if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {469ret = s.to_json();470}471472return ret;473}474475GDScriptTextDocument::GDScriptTextDocument() {476file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);477}478479void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {480String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);481GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);482}483484void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {485callable_mp(ScriptEditor::get_singleton(), &ScriptEditor::goto_help).call_deferred(p_symbol_id);486487DisplayServer::get_singleton()->window_move_to_foreground();488}489490Array GDScriptTextDocument::find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list) {491Array arr;492const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);493if (symbol) {494LSP::Location location;495location.uri = symbol->uri;496if (!location.uri.is_empty()) {497location.range = symbol->selectionRange;498const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);499if (file_checker->file_exists(path)) {500arr.push_back(location.to_json());501}502r_list.push_back(symbol);503}504} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {505List<const LSP::DocumentSymbol *> list;506GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list);507for (const LSP::DocumentSymbol *&E : list) {508if (const LSP::DocumentSymbol *s = E) {509if (!s->uri.is_empty()) {510LSP::Location location;511location.uri = s->uri;512location.range = s->selectionRange;513arr.push_back(location.to_json());514r_list.push_back(s);515}516}517}518}519return arr;520}521522523