| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  gdscript_text_document.cpp                                           */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                      https://godotengine.org                          */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gdscript_text_document.h"
 | 
					
						
							|  |  |  | #include "../gdscript.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | #include "core/os/os.h"
 | 
					
						
							|  |  |  | #include "editor/editor_settings.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-30 16:10:13 +08:00
										 |  |  | #include "editor/plugins/script_text_editor.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | #include "gdscript_extend_parser.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | #include "gdscript_language_protocol.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GDScriptTextDocument::_bind_methods() { | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange); | 
					
						
							| 
									
										
										
										
											2019-10-04 20:53:40 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("nativeSymbol"), &GDScriptTextDocument::nativeSymbol); | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion); | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve); | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover); | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition); | 
					
						
							| 
									
										
										
										
											2020-01-04 20:02:26 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration); | 
					
						
							| 
									
										
										
										
											2020-01-09 00:44:32 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("signatureHelp"), &GDScriptTextDocument::signatureHelp); | 
					
						
							| 
									
										
										
										
											2019-06-30 16:10:13 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor); | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GDScriptTextDocument::didOpen(const Variant &p_param) { | 
					
						
							|  |  |  | 	lsp::TextDocumentItem doc = load_document_item(p_param); | 
					
						
							|  |  |  | 	sync_script_content(doc.uri, doc.text); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GDScriptTextDocument::didChange(const Variant &p_param) { | 
					
						
							|  |  |  | 	lsp::TextDocumentItem doc = load_document_item(p_param); | 
					
						
							|  |  |  | 	Dictionary dict = p_param; | 
					
						
							|  |  |  | 	Array contentChanges = dict["contentChanges"]; | 
					
						
							|  |  |  | 	for (int i = 0; i < contentChanges.size(); ++i) { | 
					
						
							|  |  |  | 		lsp::TextDocumentContentChangeEvent evt; | 
					
						
							|  |  |  | 		evt.load(contentChanges[i]); | 
					
						
							|  |  |  | 		doc.text = evt.text; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sync_script_content(doc.uri, doc.text); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | lsp::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) { | 
					
						
							|  |  |  | 	lsp::TextDocumentItem doc; | 
					
						
							|  |  |  | 	Dictionary params = p_param; | 
					
						
							|  |  |  | 	doc.load(params["textDocument"]); | 
					
						
							|  |  |  | 	return doc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 20:53:40 +08:00
										 |  |  | void GDScriptTextDocument::notify_client_show_symbol(const lsp::DocumentSymbol *symbol) { | 
					
						
							|  |  |  | 	ERR_FAIL_NULL(symbol); | 
					
						
							|  |  |  | 	GDScriptLanguageProtocol::get_singleton()->notify_client("gdscript/show_native_symbol", symbol->to_json(true)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | void GDScriptTextDocument::initialize() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 		const HashMap<StringName, ClassMembers> &native_members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members; | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const StringName *class_ptr = native_members.next(NULL); | 
					
						
							|  |  |  | 		while (class_ptr) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const ClassMembers &members = native_members.get(*class_ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const String *name = members.next(NULL); | 
					
						
							|  |  |  | 			while (name) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				const lsp::DocumentSymbol *symbol = members.get(*name); | 
					
						
							| 
									
										
										
										
											2019-06-25 12:12:41 +08:00
										 |  |  | 				lsp::CompletionItem item = symbol->make_completion_item(); | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 				item.data = JOIN_SYMBOLS(String(*class_ptr), *name); | 
					
						
							|  |  |  | 				native_member_completions.push_back(item.to_json()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				name = members.next(name); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			class_ptr = native_members.next(class_ptr); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 20:53:40 +08:00
										 |  |  | Variant GDScriptTextDocument::nativeSymbol(const Dictionary &p_params) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Variant ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lsp::NativeSymbolInspectParams params; | 
					
						
							|  |  |  | 	params.load(p_params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_native_symbol(params)) { | 
					
						
							|  |  |  | 		ret = symbol->to_json(true); | 
					
						
							|  |  |  | 		notify_client_show_symbol(symbol); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) { | 
					
						
							|  |  |  | 	Dictionary params = p_params["textDocument"]; | 
					
						
							|  |  |  | 	String uri = params["uri"]; | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 	String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri); | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 	Array arr; | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 	if (const Map<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) { | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 		Vector<lsp::DocumentedSymbolInformation> list; | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 		parser->get()->get_symbols().symbol_tree_as_list(uri, list); | 
					
						
							|  |  |  | 		for (int i = 0; i < list.size(); i++) { | 
					
						
							|  |  |  | 			arr.push_back(list[i].to_json()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return arr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Array GDScriptTextDocument::completion(const Dictionary &p_params) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 	Array arr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lsp::CompletionParams params; | 
					
						
							|  |  |  | 	params.load(p_params); | 
					
						
							|  |  |  | 	Dictionary request_data = params.to_json(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	List<ScriptCodeCompletionOption> options; | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 	GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options); | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 	if (!options.empty()) { | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 		int i = 0; | 
					
						
							|  |  |  | 		arr.resize(options.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 		for (const List<ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const ScriptCodeCompletionOption &option = E->get(); | 
					
						
							|  |  |  | 			lsp::CompletionItem item; | 
					
						
							|  |  |  | 			item.label = option.display; | 
					
						
							|  |  |  | 			item.data = request_data; | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 			switch (option.kind) { | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_ENUM: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Enum; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_CLASS: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Class; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_MEMBER: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Property; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_FUNCTION: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Method; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_SIGNAL: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Event; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_CONSTANT: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Constant; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_VARIABLE: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Variable; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_FILE_PATH: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::File; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_NODE_PATH: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Snippet; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case ScriptCodeCompletionOption::KIND_PLAIN_TEXT: | 
					
						
							|  |  |  | 					item.kind = lsp::CompletionItemKind::Text; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 12:12:41 +08:00
										 |  |  | 			arr[i] = item.to_json(); | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 			i++; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 	} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 		arr = native_member_completions.duplicate(); | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 		for (Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 			ExtendGDScriptParser *script = E->get(); | 
					
						
							|  |  |  | 			const Array &items = script->get_member_completions(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const int start_size = arr.size(); | 
					
						
							|  |  |  | 			arr.resize(start_size + items.size()); | 
					
						
							|  |  |  | 			for (int i = start_size; i < arr.size(); i++) { | 
					
						
							|  |  |  | 				arr[i] = items[i - start_size]; | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 	return arr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) { | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	lsp::CompletionItem item; | 
					
						
							|  |  |  | 	item.load(p_params); | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	lsp::CompletionParams params; | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 	Variant data = p_params["data"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const lsp::DocumentSymbol *symbol = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (data.get_type() == Variant::DICTIONARY) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 		params.load(p_params["data"]); | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 		symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function); | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} else if (data.get_type() == Variant::STRING) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 		String query = data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 12:12:41 +08:00
										 |  |  | 		Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (param_symbols.size() >= 2) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			String class_ = param_symbols[0]; | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 			StringName class_name = class_; | 
					
						
							| 
									
										
										
										
											2019-06-25 12:12:41 +08:00
										 |  |  | 			String member_name = param_symbols[param_symbols.size() - 1]; | 
					
						
							|  |  |  | 			String inner_class_name; | 
					
						
							|  |  |  | 			if (param_symbols.size() >= 3) { | 
					
						
							|  |  |  | 				inner_class_name = param_symbols[1]; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 			if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 				if (const lsp::DocumentSymbol *const *member = members->getptr(member_name)) { | 
					
						
							|  |  |  | 					symbol = *member; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!symbol) { | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 				if (const Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) { | 
					
						
							| 
									
										
										
										
											2019-06-25 12:12:41 +08:00
										 |  |  | 					symbol = E->get()->get_member_symbol(member_name, inner_class_name); | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	if (symbol) { | 
					
						
							|  |  |  | 		item.documentation = symbol->render(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 14:06:39 +08:00
										 |  |  | 	if ((item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function) && !item.label.ends_with("):")) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 		item.insertText = item.label + "("; | 
					
						
							| 
									
										
										
										
											2020-01-10 14:23:01 +08:00
										 |  |  | 		if (symbol && symbol->children.empty()) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 			item.insertText += ")"; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else if (item.kind == lsp::CompletionItemKind::Event) { | 
					
						
							|  |  |  | 		if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) { | 
					
						
							|  |  |  | 			const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\""; | 
					
						
							|  |  |  | 			item.insertText = quote_style + item.label + quote_style; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 12:12:41 +08:00
										 |  |  | 	return item.to_json(true); | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) { | 
					
						
							|  |  |  | 	Array arr; | 
					
						
							|  |  |  | 	return arr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Array GDScriptTextDocument::codeLens(const Dictionary &p_params) { | 
					
						
							|  |  |  | 	Array arr; | 
					
						
							|  |  |  | 	return arr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-03 16:26:55 +08:00
										 |  |  | Array GDScriptTextDocument::documentLink(const Dictionary &p_params) { | 
					
						
							|  |  |  | 	Array ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lsp::DocumentLinkParams params; | 
					
						
							|  |  |  | 	params.load(p_params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	List<lsp::DocumentLink> links; | 
					
						
							|  |  |  | 	GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links); | 
					
						
							|  |  |  | 	for (const List<lsp::DocumentLink>::Element *E = links.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		ret.push_back(E->get().to_json()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) { | 
					
						
							|  |  |  | 	Array arr; | 
					
						
							|  |  |  | 	return arr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Variant GDScriptTextDocument::hover(const Dictionary &p_params) { | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	lsp::TextDocumentPositionParams params; | 
					
						
							|  |  |  | 	params.load(p_params); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 	const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params); | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	if (symbol) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 		lsp::Hover hover; | 
					
						
							|  |  |  | 		hover.contents = symbol->render(); | 
					
						
							| 
									
										
										
										
											2020-02-12 17:58:19 +08:00
										 |  |  | 		hover.range.start = params.position; | 
					
						
							|  |  |  | 		hover.range.end = params.position; | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 		return hover.to_json(); | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 	} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 		Dictionary ret; | 
					
						
							|  |  |  | 		Array contents; | 
					
						
							|  |  |  | 		List<const lsp::DocumentSymbol *> list; | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 		GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list); | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 		for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 			if (const lsp::DocumentSymbol *s = E->get()) { | 
					
						
							|  |  |  | 				contents.push_back(s->render().value); | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ret["contents"] = contents; | 
					
						
							|  |  |  | 		return ret; | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 	return Variant(); | 
					
						
							| 
									
										
										
										
											2019-06-14 22:38:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | Array GDScriptTextDocument::definition(const Dictionary &p_params) { | 
					
						
							| 
									
										
										
										
											2020-01-04 20:02:26 +08:00
										 |  |  | 	lsp::TextDocumentPositionParams params; | 
					
						
							|  |  |  | 	params.load(p_params); | 
					
						
							|  |  |  | 	List<const lsp::DocumentSymbol *> symbols; | 
					
						
							|  |  |  | 	Array arr = this->find_symbols(params, symbols); | 
					
						
							|  |  |  | 	return arr; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-04 20:02:26 +08:00
										 |  |  | Variant GDScriptTextDocument::declaration(const Dictionary &p_params) { | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	lsp::TextDocumentPositionParams params; | 
					
						
							|  |  |  | 	params.load(p_params); | 
					
						
							| 
									
										
										
										
											2020-01-04 20:02:26 +08:00
										 |  |  | 	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; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 00:44:32 +08:00
										 |  |  | Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) { | 
					
						
							|  |  |  | 	Variant ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lsp::TextDocumentPositionParams params; | 
					
						
							|  |  |  | 	params.load(p_params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lsp::SignatureHelp s; | 
					
						
							|  |  |  | 	if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) { | 
					
						
							|  |  |  | 		ret = s.to_json(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-04 20:02:26 +08:00
										 |  |  | 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); | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	if (symbol) { | 
					
						
							|  |  |  | 		lsp::Location location; | 
					
						
							|  |  |  | 		location.uri = symbol->uri; | 
					
						
							|  |  |  | 		location.range = symbol->range; | 
					
						
							| 
									
										
										
										
											2019-06-26 20:21:42 +08:00
										 |  |  | 		const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri); | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 		if (file_checker->file_exists(path)) { | 
					
						
							|  |  |  | 			arr.push_back(location.to_json()); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-01-04 20:02:26 +08:00
										 |  |  | 		r_list.push_back(symbol); | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 	} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { | 
					
						
							|  |  |  | 		List<const lsp::DocumentSymbol *> list; | 
					
						
							| 
									
										
										
										
											2020-01-04 20:02:26 +08:00
										 |  |  | 		GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list); | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 		for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2019-06-24 18:25:12 +08:00
										 |  |  | 			if (const lsp::DocumentSymbol *s = E->get()) { | 
					
						
							| 
									
										
										
										
											2019-06-30 16:10:13 +08:00
										 |  |  | 				if (!s->uri.empty()) { | 
					
						
							|  |  |  | 					lsp::Location location; | 
					
						
							|  |  |  | 					location.uri = s->uri; | 
					
						
							|  |  |  | 					location.range = s->range; | 
					
						
							|  |  |  | 					arr.push_back(location.to_json()); | 
					
						
							| 
									
										
										
										
											2020-01-04 20:02:26 +08:00
										 |  |  | 					r_list.push_back(s); | 
					
						
							| 
									
										
										
										
											2019-06-30 16:10:13 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-06-23 21:10:28 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-06-23 01:48:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return arr; | 
					
						
							|  |  |  | } |