Add GDScript Language Protocol plugin
This commit is contained in:
parent
61ed6efa5b
commit
f58560ac36
|
@ -9,3 +9,4 @@ env_gdscript.add_source_files(env.modules_sources, "*.cpp")
|
|||
|
||||
if env['tools']:
|
||||
env_gdscript.add_source_files(env.modules_sources, "./editor/*.cpp")
|
||||
env_gdscript.add_source_files(env.modules_sources, "./language_server/*.cpp")
|
||||
|
|
|
@ -8255,6 +8255,10 @@ int GDScriptParser::get_error_column() const {
|
|||
return error_column;
|
||||
}
|
||||
|
||||
bool GDScriptParser::has_error() const {
|
||||
return error_set;
|
||||
}
|
||||
|
||||
Error GDScriptParser::_parse(const String &p_base_path) {
|
||||
|
||||
base_path = p_base_path;
|
||||
|
|
|
@ -632,6 +632,7 @@ private:
|
|||
Error _parse(const String &p_base_path);
|
||||
|
||||
public:
|
||||
bool has_error() const;
|
||||
String get_error() const;
|
||||
int get_error_line() const;
|
||||
int get_error_column() const;
|
||||
|
|
|
@ -0,0 +1,237 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_extend_parser.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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_extend_parser.h"
|
||||
#include "../gdscript.h"
|
||||
|
||||
void ExtendGDScriptParser::update_diagnostics() {
|
||||
|
||||
diagnostics.clear();
|
||||
|
||||
if (has_error()) {
|
||||
lsp::Diagnostic diagnostic;
|
||||
diagnostic.severity = lsp::DiagnosticSeverity::Error;
|
||||
diagnostic.message = get_error();
|
||||
diagnostic.source = "gdscript";
|
||||
diagnostic.code = -1;
|
||||
lsp::Range range;
|
||||
lsp::Position pos;
|
||||
int line = get_error_line() - 1;
|
||||
const String &line_text = get_lines()[line];
|
||||
pos.line = line;
|
||||
pos.character = line_text.length() - line_text.strip_edges(true, false).length();
|
||||
range.start = pos;
|
||||
range.end = range.start;
|
||||
range.end.character = line_text.strip_edges(false).length();
|
||||
diagnostic.range = range;
|
||||
diagnostics.push_back(diagnostic);
|
||||
}
|
||||
|
||||
const List<GDScriptWarning> &warnings = get_warnings();
|
||||
for (const List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) {
|
||||
const GDScriptWarning &warning = E->get();
|
||||
lsp::Diagnostic diagnostic;
|
||||
diagnostic.severity = lsp::DiagnosticSeverity::Warning;
|
||||
diagnostic.message = warning.get_message();
|
||||
diagnostic.source = "gdscript";
|
||||
diagnostic.code = warning.code;
|
||||
lsp::Range range;
|
||||
lsp::Position pos;
|
||||
int line = warning.line - 1;
|
||||
const String &line_text = get_lines()[line];
|
||||
pos.line = line;
|
||||
pos.character = line_text.length() - line_text.strip_edges(true, false).length();
|
||||
range.start = pos;
|
||||
range.end = pos;
|
||||
range.end.character = line_text.strip_edges(false).length();
|
||||
diagnostic.range = range;
|
||||
diagnostics.push_back(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
void ExtendGDScriptParser::update_symbols() {
|
||||
const GDScriptParser::Node *head = get_parse_tree();
|
||||
if (const GDScriptParser::ClassNode *gdclass = dynamic_cast<const GDScriptParser::ClassNode *>(head)) {
|
||||
parse_class_symbol(gdclass, class_symbol);
|
||||
}
|
||||
}
|
||||
|
||||
void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol) {
|
||||
r_symbol.children.clear();
|
||||
r_symbol.name = p_class->name;
|
||||
if (r_symbol.name.empty())
|
||||
r_symbol.name = path.get_file();
|
||||
r_symbol.kind = lsp::SymbolKind::Class;
|
||||
r_symbol.detail = p_class->get_datatype().to_string();
|
||||
r_symbol.deprecated = false;
|
||||
r_symbol.range.start.line = p_class->line - 1;
|
||||
r_symbol.range.start.character = p_class->column;
|
||||
r_symbol.range.end.line = p_class->end_line - 1;
|
||||
r_symbol.selectionRange.start.line = r_symbol.range.start.line;
|
||||
|
||||
for (int i = 0; i < p_class->variables.size(); ++i) {
|
||||
|
||||
const GDScriptParser::ClassNode::Member &m = p_class->variables[i];
|
||||
|
||||
lsp::DocumentSymbol symbol;
|
||||
symbol.name = m.identifier;
|
||||
symbol.kind = lsp::SymbolKind::Variable;
|
||||
symbol.detail = m.data_type.to_string();
|
||||
symbol.deprecated = false;
|
||||
const int line = m.line - 1;
|
||||
symbol.range.start.line = line;
|
||||
symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length();
|
||||
symbol.range.end.line = line;
|
||||
symbol.range.end.character = lines[line].length();
|
||||
symbol.selectionRange.start.line = symbol.range.start.line;
|
||||
|
||||
r_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
for (int i = 0; i < p_class->_signals.size(); ++i) {
|
||||
const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i];
|
||||
|
||||
lsp::DocumentSymbol symbol;
|
||||
symbol.name = signal.name;
|
||||
symbol.kind = lsp::SymbolKind::Event;
|
||||
symbol.deprecated = false;
|
||||
const int line = signal.line - 1;
|
||||
symbol.range.start.line = line;
|
||||
symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length();
|
||||
symbol.range.end.line = symbol.range.start.line;
|
||||
symbol.range.end.character = lines[line].length();
|
||||
symbol.selectionRange.start.line = symbol.range.start.line;
|
||||
|
||||
r_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
|
||||
lsp::DocumentSymbol symbol;
|
||||
symbol.name = E->key();
|
||||
symbol.kind = lsp::SymbolKind::Constant;
|
||||
symbol.deprecated = false;
|
||||
const int line = E->get().expression->line - 1;
|
||||
symbol.range.start.line = line;
|
||||
symbol.range.start.character = E->get().expression->column;
|
||||
symbol.range.end.line = symbol.range.start.line;
|
||||
symbol.range.end.character = lines[line].length();
|
||||
symbol.selectionRange.start.line = symbol.range.start.line;
|
||||
|
||||
r_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
for (int i = 0; i < p_class->functions.size(); ++i) {
|
||||
const GDScriptParser::FunctionNode *func = p_class->functions[i];
|
||||
lsp::DocumentSymbol symbol;
|
||||
parse_function_symbol(func, symbol);
|
||||
r_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
for (int i = 0; i < p_class->static_functions.size(); ++i) {
|
||||
const GDScriptParser::FunctionNode *func = p_class->static_functions[i];
|
||||
lsp::DocumentSymbol symbol;
|
||||
parse_function_symbol(func, symbol);
|
||||
r_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
for (int i = 0; i < p_class->subclasses.size(); ++i) {
|
||||
const GDScriptParser::ClassNode *subclass = p_class->subclasses[i];
|
||||
lsp::DocumentSymbol symbol;
|
||||
parse_class_symbol(subclass, symbol);
|
||||
r_symbol.children.push_back(symbol);
|
||||
}
|
||||
}
|
||||
|
||||
void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol) {
|
||||
r_symbol.name = p_func->name;
|
||||
r_symbol.kind = lsp::SymbolKind::Function;
|
||||
r_symbol.detail = p_func->get_datatype().to_string();
|
||||
r_symbol.deprecated = false;
|
||||
const int line = p_func->line - 1;
|
||||
r_symbol.range.start.line = line;
|
||||
r_symbol.range.start.character = p_func->column;
|
||||
r_symbol.range.end.line = MAX(p_func->body->end_line - 2, p_func->body->line);
|
||||
r_symbol.range.end.character = lines[r_symbol.range.end.line].length();
|
||||
r_symbol.selectionRange.start.line = r_symbol.range.start.line;
|
||||
|
||||
for (const Map<StringName, LocalVarNode *>::Element *E = p_func->body->variables.front(); E; E = E->next()) {
|
||||
lsp::DocumentSymbol symbol;
|
||||
symbol.name = E->key();
|
||||
symbol.kind = lsp::SymbolKind::Variable;
|
||||
symbol.range.start.line = E->get()->line - 1;
|
||||
symbol.range.start.character = E->get()->column;
|
||||
symbol.range.end.line = symbol.range.start.line;
|
||||
symbol.range.end.character = lines[symbol.range.end.line].length();
|
||||
r_symbol.children.push_back(symbol);
|
||||
}
|
||||
for (int i = 0; i < p_func->arguments.size(); i++) {
|
||||
lsp::DocumentSymbol symbol;
|
||||
symbol.kind = lsp::SymbolKind::Variable;
|
||||
symbol.name = p_func->arguments[i];
|
||||
symbol.range.start.line = p_func->body->line - 1;
|
||||
symbol.range.start.character = p_func->body->column;
|
||||
symbol.range.end = symbol.range.start;
|
||||
r_symbol.children.push_back(symbol);
|
||||
}
|
||||
}
|
||||
|
||||
String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) {
|
||||
|
||||
String longthing;
|
||||
int len = lines.size();
|
||||
for (int i = 0; i < len; i++) {
|
||||
|
||||
if (i == p_cursor.line) {
|
||||
longthing += lines[i].substr(0, p_cursor.character);
|
||||
longthing += String::chr(0xFFFF); //not unicode, represents the cursor
|
||||
longthing += lines[i].substr(p_cursor.character, lines[i].size());
|
||||
} else {
|
||||
|
||||
longthing += lines[i];
|
||||
}
|
||||
|
||||
if (i != len - 1)
|
||||
longthing += "\n";
|
||||
}
|
||||
|
||||
return longthing;
|
||||
}
|
||||
|
||||
Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) {
|
||||
path = p_path;
|
||||
code = p_code;
|
||||
lines = p_code.split("\n");
|
||||
|
||||
Error err = GDScriptParser::parse(p_code, p_path.get_base_dir(), false, p_path, false, NULL, false);
|
||||
update_diagnostics();
|
||||
update_symbols();
|
||||
|
||||
return err;
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_extend_parser.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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 GDSCRIPT_EXTEND_PARSER_H
|
||||
#define GDSCRIPT_EXTEND_PARSER_H
|
||||
|
||||
#include "../gdscript_parser.h"
|
||||
#include "core/variant.h"
|
||||
#include "lsp.hpp"
|
||||
|
||||
class ExtendGDScriptParser : public GDScriptParser {
|
||||
String path;
|
||||
String code;
|
||||
Vector<String> lines;
|
||||
|
||||
lsp::DocumentSymbol class_symbol;
|
||||
Vector<lsp::Diagnostic> diagnostics;
|
||||
|
||||
void update_diagnostics();
|
||||
void update_symbols();
|
||||
|
||||
void parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol);
|
||||
void parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol);
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ const String &get_path() const { return path; }
|
||||
_FORCE_INLINE_ const String &get_code() const { return code; }
|
||||
_FORCE_INLINE_ const Vector<String> &get_lines() const { return lines; }
|
||||
_FORCE_INLINE_ const lsp::DocumentSymbol &get_symbols() const { return class_symbol; }
|
||||
_FORCE_INLINE_ const Vector<lsp::Diagnostic> &get_diagnostics() const { return diagnostics; }
|
||||
|
||||
String get_text_for_completion(const lsp::Position &p_cursor);
|
||||
|
||||
Error parse(const String &p_code, const String &p_path);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,173 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_language_protocol.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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_language_protocol.h"
|
||||
#include "core/io/json.h"
|
||||
#include "core/os/copymem.h"
|
||||
#include "core/project_settings.h"
|
||||
|
||||
GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = NULL;
|
||||
|
||||
void GDScriptLanguageProtocol::on_data_received(int p_id) {
|
||||
lastest_client_id = p_id;
|
||||
Ref<WebSocketPeer> peer = server->get_peer(p_id);
|
||||
PoolByteArray data;
|
||||
if (OK == peer->get_packet_buffer(data)) {
|
||||
String message;
|
||||
message.parse_utf8((const char *)data.read().ptr(), data.size());
|
||||
if (message.begins_with("Content-Length:")) return;
|
||||
String output = process_message(message);
|
||||
if (!output.empty()) {
|
||||
CharString charstr = output.utf8();
|
||||
peer->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::on_client_connected(int p_id, const String &p_protocal) {
|
||||
clients.set(p_id, server->get_peer(p_id));
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::on_client_disconnected(int p_id, bool p_was_clean_close) {
|
||||
clients.erase(p_id);
|
||||
}
|
||||
|
||||
String GDScriptLanguageProtocol::process_message(const String &p_text) {
|
||||
String ret = process_string(p_text);
|
||||
if (ret.empty()) {
|
||||
return ret;
|
||||
} else {
|
||||
return format_output(ret);
|
||||
}
|
||||
}
|
||||
|
||||
String GDScriptLanguageProtocol::format_output(const String &p_text) {
|
||||
|
||||
String header = "Content-Length: ";
|
||||
CharString charstr = p_text.utf8();
|
||||
size_t len = charstr.length();
|
||||
header += itos(len);
|
||||
header += "\r\n\r\n";
|
||||
|
||||
return header + p_text;
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("initialize", "params"), &GDScriptLanguageProtocol::initialize);
|
||||
ClassDB::bind_method(D_METHOD("initialized", "params"), &GDScriptLanguageProtocol::initialized);
|
||||
ClassDB::bind_method(D_METHOD("on_data_received"), &GDScriptLanguageProtocol::on_data_received);
|
||||
ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected);
|
||||
ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected);
|
||||
}
|
||||
|
||||
Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) {
|
||||
|
||||
lsp::InitializeResult ret;
|
||||
|
||||
return ret.to_json();
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::initialized(const Variant &p_params) {
|
||||
|
||||
Dictionary params;
|
||||
params["type"] = 3;
|
||||
params["message"] = "GDScript Language Server initialized!";
|
||||
Dictionary test_message = make_notification("window/showMessage", params);
|
||||
|
||||
if (Ref<WebSocketPeer> *peer = clients.getptr(lastest_client_id)) {
|
||||
String msg = JSON::print(test_message);
|
||||
msg = format_output(msg);
|
||||
CharString charstr = msg.utf8();
|
||||
(*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::poll() {
|
||||
server->poll();
|
||||
}
|
||||
|
||||
Error GDScriptLanguageProtocol::start(int p_port) {
|
||||
if (server == NULL) {
|
||||
server = dynamic_cast<WebSocketServer *>(ClassDB::instance("WebSocketServer"));
|
||||
server->set_buffers(8192, 1024, 8192, 1024); // 8mb should be way more than enough
|
||||
server->connect("data_received", this, "on_data_received");
|
||||
server->connect("client_connected", this, "on_client_connected");
|
||||
server->connect("client_disconnected", this, "on_client_disconnected");
|
||||
}
|
||||
return server->listen(p_port);
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::stop() {
|
||||
server->stop();
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::notify_all_clients(const String &p_method, const Variant &p_params) {
|
||||
|
||||
Dictionary message = make_notification(p_method, p_params);
|
||||
String msg = JSON::print(message);
|
||||
msg = format_output(msg);
|
||||
CharString charstr = msg.utf8();
|
||||
const int *p_id = clients.next(NULL);
|
||||
while (p_id != NULL) {
|
||||
Ref<WebSocketPeer> peer = clients.get(*p_id);
|
||||
(*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
|
||||
p_id = clients.next(p_id);
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client) {
|
||||
|
||||
if (p_client == -1) {
|
||||
p_client = lastest_client_id;
|
||||
}
|
||||
|
||||
Ref<WebSocketPeer> *peer = clients.getptr(p_client);
|
||||
ERR_FAIL_COND(peer == NULL);
|
||||
|
||||
Dictionary message = make_notification(p_method, p_params);
|
||||
String msg = JSON::print(message);
|
||||
msg = format_output(msg);
|
||||
CharString charstr = msg.utf8();
|
||||
|
||||
(*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
|
||||
}
|
||||
|
||||
GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
|
||||
server = NULL;
|
||||
singleton = this;
|
||||
set_scope("textDocument", &text_document);
|
||||
set_scope("workspace", &workspace);
|
||||
workspace.root = ProjectSettings::get_singleton()->get_resource_path();
|
||||
}
|
||||
|
||||
GDScriptLanguageProtocol::~GDScriptLanguageProtocol() {
|
||||
memdelete(server);
|
||||
server = NULL;
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_language_protocol.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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 GDSCRIPT_PROTOCAL_SERVER_H
|
||||
#define GDSCRIPT_PROTOCAL_SERVER_H
|
||||
|
||||
#include "gdscript_text_document.h"
|
||||
#include "gdscript_workspace.h"
|
||||
#include "lsp.hpp"
|
||||
#include "modules/jsonrpc/jsonrpc.h"
|
||||
#include "modules/websocket/websocket_peer.h"
|
||||
#include "modules/websocket/websocket_server.h"
|
||||
|
||||
class GDScriptLanguageProtocol : public JSONRPC {
|
||||
GDCLASS(GDScriptLanguageProtocol, JSONRPC)
|
||||
|
||||
enum LSPErrorCode {
|
||||
RequestCancelled = -32800,
|
||||
ContentModified = -32801,
|
||||
};
|
||||
|
||||
static GDScriptLanguageProtocol *singleton;
|
||||
|
||||
HashMap<int, Ref<WebSocketPeer> > clients;
|
||||
WebSocketServer *server;
|
||||
int lastest_client_id;
|
||||
|
||||
GDScriptTextDocument text_document;
|
||||
GDScriptWorkspace workspace;
|
||||
|
||||
void on_data_received(int p_id);
|
||||
void on_client_connected(int p_id, const String &p_protocal);
|
||||
void on_client_disconnected(int p_id, bool p_was_clean_close);
|
||||
|
||||
String process_message(const String &p_text);
|
||||
String format_output(const String &p_text);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
Dictionary initialize(const Dictionary &p_params);
|
||||
void initialized(const Variant &p_params);
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; }
|
||||
_FORCE_INLINE_ GDScriptWorkspace &get_workspace() { return workspace; }
|
||||
|
||||
void poll();
|
||||
Error start(int p_port);
|
||||
void stop();
|
||||
|
||||
void notify_all_clients(const String &p_method, const Variant &p_params = Variant());
|
||||
void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client = -1);
|
||||
|
||||
GDScriptLanguageProtocol();
|
||||
~GDScriptLanguageProtocol();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,86 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_language_server.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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_language_server.h"
|
||||
#include "core/os/file_access.h"
|
||||
#include "core/os/os.h"
|
||||
#include "editor/editor_node.h"
|
||||
|
||||
GDScriptLanguageServer::GDScriptLanguageServer() {
|
||||
thread = NULL;
|
||||
thread_exit = false;
|
||||
_EDITOR_DEF("network/language_server/remote_port", 6008);
|
||||
}
|
||||
|
||||
void GDScriptLanguageServer::_notification(int p_what) {
|
||||
|
||||
switch (p_what) {
|
||||
case NOTIFICATION_ENTER_TREE:
|
||||
start();
|
||||
break;
|
||||
case NOTIFICATION_EXIT_TREE:
|
||||
stop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageServer::thread_main(void *p_userdata) {
|
||||
GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata);
|
||||
while (!self->thread_exit) {
|
||||
self->protocol.poll();
|
||||
OS::get_singleton()->delay_usec(10);
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageServer::start() {
|
||||
int port = (int)_EDITOR_GET("network/language_server/remote_port");
|
||||
if (protocol.start(port) == OK) {
|
||||
EditorNode::get_log()->add_message("** GDScript Language Server Started **");
|
||||
ERR_FAIL_COND(thread != NULL || thread_exit);
|
||||
thread_exit = false;
|
||||
thread = Thread::create(GDScriptLanguageServer::thread_main, this);
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageServer::stop() {
|
||||
ERR_FAIL_COND(NULL == thread || thread_exit);
|
||||
thread_exit = true;
|
||||
Thread::wait_to_finish(thread);
|
||||
memdelete(thread);
|
||||
thread = NULL;
|
||||
protocol.stop();
|
||||
EditorNode::get_log()->add_message("** GDScript Language Server Stopped **");
|
||||
}
|
||||
|
||||
void register_lsp_types() {
|
||||
ClassDB::register_class<GDScriptLanguageProtocol>();
|
||||
ClassDB::register_class<GDScriptTextDocument>();
|
||||
ClassDB::register_class<GDScriptWorkspace>();
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_language_server.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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 GDSCRIPT_LANGUAGE_SERVER_H
|
||||
#define GDSCRIPT_LANGUAGE_SERVER_H
|
||||
|
||||
#include "../gdscript_parser.h"
|
||||
#include "editor/editor_plugin.h"
|
||||
#include "gdscript_language_protocol.h"
|
||||
|
||||
class GDScriptLanguageServer : public EditorPlugin {
|
||||
GDCLASS(GDScriptLanguageServer, EditorPlugin);
|
||||
|
||||
GDScriptLanguageProtocol protocol;
|
||||
|
||||
Thread *thread;
|
||||
bool thread_exit;
|
||||
static void thread_main(void *p_userdata);
|
||||
|
||||
private:
|
||||
void _notification(int p_what);
|
||||
void _iteration();
|
||||
|
||||
public:
|
||||
Error parse_script_file(const String &p_path);
|
||||
GDScriptLanguageServer();
|
||||
void start();
|
||||
void stop();
|
||||
};
|
||||
|
||||
void register_lsp_types();
|
||||
|
||||
#endif // GDSCRIPT_LANGUAGE_SERVER_H
|
|
@ -0,0 +1,177 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_text_document.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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"
|
||||
#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);
|
||||
ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol);
|
||||
ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);
|
||||
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);
|
||||
}
|
||||
|
||||
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"]);
|
||||
print_line(doc.text);
|
||||
return doc;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
|
||||
Dictionary params = p_params["textDocument"];
|
||||
String uri = params["uri"];
|
||||
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(uri);
|
||||
Array arr;
|
||||
if (const Map<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace().scripts.find(path)) {
|
||||
Vector<lsp::SymbolInformation> list;
|
||||
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) {
|
||||
Array arr;
|
||||
|
||||
lsp::CompletionParams params;
|
||||
params.load(p_params);
|
||||
Dictionary request_data = params.to_json();
|
||||
|
||||
List<ScriptCodeCompletionOption> options;
|
||||
GDScriptLanguageProtocol::get_singleton()->get_workspace().completion(params, &options);
|
||||
|
||||
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.insertText = option.insert_text;
|
||||
item.data = request_data;
|
||||
|
||||
if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "'" || params.context.triggerCharacter == "\"") && (option.insert_text.begins_with("'") || option.insert_text.begins_with("\""))) {
|
||||
item.insertText = option.insert_text.substr(1, option.insert_text.length() - 2);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
arr.push_back(item.to_json());
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
|
||||
Dictionary params = p_params["textDocument"];
|
||||
String path = params["uri"];
|
||||
Array arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
|
||||
Array arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
Variant GDScriptTextDocument::documentLink(const Dictionary &p_params) {
|
||||
Variant ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
|
||||
Array arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
|
||||
Variant ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::sync_script_content(const String &p_uri, const String &p_content) {
|
||||
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace().get_file_path(p_uri);
|
||||
GDScriptLanguageProtocol::get_singleton()->get_workspace().parse_script(path, p_content);
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_text_document.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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 GDSCRIPT_TEXT_DOCUMENT_H
|
||||
#define GDSCRIPT_TEXT_DOCUMENT_H
|
||||
|
||||
#include "core/reference.h"
|
||||
#include "lsp.hpp"
|
||||
|
||||
class GDScriptTextDocument : public Reference {
|
||||
GDCLASS(GDScriptTextDocument, Reference)
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
void didOpen(const Variant &p_param);
|
||||
void didChange(const Variant &p_param);
|
||||
|
||||
void sync_script_content(const String &p_path, const String &p_content);
|
||||
|
||||
private:
|
||||
lsp::TextDocumentItem load_document_item(const Variant &p_param);
|
||||
|
||||
public:
|
||||
Array documentSymbol(const Dictionary &p_params);
|
||||
Array completion(const Dictionary &p_params);
|
||||
Array foldingRange(const Dictionary &p_params);
|
||||
Array codeLens(const Dictionary &p_params);
|
||||
Variant documentLink(const Dictionary &p_params);
|
||||
Array colorPresentation(const Dictionary &p_params);
|
||||
Variant hover(const Dictionary &p_params);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,153 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_workspace.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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_workspace.h"
|
||||
#include "../gdscript.h"
|
||||
#include "../gdscript_parser.h"
|
||||
#include "core/project_settings.h"
|
||||
#include "gdscript_language_protocol.h"
|
||||
|
||||
void GDScriptWorkspace::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("symbol"), &GDScriptWorkspace::symbol);
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::remove_cache_parser(const String &p_path) {
|
||||
Map<String, ExtendGDScriptParser *>::Element *parser = parse_results.find(p_path);
|
||||
Map<String, ExtendGDScriptParser *>::Element *script = scripts.find(p_path);
|
||||
if (parser && script) {
|
||||
if (script->get() && script->get() == script->get()) {
|
||||
memdelete(script->get());
|
||||
} else {
|
||||
memdelete(script->get());
|
||||
memdelete(parser->get());
|
||||
}
|
||||
parse_results.erase(p_path);
|
||||
scripts.erase(p_path);
|
||||
} else if (parser) {
|
||||
memdelete(parser->get());
|
||||
parse_results.erase(p_path);
|
||||
} else if (script) {
|
||||
memdelete(script->get());
|
||||
scripts.erase(p_path);
|
||||
}
|
||||
}
|
||||
|
||||
Array GDScriptWorkspace::symbol(const Dictionary &p_params) {
|
||||
String query = p_params["query"];
|
||||
Array arr;
|
||||
if (!query.empty()) {
|
||||
for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
|
||||
Vector<lsp::SymbolInformation> script_symbols;
|
||||
E->get()->get_symbols().symbol_tree_as_list(E->key(), script_symbols);
|
||||
for (int i = 0; i < script_symbols.size(); ++i) {
|
||||
if (query.is_subsequence_ofi(script_symbols[i].name)) {
|
||||
arr.push_back(script_symbols[i].to_json());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) {
|
||||
ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser);
|
||||
Error err = parser->parse(p_content, p_path);
|
||||
Map<String, ExtendGDScriptParser *>::Element *last_parser = parse_results.find(p_path);
|
||||
Map<String, ExtendGDScriptParser *>::Element *last_script = scripts.find(p_path);
|
||||
|
||||
if (err == OK) {
|
||||
remove_cache_parser(p_path);
|
||||
parse_results[p_path] = parser;
|
||||
scripts[p_path] = parser;
|
||||
} else {
|
||||
if (last_parser && last_script && last_parser->get() != last_script->get()) {
|
||||
memdelete(last_parser->get());
|
||||
}
|
||||
parse_results[p_path] = parser;
|
||||
}
|
||||
|
||||
publish_diagnostics(p_path);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
String GDScriptWorkspace::get_file_path(const String &p_uri) const {
|
||||
String path = p_uri.replace("file://", "").http_unescape();
|
||||
path = path.replace(root + "/", "res://");
|
||||
return ProjectSettings::get_singleton()->localize_path(path);
|
||||
}
|
||||
|
||||
String GDScriptWorkspace::get_file_uri(const String &p_path) const {
|
||||
String path = ProjectSettings::get_singleton()->globalize_path(p_path);
|
||||
return "file://" + path;
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::publish_diagnostics(const String &p_path) {
|
||||
Dictionary params;
|
||||
Array errors;
|
||||
const Map<String, ExtendGDScriptParser *>::Element *ele = parse_results.find(p_path);
|
||||
if (ele) {
|
||||
const Vector<lsp::Diagnostic> &list = ele->get()->get_diagnostics();
|
||||
errors.resize(list.size());
|
||||
for (int i = 0; i < list.size(); ++i) {
|
||||
errors[i] = list[i].to_json();
|
||||
}
|
||||
}
|
||||
params["diagnostics"] = errors;
|
||||
params["uri"] = get_file_uri(p_path);
|
||||
GDScriptLanguageProtocol::get_singleton()->notify_client("textDocument/publishDiagnostics", params);
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options) {
|
||||
String path = get_file_path(p_params.textDocument.uri);
|
||||
String call_hint;
|
||||
bool forced = false;
|
||||
if (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.find(path)) {
|
||||
String code = E->get()->get_text_for_completion(p_params.position);
|
||||
GDScriptLanguage::get_singleton()->complete_code(code, path, NULL, r_options, forced, call_hint);
|
||||
}
|
||||
}
|
||||
|
||||
GDScriptWorkspace::GDScriptWorkspace() {
|
||||
ProjectSettings::get_singleton()->get_resource_path();
|
||||
}
|
||||
|
||||
GDScriptWorkspace::~GDScriptWorkspace() {
|
||||
Set<String> cached_parsers;
|
||||
for (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.front(); E; E = E->next()) {
|
||||
cached_parsers.insert(E->key());
|
||||
}
|
||||
for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
|
||||
cached_parsers.insert(E->key());
|
||||
}
|
||||
for (Set<String>::Element *E = cached_parsers.front(); E; E = E->next()) {
|
||||
remove_cache_parser(E->get());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
/*************************************************************************/
|
||||
/* gdscript_workspace.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
||||
/* */
|
||||
/* 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 GDSCRIPT_WORKSPACE_H
|
||||
#define GDSCRIPT_WORKSPACE_H
|
||||
|
||||
#include "../gdscript_parser.h"
|
||||
#include "core/variant.h"
|
||||
#include "gdscript_extend_parser.h"
|
||||
#include "lsp.hpp"
|
||||
|
||||
class GDScriptWorkspace : public Reference {
|
||||
GDCLASS(GDScriptWorkspace, Reference);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
void remove_cache_parser(const String &p_path);
|
||||
|
||||
public:
|
||||
String root;
|
||||
Map<String, ExtendGDScriptParser *> scripts;
|
||||
Map<String, ExtendGDScriptParser *> parse_results;
|
||||
|
||||
public:
|
||||
Array symbol(const Dictionary &p_params);
|
||||
|
||||
public:
|
||||
Error parse_script(const String &p_path, const String &p_content);
|
||||
String get_file_path(const String &p_uri) const;
|
||||
String get_file_uri(const String &p_path) const;
|
||||
void publish_diagnostics(const String &p_path);
|
||||
void completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options);
|
||||
|
||||
GDScriptWorkspace();
|
||||
~GDScriptWorkspace();
|
||||
};
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
|
@ -36,6 +36,7 @@
|
|||
#include "editor/gdscript_highlighter.h"
|
||||
#include "gdscript.h"
|
||||
#include "gdscript_tokenizer.h"
|
||||
#include "language_server/gdscript_language_server.h"
|
||||
|
||||
GDScriptLanguage *script_language_gd = NULL;
|
||||
Ref<ResourceFormatLoaderGDScript> resource_loader_gd;
|
||||
|
@ -130,6 +131,8 @@ static void _editor_init() {
|
|||
Ref<EditorExportGDScript> gd_export;
|
||||
gd_export.instance();
|
||||
EditorExport::get_singleton()->add_export_plugin(gd_export);
|
||||
EditorNode::get_singleton()->add_editor_plugin(memnew(GDScriptLanguageServer));
|
||||
register_lsp_types();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue