summaryrefslogtreecommitdiff
path: root/src/main/lsp/rename.c
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2024-09-17 08:47:03 +0200
committerAlbert Cervin <albert@acervin.com>2025-11-01 22:11:14 +0100
commit4459b8b3aa9d73895391785a99dcc87134e80601 (patch)
treea5204f447a0b2b05f63504c7fe958ef9bbf1918a /src/main/lsp/rename.c
parent4689f3f38277bb64981fc960e8e384e2d065d659 (diff)
downloaddged-4459b8b3aa9d73895391785a99dcc87134e80601.tar.gz
dged-4459b8b3aa9d73895391785a99dcc87134e80601.tar.xz
dged-4459b8b3aa9d73895391785a99dcc87134e80601.zip
More lsp support
This makes the LSP support complete for now: - Completion - Diagnostics - Goto implementation/declaration - Rename - Documentation - Find references
Diffstat (limited to 'src/main/lsp/rename.c')
-rw-r--r--src/main/lsp/rename.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/main/lsp/rename.c b/src/main/lsp/rename.c
new file mode 100644
index 0000000..6adc9a1
--- /dev/null
+++ b/src/main/lsp/rename.c
@@ -0,0 +1,61 @@
+#include "rename.h"
+
+#include "dged/buffer.h"
+#include "dged/buffer_view.h"
+#include "dged/minibuffer.h"
+#include "dged/window.h"
+
+#include "lsp.h"
+
+static void handle_rename_response(struct lsp_server *server,
+ struct lsp_response *response,
+ void *userdata) {
+ (void)userdata;
+ if (response->value.result.type == Json_Null) {
+ minibuffer_echo_timeout(4, "rename: no edits");
+ return;
+ }
+
+ struct workspace_edit edit =
+ workspace_edit_from_json(&response->value.result);
+ apply_edits(server, &edit);
+ workspace_edit_free(&edit);
+}
+
+void lsp_rename(struct lsp_server *server, struct buffer *buffer,
+ struct location location, struct s8 new_name) {
+ uint64_t id = new_pending_request(server, handle_rename_response, NULL);
+ struct versioned_text_document_identifier doc =
+ versioned_identifier_from_buffer(buffer);
+
+ struct rename_params params = {
+ .position.uri = doc.uri,
+ .position.position = location,
+ .new_name = new_name,
+ };
+
+ struct s8 json_payload = rename_params_to_json(&params);
+ lsp_send(lsp_backend(server),
+ lsp_create_request(id, s8("textDocument/rename"), json_payload));
+
+ versioned_text_document_identifier_free(&doc);
+ s8delete(json_payload);
+}
+
+int32_t lsp_rename_cmd(struct command_ctx ctx, int argc, const char **argv) {
+ if (argc == 0) {
+ return minibuffer_prompt(ctx, "rename to: ");
+ }
+
+ struct buffer_view *bv = window_buffer_view(ctx.active_window);
+ struct lsp_server *server = lsp_server_for_lang_id(bv->buffer->lang.id);
+ if (server == NULL) {
+ minibuffer_echo_timeout(4, "no lsp server associated with %s",
+ bv->buffer->name);
+ return 0;
+ }
+
+ lsp_rename(server, bv->buffer, bv->dot, s8(argv[0]));
+
+ return 0;
+}