diff options
author | geequlim <geequlim@gmail.com> | 2019-06-23 01:48:31 +0800 |
---|---|---|
committer | geequlim <geequlim@gmail.com> | 2019-08-11 13:30:15 +0800 |
commit | 37aafaaa9cc7d66c85fd9395e46b2386d899ba12 (patch) | |
tree | 2eef77e9c4ffe73e8de284567cf81dd6d5be642e | |
parent | f58560ac361fbe0fcc38df6a8f80818e55517aef (diff) |
Add a symbol pool to cache all native symbols and workspackes symbols.
Implement hover
Implement completion documentation resolve
Implement hover documentation
Implement go to definition
8 files changed, 862 insertions, 34 deletions
diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 32437f663a..9ec93a813e 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -30,6 +30,9 @@ #include "gdscript_extend_parser.h" #include "../gdscript.h" +#include "core/io/json.h" +#include "gdscript_language_protocol.h" +#include "gdscript_workspace.h" void ExtendGDScriptParser::update_diagnostics() { @@ -43,7 +46,7 @@ void ExtendGDScriptParser::update_diagnostics() { diagnostic.code = -1; lsp::Range range; lsp::Position pos; - int line = get_error_line() - 1; + int line = LINE_NUMBER_TO_INDEX(get_error_line()); const String &line_text = get_lines()[line]; pos.line = line; pos.character = line_text.length() - line_text.strip_edges(true, false).length(); @@ -64,7 +67,7 @@ void ExtendGDScriptParser::update_diagnostics() { diagnostic.code = warning.code; lsp::Range range; lsp::Position pos; - int line = warning.line - 1; + int line = LINE_NUMBER_TO_INDEX(warning.line); const String &line_text = get_lines()[line]; pos.line = line; pos.character = line_text.length() - line_text.strip_edges(true, false).length(); @@ -84,17 +87,23 @@ void ExtendGDScriptParser::update_symbols() { } void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol) { + + const String uri = get_uri(); + + r_symbol.uri = uri; + r_symbol.script_path = path; r_symbol.children.clear(); r_symbol.name = p_class->name; if (r_symbol.name.empty()) r_symbol.name = path.get_file(); r_symbol.kind = lsp::SymbolKind::Class; - r_symbol.detail = p_class->get_datatype().to_string(); r_symbol.deprecated = false; - r_symbol.range.start.line = p_class->line - 1; + r_symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_class->line); r_symbol.range.start.character = p_class->column; - r_symbol.range.end.line = p_class->end_line - 1; + r_symbol.range.end.line = LINE_NUMBER_TO_INDEX(p_class->end_line); r_symbol.selectionRange.start.line = r_symbol.range.start.line; + r_symbol.detail = "class " + r_symbol.name; + r_symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(p_class->line)); for (int i = 0; i < p_class->variables.size(); ++i) { @@ -103,14 +112,22 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p lsp::DocumentSymbol symbol; symbol.name = m.identifier; symbol.kind = lsp::SymbolKind::Variable; - symbol.detail = m.data_type.to_string(); symbol.deprecated = false; - const int line = m.line - 1; + const int line = LINE_NUMBER_TO_INDEX(m.line); symbol.range.start.line = line; symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length(); symbol.range.end.line = line; symbol.range.end.character = lines[line].length(); symbol.selectionRange.start.line = symbol.range.start.line; + symbol.detail = "var " + m.identifier; + if (m.data_type.kind != GDScriptParser::DataType::UNRESOLVED) { + symbol.detail += ": " + m.data_type.to_string(); + } + symbol.detail += " = " + String(m.default_value); + + symbol.documentation = parse_documentation(line); + symbol.uri = uri; + symbol.script_path = path; r_symbol.children.push_back(symbol); } @@ -122,27 +139,49 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p symbol.name = signal.name; symbol.kind = lsp::SymbolKind::Event; symbol.deprecated = false; - const int line = signal.line - 1; + const int line = LINE_NUMBER_TO_INDEX(signal.line); symbol.range.start.line = line; symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length(); symbol.range.end.line = symbol.range.start.line; symbol.range.end.character = lines[line].length(); symbol.selectionRange.start.line = symbol.range.start.line; + symbol.documentation = parse_documentation(line); + symbol.uri = uri; + symbol.script_path = path; + symbol.detail = "signal " + signal.name + "("; + for (int j = 0; j < signal.arguments.size(); j++) { + if (j > 0) { + symbol.detail += ", "; + } + symbol.detail += signal.arguments[j]; + } + symbol.detail += ")"; r_symbol.children.push_back(symbol); } for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { lsp::DocumentSymbol symbol; + const GDScriptParser::ClassNode::Constant &c = E->value(); + const GDScriptParser::ConstantNode *node = dynamic_cast<const GDScriptParser::ConstantNode *>(c.expression); symbol.name = E->key(); symbol.kind = lsp::SymbolKind::Constant; symbol.deprecated = false; - const int line = E->get().expression->line - 1; + const int line = LINE_NUMBER_TO_INDEX(E->get().expression->line); symbol.range.start.line = line; symbol.range.start.character = E->get().expression->column; symbol.range.end.line = symbol.range.start.line; symbol.range.end.character = lines[line].length(); symbol.selectionRange.start.line = symbol.range.start.line; + symbol.documentation = parse_documentation(line); + symbol.uri = uri; + symbol.script_path = path; + + symbol.detail = "const " + symbol.name; + if (c.type.kind != GDScriptParser::DataType::UNRESOLVED) { + symbol.detail += ": " + c.type.to_string(); + } + symbol.detail += " = " + String(node->value); r_symbol.children.push_back(symbol); } @@ -170,39 +209,120 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p } void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol) { + + const String uri = get_uri(); + r_symbol.name = p_func->name; r_symbol.kind = lsp::SymbolKind::Function; - r_symbol.detail = p_func->get_datatype().to_string(); + r_symbol.detail = "func " + p_func->name + "("; r_symbol.deprecated = false; - const int line = p_func->line - 1; + const int line = LINE_NUMBER_TO_INDEX(p_func->line); r_symbol.range.start.line = line; r_symbol.range.start.character = p_func->column; r_symbol.range.end.line = MAX(p_func->body->end_line - 2, p_func->body->line); r_symbol.range.end.character = lines[r_symbol.range.end.line].length(); r_symbol.selectionRange.start.line = r_symbol.range.start.line; + r_symbol.documentation = GDScriptWorkspace::marked_documentation(parse_documentation(line)); + r_symbol.uri = uri; + r_symbol.script_path = path; + + String arguments; + for (int i = 0; i < p_func->arguments.size(); i++) { + lsp::DocumentSymbol symbol; + symbol.kind = lsp::SymbolKind::Variable; + symbol.name = p_func->arguments[i]; + symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_func->body->line); + symbol.range.start.character = p_func->body->column; + symbol.range.end = symbol.range.start; + symbol.uri = uri; + symbol.script_path = path; + r_symbol.children.push_back(symbol); + if (i > 0) { + arguments += ", "; + } + arguments += String(p_func->arguments[i]); + if (p_func->argument_types[i].kind != GDScriptParser::DataType::UNRESOLVED) { + arguments += ": " + p_func->argument_types[i].to_string(); + } + int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size()); + if (default_value_idx >= 0) { + const GDScriptParser::ConstantNode *const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(p_func->default_values[default_value_idx]); + if (const_node == NULL) { + const GDScriptParser::OperatorNode *operator_node = dynamic_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[default_value_idx]); + if (operator_node) { + const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(operator_node->next); + } + } + + if (const_node) { + String value = JSON::print(const_node->value); + arguments += " = " + value; + } + } + } + r_symbol.detail += arguments + ")"; + if (p_func->return_type.kind != GDScriptParser::DataType::UNRESOLVED) { + r_symbol.detail += " -> " + p_func->return_type.to_string(); + } for (const Map<StringName, LocalVarNode *>::Element *E = p_func->body->variables.front(); E; E = E->next()) { lsp::DocumentSymbol symbol; + const GDScriptParser::LocalVarNode *var = E->value(); symbol.name = E->key(); symbol.kind = lsp::SymbolKind::Variable; - symbol.range.start.line = E->get()->line - 1; + symbol.range.start.line = LINE_NUMBER_TO_INDEX(E->get()->line); symbol.range.start.character = E->get()->column; symbol.range.end.line = symbol.range.start.line; symbol.range.end.character = lines[symbol.range.end.line].length(); + symbol.uri = uri; + symbol.script_path = path; + symbol.detail = "var " + symbol.name; + if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) { + symbol.detail += ": " + var->datatype.to_string(); + } + symbol.documentation = GDScriptWorkspace::marked_documentation(parse_documentation(line)); r_symbol.children.push_back(symbol); } - for (int i = 0; i < p_func->arguments.size(); i++) { - lsp::DocumentSymbol symbol; - symbol.kind = lsp::SymbolKind::Variable; - symbol.name = p_func->arguments[i]; - symbol.range.start.line = p_func->body->line - 1; - symbol.range.start.character = p_func->body->column; - symbol.range.end = symbol.range.start; - r_symbol.children.push_back(symbol); +} + +String ExtendGDScriptParser::parse_documentation(int p_line) { + ERR_FAIL_INDEX_V(p_line, lines.size(), String()); + + List<String> doc_lines; + + // inline comment + String inline_comment = lines[p_line]; + int comment_start = inline_comment.find("#"); + if (comment_start != -1) { + inline_comment = inline_comment.substr(comment_start, inline_comment.length()); + if (inline_comment.length() > 1) { + doc_lines.push_back(inline_comment.substr(1, inline_comment.length())); + } + } + + // upper line comments + for (int i = p_line - 1; i >= 0; --i) { + String line_comment = lines[i].strip_edges(true, false); + if (line_comment.begins_with("#")) { + if (line_comment.length() > 1) { + doc_lines.push_front(line_comment.substr(1, line_comment.length())); + } else { + doc_lines.push_front(""); + } + } else { + break; + } + } + + String doc; + for (List<String>::Element *E = doc_lines.front(); E; E = E->next()) { + String content = E->get(); + doc += content + "\n"; } + return doc; } -String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) { +String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) const { String longthing; int len = lines.size(); @@ -224,6 +344,164 @@ String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_curs return longthing; } +String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol, bool p_func_requred) const { + String longthing; + int len = lines.size(); + for (int i = 0; i < len; i++) { + + if (i == p_cursor.line) { + String line = lines[i]; + String first_part = line.substr(0, p_cursor.character); + String last_part = line.substr(p_cursor.character, lines[i].size()); + if (!p_symbol.empty()) { + String left_cursor_text; + for (int c = p_cursor.character - 1; c >= 0; c--) { + left_cursor_text = line.substr(c, p_cursor.character - c); + if (p_symbol.begins_with(left_cursor_text)) { + first_part = line.substr(0, c); + first_part += p_symbol; + break; + } + } + } + + longthing += first_part; + longthing += String::chr(0xFFFF); //not unicode, represents the cursor + if (p_func_requred) { + longthing += "("; // tell the parser this is a function call + } + longthing += last_part; + } else { + + longthing += lines[i]; + } + + if (i != len - 1) + longthing += "\n"; + } + + return longthing; +} + +String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const { + + ERR_FAIL_INDEX_V(p_position.line, lines.size(), ""); + String line = lines[p_position.line]; + ERR_FAIL_INDEX_V(p_position.character, line.size(), ""); + + int start_pos = p_position.character; + for (int c = p_position.character; c >= 0; c--) { + start_pos = c; + CharType ch = line[c]; + bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'; + if (!valid_char) { + break; + } + } + + int end_pos = p_position.character; + for (int c = p_position.character; c < line.length(); c++) { + CharType ch = line[c]; + bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'; + if (!valid_char) { + break; + } + end_pos = c; + } + if (start_pos < end_pos) { + p_offset.x = start_pos - p_position.character; + p_offset.y = end_pos - p_position.character; + return line.substr(start_pos + 1, end_pos - start_pos); + } + + return ""; +} + +String ExtendGDScriptParser::get_uri() const { + return GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_uri(path); +} + +const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const { + const lsp::DocumentSymbol *ret = NULL; + if (p_line < p_parent.range.start.line) { + return ret; + } else if (p_parent.range.start.line == p_line) { + return &p_parent; + } else { + for (int i = 0; i < p_parent.children.size(); i++) { + ret = search_symbol_defined_at_line(p_line, p_parent.children[i]); + if (ret) { + break; + } + } + } + return ret; +} + +const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const { + if (p_line <= 0) { + return &class_symbol; + } + return search_symbol_defined_at_line(p_line, class_symbol); +} + +const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name) const { + const GDScriptParser::Node *head = get_parse_tree(); + + if (const GDScriptParser::ClassNode *gdclass = dynamic_cast<const GDScriptParser::ClassNode *>(head)) { + + if (const Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = gdclass->constant_expressions.find(p_name)) { + return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(E->get().expression->line)); + } + + for (int i = 0; i < gdclass->subclasses.size(); i++) { + const ClassNode *m = gdclass->subclasses[i]; + if (m && m->name == p_name) { + return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m->line)); + } + } + + for (int i = 0; i < gdclass->variables.size(); i++) { + const GDScriptParser::ClassNode::Member &m = gdclass->variables[i]; + if (m.identifier == p_name) { + return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line)); + } + } + + for (int i = 0; i < gdclass->functions.size(); i++) { + const GDScriptParser::FunctionNode *m = gdclass->functions[i]; + if (m->name == p_name) { + return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m->line)); + } + } + + for (int i = 0; i < gdclass->static_functions.size(); i++) { + const GDScriptParser::FunctionNode *m = gdclass->static_functions[i]; + if (m->name == p_name) { + return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m->line)); + } + } + + for (int i = 0; i < gdclass->_signals.size(); i++) { + const GDScriptParser::ClassNode::Signal &m = gdclass->_signals[i]; + if (m.name == p_name) { + return get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line)); + } + } + } + + return NULL; +} + +void ExtendGDScriptParser::dump_symbols(HashMap<String, lsp::DocumentedSymbolInformation> &r_symbols) { + Vector<lsp::DocumentedSymbolInformation> list; + class_symbol.symbol_tree_as_list(path, list, path, true); + for (int i = 0; i < list.size(); i++) { + const lsp::DocumentedSymbolInformation &symbol = list[i]; + r_symbols.set(symbol.name, symbol); + } +} + Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) { path = p_path; code = p_code; @@ -232,6 +510,5 @@ Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) { Error err = GDScriptParser::parse(p_code, p_path.get_base_dir(), false, p_path, false, NULL, false); update_diagnostics(); update_symbols(); - return err; } diff --git a/modules/gdscript/language_server/gdscript_extend_parser.h b/modules/gdscript/language_server/gdscript_extend_parser.h index 60af5c7465..a1a2e2c31f 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.h +++ b/modules/gdscript/language_server/gdscript_extend_parser.h @@ -35,6 +35,10 @@ #include "core/variant.h" #include "lsp.hpp" +#ifndef LINE_NUMBER_TO_INDEX +#define LINE_NUMBER_TO_INDEX(p_line) ((p_line)-1) +#endif + class ExtendGDScriptParser : public GDScriptParser { String path; String code; @@ -48,6 +52,9 @@ class ExtendGDScriptParser : public GDScriptParser { void parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol); void parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol); + String parse_documentation(int p_line); + + const lsp::DocumentSymbol *search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const; public: _FORCE_INLINE_ const String &get_path() const { return path; } @@ -56,7 +63,15 @@ public: _FORCE_INLINE_ const lsp::DocumentSymbol &get_symbols() const { return class_symbol; } _FORCE_INLINE_ const Vector<lsp::Diagnostic> &get_diagnostics() const { return diagnostics; } - String get_text_for_completion(const lsp::Position &p_cursor); + 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; + String get_uri() const; + + const lsp::DocumentSymbol *get_symbol_defined_at_line(int p_line) const; + const lsp::DocumentSymbol *get_member_symbol(const String &p_name) const; + + void dump_symbols(HashMap<String, lsp::DocumentedSymbolInformation> &r_symbols); Error parse(const String &p_code, const String &p_path); }; diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index b4c4d7f236..7f74e68a94 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -90,7 +90,7 @@ void GDScriptLanguageProtocol::_bind_methods() { Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { lsp::InitializeResult ret; - + workspace.initialize(); return ret.to_json(); } @@ -163,6 +163,7 @@ GDScriptLanguageProtocol::GDScriptLanguageProtocol() { server = NULL; singleton = this; set_scope("textDocument", &text_document); + set_scope("completionItem", &text_document); set_scope("workspace", &workspace); workspace.root = ProjectSettings::get_singleton()->get_resource_path(); } diff --git a/modules/gdscript/language_server/gdscript_text_document.cpp b/modules/gdscript/language_server/gdscript_text_document.cpp index ac4b4363e2..7e5d2a512a 100644 --- a/modules/gdscript/language_server/gdscript_text_document.cpp +++ b/modules/gdscript/language_server/gdscript_text_document.cpp @@ -30,6 +30,7 @@ #include "gdscript_text_document.h" #include "../gdscript.h" +#include "gdscript_extend_parser.h" #include "gdscript_language_protocol.h" void GDScriptTextDocument::_bind_methods() { @@ -37,11 +38,13 @@ void GDScriptTextDocument::_bind_methods() { ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange); ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol); ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion); + ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve); ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange); ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens); ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink); ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation); ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover); + ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition); } void GDScriptTextDocument::didOpen(const Variant &p_param) { @@ -75,7 +78,7 @@ Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) { String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(uri); Array arr; if (const Map<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.find(path)) { - Vector<lsp::SymbolInformation> list; + Vector<lsp::DocumentedSymbolInformation> list; parser->get()->get_symbols().symbol_tree_as_list(uri, list); for (int i = 0; i < list.size(); i++) { arr.push_back(list[i].to_json()); @@ -144,6 +147,18 @@ Array GDScriptTextDocument::completion(const Dictionary &p_params) { return arr; } +Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) { + lsp::CompletionItem item; + item.load(p_params); + lsp::CompletionParams params; + params.load(p_params["data"]); + const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function); + if (symbol) { + item.documentation = symbol->render(); + } + return item.to_json(); +} + Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) { Dictionary params = p_params["textDocument"]; String path = params["uri"]; @@ -168,9 +183,49 @@ Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) { Variant GDScriptTextDocument::hover(const Dictionary &p_params) { Variant ret; + + lsp::TextDocumentPositionParams params; + params.load(p_params); + + const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace().resolve_symbol(params); + if (symbol) { + lsp::Hover hover; + hover.contents = symbol->render(); + ret = hover.to_json(); + } + return ret; } +Array GDScriptTextDocument::definition(const Dictionary &p_params) { + Array arr; + + lsp::TextDocumentPositionParams params; + params.load(p_params); + + 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()); + } + } + + return arr; +} + +GDScriptTextDocument::GDScriptTextDocument() { + file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES); +} + +GDScriptTextDocument::~GDScriptTextDocument() { + memdelete(file_checker); +} + void GDScriptTextDocument::sync_script_content(const String &p_uri, const String &p_content) { String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(p_uri); GDScriptLanguageProtocol::get_singleton()->get_workspace().parse_script(path, p_content); diff --git a/modules/gdscript/language_server/gdscript_text_document.h b/modules/gdscript/language_server/gdscript_text_document.h index f1612b5a8c..68d89c7ba4 100644 --- a/modules/gdscript/language_server/gdscript_text_document.h +++ b/modules/gdscript/language_server/gdscript_text_document.h @@ -31,6 +31,7 @@ #ifndef GDSCRIPT_TEXT_DOCUMENT_H #define GDSCRIPT_TEXT_DOCUMENT_H +#include "core/os/file_access.h" #include "core/reference.h" #include "lsp.hpp" @@ -39,6 +40,8 @@ class GDScriptTextDocument : public Reference { protected: static void _bind_methods(); + FileAccess *file_checker; + void didOpen(const Variant &p_param); void didChange(const Variant &p_param); @@ -50,11 +53,16 @@ private: public: Array documentSymbol(const Dictionary &p_params); Array completion(const Dictionary &p_params); + Dictionary resolve(const Dictionary &p_params); Array foldingRange(const Dictionary &p_params); Array codeLens(const Dictionary &p_params); Variant documentLink(const Dictionary &p_params); Array colorPresentation(const Dictionary &p_params); Variant hover(const Dictionary &p_params); + Array definition(const Dictionary &p_params); + + GDScriptTextDocument(); + virtual ~GDScriptTextDocument(); }; #endif diff --git a/modules/gdscript/language_server/gdscript_workspace.cpp b/modules/gdscript/language_server/gdscript_workspace.cpp index 7833fa5453..068930002b 100644 --- a/modules/gdscript/language_server/gdscript_workspace.cpp +++ b/modules/gdscript/language_server/gdscript_workspace.cpp @@ -32,6 +32,8 @@ #include "../gdscript.h" #include "../gdscript_parser.h" #include "core/project_settings.h" +#include "core/script_language.h" +#include "editor/editor_help.h" #include "gdscript_language_protocol.h" void GDScriptWorkspace::_bind_methods() { @@ -59,12 +61,156 @@ void GDScriptWorkspace::remove_cache_parser(const String &p_path) { } } +const lsp::DocumentSymbol *GDScriptWorkspace::get_native_symbol(const String &p_class, const String &p_member) const { + + StringName class_name = p_class; + StringName end_pos; + + while (class_name != end_pos) { + if (const Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.find(class_name)) { + const lsp::DocumentSymbol &class_symbol = E->value(); + + if (p_member.empty()) { + return &class_symbol; + } else { + for (int i = 0; i < class_symbol.children.size(); i++) { + const lsp::DocumentSymbol &symbol = class_symbol.children[i]; + if (symbol.name == p_member) { + return &symbol; + } + } + } + } + class_name = ClassDB::get_parent_class(class_name); + } + + return NULL; +} + +const lsp::DocumentSymbol *GDScriptWorkspace::get_script_symbol(const String &p_path) const { + const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path); + if (S) { + return &(S->get()->get_symbols()); + } + return NULL; +} + +void GDScriptWorkspace::reload_all_workspace_scripts() { + List<String> pathes; + list_script_files("res://", pathes); + for (List<String>::Element *E = pathes.front(); E; E = E->next()) { + const String &path = E->get(); + Error err; + String content = FileAccess::get_file_as_string(path, &err); + ERR_CONTINUE(err != OK); + err = parse_script(path, content); + + if (err != OK) { + Map<String, ExtendGDScriptParser *>::Element *S = parse_results.find(path); + String err_msg = "Failed parse script " + path; + if (S) { + err_msg += "\n" + S->get()->get_error(); + } + ERR_EXPLAIN(err_msg); + ERR_CONTINUE(err != OK); + } + } +} + +void GDScriptWorkspace::list_script_files(const String &p_root_dir, List<String> &r_files) { + Error err; + DirAccessRef dir = DirAccess::open(p_root_dir, &err); + if (OK == err) { + dir->list_dir_begin(); + String file_name = dir->get_next(); + while (file_name.length()) { + if (dir->current_is_dir() && file_name != "." && file_name != ".." && file_name != "./") { + list_script_files(p_root_dir.plus_file(file_name), r_files); + } else if (file_name.ends_with(".gd")) { + String script_file = p_root_dir.plus_file(file_name); + r_files.push_back(script_file); + } + file_name = dir->get_next(); + } + } +} + +ExtendGDScriptParser *GDScriptWorkspace::get_parse_successed_script(const String &p_path) { + const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path); + if (!S) { + parse_local_script(p_path); + S = scripts.find(p_path); + } + if (S) { + return S->get(); + } + return NULL; +} + +ExtendGDScriptParser *GDScriptWorkspace::get_parse_result(const String &p_path) { + const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path); + if (!S) { + S = parse_results.find(p_path); + if (!S) { + parse_local_script(p_path); + S = scripts.find(p_path); + } + } + if (S) { + return S->get(); + } + return NULL; +} + +String GDScriptWorkspace::marked_documentation(const String &p_bbcode) { + + String markdown = p_bbcode.strip_edges(); + + Vector<String> lines = markdown.split("\n"); + bool in_code_block = false; + int code_block_indent = -1; + + markdown = ""; + for (int i = 0; i < lines.size(); i++) { + String line = lines[i]; + line = line.replace("[code]", "`"); + line = line.replace("[/code]", "`"); + line = line.replace("[i]", "*"); + line = line.replace("[/i]", "*"); + line = line.replace("[b]", "**"); + line = line.replace("[/b]", "**"); + line = line.replace("[u]", "__"); + line = line.replace("[/u]", "__"); + int block_start = line.find("[codeblock]"); + if (block_start != -1) { + code_block_indent = block_start; + in_code_block = true; + line = "'''gdscript"; + line = "\n"; + } else if (in_code_block) { + line = "\t" + line.substr(code_block_indent, line.length()); + } else { + line = line.strip_edges(); + } + if (in_code_block && line.find("[/codeblock]") != -1) { + line = "'''\n"; + line = "\n"; + in_code_block = false; + } + if (!in_code_block && i < lines.size() - 1) { + line += "\n"; + } + markdown += line + "\n"; + } + return markdown; +} + Array GDScriptWorkspace::symbol(const Dictionary &p_params) { String query = p_params["query"]; Array arr; if (!query.empty()) { for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) { - Vector<lsp::SymbolInformation> script_symbols; + Vector<lsp::DocumentedSymbolInformation> script_symbols; E->get()->get_symbols().symbol_tree_as_list(E->key(), script_symbols); for (int i = 0; i < script_symbols.size(); ++i) { if (query.is_subsequence_ofi(script_symbols[i].name)) { @@ -76,6 +222,102 @@ Array GDScriptWorkspace::symbol(const Dictionary &p_params) { return arr; } +Error GDScriptWorkspace::initialize() { + if (initialized) return OK; + + DocData *doc = EditorHelp::get_doc_data(); + for (Map<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) { + + const DocData::ClassDoc &class_data = E->value(); + lsp::DocumentSymbol class_symbol; + String class_name = E->key(); + class_symbol.name = class_name; + class_symbol.kind = lsp::SymbolKind::Class; + class_symbol.detail = String("<Native> class ") + class_name; + if (!class_data.inherits.empty()) { + class_symbol.detail += " extends " + class_data.inherits; + } + class_symbol.documentation = marked_documentation(class_data.brief_description) + "\n" + marked_documentation(class_data.description); + + for (int i = 0; i < class_data.constants.size(); i++) { + const DocData::ConstantDoc &const_data = class_data.constants[i]; + lsp::DocumentSymbol symbol; + symbol.name = const_data.name; + symbol.kind = lsp::SymbolKind::Constant; + symbol.detail = "const " + class_name + "." + const_data.name; + if (const_data.enumeration.length()) { + symbol.detail += ": " + const_data.enumeration; + } + symbol.detail += " = " + const_data.value; + symbol.documentation = marked_documentation(const_data.description); + class_symbol.children.push_back(symbol); + } + + Vector<DocData::PropertyDoc> properties; + properties.append_array(class_data.properties); + const int theme_prop_start_idx = properties.size(); + properties.append_array(class_data.theme_properties); + + for (int i = 0; i < class_data.properties.size(); i++) { + const DocData::PropertyDoc &data = class_data.properties[i]; + lsp::DocumentSymbol symbol; + symbol.name = data.name; + symbol.kind = lsp::SymbolKind::Property; + symbol.detail = String(i >= theme_prop_start_idx ? "<Theme> var" : "var") + " " + class_name + "." + data.name; + if (data.enumeration.length()) { + symbol.detail += ": " + data.enumeration; + } else { + symbol.detail += ": " + data.type; + } + symbol.documentation = marked_documentation(data.description); + class_symbol.children.push_back(symbol); + } + + Vector<DocData::MethodDoc> methods_signals; + methods_signals.append_array(class_data.methods); + const int signal_start_idx = methods_signals.size(); + methods_signals.append_array(class_data.signals); + + for (int i = 0; i < methods_signals.size(); i++) { + const DocData::MethodDoc &data = methods_signals[i]; + + lsp::DocumentSymbol symbol; + symbol.name = data.name; + symbol.kind = i >= signal_start_idx ? lsp::SymbolKind::Event : lsp::SymbolKind::Method; + + String params = ""; + bool arg_default_value_started = false; + for (int j = 0; j < data.arguments.size(); j++) { + const DocData::ArgumentDoc &arg = data.arguments[j]; + if (!arg_default_value_started && !arg.default_value.empty()) { + arg_default_value_started = true; + } + String arg_str = arg.name + ": " + arg.type; + if (arg_default_value_started) { + arg_str += " = " + arg.default_value; + } + if (j < data.arguments.size() - 1) { + arg_str += ", "; + } + params += arg_str; + } + if (data.qualifiers.find("vararg") != -1) { + params += params.empty() ? "..." : ", ..."; + } + + symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + data.return_type; + symbol.documentation = marked_documentation(data.description); + class_symbol.children.push_back(symbol); + } + + native_symbols.insert(class_name, class_symbol); + } + + reload_all_workspace_scripts(); + + return OK; +} + Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) { ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser); Error err = parser->parse(p_content, p_path); @@ -98,6 +340,15 @@ Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_cont return err; } +Error GDScriptWorkspace::parse_local_script(const String &p_path) { + Error err; + String content = FileAccess::get_file_as_string(p_path, &err); + if (err == OK) { + err = parse_script(p_path, content); + } + return err; +} + String GDScriptWorkspace::get_file_path(const String &p_uri) const { String path = p_uri.replace("file://", "").http_unescape(); path = path.replace(root + "/", "res://"); @@ -135,18 +386,77 @@ void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<S } } +const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name, bool p_func_requred) { + + const lsp::DocumentSymbol *symbol = NULL; + + String path = get_file_path(p_doc_pos.textDocument.uri); + if (const ExtendGDScriptParser *parser = get_parse_result(path)) { + + String symbol_identifier = p_symbol_name; + lsp::Position pos = p_doc_pos.position; + if (symbol_identifier.empty()) { + Vector2i offset; + symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset); + pos.character += offset.y; + } + + if (!symbol_identifier.empty()) { + + if (ScriptServer::is_global_class(symbol_identifier)) { + + String class_path = ScriptServer::get_global_class_path(symbol_identifier); + symbol = get_script_symbol(class_path); + + } else { + + ScriptLanguage::LookupResult ret; + if (OK == GDScriptLanguage::get_singleton()->lookup_code(parser->get_text_for_lookup_symbol(pos, symbol_identifier, p_func_requred), symbol_identifier, path, NULL, ret)) { + + if (ret.type == ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION) { + + String target_script_path = path; + if (!ret.script.is_null()) { + target_script_path = ret.script->get_path(); + } + + if (const ExtendGDScriptParser *target_parser = get_parse_result(target_script_path)) { + symbol = target_parser->get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(ret.location)); + } + + } else { + + String member = ret.class_member; + if (member.empty() && symbol_identifier != ret.class_name) { + member = symbol_identifier; + } + symbol = get_native_symbol(ret.class_name, member); + } + } else { + symbol = parser->get_member_symbol(symbol_identifier); + } + } + } + } + + return symbol; +} + GDScriptWorkspace::GDScriptWorkspace() { ProjectSettings::get_singleton()->get_resource_path(); } GDScriptWorkspace::~GDScriptWorkspace() { Set<String> cached_parsers; + for (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.front(); E; E = E->next()) { cached_parsers.insert(E->key()); } + for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) { cached_parsers.insert(E->key()); } + for (Set<String>::Element *E = cached_parsers.front(); E; E = E->next()) { remove_cache_parser(E->get()); } diff --git a/modules/gdscript/language_server/gdscript_workspace.h b/modules/gdscript/language_server/gdscript_workspace.h index 02ddc7cd71..ebefd12587 100644 --- a/modules/gdscript/language_server/gdscript_workspace.h +++ b/modules/gdscript/language_server/gdscript_workspace.h @@ -42,6 +42,17 @@ class GDScriptWorkspace : public Reference { protected: static void _bind_methods(); void remove_cache_parser(const String &p_path); + bool initialized = false; + Map<StringName, lsp::DocumentSymbol> native_symbols; + + const lsp::DocumentSymbol *get_native_symbol(const String &p_class, const String &p_member = "") const; + const lsp::DocumentSymbol *get_script_symbol(const String &p_path) const; + + void reload_all_workspace_scripts(); + + void list_script_files(const String &p_root_dir, List<String> &r_files); + ExtendGDScriptParser *get_parse_successed_script(const String &p_path); + ExtendGDScriptParser *get_parse_result(const String &p_path); public: String root; @@ -52,11 +63,15 @@ public: Array symbol(const Dictionary &p_params); public: + Error initialize(); Error parse_script(const String &p_path, const String &p_content); + Error parse_local_script(const String &p_path); String get_file_path(const String &p_uri) const; String get_file_uri(const String &p_path) const; void publish_diagnostics(const String &p_path); void completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options); + const lsp::DocumentSymbol *resolve_symbol(const lsp::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name = "", bool p_func_requred = false); + static String marked_documentation(const String &p_bbcode); GDScriptWorkspace(); ~GDScriptWorkspace(); diff --git a/modules/gdscript/language_server/lsp.hpp b/modules/gdscript/language_server/lsp.hpp index 81fa4c6181..7e98bfa279 100644 --- a/modules/gdscript/language_server/lsp.hpp +++ b/modules/gdscript/language_server/lsp.hpp @@ -610,6 +610,70 @@ struct Diagnostic { }; /** + * Describes the content type that a client supports in various + * result literals like `Hover`, `ParameterInfo` or `CompletionItem`. + * + * Please note that `MarkupKinds` must not start with a `$`. This kinds + * are reserved for internal usage. + */ +namespace MarkupKind { +static const String PlainText = "plaintext"; +static const String Markdown = "markdown"; +}; // namespace MarkupKind + +/** + * A `MarkupContent` literal represents a string value which content is interpreted base on its + * kind flag. Currently the protocol supports `plaintext` and `markdown` as markup kinds. + * + * If the kind is `markdown` then the value can contain fenced code blocks like in GitHub issues. + * See https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting + * + * Here is an example how such a string can be constructed using JavaScript / TypeScript: + * ```typescript + * let markdown: MarkdownContent = { + * kind: MarkupKind.Markdown, + * value: [ + * '# Header', + * 'Some text', + * '```typescript', + * 'someCode();', + * '```' + * ].join('\n') + * }; + * ``` + * + * *Please Note* that clients might sanitize the return markdown. A client could decide to + * remove HTML from the markdown to avoid script execution. + */ +struct MarkupContent { + /** + * The type of the Markup + */ + String kind; + + /** + * The content itself + */ + String value; + + MarkupContent() { + kind = MarkupKind::Markdown; + } + + MarkupContent(const String &p_value) { + value = p_value; + kind = MarkupKind::Markdown; + } + + Dictionary to_json() const { + Dictionary dict; + dict["kind"] = kind; + dict["value"] = value; + return dict; + } +}; + +/** * A symbol kind. */ namespace SymbolKind { @@ -693,6 +757,18 @@ struct SymbolInformation { } }; +struct DocumentedSymbolInformation : public SymbolInformation { + /** + * A human-readable string with additional information + */ + String detail; + + /** + * A human-readable string that represents a doc-comment. + */ + String documentation; +}; + /** * Represents programming constructs like variables, classes, interfaces etc. that appear in a document. Document symbols can be * hierarchical and they have two ranges: one that encloses its definition and one that points to its most interesting range, @@ -712,6 +788,11 @@ struct DocumentSymbol { String detail; /** + * Documentation for this symbol + */ + String documentation; + + /** * The kind of this symbol. */ int kind = SymbolKind::File; @@ -734,6 +815,9 @@ struct DocumentSymbol { */ Range selectionRange; + DocumentUri uri; + String script_path; + /** * Children of this symbol, e.g. properties of a class. */ @@ -756,19 +840,39 @@ struct DocumentSymbol { return dict; } - void symbol_tree_as_list(const String &p_uri, Vector<SymbolInformation> &r_list, const String &p_container = "") const { - SymbolInformation si; - si.name = name; + void symbol_tree_as_list(const String &p_uri, Vector<DocumentedSymbolInformation> &r_list, const String &p_container = "", bool p_join_name = false) const { + DocumentedSymbolInformation si; + if (p_join_name && !p_container.empty()) { + si.name = p_container + ">" + name; + } else { + si.name = name; + } si.kind = kind; si.containerName = p_container; si.deprecated = deprecated; si.location.uri = p_uri; si.location.range = range; + si.detail = detail; + si.documentation = documentation; r_list.push_back(si); for (int i = 0; i < children.size(); i++) { - children[i].symbol_tree_as_list(p_uri, r_list, name); + children[i].symbol_tree_as_list(p_uri, r_list, si.name, p_join_name); } } + + MarkupContent render() const { + MarkupContent markdown; + if (detail.length()) { + markdown.value = "\t" + detail + "\n\n"; + } + if (documentation.length()) { + markdown.value += documentation + "\n\n"; + } + if (script_path.length()) { + markdown.value += "Defined in [" + script_path + "](" + uri + ")"; + } + return markdown; + } }; /** @@ -895,7 +999,7 @@ struct CompletionItem { /** * A human-readable string that represents a doc-comment. */ - String documentation; + MarkupContent documentation; /** * Indicates if this item is deprecated. @@ -988,9 +1092,8 @@ struct CompletionItem { Dictionary dict; dict["label"] = label; dict["kind"] = kind; - dict["kind"] = kind; dict["detail"] = detail; - dict["documentation"] = documentation; + dict["documentation"] = documentation.to_json(); dict["deprecated"] = deprecated; dict["preselect"] = preselect; dict["sortText"] = sortText; @@ -1001,6 +1104,27 @@ struct CompletionItem { dict["data"] = data; return dict; } + + void load(const Dictionary &p_dict) { + if (p_dict.has("label")) label = p_dict["label"]; + if (p_dict.has("kind")) kind = p_dict["kind"]; + if (p_dict.has("detail")) detail = p_dict["detail"]; + if (p_dict.has("documentation")) { + Variant doc = p_dict["documentation"]; + if (doc.get_type() == Variant::STRING) { + documentation.value = doc; + } else if (doc.get_type() == Variant::DICTIONARY) { + Dictionary v = doc; + documentation.value = v["value"]; + } + } + if (p_dict.has("deprecated")) deprecated = p_dict["deprecated"]; + if (p_dict.has("preselect")) preselect = p_dict["preselect"]; + if (p_dict.has("sortText")) sortText = p_dict["sortText"]; + if (p_dict.has("filterText")) filterText = p_dict["filterText"]; + if (p_dict.has("insertText")) insertText = p_dict["insertText"]; + if (p_dict.has("data")) data = p_dict["data"]; + } }; /** @@ -1137,6 +1261,29 @@ struct CompletionParams : public TextDocumentPositionParams { } }; +/** + * The result of a hover request. + */ +struct Hover { + /** + * The hover's content + */ + MarkupContent contents; + + /** + * An optional range is a range inside a text document + * that is used to visualize a hover, e.g. by changing the background color. + */ + Range range; + + Dictionary to_json() const { + Dictionary dict; + dict["range"] = range.to_json(); + dict["contents"] = contents.to_json(); + return dict; + } +}; + struct ServerCapabilities { /** * Defines how text documents are synced. Is either a detailed structure defining each notification or @@ -1162,7 +1309,7 @@ struct ServerCapabilities { /** * The server provides goto definition support. */ - bool definitionProvider = false; + bool definitionProvider = true; /** * The server provides Goto Type Definition support. |