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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
#include "ted-internal.h"
struct RenameSymbol {
LSPServerRequestID request_id;
};
static void rename_symbol_clear(Ted *ted) {
RenameSymbol *rs = ted->rename_symbol;
ted_cancel_lsp_request(ted, &rs->request_id);
}
void rename_symbol_quit(Ted *ted) {
rename_symbol_clear(ted);
free(ted->rename_symbol);
ted->rename_symbol = NULL;
}
void rename_symbol_at_cursor(Ted *ted, TextBuffer *buffer, const char *new_name) {
if (!buffer) return;
RenameSymbol *rs = ted->rename_symbol;
LSP *lsp = buffer_lsp(buffer);
if (!lsp) return;
if (!rs->request_id.id) {
// send the request
LSPRequest request = {.type = LSP_REQUEST_RENAME};
LSPRequestRename *data = &request.data.rename;
data->position = buffer_cursor_pos_as_lsp_document_position(buffer);
data->new_name = str_dup(new_name);
rs->request_id = lsp_send_request(lsp, &request);
}
}
void rename_symbol_frame(Ted *ted) {
RenameSymbol *rs = ted->rename_symbol;
if (rs->request_id.id) {
// we're just waitin'
ted->cursor = ted->cursor_wait;
}
}
static void rename_symbol_menu_open(Ted *ted) {
ted_switch_to_buffer(ted, ted->line_buffer);
}
static void rename_symbol_menu_update(Ted *ted) {
TextBuffer *line_buffer = ted->line_buffer;
if (line_buffer_is_submitted(line_buffer)) {
char *new_name = str32_to_utf8_cstr(buffer_get_line(line_buffer, 0));
rename_symbol_at_cursor(ted, ted->prev_active_buffer, new_name);
free(new_name);
}
}
static void rename_symbol_menu_render(Ted *ted) {
RenameSymbol *rs = ted->rename_symbol;
// highlight symbol
TextBuffer *buffer = ted->prev_active_buffer;
if (!buffer) {
menu_close(ted);
return;
}
if (rs->request_id.id) {
// already entered a new name
return;
}
const Settings *settings = buffer_settings(buffer);
const float padding = settings->padding;
const u32 *colors = settings->colors;
const float line_buffer_height = ted_line_buffer_height(ted);
u32 sym_start=0, sym_end=0;
BufferPos cursor_pos = buffer_cursor_pos(buffer);
buffer_word_span_at_pos(buffer, cursor_pos, &sym_start, &sym_end);
BufferPos bpos0 = {
.line = cursor_pos.line,
.index = sym_start
};
BufferPos bpos1 = {
.line = cursor_pos.line,
.index = sym_end
};
// symbol should span from pos0 to pos1
vec2 p0 = buffer_pos_to_pixels(buffer, bpos0);
vec2 p1 = buffer_pos_to_pixels(buffer, bpos1);
p1.y += text_font_char_height(buffer_font(buffer));
Rect highlight = rect_endpoints(p0, p1);
gl_geometry_rect_border(highlight, settings->border_thickness, colors[COLOR_BORDER]);
gl_geometry_rect(highlight, colors[COLOR_HOVER_HL]);
const float width = ted_get_menu_width(ted);
const float height = line_buffer_height + 2 * padding;
Rect bounds = {
.pos = {(ted->window_width - width) / 2, padding},
.size = {width, height},
};
gl_geometry_rect(bounds, colors[COLOR_MENU_BG]);
gl_geometry_rect_border(bounds, settings->border_thickness, colors[COLOR_BORDER]);
gl_geometry_draw();
rect_shrink(&bounds, padding);
const char *text = "Rename symbol to...";
text_utf8(ted->font_bold, text, bounds.pos.x, bounds.pos.y, colors[COLOR_TEXT]);
rect_shrink_left(&bounds, text_get_size_vec2(ted->font_bold, text).x + padding);
text_render(ted->font_bold);
buffer_render(ted->line_buffer, bounds);
}
static bool rename_symbol_menu_close(Ted *ted) {
rename_symbol_clear(ted);
buffer_clear(ted->line_buffer);
return true;
}
void rename_symbol_process_lsp_response(Ted *ted, const LSPResponse *response) {
RenameSymbol *rs = ted->rename_symbol;
if (response->request.type != LSP_REQUEST_RENAME
|| response->request.id != rs->request_id.id) {
return;
}
if (response->error) {
ted_error(ted, "%s", response->error);
goto cleanup;
}
const LSPResponseRename *data = &response->data.rename;
LSP *lsp = ted_get_lsp_by_id(ted, rs->request_id.lsp);
if (!lsp) {
// LSP crashed or something
goto cleanup;
}
arr_foreach_ptr(data->changes, const LSPWorkspaceChange, change) {
if (change->type == LSP_CHANGE_DELETE && change->data.delete.recursive) {
ted_error(ted, "refusing to perform rename because it involves a recurisve deletion\n"
"I'm too scared to go through with this");
goto cleanup;
}
}
arr_foreach_ptr(data->changes, const LSPWorkspaceChange, change) {
switch (change->type) {
case LSP_CHANGE_EDIT: {
const 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);
// 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;
}
const 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: {
const LSPWorkspaceChangeRename *rename = &change->data.rename;
const char *old = lsp_document_path(lsp, rename->old);
const char *new = lsp_document_path(lsp, rename->new);
FsType new_type = fs_path_type(new);
if (new_type == FS_DIRECTORY) {
ted_error(ted, "Aborting rename since it's asking to overwrite a directory.");
goto done;
}
if (rename->ignore_if_exists && new_type != FS_NON_EXISTENT) {
break;
}
if (!rename->overwrite && new_type != FS_NON_EXISTENT) {
ted_error(ted, "Aborting rename since it would overwrite a file.");
goto done;
}
os_rename_overwrite(old, new);
if (ted_close_buffer_with_file(ted, old))
ted_open_file(ted, new);
} break;
case LSP_CHANGE_DELETE: {
const LSPWorkspaceChangeDelete *delete = &change->data.delete;
const char *path = lsp_document_path(lsp, delete->document);
remove(path);
ted_close_buffer_with_file(ted, path);
} break;
case LSP_CHANGE_CREATE: {
const LSPWorkspaceChangeCreate *create = &change->data.create;
const char *path = lsp_document_path(lsp, create->document);
FILE *fp = fopen(path, create->overwrite ? "wb" : "ab");
if (fp) fclose(fp);
ted_open_file(ted, path);
} break;
}
}
done:
{
// end all edit chains in all buffers
// they're almost definitely all created by us
arr_foreach_ptr(ted->buffers, TextBufferPtr, pbuffer) {
buffer_end_edit_chain(*pbuffer);
}
ted_save_all(ted);
}
cleanup:
rename_symbol_clear(ted);
if (menu_is_open(ted, MENU_RENAME_SYMBOL))
menu_close(ted);
}
void rename_symbol_init(Ted *ted) {
ted->rename_symbol = calloc(1, sizeof *ted->rename_symbol);
MenuInfo menu = {
.open = rename_symbol_menu_open,
.close = rename_symbol_menu_close,
.update = rename_symbol_menu_update,
.render = rename_symbol_menu_render,
};
strbuf_cpy(menu.name, MENU_RENAME_SYMBOL);
menu_register(ted, &menu);
}
|