summaryrefslogtreecommitdiff
path: root/ide-rename-symbol.c
blob: ee2f6e1eb681cda98684adfa528d9e236703a2f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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);
}