summaryrefslogtreecommitdiff
path: root/ide-rename-symbol.c
blob: 4df948ec07e8391b4250e660a5015c898f1e6db6 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#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) {
			switch (change->type) {
			case LSP_CHANGE_EDIT: {
				LSPWorkspaceChangeEdit *change_data = &change->data.edit;
				const char *path = lsp_document_path(lsp, change_data->document);
				if (!ted_open_file(ted, path)) goto done;
				
				TextBuffer *buffer = ted_get_buffer_with_file(ted, path);
				if (!buffer->will_chain_edits) {
					// chain all edits together so they can be undone with one ctrl+z
					buffer_start_edit_chain(buffer);
				}
				
				if (!buffer) {
					// this should never happen since we just
					// successfully opened it
					assert(0);
					goto done;
				}
				
				
				LSPTextEdit *edit = &change_data->edit;
				BufferPos start = buffer_pos_from_lsp(buffer, edit->range.start);
				BufferPos end = buffer_pos_from_lsp(buffer, edit->range.end);
				buffer_delete_chars_between(buffer, start, end);
				buffer_insert_utf8_at_pos(buffer, start, lsp_response_string(response, edit->new_text));
				
				}
				break;
			case LSP_CHANGE_RENAME:
			case LSP_CHANGE_DELETE:
			case LSP_CHANGE_CREATE:
				assert(0);
				break;
			}
		}
		done:
		
		// end all edit chains in all buffers
		// they're almost definitely all created by us
		for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) {
			if (ted->buffers_used[i]) {
				TextBuffer *buffer = &ted->buffers[i];
				buffer_end_edit_chain(buffer);
			}
		}
	}
	
	rename_symbol_clear(ted);
}