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:
BooksBaum
2023-07-28 17:06:08 +02:00
committed by ryanabx
parent 221884e6bc
commit 0202a36a7a
18 changed files with 1661 additions and 360 deletions

View File

@ -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;