From b45acfb66cd4c4474d3fd951d2a1b0fe3b63ae0b Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 31 Dec 2022 12:12:07 -0500 Subject: better LSP request cancelling --- ide-autocomplete.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'ide-autocomplete.c') diff --git a/ide-autocomplete.c b/ide-autocomplete.c index e5dd848..76871af 100644 --- a/ide-autocomplete.c +++ b/ide-autocomplete.c @@ -76,8 +76,9 @@ void autocomplete_close(Ted *ted) { Autocomplete *ac = &ted->autocomplete; if (ac->open) { ac->open = false; - ac->waiting_for_lsp = false; autocomplete_clear_completions(ted); + ted_cancel_lsp_request(ted, ac->last_request_lsp, ac->last_request_id); + ac->last_request_id = 0; } } @@ -110,6 +111,8 @@ static void autocomplete_send_completion_request(Ted *ted, TextBuffer *buffer, B LSP *lsp = buffer_lsp(buffer); Autocomplete *ac = &ted->autocomplete; + ted_cancel_lsp_request(ted, ac->last_request_lsp, ac->last_request_id); + LSPRequest request = { .type = LSP_REQUEST_COMPLETION }; @@ -132,9 +135,10 @@ static void autocomplete_send_completion_request(Ted *ted, TextBuffer *buffer, B }; if (trigger < UNICODE_CODE_POINTS) unicode_utf32_to_utf8(request.data.completion.context.trigger_character, trigger); - if (lsp_send_request(lsp, &request)) { - ac->waiting_for_lsp = true; - ac->lsp_request_time = ted->frame_time; + ac->last_request_id = lsp_send_request(lsp, &request); + if (ac->last_request_id) { + ac->last_request_lsp = lsp->id; + ac->last_request_time = ted->frame_time; // *technically sepaking* this can mess things up if a complete // list arrives only after the user has typed some stuff // (in that case we'll send a TriggerKind = incomplete request even though it makes no sense). @@ -238,14 +242,14 @@ static void autocomplete_process_lsp_response(Ted *ted, const LSPResponse *respo const LSPRequest *request = &response->request; if (request->type != LSP_REQUEST_COMPLETION) return; - Autocomplete *ac = &ted->autocomplete; - ac->waiting_for_lsp = false; + if (request->id != ac->last_request_id) + return; // old request + ac->last_request_id = 0; if (!ac->open) { // user hit escape or down or something before completions arrived. return; } - const LSPResponseCompletion *completion = &response->data.completion; size_t ncompletions = arr_len(completion->items); @@ -350,16 +354,17 @@ static void autocomplete_frame(Ted *ted) { autocomplete_find_completions(ted, TRIGGER_INCOMPLETE); size_t ncompletions = arr_len(ac->suggested); + bool waiting_for_lsp = ac->last_request_id != 0; - if (ac->waiting_for_lsp && ncompletions == 0) { + if (waiting_for_lsp && ncompletions == 0) { double now = ted->frame_time; - if (now - ac->lsp_request_time < 0.2) { + if (now - ac->last_request_time < 0.2) { // don't show "Loading..." unless we've actually been loading for a bit of time return; } } - if (!ac->waiting_for_lsp && ncompletions == 0) { + if (!waiting_for_lsp && ncompletions == 0) { // no completions. close menu. autocomplete_close(ted); return; @@ -373,7 +378,7 @@ static void autocomplete_frame(Ted *ted) { float menu_width = 400, menu_height = (float)ncompletions_visible * char_height; - if (ac->waiting_for_lsp && ncompletions == 0) { + if (waiting_for_lsp && ncompletions == 0) { menu_height = 200.f; } @@ -468,7 +473,7 @@ static void autocomplete_frame(Ted *ted) { state.min_x = x + padding; state.min_y = y; state.max_x = x + menu_width - padding; state.max_y = y + menu_height; rgba_u32_to_floats(colors[COLOR_TEXT], state.color); - if (ac->waiting_for_lsp && ncompletions == 0) { + if (waiting_for_lsp && ncompletions == 0) { state.x = x + padding; state.y = y; text_utf8_with_state(font, &state, "Loading..."); } else { -- cgit v1.2.3