diff options
Diffstat (limited to 'modules/gdscript/language_server')
11 files changed, 375 insertions, 101 deletions
diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 6b5c26ec81..0f6f13944b 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -112,10 +112,11 @@ void ExtendGDScriptParser::update_document_links(const String &p_code) { FileAccessRef fs = FileAccess::create(FileAccess::ACCESS_RESOURCES); tokenizer.set_code(p_code); while (true) { - if (tokenizer.get_token() == GDScriptTokenizer::TK_EOF) { + GDScriptTokenizerText::Token token = tokenizer.get_token(); + if (token == GDScriptTokenizer::TK_EOF || token == GDScriptTokenizer::TK_ERROR) { break; - } else if (tokenizer.get_token() == GDScriptTokenizer::TK_CONSTANT) { - Variant const_val = tokenizer.get_token_constant(); + } else if (token == GDScriptTokenizer::TK_CONSTANT) { + const Variant &const_val = tokenizer.get_token_constant(); if (const_val.get_type() == Variant::STRING) { String path = const_val; bool exists = fs->file_exists(path); @@ -522,6 +523,51 @@ const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(i return ret; } +Error ExtendGDScriptParser::get_left_function_call(const lsp::Position &p_position, lsp::Position &r_func_pos, int &r_arg_index) const { + + ERR_FAIL_INDEX_V(p_position.line, lines.size(), ERR_INVALID_PARAMETER); + + int bracket_stack = 0; + int index = 0; + + bool found = false; + for (int l = p_position.line; l >= 0; --l) { + String line = lines[l]; + int c = line.length() - 1; + if (l == p_position.line) { + c = MIN(c, p_position.character - 1); + } + + while (c >= 0) { + const CharType &character = line[c]; + if (character == ')') { + ++bracket_stack; + } else if (character == '(') { + --bracket_stack; + if (bracket_stack < 0) { + found = true; + } + } + if (bracket_stack <= 0 && character == ',') { + ++index; + } + --c; + if (found) { + r_func_pos.character = c; + break; + } + } + + if (found) { + r_func_pos.line = l; + r_arg_index = index; + return OK; + } + } + + return ERR_METHOD_NOT_FOUND; +} + const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const { if (p_line <= 0) { return &class_symbol; @@ -711,7 +757,7 @@ Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode Array static_functions; for (int i = 0; i < p_class->static_functions.size(); ++i) { - static_functions.append(dump_function_api(p_class->functions[i])); + static_functions.append(dump_function_api(p_class->static_functions[i])); } class_api["static_functions"] = static_functions; diff --git a/modules/gdscript/language_server/gdscript_extend_parser.h b/modules/gdscript/language_server/gdscript_extend_parser.h index a6e0ca5534..43dfce994b 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.h +++ b/modules/gdscript/language_server/gdscript_extend_parser.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -83,6 +83,8 @@ public: _FORCE_INLINE_ const ClassMembers &get_members() const { return members; } _FORCE_INLINE_ const HashMap<String, ClassMembers> &get_inner_classes() const { return inner_classes; } + Error get_left_function_call(const lsp::Position &p_position, lsp::Position &r_func_pos, int &r_arg_index) const; + String get_text_for_completion(const lsp::Position &p_cursor) const; String get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol = "", bool p_func_requred = false) const; String get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const; diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index 90646f73ba..7133c6b4be 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -156,7 +156,7 @@ void GDScriptLanguageProtocol::poll() { server->poll(); } -Error GDScriptLanguageProtocol::start(int p_port) { +Error GDScriptLanguageProtocol::start(int p_port, const IP_Address &p_bind_ip) { if (server == NULL) { server = dynamic_cast<WebSocketServer *>(ClassDB::instance("WebSocketServer")); ERR_FAIL_COND_V(!server, FAILED); @@ -165,6 +165,7 @@ Error GDScriptLanguageProtocol::start(int p_port) { server->connect("client_connected", this, "on_client_connected"); server->connect("client_disconnected", this, "on_client_disconnected"); } + server->set_bind_ip(p_bind_ip); return server->listen(p_port); } diff --git a/modules/gdscript/language_server/gdscript_language_protocol.h b/modules/gdscript/language_server/gdscript_language_protocol.h index 136b45fd78..52c680ab19 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.h +++ b/modules/gdscript/language_server/gdscript_language_protocol.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -77,7 +77,7 @@ public: _FORCE_INLINE_ bool is_initialized() const { return _initialized; } void poll(); - Error start(int p_port); + Error start(int p_port, const IP_Address &p_bind_ip); void stop(); void notify_all_clients(const String &p_method, const Variant &p_params = Variant()); diff --git a/modules/gdscript/language_server/gdscript_language_server.cpp b/modules/gdscript/language_server/gdscript_language_server.cpp index 8d58b99e02..7170c63058 100644 --- a/modules/gdscript/language_server/gdscript_language_server.cpp +++ b/modules/gdscript/language_server/gdscript_language_server.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,14 +31,20 @@ #include "gdscript_language_server.h" #include "core/os/file_access.h" #include "core/os/os.h" +#include "editor/editor_log.h" #include "editor/editor_node.h" GDScriptLanguageServer::GDScriptLanguageServer() { thread = NULL; - thread_exit = false; - _EDITOR_DEF("network/language_server/remote_port", 6008); - _EDITOR_DEF("network/language_server/enable_smart_resolve", false); + thread_running = false; + started = false; + + use_thread = false; + port = 6008; + _EDITOR_DEF("network/language_server/remote_port", port); + _EDITOR_DEF("network/language_server/enable_smart_resolve", true); _EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false); + _EDITOR_DEF("network/language_server/use_thread", use_thread); } void GDScriptLanguageServer::_notification(int p_what) { @@ -50,12 +56,25 @@ void GDScriptLanguageServer::_notification(int p_what) { case NOTIFICATION_EXIT_TREE: stop(); break; + case NOTIFICATION_INTERNAL_PROCESS: { + if (started && !use_thread) { + protocol.poll(); + } + } break; + case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { + int port = (int)_EDITOR_GET("network/language_server/remote_port"); + bool use_thread = (bool)_EDITOR_GET("network/language_server/use_thread"); + if (port != this->port || use_thread != this->use_thread) { + this->stop(); + this->start(); + } + } break; } } void GDScriptLanguageServer::thread_main(void *p_userdata) { GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata); - while (!self->thread_exit) { + while (self->thread_running) { // Poll 20 times per second self->protocol.poll(); OS::get_singleton()->delay_usec(50000); @@ -63,22 +82,30 @@ void GDScriptLanguageServer::thread_main(void *p_userdata) { } void GDScriptLanguageServer::start() { - int port = (int)_EDITOR_GET("network/language_server/remote_port"); - if (protocol.start(port) == OK) { + port = (int)_EDITOR_GET("network/language_server/remote_port"); + use_thread = (bool)_EDITOR_GET("network/language_server/use_thread"); + if (protocol.start(port, IP_Address("127.0.0.1")) == OK) { EditorNode::get_log()->add_message("--- GDScript language server started ---", EditorLog::MSG_TYPE_EDITOR); - ERR_FAIL_COND(thread != NULL || thread_exit); - thread_exit = false; - thread = Thread::create(GDScriptLanguageServer::thread_main, this); + if (use_thread) { + ERR_FAIL_COND(thread != NULL); + thread_running = true; + thread = Thread::create(GDScriptLanguageServer::thread_main, this); + } + set_process_internal(!use_thread); + started = true; } } void GDScriptLanguageServer::stop() { - ERR_FAIL_COND(NULL == thread || thread_exit); - thread_exit = true; - Thread::wait_to_finish(thread); - memdelete(thread); - thread = NULL; + if (use_thread) { + ERR_FAIL_COND(NULL == thread); + thread_running = false; + Thread::wait_to_finish(thread); + memdelete(thread); + thread = NULL; + } protocol.stop(); + started = false; EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR); } diff --git a/modules/gdscript/language_server/gdscript_language_server.h b/modules/gdscript/language_server/gdscript_language_server.h index 83c2320d98..228d29bf42 100644 --- a/modules/gdscript/language_server/gdscript_language_server.h +++ b/modules/gdscript/language_server/gdscript_language_server.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -41,7 +41,10 @@ class GDScriptLanguageServer : public EditorPlugin { GDScriptLanguageProtocol protocol; Thread *thread; - bool thread_exit; + bool thread_running; + bool started; + bool use_thread; + int port; static void thread_main(void *p_userdata); private: diff --git a/modules/gdscript/language_server/gdscript_text_document.cpp b/modules/gdscript/language_server/gdscript_text_document.cpp index b83db718b8..0572c5f746 100644 --- a/modules/gdscript/language_server/gdscript_text_document.cpp +++ b/modules/gdscript/language_server/gdscript_text_document.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -49,6 +49,8 @@ void GDScriptTextDocument::_bind_methods() { ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation); ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover); ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition); + ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration); + ClassDB::bind_method(D_METHOD("signatureHelp"), &GDScriptTextDocument::signatureHelp); ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor); } @@ -267,7 +269,7 @@ Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) { if ((item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function) && !item.label.ends_with("):")) { item.insertText = item.label + "("; - if (symbol && symbol->detail.find(",") == -1) { + if (symbol && symbol->children.empty()) { item.insertText += ")"; } } else if (item.kind == lsp::CompletionItemKind::Event) { @@ -281,8 +283,6 @@ Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) { } Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) { - Dictionary params = p_params["textDocument"]; - String path = params["uri"]; Array arr; return arr; } @@ -342,68 +342,64 @@ Variant GDScriptTextDocument::hover(const Dictionary &p_params) { } Array GDScriptTextDocument::definition(const Dictionary &p_params) { - Array arr; - lsp::TextDocumentPositionParams params; params.load(p_params); + List<const lsp::DocumentSymbol *> symbols; + Array arr = this->find_symbols(params, symbols); + return arr; +} - const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params); - if (symbol) { - lsp::Location location; - location.uri = symbol->uri; - location.range = symbol->range; - - const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri); - if (file_checker->file_exists(path)) { - arr.push_back(location.to_json()); - } else if (!symbol->native_class.empty()) { - if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) { - String id; - switch (symbol->kind) { - case lsp::SymbolKind::Class: - id = "class_name:" + symbol->name; - break; - case lsp::SymbolKind::Constant: - id = "class_constant:" + symbol->native_class + ":" + symbol->name; - break; - case lsp::SymbolKind::Property: - case lsp::SymbolKind::Variable: - id = "class_property:" + symbol->native_class + ":" + symbol->name; - break; - case lsp::SymbolKind::Enum: - id = "class_enum:" + symbol->native_class + ":" + symbol->name; - break; - case lsp::SymbolKind::Method: - case lsp::SymbolKind::Function: - id = "class_method:" + symbol->native_class + ":" + symbol->name; - break; - default: - id = "class_global:" + symbol->native_class + ":" + symbol->name; - break; - } - call_deferred("show_native_symbol_in_editor", id); - } else { - notify_client_show_symbol(symbol); +Variant GDScriptTextDocument::declaration(const Dictionary &p_params) { + lsp::TextDocumentPositionParams params; + params.load(p_params); + List<const lsp::DocumentSymbol *> symbols; + Array arr = this->find_symbols(params, symbols); + if (arr.empty() && !symbols.empty() && !symbols.front()->get()->native_class.empty()) { // Find a native symbol + const lsp::DocumentSymbol *symbol = symbols.front()->get(); + if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) { + String id; + switch (symbol->kind) { + case lsp::SymbolKind::Class: + id = "class_name:" + symbol->name; + break; + case lsp::SymbolKind::Constant: + id = "class_constant:" + symbol->native_class + ":" + symbol->name; + break; + case lsp::SymbolKind::Property: + case lsp::SymbolKind::Variable: + id = "class_property:" + symbol->native_class + ":" + symbol->name; + break; + case lsp::SymbolKind::Enum: + id = "class_enum:" + symbol->native_class + ":" + symbol->name; + break; + case lsp::SymbolKind::Method: + case lsp::SymbolKind::Function: + id = "class_method:" + symbol->native_class + ":" + symbol->name; + break; + default: + id = "class_global:" + symbol->native_class + ":" + symbol->name; + break; } + call_deferred("show_native_symbol_in_editor", id); + } else { + notify_client_show_symbol(symbol); } - } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + } + return arr; +} - List<const lsp::DocumentSymbol *> list; - GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list); - for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) { +Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) { + Variant ret; - if (const lsp::DocumentSymbol *s = E->get()) { - if (!s->uri.empty()) { - lsp::Location location; - location.uri = s->uri; - location.range = s->range; - arr.push_back(location.to_json()); - } - } - } + lsp::TextDocumentPositionParams params; + params.load(p_params); + + lsp::SignatureHelp s; + if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) { + ret = s.to_json(); } - return arr; + return ret; } GDScriptTextDocument::GDScriptTextDocument() { @@ -423,3 +419,33 @@ void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_i ScriptEditor::get_singleton()->call_deferred("_help_class_goto", p_symbol_id); OS::get_singleton()->move_window_to_foreground(); } + +Array GDScriptTextDocument::find_symbols(const lsp::TextDocumentPositionParams &p_location, List<const lsp::DocumentSymbol *> &r_list) { + Array arr; + const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location); + if (symbol) { + lsp::Location location; + location.uri = symbol->uri; + location.range = symbol->range; + const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri); + if (file_checker->file_exists(path)) { + arr.push_back(location.to_json()); + } + r_list.push_back(symbol); + } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + List<const lsp::DocumentSymbol *> list; + GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list); + for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) { + if (const lsp::DocumentSymbol *s = E->get()) { + if (!s->uri.empty()) { + lsp::Location location; + location.uri = s->uri; + location.range = s->range; + arr.push_back(location.to_json()); + r_list.push_back(s); + } + } + } + } + return arr; +} diff --git a/modules/gdscript/language_server/gdscript_text_document.h b/modules/gdscript/language_server/gdscript_text_document.h index 235e2c3f6e..b2fd0c31f9 100644 --- a/modules/gdscript/language_server/gdscript_text_document.h +++ b/modules/gdscript/language_server/gdscript_text_document.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -51,6 +51,7 @@ protected: Array native_member_completions; private: + Array find_symbols(const lsp::TextDocumentPositionParams &p_location, List<const lsp::DocumentSymbol *> &r_list); lsp::TextDocumentItem load_document_item(const Variant &p_param); void notify_client_show_symbol(const lsp::DocumentSymbol *symbol); @@ -65,6 +66,8 @@ public: Array colorPresentation(const Dictionary &p_params); Variant hover(const Dictionary &p_params); Array definition(const Dictionary &p_params); + Variant declaration(const Dictionary &p_params); + Variant signatureHelp(const Dictionary &p_params); void initialize(); diff --git a/modules/gdscript/language_server/gdscript_workspace.cpp b/modules/gdscript/language_server/gdscript_workspace.cpp index f9a974bad3..1c0590cff1 100644 --- a/modules/gdscript/language_server/gdscript_workspace.cpp +++ b/modules/gdscript/language_server/gdscript_workspace.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -252,6 +252,12 @@ Error GDScriptWorkspace::initialize() { bool arg_default_value_started = false; for (int j = 0; j < data.arguments.size(); j++) { const DocData::ArgumentDoc &arg = data.arguments[j]; + + lsp::DocumentSymbol symbol_arg; + symbol_arg.name = arg.name; + symbol_arg.kind = lsp::SymbolKind::Variable; + symbol_arg.detail = arg.type; + if (!arg_default_value_started && !arg.default_value.empty()) { arg_default_value_started = true; } @@ -263,6 +269,8 @@ Error GDScriptWorkspace::initialize() { arg_str += ", "; } params += arg_str; + + symbol.children.push_back(symbol_arg); } if (data.qualifiers.find("vararg") != -1) { params += params.empty() ? "..." : ", ..."; @@ -513,6 +521,49 @@ Dictionary GDScriptWorkspace::generate_script_api(const String &p_path) { return api; } +Error GDScriptWorkspace::resolve_signature(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::SignatureHelp &r_signature) { + if (const ExtendGDScriptParser *parser = get_parse_result(get_file_path(p_doc_pos.textDocument.uri))) { + + lsp::TextDocumentPositionParams text_pos; + text_pos.textDocument = p_doc_pos.textDocument; + + if (parser->get_left_function_call(p_doc_pos.position, text_pos.position, r_signature.activeParameter) == OK) { + + List<const lsp::DocumentSymbol *> symbols; + + if (const lsp::DocumentSymbol *symbol = resolve_symbol(text_pos)) { + symbols.push_back(symbol); + } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(text_pos, symbols); + } + + for (List<const lsp::DocumentSymbol *>::Element *E = symbols.front(); E; E = E->next()) { + const lsp::DocumentSymbol *symbol = E->get(); + if (symbol->kind == lsp::SymbolKind::Method || symbol->kind == lsp::SymbolKind::Function) { + + lsp::SignatureInformation signature_info; + signature_info.label = symbol->detail; + signature_info.documentation = symbol->render(); + + for (int i = 0; i < symbol->children.size(); i++) { + const lsp::DocumentSymbol &arg = symbol->children[i]; + lsp::ParameterInformation arg_info; + arg_info.label = arg.name; + signature_info.parameters.push_back(arg_info); + } + r_signature.signatures.push_back(signature_info); + break; + } + } + + if (r_signature.signatures.size()) { + return OK; + } + } + } + return ERR_METHOD_NOT_FOUND; +} + GDScriptWorkspace::GDScriptWorkspace() { ProjectSettings::get_singleton()->get_resource_path(); } diff --git a/modules/gdscript/language_server/gdscript_workspace.h b/modules/gdscript/language_server/gdscript_workspace.h index a416ae1075..146a5cb7c9 100644 --- a/modules/gdscript/language_server/gdscript_workspace.h +++ b/modules/gdscript/language_server/gdscript_workspace.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -83,6 +83,7 @@ public: const lsp::DocumentSymbol *resolve_native_symbol(const lsp::NativeSymbolInspectParams &p_params); void resolve_document_links(const String &p_uri, List<lsp::DocumentLink> &r_list); Dictionary generate_script_api(const String &p_path); + Error resolve_signature(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::SignatureHelp &r_signature); GDScriptWorkspace(); ~GDScriptWorkspace(); diff --git a/modules/gdscript/language_server/lsp.hpp b/modules/gdscript/language_server/lsp.hpp index 35471d63d6..a2dcc48820 100644 --- a/modules/gdscript/language_server/lsp.hpp +++ b/modules/gdscript/language_server/lsp.hpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -330,8 +330,6 @@ struct CompletionOptions { triggerCharacters.push_back("$"); triggerCharacters.push_back("'"); triggerCharacters.push_back("\""); - triggerCharacters.push_back("("); - triggerCharacters.push_back(","); } Dictionary to_json() const { @@ -1402,6 +1400,120 @@ struct Hover { } }; +/** + * Represents a parameter of a callable-signature. A parameter can + * have a label and a doc-comment. + */ +struct ParameterInformation { + + /** + * The label of this parameter information. + * + * Either a string or an inclusive start and exclusive end offsets within its containing + * signature label. (see SignatureInformation.label). The offsets are based on a UTF-16 + * string representation as `Position` and `Range` does. + * + * *Note*: a label of type string should be a substring of its containing signature label. + * Its intended use case is to highlight the parameter label part in the `SignatureInformation.label`. + */ + String label; + + /** + * The human-readable doc-comment of this parameter. Will be shown + * in the UI but can be omitted. + */ + MarkupContent documentation; + + Dictionary to_json() const { + Dictionary dict; + dict["label"] = label; + dict["documentation"] = documentation.to_json(); + return dict; + } +}; + +/** + * Represents the signature of something callable. A signature + * can have a label, like a function-name, a doc-comment, and + * a set of parameters. + */ +struct SignatureInformation { + /** + * The label of this signature. Will be shown in + * the UI. + */ + String label; + + /** + * The human-readable doc-comment of this signature. Will be shown + * in the UI but can be omitted. + */ + MarkupContent documentation; + + /** + * The parameters of this signature. + */ + Vector<ParameterInformation> parameters; + + Dictionary to_json() const { + Dictionary dict; + dict["label"] = label; + dict["documentation"] = documentation.to_json(); + Array args; + for (int i = 0; i < parameters.size(); i++) { + args.push_back(parameters[i].to_json()); + } + dict["parameters"] = args; + return dict; + } +}; + +/** + * Signature help represents the signature of something + * callable. There can be multiple signature but only one + * active and only one active parameter. + */ +struct SignatureHelp { + /** + * One or more signatures. + */ + Vector<SignatureInformation> signatures; + + /** + * The active signature. If omitted or the value lies outside the + * range of `signatures` the value defaults to zero or is ignored if + * `signatures.length === 0`. Whenever possible implementors should + * make an active decision about the active signature and shouldn't + * rely on a default value. + * In future version of the protocol this property might become + * mandatory to better express this. + */ + int activeSignature = 0; + + /** + * The active parameter of the active signature. If omitted or the value + * lies outside the range of `signatures[activeSignature].parameters` + * defaults to 0 if the active signature has parameters. If + * the active signature has no parameters it is ignored. + * In future version of the protocol this property might become + * mandatory to better express the active parameter if the + * active signature does have any. + */ + int activeParameter = 0; + + Dictionary to_json() const { + Dictionary dict; + Array sigs; + for (int i = 0; i < signatures.size(); i++) { + sigs.push_back(signatures[i].to_json()); + } + dict["signatures"] = sigs; + dict["activeSignature"] = activeSignature; + dict["activeParameter"] = activeParameter; + return dict; + } +}; + struct ServerCapabilities { /** * Defines how text documents are synced. Is either a detailed structure defining each notification or @@ -1532,6 +1644,8 @@ struct ServerCapabilities { Dictionary dict; dict["textDocumentSync"] = (int)textDocumentSync.change; dict["completionProvider"] = completionProvider.to_json(); + signatureHelpProvider.triggerCharacters.push_back(","); + signatureHelpProvider.triggerCharacters.push_back("("); dict["signatureHelpProvider"] = signatureHelpProvider.to_json(); dict["codeLensProvider"] = false; // codeLensProvider.to_json(); dict["documentOnTypeFormattingProvider"] = documentOnTypeFormattingProvider.to_json(); |