summaryrefslogtreecommitdiff
path: root/ide-definitions.c
blob: b853207794d650de175d85aafd8e22618ccfb341 (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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// this file deals with ctrl+click "go to definition", and
// the definitions menu (Ctrl+D)

#include "ted-internal.h"

struct Definitions {
	LSPServerRequestID last_request;
	double last_request_time;
	/// last query string which we sent a request for
	char *last_request_query;
	/// for "go to definition of..." menu
	Selector *selector;
	/// an array of all definitions (gotten from workspace/symbols) for "go to definition" menu
	SymbolInfo *all_definitions;
};

void definition_cancel_lookup(Ted *ted) {
	Definitions *defs = ted->definitions;
	ted_cancel_lsp_request(ted, &defs->last_request);
}

static SymbolKind symbol_kind_to_ted(LSPSymbolKind kind) {
	switch (kind) {
	case LSP_SYMBOL_OTHER:
	case LSP_SYMBOL_FILE:
	case LSP_SYMBOL_MODULE:
	case LSB_SYMBOL_NAMESPACE:
	case LSP_SYMBOL_PACKAGE:
		return SYMBOL_OTHER;
	
	case LSP_SYMBOL_CLASS:
	case LSP_SYMBOL_TYPEPARAMETER:
	case LSP_SYMBOL_ENUM:
	case LSP_SYMBOL_INTERFACE:
	case LSP_SYMBOL_STRUCT:
	case LSP_SYMBOL_EVENT: // i have no clue what this is. let's say it's a type.
		return SYMBOL_TYPE;
	
	case LSP_SYMBOL_PROPERTY:
	case LSP_SYMBOL_FIELD:
	case LSP_SYMBOL_KEY:
		return SYMBOL_FIELD;
	
	case LSP_SYMBOL_CONSTRUCTOR:
	case LSP_SYMBOL_FUNCTION:
	case LSP_SYMBOL_OPERATOR:
	case LSP_SYMBOL_METHOD:
		return SYMBOL_FUNCTION;
	
	case LSP_SYMBOL_VARIABLE:
		return SYMBOL_VARIABLE;
	
	case LSP_SYMBOL_CONSTANT:
	case LSP_SYMBOL_STRING:
	case LSP_SYMBOL_NUMBER:
	case LSP_SYMBOL_BOOLEAN:
	case LSP_SYMBOL_ARRAY:
	case LSP_SYMBOL_OBJECT:
	case LSP_SYMBOL_ENUMMEMBER:
	case LSP_SYMBOL_NULL:
		return SYMBOL_CONSTANT;
	}
	
	return SYMBOL_OTHER;
}

void definition_goto(Ted *ted, LSP *lsp, const char *name, LSPDocumentPosition position, GotoType type) {
	Definitions *defs = ted->definitions;
	if (lsp) {
		// cancel old request
		definition_cancel_lookup(ted);
		LSPRequestType request_type = LSP_REQUEST_DEFINITION;
		switch (type) {
		case GOTO_DEFINITION:
			request_type = LSP_REQUEST_DEFINITION;
			break;
		case GOTO_DECLARATION:
			request_type = LSP_REQUEST_DECLARATION;
			break;
		case GOTO_TYPE_DEFINITION:
			request_type = LSP_REQUEST_TYPE_DEFINITION;
			break;
		case GOTO_IMPLEMENTATION:
			request_type = LSP_REQUEST_IMPLEMENTATION;
			break;
		}
		// send that request
		LSPRequest request = {.type = request_type};
		request.data.definition.position = position;
		LSPServerRequestID id = lsp_send_request(lsp, &request);
		if (id.id == 0 && request.type == LSP_REQUEST_IMPLEMENTATION) {
			// if we can't go to the implementation, try going to the definition
			request.type = LSP_REQUEST_DEFINITION;
			id = lsp_send_request(lsp, &request);
		}
		defs->last_request = id;
		defs->last_request_time = ted->frame_time;
	} else {
		// just go to the tag
		tag_goto(ted, name);
	}
}

void definitions_frame(Ted *ted) {
	Definitions *defs = ted->definitions;
	if (defs->last_request.id && ted->frame_time - defs->last_request_time > 0.2) {
		ted->cursor = ted->cursor_wait;
	}
}

static void definitions_clear_entries(Definitions *defs) {
	arr_foreach_ptr(defs->all_definitions, SymbolInfo, def) {
		free(def->name);
		free(def->detail);
	}
	arr_clear(defs->all_definitions);
	selector_clear(defs->selector);
}

static int definition_entry_qsort_cmp(const void *av, const void *bv) {
	const SymbolInfo *a = av, *b = bv;
	// first, sort by length
	size_t a_len = strlen(a->name), b_len = strlen(b->name);
	if (a_len < b_len) return -1;
	if (a_len > b_len) return 1;
	// then sort alphabetically
	int cmp = strcmp(a->name, b->name);
	if (cmp) return cmp;
	// then sort by detail
	if (!a->detail && b->detail) return -1;
	if (a->detail && !b->detail) return 1;
	if (!a->detail && !b->detail) return 0;
	return strcmp(a->detail, b->detail);
}

void definitions_process_lsp_response(Ted *ted, LSP *lsp, const LSPResponse *response) {
	Definitions *defs = ted->definitions;
	if (response->request.id != defs->last_request.id) {
		// response to an old/irrelevant request
		return;
	}
	
	defs->last_request.id = 0;
	
	switch (response->request.type) {
	case LSP_REQUEST_DEFINITION:
	case LSP_REQUEST_DECLARATION:
	case LSP_REQUEST_TYPE_DEFINITION:
	case LSP_REQUEST_IMPLEMENTATION: {
		// handle textDocument/definition or textDocument/declaration response
		const LSPResponseDefinition *response_def = &response->data.definition;
		
		if (!arr_len(response_def->locations)) {
			// no definition.
			const char* link = NULL;
			
			// check for document links
			TextBuffer *buffer = ted->active_buffer;
			if (buffer) {
				BufferPos pos = buffer_pos_from_lsp(buffer, response->request.data.definition.position.pos);
				link = document_link_at_buffer_pos(ted, pos);
				if (link) {
					open_with_default_application(link);
					return;
				}
			}
			
			// do the error cursor.
			ted_flash_error_cursor(ted);
			return;
		}
		LSPLocation location = response_def->locations[0];
		const char *path = lsp_document_path(lsp, location.document);
		if (!ted_open_file(ted, path)) {	
			ted_flash_error_cursor(ted);
			return;
		}
		LSPDocumentPosition position = lsp_location_start_position(location);
		ted_go_to_lsp_document_position(ted, lsp, position);
		} break;
	case LSP_REQUEST_WORKSPACE_SYMBOLS: {
		// handle workspace/symbol response
		const LSPResponseWorkspaceSymbols *response_syms = &response->data.workspace_symbols;
		const LSPSymbolInformation *symbols = response_syms->symbols;
		const Settings *settings = ted_active_settings(ted);
		const u32 *colors = settings->colors;
		
		definitions_clear_entries(defs);
		arr_set_len(defs->all_definitions, arr_len(symbols));
		for (size_t i = 0; i < arr_len(symbols); ++i) {
			const LSPSymbolInformation *symbol = &symbols[i];
			SymbolInfo *def = &defs->all_definitions[i];
			
			def->name = str_dup(lsp_response_string(response, symbol->name));
			SymbolKind kind = symbol_kind_to_ted(symbol->kind);
			def->color = colors[color_for_symbol_kind(kind)];
			def->from_lsp = true;
			def->position = lsp_location_start_position(symbol->location);
			const char *container_name = lsp_response_string(response, symbol->container);
			const char *filename = path_filename(lsp_document_path(lsp, def->position.document));
			bool has_container = *container_name != 0;
			def->detail = a_sprintf("%s%s%s:%" PRIu32,
				container_name,
				has_container ? ", " : "",
				filename,
				def->position.pos.line + 1);
		}
		
		} break;
	default:
		debug_println("?? bad request type in %s : %u:%u", __func__,  response->request.id, response->request.type);
		break;
	}
}

void definitions_send_request_if_needed(Ted *ted) {
	LSP *lsp = buffer_lsp(ted->prev_active_buffer);
	if (!lsp)
		return;
	Definitions *defs = ted->definitions;
	char *query = buffer_contents_utf8_alloc(ted->line_buffer);
	if (defs->last_request_query && strcmp(defs->last_request_query, query) == 0) {
		free(query);
		return; // no need to update symbols
	}
	LSPRequest request = {.type = LSP_REQUEST_WORKSPACE_SYMBOLS};
	LSPRequestWorkspaceSymbols *syms = &request.data.workspace_symbols;
	syms->query = str_dup(query);
	// cancel old request
	definition_cancel_lookup(ted);
	defs->last_request = lsp_send_request(lsp, &request);
	defs->last_request_time = ted->frame_time;
	free(defs->last_request_query);
	defs->last_request_query = query;
}

static void definitions_selector_open(Ted *ted) {
	Definitions *defs = ted->definitions;
	definitions_clear_entries(defs);
	LSP *lsp = ted->prev_active_buffer
		? buffer_lsp(ted->prev_active_buffer)
		: ted_active_lsp(ted);
	
	if (lsp) {
		definitions_send_request_if_needed(ted);
	} else {
		defs->all_definitions = tags_get_symbols(ted);
	}
	ted_switch_to_buffer(ted, ted->line_buffer);
	buffer_select_all(ted->active_buffer);
	selector_set_cursor(defs->selector, 0);
}


static bool definitions_selector_close(Ted *ted) {
	Definitions *defs = ted->definitions;
	definitions_clear_entries(defs);
	ted_cancel_lsp_request(ted, &defs->last_request);
	free(defs->last_request_query);
	defs->last_request_query = NULL;
	return true;
}

static void definitions_selector_update(Ted *ted) {
	Definitions *defs = ted->definitions;
	Selector *sel = defs->selector;
	
	// send new request if search term has changed.
	// this is needed because e.g. clangd gives an incomplete list
	definitions_send_request_if_needed(ted);
	
	char *chosen = selector_update(ted, sel);
	if (chosen) {
		// for LSP go-to-definition, we ignore `chosen` and use the cursor instead.
		// this is because a single symbol can have multiple definitions,
		// e.g. with overloading.
		SelectorEntry cursor_entry = {0};
		if (!selector_get_cursor_entry(sel, &cursor_entry)) {
			assert(0); // shouldn't happen since `chosen` is true.
			return;
		}
		
		u64 def_idx = cursor_entry.userdata;
		if (def_idx >= arr_len(defs->all_definitions)) {
			assert(0);
			return;
		}
		SymbolInfo *info = &defs->all_definitions[def_idx];
		if (info->from_lsp) {
			// NOTE: we need to get this before calling menu_close,
			// since that clears selector_all_definitions
			LSPDocumentPosition position = info->position;
			
			menu_close(ted);
			ted_go_to_lsp_document_position(ted, NULL, position);
		} else {
			menu_close(ted);
			tag_goto(ted, chosen);
		}
		free(chosen);
	}
}

static void definitions_selector_render(Ted *ted) {
	Rect bounds = selection_menu_render_bg(ted);
	Definitions *defs = ted->definitions;
	Selector *sel = defs->selector;
	selector_set_bounds(sel, bounds);
	selector_render(ted, sel);
}

void definitions_init(Ted *ted) {
	MenuInfo info = {
		.open = definitions_selector_open,
		.close = definitions_selector_close,
		.render = definitions_selector_render,
		.update = definitions_selector_update,
	};
	strbuf_cpy(info.name, MENU_GOTO_DEFINITION);
	menu_register(ted, &info);
	
	Definitions *defs = ted->definitions = ted_calloc(ted, 1, sizeof *ted->definitions);
	defs->selector = selector_new();
}

void definitions_quit(Ted *ted) {
	Definitions *defs = ted->definitions;
	selector_free(defs->selector);
	free(defs);
	ted->definitions = NULL;
}