summaryrefslogtreecommitdiff
path: root/ide-definitions.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-30 12:42:32 -0500
committerpommicket <pommicket@gmail.com>2022-12-30 12:42:32 -0500
commit66367583a3b57e37e9198838222f45d99e77d8ae (patch)
tree74764837b87f3d25e726265dbf3d6ae7cede3b7f /ide-definitions.c
parent65cbe00bc124bdc1bd83bcd42ad5fbed521412b2 (diff)
definitions menu, but it's acting weird
Diffstat (limited to 'ide-definitions.c')
-rw-r--r--ide-definitions.c128
1 files changed, 108 insertions, 20 deletions
diff --git a/ide-definitions.c b/ide-definitions.c
index e63e977..a9ff945 100644
--- a/ide-definitions.c
+++ b/ide-definitions.c
@@ -51,6 +51,7 @@ void definition_goto(Ted *ted, LSP *lsp, const char *name, LSPDocumentPosition p
LSPRequest request = {.type = LSP_REQUEST_DEFINITION};
request.data.definition.position = position;
LSPRequestID id = lsp_send_request(lsp, &request);
+ // @TODO : cancel old request
defs->last_request_id = id;
defs->last_request_time = ted->frame_time;
} else {
@@ -64,38 +65,125 @@ void definition_cancel_lookup(Ted *ted) {
defs->last_request_id = 0;
}
+
+void definitions_frame(Ted *ted) {
+ Definitions *defs = &ted->definitions;
+ if (defs->last_request_id && timespec_sub(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->selector_all_entries, SelectorEntry, entry) {
+ free((char*)entry->name);
+ }
+ arr_clear(defs->selector_all_entries);
+ arr_clear(defs->selector.entries);
+ defs->selector.n_entries = 0;
+}
+
+
void definitions_process_lsp_response(Ted *ted, LSP *lsp, const LSPResponse *response) {
- if (response->request.type != LSP_REQUEST_DEFINITION)
- return;
-
- const LSPResponseDefinition *response_def = &response->data.definition;
Definitions *defs = &ted->definitions;
-
if (response->request.id != defs->last_request_id) {
- // response to an old request
+ // response to an old/irrelevant request
return;
}
defs->last_request_id = 0;
- if (!arr_len(response_def->locations)) {
- // no definition. do the error cursor.
- ted_flash_error_cursor(ted);
- return;
+ switch (response->request.type) {
+ case LSP_REQUEST_DEFINITION: {
+ const LSPResponseDefinition *response_def = &response->data.definition;
+
+ if (!arr_len(response_def->locations)) {
+ // no definition. 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: {
+ 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->selector_all_entries, arr_len(symbols));
+ for (size_t i = 0; i < arr_len(symbols); ++i) {
+ const LSPSymbolInformation *symbol = &symbols[i];
+ SelectorEntry *entry = &defs->selector_all_entries[i];
+
+ entry->name = str_dup(lsp_response_string(response, symbol->name));
+ SymbolKind kind = symbol_kind_to_ted(symbol->kind);
+ entry->color = colors[color_for_symbol_kind(kind)];
+ }
+
+ } break;
+ default:
+ debug_println("?? bad request type in %s", __func__);
+ break;
}
- 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;
+}
+
+void definitions_selector_open(Ted *ted) {
+ Definitions *defs = &ted->definitions;
+ definitions_clear_entries(defs);
+ LSP *lsp = buffer_lsp(ted->prev_active_buffer);
+ if (lsp) {
+ LSPRequest request = {.type = LSP_REQUEST_WORKSPACE_SYMBOLS};
+ LSPRequestWorkspaceSymbols *syms = &request.data.workspace_symbols;
+ syms->query = str_dup("");
+ defs->last_request_id = lsp_send_request(lsp, &request);
+ defs->last_request_time = ted->frame_time;
+ } else {
+ defs->selector_all_entries = tags_get_entries(ted);
}
- LSPDocumentPosition position = lsp_location_start_position(location);
- ted_go_to_lsp_document_position(ted, lsp, position);
+ ted_switch_to_buffer(ted, &ted->line_buffer);
+ buffer_select_all(ted->active_buffer);
+ defs->selector.cursor = 0;
}
-void definitions_frame(Ted *ted) {
+
+void definitions_selector_close(Ted *ted) {
Definitions *defs = &ted->definitions;
- if (defs->last_request_id && timespec_sub(ted->frame_time, defs->last_request_time) > 0.2) {
- ted->cursor = ted->cursor_wait;
+ definitions_clear_entries(defs);
+ // @TODO : cancel
+ defs->last_request_id = 0;
+}
+
+char *definitions_selector_update(Ted *ted) {
+ Definitions *defs = &ted->definitions;
+ Selector *sel = &defs->selector;
+ sel->enable_cursor = true;
+
+ // create selector entries based on search term
+ char *search_term = str32_to_utf8_cstr(buffer_get_line(&ted->line_buffer, 0));
+
+ arr_clear(sel->entries);
+
+ arr_foreach_ptr(defs->selector_all_entries, SelectorEntry, entry) {
+ if (!search_term || stristr(entry->name, search_term)) {
+ arr_add(sel->entries, *entry);
+ }
}
+
+ sel->n_entries = arr_len(sel->entries);
+
+ return selector_update(ted, sel);
+}
+
+void definitions_selector_render(Ted *ted, Rect bounds) {
+ Definitions *defs = &ted->definitions;
+ Selector *sel = &defs->selector;
+ sel->bounds = bounds;
+ selector_render(ted, sel);
}