summaryrefslogtreecommitdiffstats
path: root/modules/gdscript/language_server
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gdscript/language_server')
-rw-r--r--modules/gdscript/language_server/gdscript_extend_parser.cpp462
-rw-r--r--modules/gdscript/language_server/gdscript_extend_parser.h78
-rw-r--r--modules/gdscript/language_server/gdscript_language_protocol.cpp10
-rw-r--r--modules/gdscript/language_server/gdscript_text_document.cpp55
-rw-r--r--modules/gdscript/language_server/gdscript_text_document.h2
-rw-r--r--modules/gdscript/language_server/gdscript_workspace.cpp199
-rw-r--r--modules/gdscript/language_server/gdscript_workspace.h8
-rw-r--r--modules/gdscript/language_server/godot_lsp.h173
8 files changed, 637 insertions, 350 deletions
diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp
index 3a5a54e275..362f253a99 100644
--- a/modules/gdscript/language_server/gdscript_extend_parser.cpp
+++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp
@@ -32,9 +32,94 @@
#include "../gdscript.h"
#include "../gdscript_analyzer.h"
+#include "editor/editor_settings.h"
#include "gdscript_language_protocol.h"
#include "gdscript_workspace.h"
+int get_indent_size() {
+ if (EditorSettings::get_singleton()) {
+ return EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size");
+ } else {
+ return 4;
+ }
+}
+
+lsp::Position GodotPosition::to_lsp(const Vector<String> &p_lines) const {
+ lsp::Position res;
+
+ // Special case: `line = 0` -> root class (range covers everything).
+ if (this->line <= 0) {
+ return res;
+ }
+ // Special case: `line = p_lines.size() + 1` -> root class (range covers everything).
+ if (this->line >= p_lines.size() + 1) {
+ res.line = p_lines.size();
+ return res;
+ }
+ res.line = this->line - 1;
+ // Note: character outside of `pos_line.length()-1` is valid.
+ res.character = this->column - 1;
+
+ String pos_line = p_lines[res.line];
+ if (pos_line.contains("\t")) {
+ int tab_size = get_indent_size();
+
+ int in_col = 1;
+ int res_char = 0;
+
+ while (res_char < pos_line.size() && in_col < this->column) {
+ if (pos_line[res_char] == '\t') {
+ in_col += tab_size;
+ res_char++;
+ } else {
+ in_col++;
+ res_char++;
+ }
+ }
+
+ res.character = res_char;
+ }
+
+ return res;
+}
+
+GodotPosition GodotPosition::from_lsp(const lsp::Position p_pos, const Vector<String> &p_lines) {
+ GodotPosition res(p_pos.line + 1, p_pos.character + 1);
+
+ // Line outside of actual text is valid (-> pos/cursor at end of text).
+ if (res.line > p_lines.size()) {
+ return res;
+ }
+
+ String line = p_lines[p_pos.line];
+ int tabs_before_char = 0;
+ for (int i = 0; i < p_pos.character && i < line.length(); i++) {
+ if (line[i] == '\t') {
+ tabs_before_char++;
+ }
+ }
+
+ if (tabs_before_char > 0) {
+ int tab_size = get_indent_size();
+ res.column += tabs_before_char * (tab_size - 1);
+ }
+
+ return res;
+}
+
+lsp::Range GodotRange::to_lsp(const Vector<String> &p_lines) const {
+ lsp::Range res;
+ res.start = start.to_lsp(p_lines);
+ res.end = end.to_lsp(p_lines);
+ return res;
+}
+
+GodotRange GodotRange::from_lsp(const lsp::Range &p_range, const Vector<String> &p_lines) {
+ GodotPosition start = GodotPosition::from_lsp(p_range.start, p_lines);
+ GodotPosition end = GodotPosition::from_lsp(p_range.end, p_lines);
+ return GodotRange(start, end);
+}
+
void ExtendGDScriptParser::update_diagnostics() {
diagnostics.clear();
@@ -90,7 +175,7 @@ void ExtendGDScriptParser::update_symbols() {
const lsp::DocumentSymbol &symbol = class_symbol.children[i];
members.insert(symbol.name, &symbol);
- // cache level one inner classes
+ // Cache level one inner classes.
if (symbol.kind == lsp::SymbolKind::Class) {
ClassMembers inner_class;
for (int j = 0; j < symbol.children.size(); j++) {
@@ -126,10 +211,7 @@ void ExtendGDScriptParser::update_document_links(const String &p_code) {
String value = const_val;
lsp::DocumentLink link;
link.target = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_uri(scr_path);
- link.range.start.line = LINE_NUMBER_TO_INDEX(token.start_line);
- link.range.end.line = LINE_NUMBER_TO_INDEX(token.end_line);
- link.range.start.character = LINE_NUMBER_TO_INDEX(token.start_column);
- link.range.end.character = LINE_NUMBER_TO_INDEX(token.end_column);
+ link.range = GodotRange(GodotPosition(token.start_line, token.start_column), GodotPosition(token.end_line, token.end_column)).to_lsp(this->lines);
document_links.push_back(link);
}
}
@@ -137,6 +219,12 @@ void ExtendGDScriptParser::update_document_links(const String &p_code) {
}
}
+lsp::Range ExtendGDScriptParser::range_of_node(const GDScriptParser::Node *p_node) const {
+ GodotPosition start(p_node->start_line, p_node->start_column);
+ GodotPosition end(p_node->end_line, p_node->end_column);
+ return GodotRange(start, end).to_lsp(this->lines);
+}
+
void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol) {
const String uri = get_uri();
@@ -149,13 +237,30 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
}
r_symbol.kind = lsp::SymbolKind::Class;
r_symbol.deprecated = false;
- r_symbol.range.start.line = p_class->start_line;
- r_symbol.range.start.character = p_class->start_column;
- r_symbol.range.end.line = lines.size();
- r_symbol.selectionRange.start.line = r_symbol.range.start.line;
+ r_symbol.range = range_of_node(p_class);
+ r_symbol.range.start.line = MAX(r_symbol.range.start.line, 0);
+ if (p_class->identifier) {
+ r_symbol.selectionRange = range_of_node(p_class->identifier);
+ }
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->start_line), is_root_class);
+ {
+ String doc = p_class->doc_data.description;
+ if (!p_class->doc_data.description.is_empty()) {
+ doc += "\n\n" + p_class->doc_data.description;
+ }
+
+ if (!p_class->doc_data.tutorials.is_empty()) {
+ doc += "\n";
+ for (const Pair<String, String> &tutorial : p_class->doc_data.tutorials) {
+ if (tutorial.first.is_empty()) {
+ doc += vformat("\n@tutorial: %s", tutorial.second);
+ } else {
+ doc += vformat("\n@tutorial(%s): %s", tutorial.first, tutorial.second);
+ }
+ }
+ }
+ r_symbol.documentation = doc;
+ }
for (int i = 0; i < p_class->members.size(); i++) {
const ClassNode::Member &m = p_class->members[i];
@@ -166,11 +271,8 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
symbol.name = m.variable->identifier->name;
symbol.kind = m.variable->property == VariableNode::PROP_NONE ? lsp::SymbolKind::Variable : lsp::SymbolKind::Property;
symbol.deprecated = false;
- symbol.range.start.line = LINE_NUMBER_TO_INDEX(m.variable->start_line);
- symbol.range.start.character = LINE_NUMBER_TO_INDEX(m.variable->start_column);
- symbol.range.end.line = LINE_NUMBER_TO_INDEX(m.variable->end_line);
- symbol.range.end.character = LINE_NUMBER_TO_INDEX(m.variable->end_column);
- symbol.selectionRange.start.line = symbol.range.start.line;
+ symbol.range = range_of_node(m.variable);
+ symbol.selectionRange = range_of_node(m.variable->identifier);
if (m.variable->exported) {
symbol.detail += "@export ";
}
@@ -182,10 +284,31 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
symbol.detail += " = " + m.variable->initializer->reduced_value.to_json_string();
}
- symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(m.variable->start_line));
+ symbol.documentation = m.variable->doc_data.description;
symbol.uri = uri;
symbol.script_path = path;
+ if (m.variable->initializer && m.variable->initializer->type == GDScriptParser::Node::LAMBDA) {
+ GDScriptParser::LambdaNode *lambda_node = (GDScriptParser::LambdaNode *)m.variable->initializer;
+ lsp::DocumentSymbol lambda;
+ parse_function_symbol(lambda_node->function, lambda);
+ // Merge lambda into current variable.
+ symbol.children.append_array(lambda.children);
+ }
+
+ if (m.variable->getter && m.variable->getter->type == GDScriptParser::Node::FUNCTION) {
+ lsp::DocumentSymbol get_symbol;
+ parse_function_symbol(m.variable->getter, get_symbol);
+ get_symbol.local = true;
+ symbol.children.push_back(get_symbol);
+ }
+ if (m.variable->setter && m.variable->setter->type == GDScriptParser::Node::FUNCTION) {
+ lsp::DocumentSymbol set_symbol;
+ parse_function_symbol(m.variable->setter, set_symbol);
+ set_symbol.local = true;
+ symbol.children.push_back(set_symbol);
+ }
+
r_symbol.children.push_back(symbol);
} break;
case ClassNode::Member::CONSTANT: {
@@ -194,12 +317,9 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
symbol.name = m.constant->identifier->name;
symbol.kind = lsp::SymbolKind::Constant;
symbol.deprecated = false;
- symbol.range.start.line = LINE_NUMBER_TO_INDEX(m.constant->start_line);
- symbol.range.start.character = LINE_NUMBER_TO_INDEX(m.constant->start_column);
- symbol.range.end.line = LINE_NUMBER_TO_INDEX(m.constant->end_line);
- symbol.range.end.character = LINE_NUMBER_TO_INDEX(m.constant->start_column);
- symbol.selectionRange.start.line = LINE_NUMBER_TO_INDEX(m.constant->start_line);
- symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(m.constant->start_line));
+ symbol.range = range_of_node(m.constant);
+ symbol.selectionRange = range_of_node(m.constant->identifier);
+ symbol.documentation = m.constant->doc_data.description;
symbol.uri = uri;
symbol.script_path = path;
@@ -231,36 +351,14 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
r_symbol.children.push_back(symbol);
} break;
- case ClassNode::Member::ENUM_VALUE: {
- lsp::DocumentSymbol symbol;
-
- symbol.name = m.enum_value.identifier->name;
- symbol.kind = lsp::SymbolKind::EnumMember;
- symbol.deprecated = false;
- symbol.range.start.line = LINE_NUMBER_TO_INDEX(m.enum_value.line);
- symbol.range.start.character = LINE_NUMBER_TO_INDEX(m.enum_value.leftmost_column);
- symbol.range.end.line = LINE_NUMBER_TO_INDEX(m.enum_value.line);
- symbol.range.end.character = LINE_NUMBER_TO_INDEX(m.enum_value.rightmost_column);
- symbol.selectionRange.start.line = LINE_NUMBER_TO_INDEX(m.enum_value.line);
- symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(m.enum_value.line));
- symbol.uri = uri;
- symbol.script_path = path;
-
- symbol.detail = symbol.name + " = " + itos(m.enum_value.value);
-
- r_symbol.children.push_back(symbol);
- } break;
case ClassNode::Member::SIGNAL: {
lsp::DocumentSymbol symbol;
symbol.name = m.signal->identifier->name;
symbol.kind = lsp::SymbolKind::Event;
symbol.deprecated = false;
- symbol.range.start.line = LINE_NUMBER_TO_INDEX(m.signal->start_line);
- symbol.range.start.character = LINE_NUMBER_TO_INDEX(m.signal->start_column);
- symbol.range.end.line = LINE_NUMBER_TO_INDEX(m.signal->end_line);
- symbol.range.end.character = LINE_NUMBER_TO_INDEX(m.signal->end_column);
- symbol.selectionRange.start.line = symbol.range.start.line;
- symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(m.signal->start_line));
+ symbol.range = range_of_node(m.signal);
+ symbol.selectionRange = range_of_node(m.signal->identifier);
+ symbol.documentation = m.signal->doc_data.description;
symbol.uri = uri;
symbol.script_path = path;
symbol.detail = "signal " + String(m.signal->identifier->name) + "(";
@@ -272,17 +370,48 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
}
symbol.detail += ")";
+ for (GDScriptParser::ParameterNode *param : m.signal->parameters) {
+ lsp::DocumentSymbol param_symbol;
+ param_symbol.name = param->identifier->name;
+ param_symbol.kind = lsp::SymbolKind::Variable;
+ param_symbol.deprecated = false;
+ param_symbol.local = true;
+ param_symbol.range = range_of_node(param);
+ param_symbol.selectionRange = range_of_node(param->identifier);
+ param_symbol.uri = uri;
+ param_symbol.script_path = path;
+ param_symbol.detail = "var " + param_symbol.name;
+ if (param->get_datatype().is_hard_type()) {
+ param_symbol.detail += ": " + param->get_datatype().to_string();
+ }
+ symbol.children.push_back(param_symbol);
+ }
+ r_symbol.children.push_back(symbol);
+ } break;
+ case ClassNode::Member::ENUM_VALUE: {
+ lsp::DocumentSymbol symbol;
+
+ symbol.name = m.enum_value.identifier->name;
+ symbol.kind = lsp::SymbolKind::EnumMember;
+ symbol.deprecated = false;
+ symbol.range.start = GodotPosition(m.enum_value.line, m.enum_value.leftmost_column).to_lsp(this->lines);
+ symbol.range.end = GodotPosition(m.enum_value.line, m.enum_value.rightmost_column).to_lsp(this->lines);
+ symbol.selectionRange = range_of_node(m.enum_value.identifier);
+ symbol.documentation = m.enum_value.doc_data.description;
+ symbol.uri = uri;
+ symbol.script_path = path;
+
+ symbol.detail = symbol.name + " = " + itos(m.enum_value.value);
+
r_symbol.children.push_back(symbol);
} break;
case ClassNode::Member::ENUM: {
lsp::DocumentSymbol symbol;
+ symbol.name = m.m_enum->identifier->name;
symbol.kind = lsp::SymbolKind::Enum;
- symbol.range.start.line = LINE_NUMBER_TO_INDEX(m.m_enum->start_line);
- symbol.range.start.character = LINE_NUMBER_TO_INDEX(m.m_enum->start_column);
- symbol.range.end.line = LINE_NUMBER_TO_INDEX(m.m_enum->end_line);
- symbol.range.end.character = LINE_NUMBER_TO_INDEX(m.m_enum->end_column);
- symbol.selectionRange.start.line = symbol.range.start.line;
- symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(m.m_enum->start_line));
+ symbol.range = range_of_node(m.m_enum);
+ symbol.selectionRange = range_of_node(m.m_enum->identifier);
+ symbol.documentation = m.m_enum->doc_data.description;
symbol.uri = uri;
symbol.script_path = path;
@@ -294,6 +423,25 @@ void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p
symbol.detail += String(m.m_enum->values[j].identifier->name) + " = " + itos(m.m_enum->values[j].value);
}
symbol.detail += "}";
+
+ for (GDScriptParser::EnumNode::Value value : m.m_enum->values) {
+ lsp::DocumentSymbol child;
+
+ child.name = value.identifier->name;
+ child.kind = lsp::SymbolKind::EnumMember;
+ child.deprecated = false;
+ child.range.start = GodotPosition(value.line, value.leftmost_column).to_lsp(this->lines);
+ child.range.end = GodotPosition(value.line, value.rightmost_column).to_lsp(this->lines);
+ child.selectionRange = range_of_node(value.identifier);
+ child.documentation = value.doc_data.description;
+ child.uri = uri;
+ child.script_path = path;
+
+ child.detail = child.name + " = " + itos(value.value);
+
+ symbol.children.push_back(child);
+ }
+
r_symbol.children.push_back(symbol);
} break;
case ClassNode::Member::FUNCTION: {
@@ -317,32 +465,29 @@ 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->identifier->name;
- r_symbol.kind = p_func->is_static ? lsp::SymbolKind::Function : lsp::SymbolKind::Method;
- r_symbol.detail = "func " + String(p_func->identifier->name) + "(";
+ bool is_named = p_func->identifier != nullptr;
+
+ r_symbol.name = is_named ? p_func->identifier->name : "";
+ r_symbol.kind = (p_func->is_static || p_func->source_lambda != nullptr) ? lsp::SymbolKind::Function : lsp::SymbolKind::Method;
+ r_symbol.detail = "func";
+ if (is_named) {
+ r_symbol.detail += " " + String(p_func->identifier->name);
+ }
+ r_symbol.detail += "(";
r_symbol.deprecated = false;
- r_symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_func->start_line);
- r_symbol.range.start.character = LINE_NUMBER_TO_INDEX(p_func->start_column);
- r_symbol.range.end.line = LINE_NUMBER_TO_INDEX(p_func->start_line);
- r_symbol.range.end.character = LINE_NUMBER_TO_INDEX(p_func->end_column);
- r_symbol.selectionRange.start.line = r_symbol.range.start.line;
- r_symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(p_func->start_line));
+ r_symbol.range = range_of_node(p_func);
+ if (is_named) {
+ r_symbol.selectionRange = range_of_node(p_func->identifier);
+ } else {
+ r_symbol.selectionRange.start = r_symbol.selectionRange.end = r_symbol.range.start;
+ }
+ r_symbol.documentation = p_func->doc_data.description;
r_symbol.uri = uri;
r_symbol.script_path = path;
String parameters;
for (int i = 0; i < p_func->parameters.size(); i++) {
const ParameterNode *parameter = p_func->parameters[i];
- lsp::DocumentSymbol symbol;
- symbol.kind = lsp::SymbolKind::Variable;
- symbol.name = parameter->identifier->name;
- symbol.range.start.line = LINE_NUMBER_TO_INDEX(parameter->start_line);
- symbol.range.start.character = LINE_NUMBER_TO_INDEX(parameter->start_column);
- symbol.range.end.line = LINE_NUMBER_TO_INDEX(parameter->end_line);
- symbol.range.end.character = LINE_NUMBER_TO_INDEX(parameter->end_column);
- symbol.uri = uri;
- symbol.script_path = path;
- r_symbol.children.push_back(symbol);
if (i > 0) {
parameters += ", ";
}
@@ -387,6 +532,13 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN
node_stack.push_back(while_node->loop);
} break;
+ case GDScriptParser::TypeNode::MATCH: {
+ GDScriptParser::MatchNode *match_node = (GDScriptParser::MatchNode *)node;
+ for (GDScriptParser::MatchBranchNode *branch_node : match_node->branches) {
+ node_stack.push_back(branch_node);
+ }
+ } break;
+
case GDScriptParser::TypeNode::MATCH_BRANCH: {
GDScriptParser::MatchBranchNode *match_node = (GDScriptParser::MatchBranchNode *)node;
node_stack.push_back(match_node->block);
@@ -400,20 +552,6 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN
}
} break;
- case GDScriptParser::TypeNode::VARIABLE: {
- GDScriptParser::VariableNode *variable_node = (GDScriptParser::VariableNode *)(node);
- lsp::DocumentSymbol symbol;
- symbol.kind = lsp::SymbolKind::Variable;
- symbol.name = variable_node->identifier->name;
- symbol.range.start.line = LINE_NUMBER_TO_INDEX(variable_node->start_line);
- symbol.range.start.character = LINE_NUMBER_TO_INDEX(variable_node->start_column);
- symbol.range.end.line = LINE_NUMBER_TO_INDEX(variable_node->end_line);
- symbol.range.end.character = LINE_NUMBER_TO_INDEX(variable_node->end_column);
- symbol.uri = uri;
- symbol.script_path = path;
- r_symbol.children.push_back(symbol);
- } break;
-
default:
continue;
}
@@ -426,10 +564,40 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN
lsp::DocumentSymbol symbol;
symbol.name = local.name;
symbol.kind = local.type == SuiteNode::Local::CONSTANT ? lsp::SymbolKind::Constant : lsp::SymbolKind::Variable;
- symbol.range.start.line = LINE_NUMBER_TO_INDEX(local.start_line);
- symbol.range.start.character = LINE_NUMBER_TO_INDEX(local.start_column);
- symbol.range.end.line = LINE_NUMBER_TO_INDEX(local.end_line);
- symbol.range.end.character = LINE_NUMBER_TO_INDEX(local.end_column);
+ switch (local.type) {
+ case SuiteNode::Local::CONSTANT:
+ symbol.range = range_of_node(local.constant);
+ symbol.selectionRange = range_of_node(local.constant->identifier);
+ break;
+ case SuiteNode::Local::VARIABLE:
+ symbol.range = range_of_node(local.variable);
+ symbol.selectionRange = range_of_node(local.variable->identifier);
+ if (local.variable->initializer && local.variable->initializer->type == GDScriptParser::Node::LAMBDA) {
+ GDScriptParser::LambdaNode *lambda_node = (GDScriptParser::LambdaNode *)local.variable->initializer;
+ lsp::DocumentSymbol lambda;
+ parse_function_symbol(lambda_node->function, lambda);
+ // Merge lambda into current variable.
+ // -> Only interested in new variables, not lambda itself.
+ symbol.children.append_array(lambda.children);
+ }
+ break;
+ case SuiteNode::Local::PARAMETER:
+ symbol.range = range_of_node(local.parameter);
+ symbol.selectionRange = range_of_node(local.parameter->identifier);
+ break;
+ case SuiteNode::Local::FOR_VARIABLE:
+ case SuiteNode::Local::PATTERN_BIND:
+ symbol.range = range_of_node(local.bind);
+ symbol.selectionRange = range_of_node(local.bind);
+ break;
+ default:
+ // Fallback.
+ symbol.range.start = GodotPosition(local.start_line, local.start_column).to_lsp(get_lines());
+ symbol.range.end = GodotPosition(local.end_line, local.end_column).to_lsp(get_lines());
+ symbol.selectionRange = symbol.range;
+ break;
+ }
+ symbol.local = true;
symbol.uri = uri;
symbol.script_path = path;
symbol.detail = local.type == SuiteNode::Local::CONSTANT ? "const " : "var ";
@@ -437,53 +605,19 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN
if (local.get_datatype().is_hard_type()) {
symbol.detail += ": " + local.get_datatype().to_string();
}
- symbol.documentation = parse_documentation(LINE_NUMBER_TO_INDEX(local.start_line));
- r_symbol.children.push_back(symbol);
- }
- }
-}
-
-String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) {
- ERR_FAIL_INDEX_V(p_line, lines.size(), String());
-
- List<String> doc_lines;
-
- 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()).strip_edges();
- if (inline_comment.length() > 1) {
- doc_lines.push_back(inline_comment.substr(2, inline_comment.length()));
+ switch (local.type) {
+ case SuiteNode::Local::CONSTANT:
+ symbol.documentation = local.constant->doc_data.description;
+ break;
+ case SuiteNode::Local::VARIABLE:
+ symbol.documentation = local.variable->doc_data.description;
+ break;
+ default:
+ break;
}
+ r_symbol.children.push_back(symbol);
}
}
-
- 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("##")) {
- line_comment = line_comment.substr(2, line_comment.length());
- if (p_docs_down) {
- doc_lines.push_back(line_comment);
- } else {
- doc_lines.push_front(line_comment);
- }
- } else {
- break;
- }
- }
-
- String doc;
- for (const String &E : doc_lines) {
- doc += E + "\n";
- }
- return doc;
}
String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) const {
@@ -492,7 +626,7 @@ String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_curs
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 += String::chr(0xFFFF); // Not unicode, represents the cursor.
longthing += lines[i].substr(p_cursor.character, lines[i].size());
} else {
longthing += lines[i];
@@ -513,7 +647,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 + 1, lines[i].length());
+ String last_part = line.substr(p_cursor.character, lines[i].length());
if (!p_symbol.is_empty()) {
String left_cursor_text;
for (int c = p_cursor.character - 1; c >= 0; c--) {
@@ -527,9 +661,9 @@ String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_c
}
longthing += first_part;
- longthing += String::chr(0xFFFF); //not unicode, represents the cursor
+ longthing += String::chr(0xFFFF); // Not unicode, represents the cursor.
if (p_func_required) {
- longthing += "("; // tell the parser this is a function call
+ longthing += "("; // Tell the parser this is a function call.
}
longthing += last_part;
} else {
@@ -544,7 +678,7 @@ String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_c
return longthing;
}
-String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const {
+String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &p_position, lsp::Range &r_range) const {
ERR_FAIL_INDEX_V(p_position.line, lines.size(), "");
String line = lines[p_position.line];
if (line.is_empty()) {
@@ -552,8 +686,32 @@ String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &
}
ERR_FAIL_INDEX_V(p_position.character, line.size(), "");
- int start_pos = p_position.character;
- for (int c = p_position.character; c >= 0; c--) {
+ // `p_position` cursor is BETWEEN chars, not ON chars.
+ // ->
+ // ```gdscript
+ // var member| := some_func|(some_variable|)
+ // ^ ^ ^
+ // | | | cursor on `some_variable, position on `)`
+ // | |
+ // | | cursor on `some_func`, pos on `(`
+ // |
+ // | cursor on `member`, pos on ` ` (space)
+ // ```
+ // -> Move position to previous character if:
+ // * Position not on valid identifier char.
+ // * Prev position is valid identifier char.
+ lsp::Position pos = p_position;
+ if (
+ pos.character >= line.length() // Cursor at end of line.
+ || (!is_ascii_identifier_char(line[pos.character]) // Not on valid identifier char.
+ && (pos.character > 0 // Not line start -> there is a prev char.
+ && is_ascii_identifier_char(line[pos.character - 1]) // Prev is valid identifier char.
+ ))) {
+ pos.character--;
+ }
+
+ int start_pos = pos.character;
+ for (int c = pos.character; c >= 0; c--) {
start_pos = c;
char32_t ch = line[c];
bool valid_char = is_ascii_identifier_char(ch);
@@ -562,8 +720,8 @@ String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &
}
}
- int end_pos = p_position.character;
- for (int c = p_position.character; c < line.length(); c++) {
+ int end_pos = pos.character;
+ for (int c = pos.character; c < line.length(); c++) {
char32_t ch = line[c];
bool valid_char = is_ascii_identifier_char(ch);
if (!valid_char) {
@@ -571,9 +729,11 @@ String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &
}
end_pos = c;
}
+
if (start_pos < end_pos) {
- p_offset.x = start_pos - p_position.character;
- p_offset.y = end_pos - p_position.character;
+ r_range.start.line = r_range.end.line = pos.line;
+ r_range.start.character = start_pos + 1;
+ r_range.end.character = end_pos + 1;
return line.substr(start_pos + 1, end_pos - start_pos);
}
@@ -584,15 +744,15 @@ 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 *ExtendGDScriptParser::search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent, const String &p_symbol_name) const {
const lsp::DocumentSymbol *ret = nullptr;
if (p_line < p_parent.range.start.line) {
return ret;
- } else if (p_parent.range.start.line == p_line) {
+ } else if (p_parent.range.start.line == p_line && (p_symbol_name.is_empty() || p_parent.name == p_symbol_name)) {
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]);
+ ret = search_symbol_defined_at_line(p_line, p_parent.children[i], p_symbol_name);
if (ret) {
break;
}
@@ -645,11 +805,11 @@ Error ExtendGDScriptParser::get_left_function_call(const lsp::Position &p_positi
return ERR_METHOD_NOT_FOUND;
}
-const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const {
+const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line, const String &p_symbol_name) const {
if (p_line <= 0) {
return &class_symbol;
}
- return search_symbol_defined_at_line(p_line, class_symbol);
+ return search_symbol_defined_at_line(p_line, class_symbol, p_symbol_name);
}
const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name, const String &p_subclass) const {
diff --git a/modules/gdscript/language_server/gdscript_extend_parser.h b/modules/gdscript/language_server/gdscript_extend_parser.h
index 4fd27de081..a808f19e5b 100644
--- a/modules/gdscript/language_server/gdscript_extend_parser.h
+++ b/modules/gdscript/language_server/gdscript_extend_parser.h
@@ -39,6 +39,9 @@
#ifndef LINE_NUMBER_TO_INDEX
#define LINE_NUMBER_TO_INDEX(p_line) ((p_line)-1)
#endif
+#ifndef COLUMN_NUMBER_TO_INDEX
+#define COLUMN_NUMBER_TO_INDEX(p_column) ((p_column)-1)
+#endif
#ifndef SYMBOL_SEPERATOR
#define SYMBOL_SEPERATOR "::"
@@ -50,6 +53,64 @@
typedef HashMap<String, const lsp::DocumentSymbol *> ClassMembers;
+/**
+ * Represents a Position as used by GDScript Parser. Used for conversion to and from `lsp::Position`.
+ *
+ * Difference to `lsp::Position`:
+ * * Line & Char/column: 1-based
+ * * LSP: both 0-based
+ * * Tabs are expanded to columns using tab size (`text_editor/behavior/indent/size`).
+ * * LSP: tab is single char
+ *
+ * Example:
+ * ```gdscript
+ * β†’β†’var my_value = 42
+ * ```
+ * `_` is at:
+ * * Godot: `column=12`
+ * * using `indent/size=4`
+ * * Note: counting starts at `1`
+ * * LSP: `character=8`
+ * * Note: counting starts at `0`
+ */
+struct GodotPosition {
+ int line;
+ int column;
+
+ GodotPosition(int p_line, int p_column) :
+ line(p_line), column(p_column) {}
+
+ lsp::Position to_lsp(const Vector<String> &p_lines) const;
+ static GodotPosition from_lsp(const lsp::Position p_pos, const Vector<String> &p_lines);
+
+ bool operator==(const GodotPosition &p_other) const {
+ return line == p_other.line && column == p_other.column;
+ }
+
+ String to_string() const {
+ return vformat("(%d,%d)", line, column);
+ }
+};
+
+struct GodotRange {
+ GodotPosition start;
+ GodotPosition end;
+
+ GodotRange(GodotPosition p_start, GodotPosition p_end) :
+ start(p_start), end(p_end) {}
+
+ lsp::Range to_lsp(const Vector<String> &p_lines) const;
+ static GodotRange from_lsp(const lsp::Range &p_range, const Vector<String> &p_lines);
+
+ bool operator==(const GodotRange &p_other) const {
+ return start == p_other.start && end == p_other.end;
+ }
+
+ String to_string() const {
+ return vformat("[%s:%s]", start.to_string(), end.to_string());
+ }
+};
+
class ExtendGDScriptParser : public GDScriptParser {
String path;
Vector<String> lines;
@@ -60,6 +121,8 @@ class ExtendGDScriptParser : public GDScriptParser {
ClassMembers members;
HashMap<String, ClassMembers> inner_classes;
+ lsp::Range range_of_node(const GDScriptParser::Node *p_node) const;
+
void update_diagnostics();
void update_symbols();
@@ -70,8 +133,7 @@ class ExtendGDScriptParser : public GDScriptParser {
Dictionary dump_function_api(const GDScriptParser::FunctionNode *p_func) const;
Dictionary dump_class_api(const GDScriptParser::ClassNode *p_class) const;
- String parse_documentation(int p_line, bool p_docs_down = false);
- const lsp::DocumentSymbol *search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const;
+ const lsp::DocumentSymbol *search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent, const String &p_symbol_name = "") const;
Array member_completions;
@@ -87,10 +149,18 @@ public:
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_required = false) const;
- String get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const;
+ String get_identifier_under_position(const lsp::Position &p_position, lsp::Range &r_range) const;
String get_uri() const;
- const lsp::DocumentSymbol *get_symbol_defined_at_line(int p_line) const;
+ /**
+ * `p_symbol_name` gets ignored if empty. Otherwise symbol must match passed in named.
+ *
+ * Necessary when multiple symbols at same line for example with `func`:
+ * `func handle_arg(arg: int):`
+ * -> Without `p_symbol_name`: returns `handle_arg`. Even if parameter (`arg`) is wanted.
+ * With `p_symbol_name`: symbol name MUST match `p_symbol_name`: returns `arg`.
+ */
+ const lsp::DocumentSymbol *get_symbol_defined_at_line(int p_line, const String &p_symbol_name = "") const;
const lsp::DocumentSymbol *get_member_symbol(const String &p_name, const String &p_subclass = "") const;
const List<lsp::DocumentLink> &get_document_links() const;
diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp
index 112db4df3a..14fc21d7dc 100644
--- a/modules/gdscript/language_server/gdscript_language_protocol.cpp
+++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp
@@ -278,6 +278,11 @@ void GDScriptLanguageProtocol::stop() {
}
void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client_id) {
+#ifdef TESTS_ENABLED
+ if (clients.is_empty()) {
+ return;
+ }
+#endif
if (p_client_id == -1) {
ERR_FAIL_COND_MSG(latest_client_id == -1,
"GDScript LSP: Can't notify client as none was connected.");
@@ -294,6 +299,11 @@ void GDScriptLanguageProtocol::notify_client(const String &p_method, const Varia
}
void GDScriptLanguageProtocol::request_client(const String &p_method, const Variant &p_params, int p_client_id) {
+#ifdef TESTS_ENABLED
+ if (clients.is_empty()) {
+ return;
+ }
+#endif
if (p_client_id == -1) {
ERR_FAIL_COND_MSG(latest_client_id == -1,
"GDScript LSP: Can't notify client as none was connected.");
diff --git a/modules/gdscript/language_server/gdscript_text_document.cpp b/modules/gdscript/language_server/gdscript_text_document.cpp
index 92a5f55978..1e927f9f6e 100644
--- a/modules/gdscript/language_server/gdscript_text_document.cpp
+++ b/modules/gdscript/language_server/gdscript_text_document.cpp
@@ -50,6 +50,8 @@ void GDScriptTextDocument::_bind_methods() {
ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);
ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve);
ClassDB::bind_method(D_METHOD("rename"), &GDScriptTextDocument::rename);
+ ClassDB::bind_method(D_METHOD("prepareRename"), &GDScriptTextDocument::prepareRename);
+ ClassDB::bind_method(D_METHOD("references"), &GDScriptTextDocument::references);
ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange);
ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);
ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);
@@ -161,11 +163,8 @@ Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri);
Array arr;
if (HashMap<String, ExtendGDScriptParser *>::ConstIterator parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) {
- Vector<lsp::DocumentedSymbolInformation> list;
- parser->value->get_symbols().symbol_tree_as_list(uri, list);
- for (int i = 0; i < list.size(); i++) {
- arr.push_back(list[i].to_json());
- }
+ lsp::DocumentSymbol symbol = parser->value->get_symbols();
+ arr.push_back(symbol.to_json(true));
}
return arr;
}
@@ -253,6 +252,48 @@ Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {
return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);
}
+Variant GDScriptTextDocument::prepareRename(const Dictionary &p_params) {
+ lsp::TextDocumentPositionParams params;
+ params.load(p_params);
+
+ lsp::DocumentSymbol symbol;
+ lsp::Range range;
+ if (GDScriptLanguageProtocol::get_singleton()->get_workspace()->can_rename(params, symbol, range)) {
+ return Variant(range.to_json());
+ }
+
+ // `null` -> rename not valid at current location.
+ return Variant();
+}
+
+Array GDScriptTextDocument::references(const Dictionary &p_params) {
+ Array res;
+
+ lsp::ReferenceParams params;
+ params.load(p_params);
+
+ const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
+ if (symbol) {
+ Vector<lsp::Location> usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_all_usages(*symbol);
+ res.resize(usages.size());
+ int declaration_adjustment = 0;
+ for (int i = 0; i < usages.size(); i++) {
+ lsp::Location usage = usages[i];
+ if (!params.context.includeDeclaration && usage.range == symbol->range) {
+ declaration_adjustment++;
+ continue;
+ }
+ res[i - declaration_adjustment] = usages[i].to_json();
+ }
+
+ if (declaration_adjustment > 0) {
+ res.resize(res.size() - declaration_adjustment);
+ }
+ }
+
+ return res;
+}
+
Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
lsp::CompletionItem item;
item.load(p_params);
@@ -450,7 +491,7 @@ Array GDScriptTextDocument::find_symbols(const lsp::TextDocumentPositionParams &
if (symbol) {
lsp::Location location;
location.uri = symbol->uri;
- location.range = symbol->range;
+ location.range = symbol->selectionRange;
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());
@@ -464,7 +505,7 @@ Array GDScriptTextDocument::find_symbols(const lsp::TextDocumentPositionParams &
if (!s->uri.is_empty()) {
lsp::Location location;
location.uri = s->uri;
- location.range = s->range;
+ location.range = s->selectionRange;
arr.push_back(location.to_json());
r_list.push_back(s);
}
diff --git a/modules/gdscript/language_server/gdscript_text_document.h b/modules/gdscript/language_server/gdscript_text_document.h
index 0121101db2..cfd0490f0a 100644
--- a/modules/gdscript/language_server/gdscript_text_document.h
+++ b/modules/gdscript/language_server/gdscript_text_document.h
@@ -65,6 +65,8 @@ public:
Array completion(const Dictionary &p_params);
Dictionary resolve(const Dictionary &p_params);
Dictionary rename(const Dictionary &p_params);
+ Variant prepareRename(const Dictionary &p_params);
+ Array references(const Dictionary &p_params);
Array foldingRange(const Dictionary &p_params);
Array codeLens(const Dictionary &p_params);
Array documentLink(const Dictionary &p_params);
diff --git a/modules/gdscript/language_server/gdscript_workspace.cpp b/modules/gdscript/language_server/gdscript_workspace.cpp
index 9f848b02f5..81933c8c87 100644
--- a/modules/gdscript/language_server/gdscript_workspace.cpp
+++ b/modules/gdscript/language_server/gdscript_workspace.cpp
@@ -46,7 +46,6 @@
void GDScriptWorkspace::_bind_methods() {
ClassDB::bind_method(D_METHOD("apply_new_signal"), &GDScriptWorkspace::apply_new_signal);
ClassDB::bind_method(D_METHOD("didDeleteFiles"), &GDScriptWorkspace::did_delete_files);
- ClassDB::bind_method(D_METHOD("symbol"), &GDScriptWorkspace::symbol);
ClassDB::bind_method(D_METHOD("parse_script", "path", "content"), &GDScriptWorkspace::parse_script);
ClassDB::bind_method(D_METHOD("parse_local_script", "path"), &GDScriptWorkspace::parse_local_script);
ClassDB::bind_method(D_METHOD("get_file_path", "uri"), &GDScriptWorkspace::get_file_path);
@@ -182,35 +181,33 @@ const lsp::DocumentSymbol *GDScriptWorkspace::get_parameter_symbol(const lsp::Do
return nullptr;
}
-const lsp::DocumentSymbol *GDScriptWorkspace::get_local_symbol(const ExtendGDScriptParser *p_parser, const String &p_symbol_identifier) {
- const lsp::DocumentSymbol *class_symbol = &p_parser->get_symbols();
+const lsp::DocumentSymbol *GDScriptWorkspace::get_local_symbol_at(const ExtendGDScriptParser *p_parser, const String &p_symbol_identifier, const lsp::Position p_position) {
+ // Go down and pick closest `DocumentSymbol` with `p_symbol_identifier`.
- for (int i = 0; i < class_symbol->children.size(); ++i) {
- int kind = class_symbol->children[i].kind;
- switch (kind) {
- case lsp::SymbolKind::Function:
- case lsp::SymbolKind::Method:
- case lsp::SymbolKind::Class: {
- const lsp::DocumentSymbol *function_symbol = &class_symbol->children[i];
+ const lsp::DocumentSymbol *current = &p_parser->get_symbols();
+ const lsp::DocumentSymbol *best_match = nullptr;
- for (int l = 0; l < function_symbol->children.size(); ++l) {
- const lsp::DocumentSymbol *local = &function_symbol->children[l];
- if (!local->detail.is_empty() && local->name == p_symbol_identifier) {
- return local;
- }
- }
- } break;
+ while (current) {
+ if (current->name == p_symbol_identifier) {
+ if (current->selectionRange.contains(p_position)) {
+ // Exact match: pos is ON symbol decl identifier.
+ return current;
+ }
- case lsp::SymbolKind::Variable: {
- const lsp::DocumentSymbol *variable_symbol = &class_symbol->children[i];
- if (variable_symbol->name == p_symbol_identifier) {
- return variable_symbol;
- }
- } break;
+ best_match = current;
+ }
+
+ const lsp::DocumentSymbol *parent = current;
+ current = nullptr;
+ for (const lsp::DocumentSymbol &child : parent->children) {
+ if (child.range.contains(p_position)) {
+ current = &child;
+ break;
+ }
}
}
- return nullptr;
+ return best_match;
}
void GDScriptWorkspace::reload_all_workspace_scripts() {
@@ -275,25 +272,6 @@ ExtendGDScriptParser *GDScriptWorkspace::get_parse_result(const String &p_path)
return nullptr;
}
-Array GDScriptWorkspace::symbol(const Dictionary &p_params) {
- String query = p_params["query"];
- Array arr;
- if (!query.is_empty()) {
- for (const KeyValue<String, ExtendGDScriptParser *> &E : scripts) {
- Vector<lsp::DocumentedSymbolInformation> script_symbols;
- E.value->get_symbols().symbol_tree_as_list(E.key, script_symbols);
- for (int i = 0; i < script_symbols.size(); ++i) {
- if (query.is_subsequence_ofn(script_symbols[i].name)) {
- lsp::DocumentedSymbolInformation symbol = script_symbols[i];
- symbol.location.uri = get_file_uri(symbol.location.uri);
- arr.push_back(symbol.to_json());
- }
- }
- }
- }
- return arr;
-}
-
Error GDScriptWorkspace::initialize() {
if (initialized) {
return OK;
@@ -423,7 +401,7 @@ Error GDScriptWorkspace::initialize() {
native_members.insert(E.key, members);
}
- // cache member completions
+ // Cache member completions.
for (const KeyValue<String, ExtendGDScriptParser *> &S : scripts) {
S.value->get_member_completions();
}
@@ -458,48 +436,110 @@ Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_cont
return err;
}
-Dictionary GDScriptWorkspace::rename(const lsp::TextDocumentPositionParams &p_doc_pos, const String &new_name) {
- Error err;
- String path = get_file_path(p_doc_pos.textDocument.uri);
+static bool is_valid_rename_target(const lsp::DocumentSymbol *p_symbol) {
+ // Must be valid symbol.
+ if (!p_symbol) {
+ return false;
+ }
+
+ // Cannot rename builtin.
+ if (!p_symbol->native_class.is_empty()) {
+ return false;
+ }
+
+ // Source must be available.
+ if (p_symbol->script_path.is_empty()) {
+ return false;
+ }
+ return true;
+}
+
+Dictionary GDScriptWorkspace::rename(const lsp::TextDocumentPositionParams &p_doc_pos, const String &new_name) {
lsp::WorkspaceEdit edit;
- List<String> paths;
- list_script_files("res://", paths);
+ const lsp::DocumentSymbol *reference_symbol = resolve_symbol(p_doc_pos);
+ if (is_valid_rename_target(reference_symbol)) {
+ Vector<lsp::Location> usages = find_all_usages(*reference_symbol);
+ for (int i = 0; i < usages.size(); ++i) {
+ lsp::Location loc = usages[i];
+
+ edit.add_change(loc.uri, loc.range.start.line, loc.range.start.character, loc.range.end.character, new_name);
+ }
+ }
+
+ return edit.to_json();
+}
+bool GDScriptWorkspace::can_rename(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::DocumentSymbol &r_symbol, lsp::Range &r_range) {
const lsp::DocumentSymbol *reference_symbol = resolve_symbol(p_doc_pos);
- if (reference_symbol) {
- String identifier = reference_symbol->name;
+ if (!is_valid_rename_target(reference_symbol)) {
+ return false;
+ }
- for (List<String>::Element *PE = paths.front(); PE; PE = PE->next()) {
- PackedStringArray content = FileAccess::get_file_as_string(PE->get(), &err).split("\n");
- for (int i = 0; i < content.size(); ++i) {
- String line = content[i];
+ String path = get_file_path(p_doc_pos.textDocument.uri);
+ if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
+ parser->get_identifier_under_position(p_doc_pos.position, r_range);
+ r_symbol = *reference_symbol;
+ return true;
+ }
- int character = line.find(identifier);
- while (character > -1) {
- lsp::TextDocumentPositionParams params;
+ return false;
+}
- lsp::TextDocumentIdentifier text_doc;
- text_doc.uri = get_file_uri(PE->get());
+Vector<lsp::Location> GDScriptWorkspace::find_usages_in_file(const lsp::DocumentSymbol &p_symbol, const String &p_file_path) {
+ Vector<lsp::Location> usages;
- params.textDocument = text_doc;
- params.position.line = i;
- params.position.character = character;
+ String identifier = p_symbol.name;
+ if (const ExtendGDScriptParser *parser = get_parse_result(p_file_path)) {
+ const PackedStringArray &content = parser->get_lines();
+ for (int i = 0; i < content.size(); ++i) {
+ String line = content[i];
- const lsp::DocumentSymbol *other_symbol = resolve_symbol(params);
+ int character = line.find(identifier);
+ while (character > -1) {
+ lsp::TextDocumentPositionParams params;
- if (other_symbol == reference_symbol) {
- edit.add_change(text_doc.uri, i, character, character + identifier.length(), new_name);
- }
+ lsp::TextDocumentIdentifier text_doc;
+ text_doc.uri = get_file_uri(p_file_path);
- character = line.find(identifier, character + 1);
+ params.textDocument = text_doc;
+ params.position.line = i;
+ params.position.character = character;
+
+ const lsp::DocumentSymbol *other_symbol = resolve_symbol(params);
+
+ if (other_symbol == &p_symbol) {
+ lsp::Location loc;
+ loc.uri = text_doc.uri;
+ loc.range.start = params.position;
+ loc.range.end.line = params.position.line;
+ loc.range.end.character = params.position.character + identifier.length();
+ usages.append(loc);
}
+
+ character = line.find(identifier, character + 1);
}
}
}
- return edit.to_json();
+ return usages;
+}
+
+Vector<lsp::Location> GDScriptWorkspace::find_all_usages(const lsp::DocumentSymbol &p_symbol) {
+ if (p_symbol.local) {
+ // Only search in current document.
+ return find_usages_in_file(p_symbol, p_symbol.script_path);
+ }
+ // Search in all documents.
+ List<String> paths;
+ list_script_files("res://", paths);
+
+ Vector<lsp::Location> usages;
+ for (List<String>::Element *PE = paths.front(); PE; PE = PE->next()) {
+ usages.append_array(find_usages_in_file(p_symbol, PE->get()));
+ }
+ return usages;
}
Error GDScriptWorkspace::parse_local_script(const String &p_path) {
@@ -636,9 +676,9 @@ const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocu
lsp::Position pos = p_doc_pos.position;
if (symbol_identifier.is_empty()) {
- Vector2i offset;
- symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
- pos.character += offset.y;
+ lsp::Range range;
+ symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, range);
+ pos.character = range.end.character;
}
if (!symbol_identifier.is_empty()) {
@@ -661,7 +701,7 @@ const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocu
}
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));
+ symbol = target_parser->get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(ret.location), symbol_identifier);
if (symbol) {
switch (symbol->kind) {
@@ -670,10 +710,6 @@ const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocu
symbol = get_parameter_symbol(symbol, symbol_identifier);
}
} break;
-
- case lsp::SymbolKind::Variable: {
- symbol = get_local_symbol(parser, symbol_identifier);
- } break;
}
}
}
@@ -686,10 +722,9 @@ const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocu
symbol = get_native_symbol(ret.class_name, member);
}
} else {
- symbol = parser->get_member_symbol(symbol_identifier);
-
+ symbol = get_local_symbol_at(parser, symbol_identifier, p_doc_pos.position);
if (!symbol) {
- symbol = get_local_symbol(parser, symbol_identifier);
+ symbol = parser->get_member_symbol(symbol_identifier);
}
}
}
@@ -703,8 +738,8 @@ void GDScriptWorkspace::resolve_related_symbols(const lsp::TextDocumentPositionP
String path = get_file_path(p_doc_pos.textDocument.uri);
if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
String symbol_identifier;
- Vector2i offset;
- symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
+ lsp::Range range;
+ symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, range);
for (const KeyValue<StringName, ClassMembers> &E : native_members) {
const ClassMembers &members = native_members.get(E.key);
diff --git a/modules/gdscript/language_server/gdscript_workspace.h b/modules/gdscript/language_server/gdscript_workspace.h
index 80653778fb..0b2d43b817 100644
--- a/modules/gdscript/language_server/gdscript_workspace.h
+++ b/modules/gdscript/language_server/gdscript_workspace.h
@@ -54,7 +54,7 @@ protected:
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;
const lsp::DocumentSymbol *get_parameter_symbol(const lsp::DocumentSymbol *p_parent, const String &symbol_identifier);
- const lsp::DocumentSymbol *get_local_symbol(const ExtendGDScriptParser *p_parser, const String &p_symbol_identifier);
+ const lsp::DocumentSymbol *get_local_symbol_at(const ExtendGDScriptParser *p_parser, const String &p_symbol_identifier, const lsp::Position p_position);
void reload_all_workspace_scripts();
@@ -74,9 +74,6 @@ public:
HashMap<StringName, ClassMembers> native_members;
public:
- Array symbol(const Dictionary &p_params);
-
-public:
Error initialize();
Error parse_script(const String &p_path, const String &p_content);
@@ -96,6 +93,9 @@ public:
Error resolve_signature(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::SignatureHelp &r_signature);
void did_delete_files(const Dictionary &p_params);
Dictionary rename(const lsp::TextDocumentPositionParams &p_doc_pos, const String &new_name);
+ bool can_rename(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::DocumentSymbol &r_symbol, lsp::Range &r_range);
+ Vector<lsp::Location> find_usages_in_file(const lsp::DocumentSymbol &p_symbol, const String &p_file_path);
+ Vector<lsp::Location> find_all_usages(const lsp::DocumentSymbol &p_symbol);
GDScriptWorkspace();
~GDScriptWorkspace();
diff --git a/modules/gdscript/language_server/godot_lsp.h b/modules/gdscript/language_server/godot_lsp.h
index 3782945e07..1ac4267c7b 100644
--- a/modules/gdscript/language_server/godot_lsp.h
+++ b/modules/gdscript/language_server/godot_lsp.h
@@ -83,6 +83,14 @@ struct Position {
*/
int character = 0;
+ _FORCE_INLINE_ bool operator==(const Position &p_other) const {
+ return line == p_other.line && character == p_other.character;
+ }
+
+ String to_string() const {
+ return vformat("(%d,%d)", line, character);
+ }
+
_FORCE_INLINE_ void load(const Dictionary &p_params) {
line = p_params["line"];
character = p_params["character"];
@@ -112,6 +120,27 @@ struct Range {
*/
Position end;
+ _FORCE_INLINE_ bool operator==(const Range &p_other) const {
+ return start == p_other.start && end == p_other.end;
+ }
+
+ bool contains(const Position &p_pos) const {
+ // Inside line range.
+ if (start.line <= p_pos.line && p_pos.line <= end.line) {
+ // If on start line: must come after start char.
+ bool start_ok = p_pos.line == start.line ? start.character <= p_pos.character : true;
+ // If on end line: must come before end char.
+ bool end_ok = p_pos.line == end.line ? p_pos.character <= end.character : true;
+ return start_ok && end_ok;
+ } else {
+ return false;
+ }
+ }
+
+ String to_string() const {
+ return vformat("[%s:%s]", start.to_string(), end.to_string());
+ }
+
_FORCE_INLINE_ void load(const Dictionary &p_params) {
start.load(p_params["start"]);
end.load(p_params["end"]);
@@ -203,6 +232,17 @@ struct TextDocumentPositionParams {
}
};
+struct ReferenceContext {
+ /**
+ * Include the declaration of the current symbol.
+ */
+ bool includeDeclaration;
+};
+
+struct ReferenceParams : TextDocumentPositionParams {
+ ReferenceContext context;
+};
+
struct DocumentLinkParams {
/**
* The document to provide document links for.
@@ -343,8 +383,8 @@ struct Command {
}
};
-// Use namespace instead of enumeration to follow the LSP specifications
-// lsp::EnumName::EnumValue is OK but lsp::EnumValue is not
+// Use namespace instead of enumeration to follow the LSP specifications.
+// `lsp::EnumName::EnumValue` is OK but `lsp::EnumValue` is not.
namespace TextDocumentSyncKind {
/**
@@ -436,7 +476,7 @@ struct RenameOptions {
/**
* Renames should be checked and tested before being executed.
*/
- bool prepareProvider = false;
+ bool prepareProvider = true;
Dictionary to_json() {
Dictionary dict;
@@ -794,12 +834,12 @@ static const String Markdown = "markdown";
*/
struct MarkupContent {
/**
- * The type of the Markup
+ * The type of the Markup.
*/
String kind;
/**
- * The content itself
+ * The content itself.
*/
String value;
@@ -821,8 +861,8 @@ struct MarkupContent {
};
// Use namespace instead of enumeration to follow the LSP specifications
-// lsp::EnumName::EnumValue is OK but lsp::EnumValue is not
-// And here C++ compilers are unhappy with our enumeration name like Color, File, RefCounted etc.
+// `lsp::EnumName::EnumValue` is OK but `lsp::EnumValue` is not.
+// And here C++ compilers are unhappy with our enumeration name like `Color`, `File`, `RefCounted` etc.
/**
* The kind of a completion entry.
*/
@@ -854,7 +894,7 @@ static const int Operator = 24;
static const int TypeParameter = 25;
}; // namespace CompletionItemKind
-// Use namespace instead of enumeration to follow the LSP specifications
+// Use namespace instead of enumeration to follow the LSP specifications.
/**
* Defines whether the insert text in a completion item should be interpreted as
* plain text or a snippet.
@@ -1070,8 +1110,8 @@ struct CompletionList {
};
// Use namespace instead of enumeration to follow the LSP specifications
-// lsp::EnumName::EnumValue is OK but lsp::EnumValue is not
-// And here C++ compilers are unhappy with our enumeration name like String, Array, Object etc
+// `lsp::EnumName::EnumValue` is OK but `lsp::EnumValue` is not
+// And here C++ compilers are unhappy with our enumeration name like `String`, `Array`, `Object` etc
/**
* A symbol kind.
*/
@@ -1105,70 +1145,6 @@ static const int TypeParameter = 26;
}; // namespace SymbolKind
/**
- * Represents information about programming constructs like variables, classes,
- * interfaces etc.
- */
-struct SymbolInformation {
- /**
- * The name of this symbol.
- */
- String name;
-
- /**
- * The kind of this symbol.
- */
- int kind = SymbolKind::File;
-
- /**
- * Indicates if this symbol is deprecated.
- */
- bool deprecated = false;
-
- /**
- * The location of this symbol. The location's range is used by a tool
- * to reveal the location in the editor. If the symbol is selected in the
- * tool the range's start information is used to position the cursor. So
- * the range usually spans more then the actual symbol's name and does
- * normally include things like visibility modifiers.
- *
- * The range doesn't have to denote a node range in the sense of a abstract
- * syntax tree. It can therefore not be used to re-construct a hierarchy of
- * the symbols.
- */
- Location location;
-
- /**
- * The name of the symbol containing this symbol. This information is for
- * user interface purposes (e.g. to render a qualifier in the user interface
- * if necessary). It can't be used to re-infer a hierarchy for the document
- * symbols.
- */
- String containerName;
-
- _FORCE_INLINE_ Dictionary to_json() const {
- Dictionary dict;
- dict["name"] = name;
- dict["kind"] = kind;
- dict["deprecated"] = deprecated;
- dict["location"] = location.to_json();
- dict["containerName"] = containerName;
- return dict;
- }
-};
-
-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,
* e.g. the range of an identifier.
@@ -1186,12 +1162,12 @@ struct DocumentSymbol {
String detail;
/**
- * Documentation for this symbol
+ * Documentation for this symbol.
*/
String documentation;
/**
- * Class name for the native symbols
+ * Class name for the native symbols.
*/
String native_class;
@@ -1206,6 +1182,13 @@ struct DocumentSymbol {
bool deprecated = false;
/**
+ * If `true`: Symbol is local to script and cannot be accessed somewhere else.
+ *
+ * For example: local variable inside a `func`.
+ */
+ bool local = false;
+
+ /**
* The range enclosing this symbol not including leading/trailing whitespace but everything else
* like comments. This information is typically used to determine if the clients cursor is
* inside the symbol to reveal in the symbol in the UI.
@@ -1238,35 +1221,21 @@ struct DocumentSymbol {
dict["documentation"] = documentation;
dict["native_class"] = native_class;
}
- Array arr;
- arr.resize(children.size());
- for (int i = 0; i < children.size(); i++) {
- arr[i] = children[i].to_json(with_doc);
+ if (!children.is_empty()) {
+ Array arr;
+ for (int i = 0; i < children.size(); i++) {
+ if (children[i].local) {
+ continue;
+ }
+ arr.push_back(children[i].to_json(with_doc));
+ }
+ if (!children.is_empty()) {
+ dict["children"] = arr;
+ }
}
- dict["children"] = arr;
return dict;
}
- 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.is_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, si.name, p_join_name);
- }
- }
-
_FORCE_INLINE_ MarkupContent render() const {
MarkupContent markdown;
if (detail.length()) {
@@ -1750,7 +1719,7 @@ struct ServerCapabilities {
/**
* The server provides find references support.
*/
- bool referencesProvider = false;
+ bool referencesProvider = true;
/**
* The server provides document highlight support.