summaryrefslogtreecommitdiff
path: root/ide-rename-symbol.c
diff options
context:
space:
mode:
Diffstat (limited to 'ide-rename-symbol.c')
-rw-r--r--ide-rename-symbol.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/ide-rename-symbol.c b/ide-rename-symbol.c
new file mode 100644
index 0000000..ee2f6e1
--- /dev/null
+++ b/ide-rename-symbol.c
@@ -0,0 +1,91 @@
+#include "ted.h"
+
+void rename_symbol_clear(Ted *ted) {
+ RenameSymbol *rs = &ted->rename_symbol;
+ ted_cancel_lsp_request(ted, &rs->request_id);
+ free(rs->new_name);
+ rs->new_name = NULL;
+ if (ted->menu == MENU_RENAME_SYMBOL)
+ menu_close(ted);
+}
+
+void rename_symbol_frame(Ted *ted) {
+ RenameSymbol *rs = &ted->rename_symbol;
+ TextBuffer *buffer = ted->prev_active_buffer;
+ LSP *lsp = buffer ? buffer_lsp(buffer) : NULL;
+
+ if (ted->menu != MENU_RENAME_SYMBOL || !buffer || !lsp) {
+ rename_symbol_clear(ted);
+ return;
+ }
+
+ if (rs->new_name && !rs->request_id.id) {
+ // send the request
+ LSPRequest request = {.type = LSP_REQUEST_RENAME};
+ LSPRequestRename *data = &request.data.rename;
+ data->position = buffer_pos_to_lsp_document_position(buffer, buffer->cursor_pos);
+ data->new_name = str_dup(rs->new_name);
+ rs->request_id = lsp_send_request(lsp, &request);
+ }
+
+ // we're just waitin'
+ ted->cursor = ted->cursor_wait;
+}
+
+void rename_symbol_process_lsp_response(Ted *ted, LSPResponse *response) {
+ RenameSymbol *rs = &ted->rename_symbol;
+ if (response->request.type != LSP_REQUEST_RENAME
+ || response->request.id != rs->request_id.id) {
+ return;
+ }
+
+ LSPResponseRename *data = &response->data.rename;
+ LSP *lsp = ted_get_lsp_by_id(ted, rs->request_id.lsp);
+ if (!lsp) {
+ // LSP crashed or something
+ return;
+ }
+
+ assert(rs->new_name);
+
+ bool perform_changes = true;
+ arr_foreach_ptr(data->changes, LSPWorkspaceChange, change) {
+ if (change->type != LSP_CHANGE_EDIT) {
+ // TODO(eventually) : support these
+ ted_error(ted, "rename is too complicated for ted to perform.");
+ perform_changes = false;
+ }
+ }
+
+ if (perform_changes) {
+ arr_foreach_ptr(data->changes, LSPWorkspaceChange, change) {
+ printf("change type %u\n",change->type);
+ switch (change->type) {
+ case LSP_CHANGE_EDIT: {
+ LSPWorkspaceChangeEdit *edit = &change->data.edit;
+ const char *path = lsp_document_path(lsp, edit->document);
+ if (!ted_open_file(ted, path)) goto done;
+
+ TextBuffer *buffer = ted_get_buffer_with_file(ted, path);
+ if (!buffer) {
+ // this should never happen since we just
+ // successfully opened it
+ assert(0);
+ goto done;
+ }
+ printf("%s\n",path);
+
+ }
+ break;
+ case LSP_CHANGE_RENAME:
+ case LSP_CHANGE_DELETE:
+ case LSP_CHANGE_CREATE:
+ assert(0);
+ break;
+ }
+ }
+ done:;
+ }
+
+ rename_symbol_clear(ted);
+}