mirror of
https://github.com/godotengine/godot.git
synced 2026-03-10 21:06:13 +00:00
Language Server: Improve hovered symbol resolution, fix renaming bugs, implement reference lookup
Co-Authored-By: Ryan Brue <56272643+ryanabx@users.noreply.github.com> Co-Authored-By: BooksBaum <15612932+booksbaum@users.noreply.github.com>
This commit is contained in:
@@ -1459,8 +1459,13 @@ static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context,
|
||||
if (p_expression->is_constant) {
|
||||
// Already has a value, so just use that.
|
||||
r_type = _type_from_variant(p_expression->reduced_value);
|
||||
if (p_expression->get_datatype().kind == GDScriptParser::DataType::ENUM) {
|
||||
r_type.type = p_expression->get_datatype();
|
||||
switch (p_expression->get_datatype().kind) {
|
||||
case GDScriptParser::DataType::ENUM:
|
||||
case GDScriptParser::DataType::CLASS:
|
||||
r_type.type = p_expression->get_datatype();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
found = true;
|
||||
} else {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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;
|
||||
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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,50 +436,112 @@ 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 (reference_symbol) {
|
||||
String identifier = reference_symbol->name;
|
||||
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];
|
||||
|
||||
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];
|
||||
|
||||
int character = line.find(identifier);
|
||||
while (character > -1) {
|
||||
lsp::TextDocumentPositionParams params;
|
||||
|
||||
lsp::TextDocumentIdentifier text_doc;
|
||||
text_doc.uri = get_file_uri(PE->get());
|
||||
|
||||
params.textDocument = text_doc;
|
||||
params.position.line = i;
|
||||
params.position.character = character;
|
||||
|
||||
const lsp::DocumentSymbol *other_symbol = resolve_symbol(params);
|
||||
|
||||
if (other_symbol == reference_symbol) {
|
||||
edit.add_change(text_doc.uri, i, character, character + identifier.length(), new_name);
|
||||
}
|
||||
|
||||
character = line.find(identifier, character + 1);
|
||||
}
|
||||
}
|
||||
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 (!is_valid_rename_target(reference_symbol)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector<lsp::Location> GDScriptWorkspace::find_usages_in_file(const lsp::DocumentSymbol &p_symbol, const String &p_file_path) {
|
||||
Vector<lsp::Location> usages;
|
||||
|
||||
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];
|
||||
|
||||
int character = line.find(identifier);
|
||||
while (character > -1) {
|
||||
lsp::TextDocumentPositionParams params;
|
||||
|
||||
lsp::TextDocumentIdentifier text_doc;
|
||||
text_doc.uri = get_file_uri(p_file_path);
|
||||
|
||||
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 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) {
|
||||
Error err;
|
||||
String content = FileAccess::get_file_as_string(p_path, &err);
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -73,9 +73,6 @@ public:
|
||||
HashMap<String, ExtendGDScriptParser *> parse_results;
|
||||
HashMap<StringName, ClassMembers> native_members;
|
||||
|
||||
public:
|
||||
Array symbol(const Dictionary &p_params);
|
||||
|
||||
public:
|
||||
Error initialize();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
@@ -1104,70 +1144,6 @@ static const int Operator = 25;
|
||||
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,
|
||||
@@ -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;
|
||||
|
||||
@@ -1205,6 +1181,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
|
||||
@@ -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.
|
||||
|
||||
132
modules/gdscript/tests/scripts/lsp/class.notest.gd
Normal file
132
modules/gdscript/tests/scripts/lsp/class.notest.gd
Normal file
@@ -0,0 +1,132 @@
|
||||
extends Node
|
||||
|
||||
class Inner1 extends Node:
|
||||
# ^^^^^^ class1 -> class1
|
||||
var member1 := 42
|
||||
# ^^^^^^^ class1:member1 -> class1:member1
|
||||
var member2 : int = 13
|
||||
# ^^^^^^^ class1:member2 -> class1:member2
|
||||
var member3 = 1337
|
||||
# ^^^^^^^ class1:member3 -> class1:member3
|
||||
|
||||
signal changed(old, new)
|
||||
# ^^^^^^^ class1:signal -> class1:signal
|
||||
func my_func(arg1: int, arg2: String, arg3):
|
||||
# | | | | | | ^^^^ class1:func:arg3 -> class1:func:arg3
|
||||
# | | | | ^^^^ class1:func:arg2 -> class1:func:arg2
|
||||
# | | ^^^^ class1:func:arg1 -> class1:func:arg1
|
||||
# ^^^^^^^ class1:func -> class1:func
|
||||
print(arg1, arg2, arg3)
|
||||
# | | | | ^^^^ -> class1:func:arg3
|
||||
# | | ^^^^ -> class1:func:arg2
|
||||
# ^^^^ -> class1:func:arg1
|
||||
changed.emit(arg1, arg3)
|
||||
# | | | ^^^^ -> class1:func:arg3
|
||||
# | ^^^^ -> class1:func:arg1
|
||||
#<^^^^^ -> class1:signal
|
||||
return arg1 + arg2.length() + arg3
|
||||
# | | | | ^^^^ -> class1:func:arg3
|
||||
# | | ^^^^ -> class1:func:arg2
|
||||
# ^^^^ -> class1:func:arg1
|
||||
|
||||
class Inner2:
|
||||
# ^^^^^^ class2 -> class2
|
||||
var member1 := 42
|
||||
# ^^^^^^^ class2:member1 -> class2:member1
|
||||
var member2 : int = 13
|
||||
# ^^^^^^^ class2:member2 -> class2:member2
|
||||
var member3 = 1337
|
||||
# ^^^^^^^ class2:member3 -> class2:member3
|
||||
|
||||
signal changed(old, new)
|
||||
# ^^^^^^^ class2:signal -> class2:signal
|
||||
func my_func(arg1: int, arg2: String, arg3):
|
||||
# | | | | | | ^^^^ class2:func:arg3 -> class2:func:arg3
|
||||
# | | | | ^^^^ class2:func:arg2 -> class2:func:arg2
|
||||
# | | ^^^^ class2:func:arg1 -> class2:func:arg1
|
||||
# ^^^^^^^ class2:func -> class2:func
|
||||
print(arg1, arg2, arg3)
|
||||
# | | | | ^^^^ -> class2:func:arg3
|
||||
# | | ^^^^ -> class2:func:arg2
|
||||
# ^^^^ -> class2:func:arg1
|
||||
changed.emit(arg1, arg3)
|
||||
# | | | ^^^^ -> class2:func:arg3
|
||||
# | ^^^^ -> class2:func:arg1
|
||||
#<^^^^^ -> class2:signal
|
||||
return arg1 + arg2.length() + arg3
|
||||
# | | | | ^^^^ -> class2:func:arg3
|
||||
# | | ^^^^ -> class2:func:arg2
|
||||
# ^^^^ -> class2:func:arg1
|
||||
|
||||
class Inner3 extends Inner2:
|
||||
# | | ^^^^^^ -> class2
|
||||
# ^^^^^^ class3 -> class3
|
||||
var whatever = "foo"
|
||||
# ^^^^^^^^ class3:whatever -> class3:whatever
|
||||
|
||||
func _init():
|
||||
# ^^^^^ class3:init
|
||||
# Note: no self-ref check here: resolves to `Object._init`.
|
||||
# usages of `Inner3.new()` DO resolve to this `_init`
|
||||
pass
|
||||
|
||||
class NestedInInner3:
|
||||
# ^^^^^^^^^^^^^^ class3:nested1 -> class3:nested1
|
||||
var some_value := 42
|
||||
# ^^^^^^^^^^ class3:nested1:some_value -> class3:nested1:some_value
|
||||
|
||||
class AnotherNestedInInner3 extends NestedInInner3:
|
||||
#! | | ^^^^^^^^^^^^^^ -> class3:nested1
|
||||
# ^^^^^^^^^^^^^^^^^^^^^ class3:nested2 -> class3:nested2
|
||||
var another_value := 13
|
||||
# ^^^^^^^^^^^^^ class3:nested2:another_value -> class3:nested2:another_value
|
||||
|
||||
func _ready():
|
||||
var inner1 = Inner1.new()
|
||||
# | | ^^^^^^ -> class1
|
||||
# ^^^^^^ func:class1 -> func:class1
|
||||
var value1 = inner1.my_func(1,"",3)
|
||||
# | | | | ^^^^^^^ -> class1:func
|
||||
# | | ^^^^^^ -> func:class1
|
||||
# ^^^^^^ func:class1:value1 -> func:class1:value1
|
||||
var value2 = inner1.member3
|
||||
# | | | | ^^^^^^^ -> class1:member3
|
||||
# | | ^^^^^^ -> func:class1
|
||||
# ^^^^^^ func:class1:value2 -> func:class1:value2
|
||||
print(value1, value2)
|
||||
# | | ^^^^^^ -> func:class1:value2
|
||||
# ^^^^^^ -> func:class1:value1
|
||||
|
||||
var inner3 = Inner3.new()
|
||||
# | | | | ^^^ -> class3:init
|
||||
# | | ^^^^^^ -> class3
|
||||
# ^^^^^^ func:class3 -> func:class3
|
||||
print(inner3)
|
||||
# ^^^^^^ -> func:class3
|
||||
|
||||
var nested1 = Inner3.NestedInInner3.new()
|
||||
# | | | | ^^^^^^^^^^^^^^ -> class3:nested1
|
||||
# | | ^^^^^^ -> class3
|
||||
# ^^^^^^^ func:class3:nested1 -> func:class3:nested1
|
||||
var value_nested1 = nested1.some_value
|
||||
# | | | | ^^^^^^^^^^ -> class3:nested1:some_value
|
||||
# | | ^^^^^^^ -> func:class3:nested1
|
||||
# ^^^^^^^^^^^^^ func:class3:nested1:value
|
||||
print(value_nested1)
|
||||
# ^^^^^^^^^^^^^ -> func:class3:nested1:value
|
||||
|
||||
var nested2 = Inner3.AnotherNestedInInner3.new()
|
||||
# | | | | ^^^^^^^^^^^^^^^^^^^^^ -> class3:nested2
|
||||
# | | ^^^^^^ -> class3
|
||||
# ^^^^^^^ func:class3:nested2 -> func:class3:nested2
|
||||
var value_nested2 = nested2.some_value
|
||||
# | | | | ^^^^^^^^^^ -> class3:nested1:some_value
|
||||
# | | ^^^^^^^ -> func:class3:nested2
|
||||
# ^^^^^^^^^^^^^ func:class3:nested2:value
|
||||
var another_value_nested2 = nested2.another_value
|
||||
# | | | | ^^^^^^^^^^^^^ -> class3:nested2:another_value
|
||||
# | | ^^^^^^^ -> func:class3:nested2
|
||||
# ^^^^^^^^^^^^^^^^^^^^^ func:class3:nested2:another_value_nested
|
||||
print(value_nested2, another_value_nested2)
|
||||
# | | ^^^^^^^^^^^^^^^^^^^^^ -> func:class3:nested2:another_value_nested
|
||||
# ^^^^^^^^^^^^^ -> func:class3:nested2:value
|
||||
26
modules/gdscript/tests/scripts/lsp/enums.notest.gd
Normal file
26
modules/gdscript/tests/scripts/lsp/enums.notest.gd
Normal file
@@ -0,0 +1,26 @@
|
||||
extends Node
|
||||
|
||||
enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
|
||||
# | | | | ^^^^^^^^^ enum:unnamed:ally -> enum:unnamed:ally
|
||||
# | | ^^^^^^^^^^ enum:unnamed:enemy -> enum:unnamed:enemy
|
||||
# ^^^^^^^^^^^^ enum:unnamed:neutral -> enum:unnamed:neutral
|
||||
enum Named {THING_1, THING_2, ANOTHER_THING = -1}
|
||||
# | | | | | | ^^^^^^^^^^^^^ enum:named:thing3 -> enum:named:thing3
|
||||
# | | | | ^^^^^^^ enum:named:thing2 -> enum:named:thing2
|
||||
# | | ^^^^^^^ enum:named:thing1 -> enum:named:thing1
|
||||
# ^^^^^ enum:named -> enum:named
|
||||
|
||||
func f(arg):
|
||||
match arg:
|
||||
UNIT_ENEMY: print(UNIT_ENEMY)
|
||||
# | ^^^^^^^^^^ -> enum:unnamed:enemy
|
||||
#<^^^^^^^^ -> enum:unnamed:enemy
|
||||
Named.THING_2: print(Named.THING_2)
|
||||
#! | | | | | ^^^^^^^ -> enum:named:thing2
|
||||
# | | | ^^^^^ -> enum:named
|
||||
#! | ^^^^^^^ -> enum:named:thing2
|
||||
#<^^^ -> enum:named
|
||||
_: print(UNIT_ENEMY, Named.ANOTHER_THING)
|
||||
#! | | | | ^^^^^^^^^^^^^ -> enum:named:thing3
|
||||
# | | ^^^^^ -> enum:named
|
||||
# ^^^^^^^^^^ -> enum:unnamed:enemy
|
||||
28
modules/gdscript/tests/scripts/lsp/indentation.notest.gd
Normal file
28
modules/gdscript/tests/scripts/lsp/indentation.notest.gd
Normal file
@@ -0,0 +1,28 @@
|
||||
extends Node
|
||||
|
||||
var root = 0
|
||||
# ^^^^ 0_indent -> 0_indent
|
||||
|
||||
func a():
|
||||
var alpha: int = root + 42
|
||||
# | | ^^^^ -> 0_indent
|
||||
# ^^^^^ 1_indent -> 1_indent
|
||||
if alpha > 42:
|
||||
# ^^^^^ -> 1_indent
|
||||
var beta := alpha + 13
|
||||
# | | ^^^^ -> 1_indent
|
||||
# ^^^^ 2_indent -> 2_indent
|
||||
if beta > alpha:
|
||||
# | | ^^^^^ -> 1_indent
|
||||
# ^^^^ -> 2_indent
|
||||
var gamma = beta + 1
|
||||
# | | ^^^^ -> 2_indent
|
||||
# ^^^^^ 3_indent -> 3_indent
|
||||
print(gamma)
|
||||
# ^^^^^ -> 3_indent
|
||||
print(beta)
|
||||
# ^^^^ -> 2_indent
|
||||
print(alpha)
|
||||
# ^^^^^ -> 1_indent
|
||||
print(root)
|
||||
# ^^^^ -> 0_indent
|
||||
91
modules/gdscript/tests/scripts/lsp/lambdas.notest.gd
Normal file
91
modules/gdscript/tests/scripts/lsp/lambdas.notest.gd
Normal file
@@ -0,0 +1,91 @@
|
||||
extends Node
|
||||
|
||||
var lambda_member1 := func(alpha: int, beta): return alpha + beta
|
||||
# | | | | | | | | ^^^^ -> \1:beta
|
||||
# | | | | | | ^^^^^ -> \1:alpha
|
||||
# | | | | ^^^^ \1:beta -> \1:beta
|
||||
# | | ^^^^^ \1:alpha -> \1:alpha
|
||||
# ^^^^^^^^^^^^^^ \1 -> \1
|
||||
|
||||
var lambda_member2 := func(alpha, beta: int) -> int:
|
||||
# | | | | | |
|
||||
# | | | | | |
|
||||
# | | | | ^^^^ \2:beta -> \2:beta
|
||||
# | | ^^^^^ \2:alpha -> \2:alpha
|
||||
# ^^^^^^^^^^^^^^ \2 -> \2
|
||||
return alpha + beta
|
||||
# | | ^^^^ -> \2:beta
|
||||
# ^^^^^ -> \2:alpha
|
||||
|
||||
var lambda_member3 := func add_values(alpha, beta): return alpha + beta
|
||||
# | | | | | | | | ^^^^ -> \3:beta
|
||||
# | | | | | | ^^^^^ -> \3:alpha
|
||||
# | | | | ^^^^ \3:beta -> \3:beta
|
||||
# | | ^^^^^ \3:alpha -> \3:alpha
|
||||
# ^^^^^^^^^^^^^^ \3 -> \3
|
||||
|
||||
var lambda_multiline = func(alpha: int, beta: int) -> int:
|
||||
# | | | | | |
|
||||
# | | | | | |
|
||||
# | | | | ^^^^ \multi:beta -> \multi:beta
|
||||
# | | ^^^^^ \multi:alpha -> \multi:alpha
|
||||
# ^^^^^^^^^^^^^^^^ \multi -> \multi
|
||||
print(alpha + beta)
|
||||
# | | ^^^^ -> \multi:beta
|
||||
# ^^^^^ -> \multi:alpha
|
||||
var tmp = alpha + beta + 42
|
||||
# | | | | ^^^^ -> \multi:beta
|
||||
# | | ^^^^^ -> \multi:alpha
|
||||
# ^^^ \multi:tmp -> \multi:tmp
|
||||
print(tmp)
|
||||
# ^^^ -> \multi:tmp
|
||||
if tmp > 50:
|
||||
# ^^^ -> \multi:tmp
|
||||
tmp += alpha
|
||||
# | ^^^^^ -> \multi:alpha
|
||||
#<^ -> \multi:tmp
|
||||
else:
|
||||
tmp -= beta
|
||||
# | ^^^^ -> \multi:beta
|
||||
#<^ -> \multi:tmp
|
||||
print(tmp)
|
||||
# ^^^ -> \multi:tmp
|
||||
return beta + tmp + alpha
|
||||
# | | | | ^^^^^ -> \multi:alpha
|
||||
# | | ^^^ -> \multi:tmp
|
||||
# ^^^^ -> \multi:beta
|
||||
|
||||
|
||||
var some_name := "foo bar"
|
||||
# ^^^^^^^^^ member:some_name -> member:some_name
|
||||
|
||||
func _ready() -> void:
|
||||
var a = lambda_member1.call(1,2)
|
||||
# ^^^^^^^^^^^^^^ -> \1
|
||||
var b = lambda_member2.call(1,2)
|
||||
# ^^^^^^^^^^^^^^ -> \2
|
||||
var c = lambda_member3.call(1,2)
|
||||
# ^^^^^^^^^^^^^^ -> \3
|
||||
var d = lambda_multiline.call(1,2)
|
||||
# ^^^^^^^^^^^^^^^^ -> \multi
|
||||
print(a,b,c,d)
|
||||
|
||||
var lambda_local = func(alpha, beta): return alpha + beta
|
||||
# | | | | | | | | ^^^^ -> \local:beta
|
||||
# | | | | | | ^^^^^ -> \local:alpha
|
||||
# | | | | ^^^^ \local:beta -> \local:beta
|
||||
# | | ^^^^^ \local:alpha -> \local:alpha
|
||||
# ^^^^^^^^^^^^ \local -> \local
|
||||
|
||||
var value := 42
|
||||
# ^^^^^ local:value -> local:value
|
||||
var lambda_capture = func(): return value + some_name.length()
|
||||
# | | | | ^^^^^^^^^ -> member:some_name
|
||||
# | | ^^^^^ -> local:value
|
||||
# ^^^^^^^^^^^^^^ \capture -> \capture
|
||||
|
||||
var z = lambda_local.call(1,2)
|
||||
# ^^^^^^^^^^^^ -> \local
|
||||
var x = lambda_capture.call()
|
||||
# ^^^^^^^^^^^^^^ -> \capture
|
||||
print(z,x)
|
||||
25
modules/gdscript/tests/scripts/lsp/local_variables.notest.gd
Normal file
25
modules/gdscript/tests/scripts/lsp/local_variables.notest.gd
Normal file
@@ -0,0 +1,25 @@
|
||||
extends Node
|
||||
|
||||
var member := 2
|
||||
# ^^^^^^ member -> member
|
||||
|
||||
func test_member() -> void:
|
||||
var test := member + 42
|
||||
# | | ^^^^^^ -> member
|
||||
# ^^^^ test -> test
|
||||
test += 3
|
||||
#<^^ -> test
|
||||
member += 5
|
||||
#<^^^^ -> member
|
||||
test = return_arg(test)
|
||||
# | ^^^^ -> test
|
||||
#<^^ -> test
|
||||
print(test)
|
||||
# ^^^^ -> test
|
||||
|
||||
func return_arg(arg: int) -> int:
|
||||
# ^^^ arg -> arg
|
||||
arg += 2
|
||||
#<^ -> arg
|
||||
return arg
|
||||
# ^^^ -> arg
|
||||
65
modules/gdscript/tests/scripts/lsp/properties.notest.gd
Normal file
65
modules/gdscript/tests/scripts/lsp/properties.notest.gd
Normal file
@@ -0,0 +1,65 @@
|
||||
extends Node
|
||||
|
||||
var prop1 := 42
|
||||
# ^^^^^ prop1 -> prop1
|
||||
var prop2 : int = 42
|
||||
# ^^^^^ prop2 -> prop2
|
||||
var prop3 := 42:
|
||||
# ^^^^^ prop3 -> prop3
|
||||
get:
|
||||
return prop3 + 13
|
||||
# ^^^^^ -> prop3
|
||||
set(value):
|
||||
# ^^^^^ prop3:value -> prop3:value
|
||||
prop3 = value - 13
|
||||
# | ^^^^^ -> prop3:value
|
||||
#<^^^ -> prop3
|
||||
var prop4: int:
|
||||
# ^^^^^ prop4 -> prop4
|
||||
get:
|
||||
return 42
|
||||
var prop5 := 42:
|
||||
# ^^^^^ prop5 -> prop5
|
||||
set(value):
|
||||
# ^^^^^ prop5:value -> prop5:value
|
||||
prop5 = value - 13
|
||||
# | ^^^^^ -> prop5:value
|
||||
#<^^^ -> prop5
|
||||
|
||||
var prop6:
|
||||
# ^^^^^ prop6 -> prop6
|
||||
get = get_prop6,
|
||||
# ^^^^^^^^^ -> get_prop6
|
||||
set = set_prop6
|
||||
# ^^^^^^^^^ -> set_prop6
|
||||
func get_prop6():
|
||||
# ^^^^^^^^^ get_prop6 -> get_prop6
|
||||
return 42
|
||||
func set_prop6(value):
|
||||
# | | ^^^^^ set_prop6:value -> set_prop6:value
|
||||
# ^^^^^^^^^ set_prop6 -> set_prop6
|
||||
print(value)
|
||||
# ^^^^^ -> set_prop6:value
|
||||
|
||||
var prop7:
|
||||
# ^^^^^ prop7 -> prop7
|
||||
get = get_prop7
|
||||
# ^^^^^^^^^ -> get_prop7
|
||||
func get_prop7():
|
||||
# ^^^^^^^^^ get_prop7 -> get_prop7
|
||||
return 42
|
||||
|
||||
var prop8:
|
||||
# ^^^^^ prop8 -> prop8
|
||||
set = set_prop8
|
||||
# ^^^^^^^^^ -> set_prop8
|
||||
func set_prop8(value):
|
||||
# | | ^^^^^ set_prop8:value -> set_prop8:value
|
||||
# ^^^^^^^^^ set_prop8 -> set_prop8
|
||||
print(value)
|
||||
# ^^^^^ -> set_prop8:value
|
||||
|
||||
const const_var := 42
|
||||
# ^^^^^^^^^ const_var -> const_var
|
||||
static var static_var := 42
|
||||
# ^^^^^^^^^^ static_var -> static_var
|
||||
106
modules/gdscript/tests/scripts/lsp/scopes.notest.gd
Normal file
106
modules/gdscript/tests/scripts/lsp/scopes.notest.gd
Normal file
@@ -0,0 +1,106 @@
|
||||
extends Node
|
||||
|
||||
var member := 2
|
||||
# ^^^^^^ public -> public
|
||||
|
||||
signal some_changed(new_value)
|
||||
# | | ^^^^^^^^^ signal:parameter -> signal:parameter
|
||||
# ^^^^^^^^^^^^ signal -> signal
|
||||
var some_value := 42:
|
||||
# ^^^^^^^^^^ property -> property
|
||||
get:
|
||||
return some_value
|
||||
# ^^^^^^^^^^ -> property
|
||||
set(value):
|
||||
# ^^^^^ property:set:value -> property:set:value
|
||||
some_changed.emit(value)
|
||||
# | ^^^^^ -> property:set:value
|
||||
#<^^^^^^^^^^ -> signal
|
||||
some_value = value
|
||||
# | ^^^^^ -> property:set:value
|
||||
#<^^^^^^^^ -> property
|
||||
|
||||
func v():
|
||||
var value := member + 2
|
||||
# | | ^^^^^^ -> public
|
||||
# ^^^^^ v:value -> v:value
|
||||
print(value)
|
||||
# ^^^^^ -> v:value
|
||||
if value > 0:
|
||||
# ^^^^^ -> v:value
|
||||
var beta := value + 2
|
||||
# | | ^^^^^ -> v:value
|
||||
# ^^^^ v:if:beta -> v:if:beta
|
||||
print(beta)
|
||||
# ^^^^ -> v:if:beta
|
||||
|
||||
for counter in beta:
|
||||
# | | ^^^^ -> v:if:beta
|
||||
# ^^^^^^^ v:if:counter -> v:if:counter
|
||||
print (counter)
|
||||
# ^^^^^^^ -> v:if:counter
|
||||
|
||||
else:
|
||||
for counter in value:
|
||||
# | | ^^^^^ -> v:value
|
||||
# ^^^^^^^ v:else:counter -> v:else:counter
|
||||
print(counter)
|
||||
# ^^^^^^^ -> v:else:counter
|
||||
|
||||
func f():
|
||||
var func1 = func(value): print(value + 13)
|
||||
# | | | | ^^^^^ -> f:func1:value
|
||||
# | | ^^^^^ f:func1:value -> f:func1:value
|
||||
# ^^^^^ f:func1 -> f:func1
|
||||
var func2 = func(value): print(value + 42)
|
||||
# | | | | ^^^^^ -> f:func2:value
|
||||
# | | ^^^^^ f:func2:value -> f:func2:value
|
||||
# ^^^^^ f:func2 -> f:func2
|
||||
|
||||
func1.call(1)
|
||||
#<^^^ -> f:func1
|
||||
func2.call(2)
|
||||
#<^^^ -> f:func2
|
||||
|
||||
func m():
|
||||
var value = 42
|
||||
# ^^^^^ m:value -> m:value
|
||||
|
||||
match value:
|
||||
# ^^^^^ -> m:value
|
||||
13:
|
||||
print(value)
|
||||
# ^^^^^ -> m:value
|
||||
[var start, _, var end]:
|
||||
# | | ^^^ m:match:array:end -> m:match:array:end
|
||||
# ^^^^^ m:match:array:start -> m:match:array:start
|
||||
print(start + end)
|
||||
# | | ^^^ -> m:match:array:end
|
||||
# ^^^^^ -> m:match:array:start
|
||||
{ "name": var name }:
|
||||
# ^^^^ m:match:dict:var -> m:match:dict:var
|
||||
print(name)
|
||||
# ^^^^ -> m:match:dict:var
|
||||
var whatever:
|
||||
# ^^^^^^^^ m:match:var -> m:match:var
|
||||
print(whatever)
|
||||
# ^^^^^^^^ -> m:match:var
|
||||
|
||||
func m2():
|
||||
var value = 42
|
||||
# ^^^^^ m2:value -> m2:value
|
||||
|
||||
match value:
|
||||
# ^^^^^ -> m2:value
|
||||
{ "name": var name }:
|
||||
# ^^^^ m2:match:dict:var -> m2:match:dict:var
|
||||
print(name)
|
||||
# ^^^^ -> m2:match:dict:var
|
||||
[var name, ..]:
|
||||
# ^^^^ m2:match:array:var -> m2:match:array:var
|
||||
print(name)
|
||||
# ^^^^ -> m2:match:array:var
|
||||
var name:
|
||||
# ^^^^ m2:match:var -> m2:match:var
|
||||
print(name)
|
||||
# ^^^^ -> m2:match:var
|
||||
@@ -0,0 +1,56 @@
|
||||
extends Node
|
||||
|
||||
var value := 42
|
||||
# ^^^^^ member:value -> member:value
|
||||
|
||||
func variable():
|
||||
var value = value + 42
|
||||
#! | | ^^^^^ -> member:value
|
||||
# ^^^^^ variable:value -> variable:value
|
||||
print(value)
|
||||
# ^^^^^ -> variable:value
|
||||
|
||||
func array():
|
||||
var value = [1,value,3,value+4]
|
||||
#! | | | | ^^^^^ -> member:value
|
||||
#! | | ^^^^^ -> member:value
|
||||
# ^^^^^ array:value -> array:value
|
||||
print(value)
|
||||
# ^^^^^ -> array:value
|
||||
|
||||
func dictionary():
|
||||
var value = {
|
||||
# ^^^^^ dictionary:value -> dictionary:value
|
||||
"key1": value,
|
||||
#! ^^^^^ -> member:value
|
||||
"key2": 1 + value + 3,
|
||||
#! ^^^^^ -> member:value
|
||||
}
|
||||
print(value)
|
||||
# ^^^^^ -> dictionary:value
|
||||
|
||||
func for_loop():
|
||||
for value in value:
|
||||
# | | ^^^^^ -> member:value
|
||||
# ^^^^^ for:value -> for:value
|
||||
print(value)
|
||||
# ^^^^^ -> for:value
|
||||
|
||||
func for_range():
|
||||
for value in range(5, value):
|
||||
# | | ^^^^^ -> member:value
|
||||
# ^^^^^ for:range:value -> for:range:value
|
||||
print(value)
|
||||
# ^^^^^ -> for:range:value
|
||||
|
||||
func matching():
|
||||
match value:
|
||||
# ^^^^^ -> member:value
|
||||
42: print(value)
|
||||
# ^^^^^ -> member:value
|
||||
[var value, ..]: print(value)
|
||||
# | | ^^^^^ -> match:array:value
|
||||
# ^^^^^ match:array:value -> match:array:value
|
||||
var value: print(value)
|
||||
# | | ^^^^^ -> match:var:value
|
||||
# ^^^^^ match:var:value -> match:var:value
|
||||
480
modules/gdscript/tests/test_lsp.h
Normal file
480
modules/gdscript/tests/test_lsp.h
Normal file
@@ -0,0 +1,480 @@
|
||||
/**************************************************************************/
|
||||
/* test_lsp.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef TEST_LSP_H
|
||||
#define TEST_LSP_H
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
|
||||
#include "tests/test_macros.h"
|
||||
|
||||
#include "../language_server/gdscript_extend_parser.h"
|
||||
#include "../language_server/gdscript_language_protocol.h"
|
||||
#include "../language_server/gdscript_workspace.h"
|
||||
#include "../language_server/godot_lsp.h"
|
||||
|
||||
#include "core/io/dir_access.h"
|
||||
#include "core/io/file_access_pack.h"
|
||||
#include "core/os/os.h"
|
||||
#include "editor/editor_help.h"
|
||||
#include "editor/editor_node.h"
|
||||
#include "modules/gdscript/gdscript_analyzer.h"
|
||||
#include "modules/regex/regex.h"
|
||||
|
||||
#include "thirdparty/doctest/doctest.h"
|
||||
|
||||
template <>
|
||||
struct doctest::StringMaker<lsp::Position> {
|
||||
static doctest::String convert(const lsp::Position &p_val) {
|
||||
return p_val.to_string().utf8().get_data();
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct doctest::StringMaker<lsp::Range> {
|
||||
static doctest::String convert(const lsp::Range &p_val) {
|
||||
return p_val.to_string().utf8().get_data();
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct doctest::StringMaker<GodotPosition> {
|
||||
static doctest::String convert(const GodotPosition &p_val) {
|
||||
return p_val.to_string().utf8().get_data();
|
||||
}
|
||||
};
|
||||
|
||||
namespace GDScriptTests {
|
||||
|
||||
// LSP GDScript test scripts are located inside project of other GDScript tests:
|
||||
// Cannot reset `ProjectSettings` (singleton) -> Cannot load another workspace and resources in there.
|
||||
// -> Reuse GDScript test project. LSP specific scripts are then placed inside `lsp` folder.
|
||||
// Access via `res://lsp/my_script.notest.gd`.
|
||||
const String root = "modules/gdscript/tests/scripts/";
|
||||
|
||||
/*
|
||||
* After use:
|
||||
* * `memdelete` returned `GDScriptLanguageProtocol`.
|
||||
* * Call `GDScriptTests::::finish_language`.
|
||||
*/
|
||||
GDScriptLanguageProtocol *initialize(const String &p_root) {
|
||||
Error err = OK;
|
||||
Ref<DirAccess> dir(DirAccess::open(p_root, &err));
|
||||
REQUIRE_MESSAGE(err == OK, "Could not open specified root directory");
|
||||
String absolute_root = dir->get_current_dir();
|
||||
init_language(absolute_root);
|
||||
|
||||
GDScriptLanguageProtocol *proto = memnew(GDScriptLanguageProtocol);
|
||||
|
||||
Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
|
||||
workspace->root = absolute_root;
|
||||
// On windows: `C:/...` -> `C%3A/...`.
|
||||
workspace->root_uri = "file:///" + absolute_root.lstrip("/").replace_first(":", "%3A");
|
||||
|
||||
return proto;
|
||||
}
|
||||
|
||||
lsp::Position pos(const int p_line, const int p_character) {
|
||||
lsp::Position p;
|
||||
p.line = p_line;
|
||||
p.character = p_character;
|
||||
return p;
|
||||
}
|
||||
|
||||
lsp::Range range(const lsp::Position p_start, const lsp::Position p_end) {
|
||||
lsp::Range r;
|
||||
r.start = p_start;
|
||||
r.end = p_end;
|
||||
return r;
|
||||
}
|
||||
|
||||
lsp::TextDocumentPositionParams pos_in(const lsp::DocumentUri &p_uri, const lsp::Position p_pos) {
|
||||
lsp::TextDocumentPositionParams params;
|
||||
params.textDocument.uri = p_uri;
|
||||
params.position = p_pos;
|
||||
return params;
|
||||
}
|
||||
|
||||
const lsp::DocumentSymbol *test_resolve_symbol_at(const String &p_uri, const lsp::Position p_pos, const String &p_expected_uri, const String &p_expected_name, const lsp::Range &p_expected_range) {
|
||||
Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
|
||||
|
||||
lsp::TextDocumentPositionParams params = pos_in(p_uri, p_pos);
|
||||
const lsp::DocumentSymbol *symbol = workspace->resolve_symbol(params);
|
||||
CHECK(symbol);
|
||||
|
||||
if (symbol) {
|
||||
CHECK_EQ(symbol->uri, p_expected_uri);
|
||||
CHECK_EQ(symbol->name, p_expected_name);
|
||||
CHECK_EQ(symbol->selectionRange, p_expected_range);
|
||||
}
|
||||
|
||||
return symbol;
|
||||
}
|
||||
|
||||
struct InlineTestData {
|
||||
lsp::Range range;
|
||||
String text;
|
||||
String name;
|
||||
String ref;
|
||||
|
||||
static bool try_parse(const Vector<String> &p_lines, const int p_line_number, InlineTestData &r_data) {
|
||||
String line = p_lines[p_line_number];
|
||||
|
||||
RegEx regex = RegEx("^\\t*#[ |]*(?<range>(?<left><)?\\^+)(\\s+(?<name>(?!->)\\S+))?(\\s+->\\s+(?<ref>\\S+))?");
|
||||
Ref<RegExMatch> match = regex.search(line);
|
||||
if (match.is_null()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find first line without leading comment above current line.
|
||||
int target_line = p_line_number;
|
||||
while (target_line >= 0) {
|
||||
String dedented = p_lines[target_line].lstrip("\t");
|
||||
if (!dedented.begins_with("#")) {
|
||||
break;
|
||||
}
|
||||
target_line--;
|
||||
}
|
||||
if (target_line < 0) {
|
||||
return false;
|
||||
}
|
||||
r_data.range.start.line = r_data.range.end.line = target_line;
|
||||
|
||||
String marker = match->get_string("range");
|
||||
int i = line.find(marker);
|
||||
REQUIRE(i >= 0);
|
||||
r_data.range.start.character = i;
|
||||
if (!match->get_string("left").is_empty()) {
|
||||
// Include `#` (comment char) in range.
|
||||
r_data.range.start.character--;
|
||||
}
|
||||
r_data.range.end.character = i + marker.length();
|
||||
|
||||
String target = p_lines[target_line];
|
||||
r_data.text = target.substr(r_data.range.start.character, r_data.range.end.character - r_data.range.start.character);
|
||||
|
||||
r_data.name = match->get_string("name");
|
||||
r_data.ref = match->get_string("ref");
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
Vector<InlineTestData> read_tests(const String &p_path) {
|
||||
Error err;
|
||||
String source = FileAccess::get_file_as_string(p_path, &err);
|
||||
REQUIRE_MESSAGE(err == OK, vformat("Cannot read '%s'", p_path));
|
||||
|
||||
// Format:
|
||||
// ```gdscript
|
||||
// var foo = bar + baz
|
||||
// # | | | | ^^^ name -> ref
|
||||
// # | | ^^^ -> ref
|
||||
// # ^^^ name
|
||||
//
|
||||
// func my_func():
|
||||
// # ^^^^^^^ name
|
||||
// var value = foo + 42
|
||||
// # ^^^^^ name
|
||||
// print(value)
|
||||
// # ^^^^^ -> ref
|
||||
// ```
|
||||
//
|
||||
// * `^`: Range marker.
|
||||
// * `name`: Unique name. Can contain any characters except whitespace chars.
|
||||
// * `ref`: Reference to unique name.
|
||||
//
|
||||
// Notes:
|
||||
// * If range should include first content-char (which is occupied by `#`): use `<` for next marker.
|
||||
// -> Range expands 1 to left (-> includes `#`).
|
||||
// * Note: Means: Range cannot be single char directly marked by `#`, but must be at least two chars (marked with `#<`).
|
||||
// * Comment must start at same ident as line its marked (-> because of tab alignment...).
|
||||
// * Use spaces to align after `#`! -> for correct alignment
|
||||
// * Between `#` and `^` can be spaces or `|` (to better visualize what's marked below).
|
||||
PackedStringArray lines = source.split("\n");
|
||||
|
||||
PackedStringArray names;
|
||||
Vector<InlineTestData> data;
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
InlineTestData d;
|
||||
if (InlineTestData::try_parse(lines, i, d)) {
|
||||
if (!d.name.is_empty()) {
|
||||
// Safety check: names must be unique.
|
||||
if (names.find(d.name) != -1) {
|
||||
FAIL(vformat("Duplicated name '%s' in '%s'. Names must be unique!", d.name, p_path));
|
||||
}
|
||||
names.append(d.name);
|
||||
}
|
||||
|
||||
data.append(d);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void test_resolve_symbol(const String &p_uri, const InlineTestData &p_test_data, const Vector<InlineTestData> &p_all_data) {
|
||||
if (p_test_data.ref.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SUBCASE(vformat("Can resolve symbol '%s' at %s to '%s'", p_test_data.text, p_test_data.range.to_string(), p_test_data.ref).utf8().get_data()) {
|
||||
const InlineTestData *target = nullptr;
|
||||
for (int i = 0; i < p_all_data.size(); i++) {
|
||||
if (p_all_data[i].name == p_test_data.ref) {
|
||||
target = &p_all_data[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
REQUIRE_MESSAGE(target, vformat("No target for ref '%s'", p_test_data.ref));
|
||||
|
||||
Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
|
||||
lsp::Position pos = p_test_data.range.start;
|
||||
|
||||
SUBCASE("start of identifier") {
|
||||
pos.character = p_test_data.range.start.character;
|
||||
test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
|
||||
}
|
||||
|
||||
SUBCASE("inside identifier") {
|
||||
pos.character = (p_test_data.range.end.character + p_test_data.range.start.character) / 2;
|
||||
test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
|
||||
}
|
||||
|
||||
SUBCASE("end of identifier") {
|
||||
pos.character = p_test_data.range.end.character;
|
||||
test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vector<InlineTestData> filter_ref_towards(const Vector<InlineTestData> &p_data, const String &p_name) {
|
||||
Vector<InlineTestData> res;
|
||||
|
||||
for (const InlineTestData &d : p_data) {
|
||||
if (d.ref == p_name) {
|
||||
res.append(d);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void test_resolve_symbols(const String &p_uri, const Vector<InlineTestData> &p_test_data, const Vector<InlineTestData> &p_all_data) {
|
||||
for (const InlineTestData &d : p_test_data) {
|
||||
test_resolve_symbol(p_uri, d, p_all_data);
|
||||
}
|
||||
}
|
||||
|
||||
void assert_no_errors_in(const String &p_path) {
|
||||
Error err;
|
||||
String source = FileAccess::get_file_as_string(p_path, &err);
|
||||
REQUIRE_MESSAGE(err == OK, vformat("Cannot read '%s'", p_path));
|
||||
|
||||
GDScriptParser parser;
|
||||
err = parser.parse(source, p_path, true);
|
||||
REQUIRE_MESSAGE(err == OK, vformat("Errors while parsing '%s'", p_path));
|
||||
|
||||
GDScriptAnalyzer analyzer(&parser);
|
||||
err = analyzer.analyze();
|
||||
REQUIRE_MESSAGE(err == OK, vformat("Errors while analyzing '%s'", p_path));
|
||||
}
|
||||
|
||||
inline lsp::Position lsp_pos(int line, int character) {
|
||||
lsp::Position p;
|
||||
p.line = line;
|
||||
p.character = character;
|
||||
return p;
|
||||
}
|
||||
|
||||
void test_position_roundtrip(lsp::Position p_lsp, GodotPosition p_gd, const PackedStringArray &p_lines) {
|
||||
GodotPosition actual_gd = GodotPosition::from_lsp(p_lsp, p_lines);
|
||||
CHECK_EQ(p_gd, actual_gd);
|
||||
lsp::Position actual_lsp = p_gd.to_lsp(p_lines);
|
||||
CHECK_EQ(p_lsp, actual_lsp);
|
||||
}
|
||||
|
||||
// Note:
|
||||
// * Cursor is BETWEEN chars
|
||||
// * `va|r` -> cursor between `a`&`r`
|
||||
// * `var`
|
||||
// ^
|
||||
// -> Character on `r` -> cursor between `a`&`r`s for tests:
|
||||
// * Line & Char:
|
||||
// * LSP: both 0-based
|
||||
// * Godot: both 1-based
|
||||
TEST_SUITE("[Modules][GDScript][LSP]") {
|
||||
TEST_CASE("Can convert positions to and from Godot") {
|
||||
String code = R"(extends Node
|
||||
|
||||
var member := 42
|
||||
|
||||
func f():
|
||||
var value := 42
|
||||
return value + member)";
|
||||
PackedStringArray lines = code.split("\n");
|
||||
|
||||
SUBCASE("line after end") {
|
||||
lsp::Position lsp = lsp_pos(7, 0);
|
||||
GodotPosition gd(8, 1);
|
||||
test_position_roundtrip(lsp, gd, lines);
|
||||
}
|
||||
SUBCASE("first char in first line") {
|
||||
lsp::Position lsp = lsp_pos(0, 0);
|
||||
GodotPosition gd(1, 1);
|
||||
test_position_roundtrip(lsp, gd, lines);
|
||||
}
|
||||
|
||||
SUBCASE("with tabs") {
|
||||
// On `v` in `value` in `var value := ...`.
|
||||
lsp::Position lsp = lsp_pos(5, 6);
|
||||
GodotPosition gd(6, 13);
|
||||
test_position_roundtrip(lsp, gd, lines);
|
||||
}
|
||||
|
||||
SUBCASE("doesn't fail with column outside of character length") {
|
||||
lsp::Position lsp = lsp_pos(2, 100);
|
||||
GodotPosition::from_lsp(lsp, lines);
|
||||
|
||||
GodotPosition gd(3, 100);
|
||||
gd.to_lsp(lines);
|
||||
}
|
||||
|
||||
SUBCASE("doesn't fail with line outside of line length") {
|
||||
lsp::Position lsp = lsp_pos(200, 100);
|
||||
GodotPosition::from_lsp(lsp, lines);
|
||||
|
||||
GodotPosition gd(300, 100);
|
||||
gd.to_lsp(lines);
|
||||
}
|
||||
|
||||
SUBCASE("special case: negative line for root class") {
|
||||
GodotPosition gd(-1, 0);
|
||||
lsp::Position expected = lsp_pos(0, 0);
|
||||
lsp::Position actual = gd.to_lsp(lines);
|
||||
CHECK_EQ(actual, expected);
|
||||
}
|
||||
SUBCASE("special case: lines.length() + 1 for root class") {
|
||||
GodotPosition gd(lines.size() + 1, 0);
|
||||
lsp::Position expected = lsp_pos(lines.size(), 0);
|
||||
lsp::Position actual = gd.to_lsp(lines);
|
||||
CHECK_EQ(actual, expected);
|
||||
}
|
||||
}
|
||||
TEST_CASE("[workspace][resolve_symbol]") {
|
||||
GDScriptLanguageProtocol *proto = initialize(root);
|
||||
REQUIRE(proto);
|
||||
Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
|
||||
|
||||
{
|
||||
String path = "res://lsp/local_variables.notest.gd";
|
||||
assert_no_errors_in(path);
|
||||
String uri = workspace->get_file_uri(path);
|
||||
Vector<InlineTestData> all_test_data = read_tests(path);
|
||||
SUBCASE("Can get correct ranges for public variables") {
|
||||
Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "member");
|
||||
test_resolve_symbols(uri, test_data, all_test_data);
|
||||
}
|
||||
SUBCASE("Can get correct ranges for local variables") {
|
||||
Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "test");
|
||||
test_resolve_symbols(uri, test_data, all_test_data);
|
||||
}
|
||||
SUBCASE("Can get correct ranges for local parameters") {
|
||||
Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "arg");
|
||||
test_resolve_symbols(uri, test_data, all_test_data);
|
||||
}
|
||||
}
|
||||
|
||||
SUBCASE("Can get correct ranges for indented variables") {
|
||||
String path = "res://lsp/indentation.notest.gd";
|
||||
assert_no_errors_in(path);
|
||||
String uri = workspace->get_file_uri(path);
|
||||
Vector<InlineTestData> all_test_data = read_tests(path);
|
||||
test_resolve_symbols(uri, all_test_data, all_test_data);
|
||||
}
|
||||
|
||||
SUBCASE("Can get correct ranges for scopes") {
|
||||
String path = "res://lsp/scopes.notest.gd";
|
||||
assert_no_errors_in(path);
|
||||
String uri = workspace->get_file_uri(path);
|
||||
Vector<InlineTestData> all_test_data = read_tests(path);
|
||||
test_resolve_symbols(uri, all_test_data, all_test_data);
|
||||
}
|
||||
|
||||
SUBCASE("Can get correct ranges for lambda") {
|
||||
String path = "res://lsp/lambdas.notest.gd";
|
||||
assert_no_errors_in(path);
|
||||
String uri = workspace->get_file_uri(path);
|
||||
Vector<InlineTestData> all_test_data = read_tests(path);
|
||||
test_resolve_symbols(uri, all_test_data, all_test_data);
|
||||
}
|
||||
|
||||
SUBCASE("Can get correct ranges for inner class") {
|
||||
String path = "res://lsp/class.notest.gd";
|
||||
assert_no_errors_in(path);
|
||||
String uri = workspace->get_file_uri(path);
|
||||
Vector<InlineTestData> all_test_data = read_tests(path);
|
||||
test_resolve_symbols(uri, all_test_data, all_test_data);
|
||||
}
|
||||
|
||||
SUBCASE("Can get correct ranges for inner class") {
|
||||
String path = "res://lsp/enums.notest.gd";
|
||||
assert_no_errors_in(path);
|
||||
String uri = workspace->get_file_uri(path);
|
||||
Vector<InlineTestData> all_test_data = read_tests(path);
|
||||
test_resolve_symbols(uri, all_test_data, all_test_data);
|
||||
}
|
||||
|
||||
SUBCASE("Can get correct ranges for shadowing & shadowed variables") {
|
||||
String path = "res://lsp/shadowing_initializer.notest.gd";
|
||||
assert_no_errors_in(path);
|
||||
String uri = workspace->get_file_uri(path);
|
||||
Vector<InlineTestData> all_test_data = read_tests(path);
|
||||
test_resolve_symbols(uri, all_test_data, all_test_data);
|
||||
}
|
||||
|
||||
SUBCASE("Can get correct ranges for properties and getter/setter") {
|
||||
String path = "res://lsp/properties.notest.gd";
|
||||
assert_no_errors_in(path);
|
||||
String uri = workspace->get_file_uri(path);
|
||||
Vector<InlineTestData> all_test_data = read_tests(path);
|
||||
test_resolve_symbols(uri, all_test_data, all_test_data);
|
||||
}
|
||||
|
||||
memdelete(proto);
|
||||
finish_language();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace GDScriptTests
|
||||
|
||||
#endif // TOOLS_ENABLED
|
||||
|
||||
#endif // TEST_LSP_H
|
||||
Reference in New Issue
Block a user