From f58560ac361fbe0fcc38df6a8f80818e55517aef Mon Sep 17 00:00:00 2001 From: geequlim Date: Fri, 14 Jun 2019 22:38:54 +0800 Subject: Add GDScript Language Protocol plugin --- .../language_server/gdscript_extend_parser.cpp | 237 +++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 modules/gdscript/language_server/gdscript_extend_parser.cpp (limited to 'modules/gdscript/language_server/gdscript_extend_parser.cpp') diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp new file mode 100644 index 0000000000..32437f663a --- /dev/null +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -0,0 +1,237 @@ +/*************************************************************************/ +/* gdscript_extend_parser.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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 */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "gdscript_extend_parser.h" +#include "../gdscript.h" + +void ExtendGDScriptParser::update_diagnostics() { + + diagnostics.clear(); + + if (has_error()) { + lsp::Diagnostic diagnostic; + diagnostic.severity = lsp::DiagnosticSeverity::Error; + diagnostic.message = get_error(); + diagnostic.source = "gdscript"; + diagnostic.code = -1; + lsp::Range range; + lsp::Position pos; + int line = get_error_line() - 1; + const String &line_text = get_lines()[line]; + pos.line = line; + pos.character = line_text.length() - line_text.strip_edges(true, false).length(); + range.start = pos; + range.end = range.start; + range.end.character = line_text.strip_edges(false).length(); + diagnostic.range = range; + diagnostics.push_back(diagnostic); + } + + const List &warnings = get_warnings(); + for (const List::Element *E = warnings.front(); E; E = E->next()) { + const GDScriptWarning &warning = E->get(); + lsp::Diagnostic diagnostic; + diagnostic.severity = lsp::DiagnosticSeverity::Warning; + diagnostic.message = warning.get_message(); + diagnostic.source = "gdscript"; + diagnostic.code = warning.code; + lsp::Range range; + lsp::Position pos; + int line = warning.line - 1; + const String &line_text = get_lines()[line]; + pos.line = line; + pos.character = line_text.length() - line_text.strip_edges(true, false).length(); + range.start = pos; + range.end = pos; + range.end.character = line_text.strip_edges(false).length(); + diagnostic.range = range; + diagnostics.push_back(diagnostic); + } +} + +void ExtendGDScriptParser::update_symbols() { + const GDScriptParser::Node *head = get_parse_tree(); + if (const GDScriptParser::ClassNode *gdclass = dynamic_cast(head)) { + parse_class_symbol(gdclass, class_symbol); + } +} + +void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol) { + 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.character = p_class->column; + r_symbol.range.end.line = p_class->end_line - 1; + r_symbol.selectionRange.start.line = r_symbol.range.start.line; + + for (int i = 0; i < p_class->variables.size(); ++i) { + + const GDScriptParser::ClassNode::Member &m = p_class->variables[i]; + + 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; + 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; + + r_symbol.children.push_back(symbol); + } + + for (int i = 0; i < p_class->_signals.size(); ++i) { + const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i]; + + lsp::DocumentSymbol symbol; + symbol.name = signal.name; + symbol.kind = lsp::SymbolKind::Event; + symbol.deprecated = false; + const int line = signal.line - 1; + 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; + + r_symbol.children.push_back(symbol); + } + + for (Map::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { + lsp::DocumentSymbol symbol; + symbol.name = E->key(); + symbol.kind = lsp::SymbolKind::Constant; + symbol.deprecated = false; + const int line = E->get().expression->line - 1; + 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; + + r_symbol.children.push_back(symbol); + } + + for (int i = 0; i < p_class->functions.size(); ++i) { + const GDScriptParser::FunctionNode *func = p_class->functions[i]; + lsp::DocumentSymbol symbol; + parse_function_symbol(func, symbol); + r_symbol.children.push_back(symbol); + } + + for (int i = 0; i < p_class->static_functions.size(); ++i) { + const GDScriptParser::FunctionNode *func = p_class->static_functions[i]; + lsp::DocumentSymbol symbol; + parse_function_symbol(func, symbol); + r_symbol.children.push_back(symbol); + } + + for (int i = 0; i < p_class->subclasses.size(); ++i) { + const GDScriptParser::ClassNode *subclass = p_class->subclasses[i]; + lsp::DocumentSymbol symbol; + parse_class_symbol(subclass, symbol); + r_symbol.children.push_back(symbol); + } +} + +void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol) { + r_symbol.name = p_func->name; + r_symbol.kind = lsp::SymbolKind::Function; + r_symbol.detail = p_func->get_datatype().to_string(); + r_symbol.deprecated = false; + const int line = p_func->line - 1; + 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; + + for (const Map::Element *E = p_func->body->variables.front(); E; E = E->next()) { + lsp::DocumentSymbol symbol; + symbol.name = E->key(); + symbol.kind = lsp::SymbolKind::Variable; + symbol.range.start.line = E->get()->line - 1; + 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(); + 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::get_text_for_completion(const lsp::Position &p_cursor) { + + String longthing; + int len = lines.size(); + for (int i = 0; i < len; i++) { + + if (i == p_cursor.line) { + longthing += lines[i].substr(0, p_cursor.character); + longthing += String::chr(0xFFFF); //not unicode, represents the cursor + longthing += lines[i].substr(p_cursor.character, lines[i].size()); + } else { + + longthing += lines[i]; + } + + if (i != len - 1) + longthing += "\n"; + } + + return longthing; +} + +Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) { + path = p_path; + code = p_code; + lines = p_code.split("\n"); + + Error err = GDScriptParser::parse(p_code, p_path.get_base_dir(), false, p_path, false, NULL, false); + update_diagnostics(); + update_symbols(); + + return err; +} -- cgit v1.2.3 From 37aafaaa9cc7d66c85fd9395e46b2386d899ba12 Mon Sep 17 00:00:00 2001 From: geequlim Date: Sun, 23 Jun 2019 01:48:31 +0800 Subject: 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 --- .../language_server/gdscript_extend_parser.cpp | 321 +++++++++++++++++++-- 1 file changed, 299 insertions(+), 22 deletions(-) (limited to 'modules/gdscript/language_server/gdscript_extend_parser.cpp') 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::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(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(p_func->default_values[default_value_idx]); + if (const_node == NULL) { + const GDScriptParser::OperatorNode *operator_node = dynamic_cast(p_func->default_values[default_value_idx]); + if (operator_node) { + const_node = dynamic_cast(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::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 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::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(head)) { + + if (const Map::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 &r_symbols) { + Vector 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; } -- cgit v1.2.3 From fa6d6a329c93224b5454b17603284913da0472a3 Mon Sep 17 00:00:00 2001 From: geequlim Date: Sun, 23 Jun 2019 21:10:28 +0800 Subject: Add optional smart resolve sulotion The smart resolvaion can guess most symbols but it might be slow so disabled by default users can turn on it in the editor setting --- .../language_server/gdscript_extend_parser.cpp | 43 ++++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'modules/gdscript/language_server/gdscript_extend_parser.cpp') diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 9ec93a813e..16af7cb92f 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -123,7 +123,9 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p if (m.data_type.kind != GDScriptParser::DataType::UNRESOLVED) { symbol.detail += ": " + m.data_type.to_string(); } - symbol.detail += " = " + String(m.default_value); + if (m.default_value.get_type() != Variant::NIL) { + symbol.detail += " = " + JSON::print(m.default_value); + } symbol.documentation = parse_documentation(line); symbol.uri = uri; @@ -493,12 +495,39 @@ const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String return NULL; } -void ExtendGDScriptParser::dump_symbols(HashMap &r_symbols) { - Vector 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); +void ExtendGDScriptParser::dump_member_symbols(Map &r_symbols) { + + const GDScriptParser::Node *head = get_parse_tree(); + if (const GDScriptParser::ClassNode *gdclass = dynamic_cast(head)) { + + for (const Map::Element *E = gdclass->constant_expressions.front(); E; E = E->next()) { + 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]; + r_symbols.insert(JOIN_SYMBOLS(path, m->name), 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]; + r_symbols.insert(JOIN_SYMBOLS(path, m.identifier), 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]; + r_symbols.insert(JOIN_SYMBOLS(path, m->name), 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]; + r_symbols.insert(JOIN_SYMBOLS(path, m->name), 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]; + r_symbols.insert(JOIN_SYMBOLS(path, m.name), get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line))); + } } } -- cgit v1.2.3 From 76c9e4ceb73b02bd95ab0512e27229516208dc60 Mon Sep 17 00:00:00 2001 From: Geequlim Date: Mon, 24 Jun 2019 18:25:12 +0800 Subject: Improved performance for completion and symbol resolvation. Improved uri and workspace path translatation on windows platform. The smart resolvation is much faster than builtin's in the server side. The smart resolve mode is still disabled as default as the clients might be slow with a planty of completion items. --- .../language_server/gdscript_extend_parser.cpp | 92 ++++++---------------- 1 file changed, 23 insertions(+), 69 deletions(-) (limited to 'modules/gdscript/language_server/gdscript_extend_parser.cpp') diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 16af7cb92f..a6fbfd3779 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -80,9 +80,18 @@ void ExtendGDScriptParser::update_diagnostics() { } void ExtendGDScriptParser::update_symbols() { + + members.clear(); + const GDScriptParser::Node *head = get_parse_tree(); if (const GDScriptParser::ClassNode *gdclass = dynamic_cast(head)) { + parse_class_symbol(gdclass, class_symbol); + + for (int i = 0; i < class_symbol.children.size(); i++) { + const lsp::DocumentSymbol &symbol = class_symbol.children[i]; + members.set(symbol.name, &symbol); + } } } @@ -448,87 +457,32 @@ const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int } 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(head)) { - - if (const Map::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)); - } - } + const lsp::DocumentSymbol *const *ptr = members.getptr(p_name); + if (ptr) { + return *ptr; } return NULL; } -void ExtendGDScriptParser::dump_member_symbols(Map &r_symbols) { +const Array &ExtendGDScriptParser::get_member_completions() { - const GDScriptParser::Node *head = get_parse_tree(); - if (const GDScriptParser::ClassNode *gdclass = dynamic_cast(head)) { + if (member_completions.empty()) { - for (const Map::Element *E = gdclass->constant_expressions.front(); E; E = E->next()) { - get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(E->get().expression->line)); - } + const String *name = members.next(NULL); + while (name) { - for (int i = 0; i < gdclass->subclasses.size(); i++) { - const ClassNode *m = gdclass->subclasses[i]; - r_symbols.insert(JOIN_SYMBOLS(path, m->name), 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]; - r_symbols.insert(JOIN_SYMBOLS(path, m.identifier), 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]; - r_symbols.insert(JOIN_SYMBOLS(path, m->name), 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]; - r_symbols.insert(JOIN_SYMBOLS(path, m->name), get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m->line))); - } + const lsp::DocumentSymbol *symbol = members.get(*name); + lsp::CompletionItem item = symbol->make_completion_item(false); + item.data = JOIN_SYMBOLS(path, *name); + member_completions.push_back(item.to_json()); - for (int i = 0; i < gdclass->_signals.size(); i++) { - const GDScriptParser::ClassNode::Signal &m = gdclass->_signals[i]; - r_symbols.insert(JOIN_SYMBOLS(path, m.name), get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line))); + name = members.next(name); } } + + return member_completions; } Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) { -- cgit v1.2.3 From b2f02317fabe284220c74c21229e4cad6ab74e93 Mon Sep 17 00:00:00 2001 From: Geequlim Date: Tue, 25 Jun 2019 12:12:41 +0800 Subject: Improve symbol resolve for inner classes Only level one inner classes would be resolved currently but it sould cover most real world use case Improve documation parseing for const values Improve documation format for native symbols --- .../language_server/gdscript_extend_parser.cpp | 108 +++++++++++++++++---- 1 file changed, 89 insertions(+), 19 deletions(-) (limited to 'modules/gdscript/language_server/gdscript_extend_parser.cpp') diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index a6fbfd3779..16f4324da8 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -91,6 +91,16 @@ void ExtendGDScriptParser::update_symbols() { for (int i = 0; i < class_symbol.children.size(); i++) { const lsp::DocumentSymbol &symbol = class_symbol.children[i]; members.set(symbol.name, &symbol); + + // cache level one inner classes + if (symbol.kind == lsp::SymbolKind::Class) { + ClassMembers inner_class; + for (int j = 0; j < symbol.children.size(); j++) { + const lsp::DocumentSymbol &s = symbol.children[j]; + inner_class.set(s.name, &s); + } + inner_classes.set(symbol.name, inner_class); + } } } } @@ -112,7 +122,8 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p 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)); + bool is_root_class = &r_symbol == &class_symbol; + r_symbol.documentation = parse_documentation(is_root_class ? 0 : LINE_NUMBER_TO_INDEX(p_class->line), is_root_class); for (int i = 0; i < p_class->variables.size(); ++i) { @@ -192,7 +203,26 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p if (c.type.kind != GDScriptParser::DataType::UNRESOLVED) { symbol.detail += ": " + c.type.to_string(); } - symbol.detail += " = " + String(node->value); + + String value_text; + if (node->value.get_type() == Variant::OBJECT) { + RES res = node->value; + if (res.is_valid() && !res->get_path().empty()) { + value_text = "preload(\"" + res->get_path() + "\")"; + if (symbol.documentation.empty()) { + if (Map::Element *S = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.find(res->get_path())) { + symbol.documentation = S->get()->class_symbol.documentation; + } + } + } else { + value_text = JSON::print(node->value); + } + } else { + value_text = JSON::print(node->value); + } + if (!value_text.empty()) { + symbol.detail += " = " + value_text; + } r_symbol.children.push_back(symbol); } @@ -296,29 +326,43 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN } } -String ExtendGDScriptParser::parse_documentation(int p_line) { +String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) { ERR_FAIL_INDEX_V(p_line, lines.size(), String()); List 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())); + if (!p_docs_down) { // 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) { + int step = p_docs_down ? 1 : -1; + int start_line = p_docs_down ? p_line : p_line - 1; + for (int i = start_line; true; i += step) { + + if (i < 0 || i >= lines.size()) break; + 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())); + line_comment = line_comment.substr(1, line_comment.length()); + if (p_docs_down) { + doc_lines.push_back(line_comment); + } else { + doc_lines.push_front(line_comment); + } } else { - doc_lines.push_front(""); + if (p_docs_down) { + doc_lines.push_back(""); + } else { + doc_lines.push_front(""); + } } } else { break; @@ -456,11 +500,20 @@ const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int return search_symbol_defined_at_line(p_line, class_symbol); } -const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name) const { +const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name, const String &p_subclass) const { - const lsp::DocumentSymbol *const *ptr = members.getptr(p_name); - if (ptr) { - return *ptr; + if (p_subclass.empty()) { + const lsp::DocumentSymbol *const *ptr = members.getptr(p_name); + if (ptr) { + return *ptr; + } + } else { + if (const ClassMembers *_class = inner_classes.getptr(p_subclass)) { + const lsp::DocumentSymbol *const *ptr = _class->getptr(p_name); + if (ptr) { + return *ptr; + } + } } return NULL; @@ -474,12 +527,29 @@ const Array &ExtendGDScriptParser::get_member_completions() { while (name) { const lsp::DocumentSymbol *symbol = members.get(*name); - lsp::CompletionItem item = symbol->make_completion_item(false); + lsp::CompletionItem item = symbol->make_completion_item(); item.data = JOIN_SYMBOLS(path, *name); member_completions.push_back(item.to_json()); name = members.next(name); } + + const String *_class = inner_classes.next(NULL); + while (_class) { + + const ClassMembers *inner_class = inner_classes.getptr(*_class); + const String *member_name = inner_class->next(NULL); + while (member_name) { + const lsp::DocumentSymbol *symbol = inner_class->get(*member_name); + lsp::CompletionItem item = symbol->make_completion_item(); + item.data = JOIN_SYMBOLS(path, JOIN_SYMBOLS(*_class, *member_name)); + member_completions.push_back(item.to_json()); + + member_name = inner_class->next(member_name); + } + + _class = inner_classes.next(_class); + } } return member_completions; -- cgit v1.2.3 From 9618b0c63e3330865350bd8bbc6a9d2faf9dd26c Mon Sep 17 00:00:00 2001 From: Geequlim Date: Tue, 25 Jun 2019 18:09:42 +0800 Subject: Check client workspace directory is valid Drop test initialize message sent to client Remove unused code property for the parser class --- modules/gdscript/language_server/gdscript_extend_parser.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'modules/gdscript/language_server/gdscript_extend_parser.cpp') diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 16f4324da8..4d90c4eec3 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -557,7 +557,6 @@ const Array &ExtendGDScriptParser::get_member_completions() { Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) { path = p_path; - code = p_code; lines = p_code.split("\n"); Error err = GDScriptParser::parse(p_code, p_path.get_base_dir(), false, p_path, false, NULL, false); -- cgit v1.2.3 From 666ed89011551ae7691c8eeeb3fff74e17b48020 Mon Sep 17 00:00:00 2001 From: Geequlim Date: Wed, 26 Jun 2019 20:21:42 +0800 Subject: Add generate script api to dictionary support Expose GDScriptLanguageProtocol singleton and classes for editor plugins (Not visiable in class tree) Fix minor bug in symbol resolve --- .../language_server/gdscript_extend_parser.cpp | 152 ++++++++++++++++++++- 1 file changed, 147 insertions(+), 5 deletions(-) (limited to 'modules/gdscript/language_server/gdscript_extend_parser.cpp') diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 4d90c4eec3..1f140e14cd 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -139,7 +139,10 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p 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._export.type != Variant::NIL) { + symbol.detail += "export "; + } + symbol.detail += "var " + m.identifier; if (m.data_type.kind != GDScriptParser::DataType::UNRESOLVED) { symbol.detail += ": " + m.data_type.to_string(); } @@ -210,7 +213,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p if (res.is_valid() && !res->get_path().empty()) { value_text = "preload(\"" + res->get_path() + "\")"; if (symbol.documentation.empty()) { - if (Map::Element *S = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.find(res->get_path())) { + if (Map::Element *S = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(res->get_path())) { symbol.documentation = S->get()->class_symbol.documentation; } } @@ -335,7 +338,7 @@ String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) { 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()); + inline_comment = inline_comment.substr(comment_start, inline_comment.length()).strip_edges(); if (inline_comment.length() > 1) { doc_lines.push_back(inline_comment.substr(1, inline_comment.length())); } @@ -407,7 +410,7 @@ String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_c 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()); + String last_part = line.substr(p_cursor.character + 1, lines[i].length()); if (!p_symbol.empty()) { String left_cursor_text; for (int c = p_cursor.character - 1; c >= 0; c--) { @@ -473,7 +476,7 @@ String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position & } String ExtendGDScriptParser::get_uri() const { - return GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_uri(path); + 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 { @@ -555,6 +558,145 @@ const Array &ExtendGDScriptParser::get_member_completions() { return member_completions; } +Dictionary ExtendGDScriptParser::dump_function_api(const GDScriptParser::FunctionNode *p_func) const { + Dictionary func; + ERR_FAIL_NULL_V(p_func, func); + func["name"] = p_func->name; + func["return_type"] = p_func->return_type.to_string(); + func["rpc_mode"] = p_func->rpc_mode; + Array arguments; + for (int i = 0; i < p_func->arguments.size(); i++) { + Dictionary arg; + arg["name"] = p_func->arguments[i]; + arg["type"] = 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(p_func->default_values[default_value_idx]); + if (const_node == NULL) { + const GDScriptParser::OperatorNode *operator_node = dynamic_cast(p_func->default_values[default_value_idx]); + if (operator_node) { + const_node = dynamic_cast(operator_node->next); + } + } + if (const_node) { + arg["default_value"] = const_node->value; + } + } + arguments.push_back(arg); + } + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_func->line))) { + func["signature"] = symbol->detail; + func["description"] = symbol->documentation; + } + func["arguments"] = arguments; + return func; +} + +Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode *p_class) const { + Dictionary class_api; + + ERR_FAIL_NULL_V(p_class, class_api); + + class_api["name"] = String(p_class->name); + class_api["path"] = path; + Array extends_class; + for (int i = 0; i < p_class->extends_class.size(); i++) { + extends_class.append(String(p_class->extends_class[i])); + } + class_api["extends_class"] = extends_class; + class_api["extends_file"] = String(p_class->extends_file); + class_api["icon"] = String(p_class->icon_path); + + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_class->line))) { + class_api["signature"] = symbol->detail; + class_api["description"] = symbol->documentation; + } + + Array subclasses; + for (int i = 0; i < p_class->subclasses.size(); i++) { + subclasses.push_back(dump_class_api(p_class->subclasses[i])); + } + class_api["sub_classes"] = subclasses; + + Array constants; + for (Map::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { + + const GDScriptParser::ClassNode::Constant &c = E->value(); + const GDScriptParser::ConstantNode *node = dynamic_cast(c.expression); + + Dictionary api; + api["name"] = E->key(); + api["value"] = node->value; + api["data_type"] = node->datatype.to_string(); + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(node->line))) { + api["signature"] = symbol->detail; + api["description"] = symbol->documentation; + } + constants.push_back(api); + } + class_api["constants"] = constants; + + Array members; + for (int i = 0; i < p_class->variables.size(); ++i) { + const GDScriptParser::ClassNode::Member &m = p_class->variables[i]; + Dictionary api; + api["name"] = m.identifier; + api["data_type"] = m.data_type.to_string(); + api["default_value"] = m.default_value; + api["setter"] = String(m.setter); + api["getter"] = String(m.getter); + api["export"] = m._export.type != Variant::NIL; + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line))) { + api["signature"] = symbol->detail; + api["description"] = symbol->documentation; + } + members.push_back(api); + } + class_api["members"] = members; + + Array signals; + for (int i = 0; i < p_class->_signals.size(); ++i) { + const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i]; + Dictionary api; + api["name"] = signal.name; + Array args; + for (int j = 0; j < signal.arguments.size(); j++) { + args.append(signal.arguments[j]); + } + api["arguments"] = args; + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(signal.line))) { + api["signature"] = symbol->detail; + api["description"] = symbol->documentation; + } + signals.push_back(api); + } + class_api["signals"] = signals; + + Array methods; + for (int i = 0; i < p_class->functions.size(); ++i) { + methods.append(dump_function_api(p_class->functions[i])); + } + class_api["methods"] = methods; + + Array static_functions; + for (int i = 0; i < p_class->static_functions.size(); ++i) { + static_functions.append(dump_function_api(p_class->functions[i])); + } + class_api["static_functions"] = static_functions; + + return class_api; +} + +Dictionary ExtendGDScriptParser::generate_api() const { + + Dictionary api; + const GDScriptParser::Node *head = get_parse_tree(); + if (const GDScriptParser::ClassNode *gdclass = dynamic_cast(head)) { + api = dump_class_api(gdclass); + } + return api; +} + Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) { path = p_path; lines = p_code.split("\n"); -- cgit v1.2.3 From 72d11cd17355585bd3f2b6d467ebb45ad55b6759 Mon Sep 17 00:00:00 2001 From: geequlim Date: Sun, 30 Jun 2019 16:10:13 +0800 Subject: Add optional goto definition support for native symbols This action will show help for target symbol in godot editor and bring the godot editor window to foreground Improved markdown documentation for symbols. --- .../language_server/gdscript_extend_parser.cpp | 91 +++++++++++++++++----- 1 file changed, 71 insertions(+), 20 deletions(-) (limited to 'modules/gdscript/language_server/gdscript_extend_parser.cpp') diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 1f140e14cd..45f9ec9c6a 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -123,7 +123,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p r_symbol.selectionRange.start.line = r_symbol.range.start.line; r_symbol.detail = "class " + r_symbol.name; bool is_root_class = &r_symbol == &class_symbol; - r_symbol.documentation = parse_documentation(is_root_class ? 0 : LINE_NUMBER_TO_INDEX(p_class->line), is_root_class); + r_symbol.documentation = parse_documentation_as_markdown(is_root_class ? 0 : LINE_NUMBER_TO_INDEX(p_class->line), is_root_class); for (int i = 0; i < p_class->variables.size(); ++i) { @@ -150,7 +150,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p symbol.detail += " = " + JSON::print(m.default_value); } - symbol.documentation = parse_documentation(line); + symbol.documentation = parse_documentation_as_markdown(line); symbol.uri = uri; symbol.script_path = path; @@ -170,7 +170,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p 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.documentation = parse_documentation_as_markdown(line); symbol.uri = uri; symbol.script_path = path; symbol.detail = "signal " + signal.name + "("; @@ -198,7 +198,7 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p 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.documentation = parse_documentation_as_markdown(line); symbol.uri = uri; symbol.script_path = path; @@ -266,7 +266,7 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN 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.documentation = parse_documentation_as_markdown(line); r_symbol.uri = uri; r_symbol.script_path = path; @@ -324,11 +324,67 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) { symbol.detail += ": " + var->datatype.to_string(); } - symbol.documentation = GDScriptWorkspace::marked_documentation(parse_documentation(line)); + symbol.documentation = parse_documentation_as_markdown(line); r_symbol.children.push_back(symbol); } } +String ExtendGDScriptParser::marked_documentation(const String &p_bbcode) { + + String markdown = p_bbcode.strip_edges(); + + Vector 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]; + 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()); + } + + if (in_code_block && line.find("[/codeblock]") != -1) { + line = "'''\n"; + line = "\n"; + in_code_block = false; + } + + if (!in_code_block) { + line = line.strip_edges(); + 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]", "__"); + line = line.replace("[method ", "`"); + line = line.replace("[member ", "`"); + line = line.replace("[signal ", "`"); + line = line.replace("[enum ", "`"); + line = line.replace("[constant ", "`"); + line = line.replace("[", "`"); + line = line.replace("]", "`"); + } + + if (!in_code_block && i < lines.size() - 1) { + line += "\n\n"; + } else if (i < lines.size() - 1) { + line += "\n"; + } + markdown += line; + } + return markdown; +} + String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) { ERR_FAIL_INDEX_V(p_line, lines.size(), String()); @@ -353,19 +409,11 @@ String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) { String line_comment = lines[i].strip_edges(true, false); if (line_comment.begins_with("#")) { - if (line_comment.length() > 1) { - line_comment = line_comment.substr(1, line_comment.length()); - if (p_docs_down) { - doc_lines.push_back(line_comment); - } else { - doc_lines.push_front(line_comment); - } + line_comment = line_comment.substr(1, line_comment.length()); + if (p_docs_down) { + doc_lines.push_back(line_comment); } else { - if (p_docs_down) { - doc_lines.push_back(""); - } else { - doc_lines.push_front(""); - } + doc_lines.push_front(line_comment); } } else { break; @@ -374,8 +422,7 @@ String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) { String doc; for (List::Element *E = doc_lines.front(); E; E = E->next()) { - String content = E->get(); - doc += content + "\n"; + doc += E->get() + "\n"; } return doc; } @@ -496,6 +543,10 @@ const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(i return ret; } +String ExtendGDScriptParser::parse_documentation_as_markdown(int p_line, bool p_docs_down) { + return marked_documentation(parse_documentation(p_line, p_docs_down)); +} + const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const { if (p_line <= 0) { return &class_symbol; -- cgit v1.2.3