summaryrefslogtreecommitdiff
path: root/modules/gdscript/language_server
diff options
context:
space:
mode:
authorgeequlim <geequlim@gmail.com>2020-01-04 20:02:26 +0800
committergeequlim <geequlim@gmail.com>2020-01-04 20:02:26 +0800
commiteb5861f9f11b9d02ef360d6ea82d73609032fec3 (patch)
treec6151f03969f695f81535db5f85e4ce624898a4d /modules/gdscript/language_server
parent118aaa55f9577c9d26b0793968c1d81c03fc736f (diff)
GDScript LSP: Implement goto declaration
Diffstat (limited to 'modules/gdscript/language_server')
-rw-r--r--modules/gdscript/language_server/gdscript_text_document.cpp121
-rw-r--r--modules/gdscript/language_server/gdscript_text_document.h2
2 files changed, 69 insertions, 54 deletions
diff --git a/modules/gdscript/language_server/gdscript_text_document.cpp b/modules/gdscript/language_server/gdscript_text_document.cpp
index 94cd2536e3..b3d1b67af5 100644
--- a/modules/gdscript/language_server/gdscript_text_document.cpp
+++ b/modules/gdscript/language_server/gdscript_text_document.cpp
@@ -49,6 +49,7 @@ void GDScriptTextDocument::_bind_methods() {
ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);
ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);
ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition);
+ ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration);
ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor);
}
@@ -340,84 +341,96 @@ Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
}
Array GDScriptTextDocument::definition(const Dictionary &p_params) {
- Array arr;
+ lsp::TextDocumentPositionParams params;
+ params.load(p_params);
+ List<const lsp::DocumentSymbol *> symbols;
+ Array arr = this->find_symbols(params, symbols);
+ return arr;
+}
+Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {
lsp::TextDocumentPositionParams params;
params.load(p_params);
+ List<const lsp::DocumentSymbol *> symbols;
+ Array arr = this->find_symbols(params, symbols);
+ if (arr.empty() && !symbols.empty() && !symbols.front()->get()->native_class.empty()) { // Find a native symbol
+ const lsp::DocumentSymbol *symbol = symbols.front()->get();
+ if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {
+ String id;
+ switch (symbol->kind) {
+ case lsp::SymbolKind::Class:
+ id = "class_name:" + symbol->name;
+ break;
+ case lsp::SymbolKind::Constant:
+ id = "class_constant:" + symbol->native_class + ":" + symbol->name;
+ break;
+ case lsp::SymbolKind::Property:
+ case lsp::SymbolKind::Variable:
+ id = "class_property:" + symbol->native_class + ":" + symbol->name;
+ break;
+ case lsp::SymbolKind::Enum:
+ id = "class_enum:" + symbol->native_class + ":" + symbol->name;
+ break;
+ case lsp::SymbolKind::Method:
+ case lsp::SymbolKind::Function:
+ id = "class_method:" + symbol->native_class + ":" + symbol->name;
+ break;
+ default:
+ id = "class_global:" + symbol->native_class + ":" + symbol->name;
+ break;
+ }
+ call_deferred("show_native_symbol_in_editor", id);
+ } else {
+ notify_client_show_symbol(symbol);
+ }
+ }
+ return arr;
+}
- const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
+GDScriptTextDocument::GDScriptTextDocument() {
+ file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
+}
+
+GDScriptTextDocument::~GDScriptTextDocument() {
+ memdelete(file_checker);
+}
+
+void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {
+ String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);
+ GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);
+}
+
+void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
+ ScriptEditor::get_singleton()->call_deferred("_help_class_goto", p_symbol_id);
+ OS::get_singleton()->move_window_to_foreground();
+}
+
+Array GDScriptTextDocument::find_symbols(const lsp::TextDocumentPositionParams &p_location, List<const lsp::DocumentSymbol *> &r_list) {
+ Array arr;
+ const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);
if (symbol) {
lsp::Location location;
location.uri = symbol->uri;
location.range = symbol->range;
-
const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);
if (file_checker->file_exists(path)) {
arr.push_back(location.to_json());
- } else if (!symbol->native_class.empty()) {
- if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {
- String id;
- switch (symbol->kind) {
- case lsp::SymbolKind::Class:
- id = "class_name:" + symbol->name;
- break;
- case lsp::SymbolKind::Constant:
- id = "class_constant:" + symbol->native_class + ":" + symbol->name;
- break;
- case lsp::SymbolKind::Property:
- case lsp::SymbolKind::Variable:
- id = "class_property:" + symbol->native_class + ":" + symbol->name;
- break;
- case lsp::SymbolKind::Enum:
- id = "class_enum:" + symbol->native_class + ":" + symbol->name;
- break;
- case lsp::SymbolKind::Method:
- case lsp::SymbolKind::Function:
- id = "class_method:" + symbol->native_class + ":" + symbol->name;
- break;
- default:
- id = "class_global:" + symbol->native_class + ":" + symbol->name;
- break;
- }
- call_deferred("show_native_symbol_in_editor", id);
- } else {
- notify_client_show_symbol(symbol);
- }
}
+ r_list.push_back(symbol);
} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
-
List<const lsp::DocumentSymbol *> list;
- GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list);
+ GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list);
for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) {
-
if (const lsp::DocumentSymbol *s = E->get()) {
if (!s->uri.empty()) {
lsp::Location location;
location.uri = s->uri;
location.range = s->range;
arr.push_back(location.to_json());
+ r_list.push_back(s);
}
}
}
}
-
return arr;
}
-
-GDScriptTextDocument::GDScriptTextDocument() {
- file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
-}
-
-GDScriptTextDocument::~GDScriptTextDocument() {
- memdelete(file_checker);
-}
-
-void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {
- String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);
- GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);
-}
-
-void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
- ScriptEditor::get_singleton()->call_deferred("_help_class_goto", p_symbol_id);
- OS::get_singleton()->move_window_to_foreground();
-}
diff --git a/modules/gdscript/language_server/gdscript_text_document.h b/modules/gdscript/language_server/gdscript_text_document.h
index 8a7c6fb98c..d93d828003 100644
--- a/modules/gdscript/language_server/gdscript_text_document.h
+++ b/modules/gdscript/language_server/gdscript_text_document.h
@@ -51,6 +51,7 @@ protected:
Array native_member_completions;
private:
+ Array find_symbols(const lsp::TextDocumentPositionParams &p_location, List<const lsp::DocumentSymbol *> &r_list);
lsp::TextDocumentItem load_document_item(const Variant &p_param);
void notify_client_show_symbol(const lsp::DocumentSymbol *symbol);
@@ -65,6 +66,7 @@ public:
Array colorPresentation(const Dictionary &p_params);
Variant hover(const Dictionary &p_params);
Array definition(const Dictionary &p_params);
+ Variant declaration(const Dictionary &p_params);
void initialize();