You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-05 12:10:55 +00:00
Make LSP send applyEdit to connect editor signals
This commit is contained in:
@@ -49,6 +49,55 @@ void GDScriptWorkspace::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_file_uri", "path"), &GDScriptWorkspace::get_file_uri);
|
||||
ClassDB::bind_method(D_METHOD("publish_diagnostics", "path"), &GDScriptWorkspace::publish_diagnostics);
|
||||
ClassDB::bind_method(D_METHOD("generate_script_api", "path"), &GDScriptWorkspace::generate_script_api);
|
||||
ClassDB::bind_method(D_METHOD("apply_new_signal", "obj", "function", "args"), &GDScriptWorkspace::apply_new_signal);
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::apply_new_signal(Object *obj, String function, PoolStringArray args) {
|
||||
String function_signature = "func " + function;
|
||||
Ref<Script> script = obj->get_script();
|
||||
|
||||
String source = script->get_source_code();
|
||||
|
||||
if (source.find(function_signature) != -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
int first_class = source.find("\nclass ");
|
||||
int start_line = 0;
|
||||
if (first_class != -1) {
|
||||
start_line = source.substr(0, first_class).split("\n").size();
|
||||
} else {
|
||||
start_line = source.split("\n").size();
|
||||
}
|
||||
|
||||
String function_body = "\n\n" + function_signature + "(";
|
||||
for (int i = 0; i < args.size(); ++i) {
|
||||
function_body += args[i];
|
||||
if (i < args.size() - 1) {
|
||||
function_body += ", ";
|
||||
}
|
||||
}
|
||||
function_body += ")";
|
||||
if (EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints")) {
|
||||
function_body += " -> void";
|
||||
}
|
||||
function_body += ":\n\tpass # Replace with function body.\n";
|
||||
|
||||
lsp::TextEdit text_edit;
|
||||
|
||||
if (first_class != -1) {
|
||||
function_body += "\n\n";
|
||||
}
|
||||
text_edit.range.end.line = text_edit.range.start.line = start_line;
|
||||
|
||||
text_edit.newText = function_body;
|
||||
|
||||
String uri = get_file_uri(script->get_path());
|
||||
|
||||
lsp::ApplyWorkspaceEditParams params;
|
||||
params.edit.add_edit(uri, text_edit);
|
||||
|
||||
GDScriptLanguageProtocol::get_singleton()->request_client("workspace/applyEdit", params.to_json());
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::did_delete_files(const Dictionary &p_params) {
|
||||
@@ -360,6 +409,9 @@ Error GDScriptWorkspace::initialize() {
|
||||
}
|
||||
}
|
||||
|
||||
EditorNode *editor_node = EditorNode::get_singleton();
|
||||
editor_node->connect("script_add_function_request", this, "apply_new_signal");
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user