From da966bf4cc547c784c1631451eb2febf5cf75b13 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 5 Aug 2023 15:25:02 -0400 Subject: more internalization --- ide-highlights.c | 24 ++++++++++++++++---- ide-hover.c | 37 +++++++++++++++++++++++++++---- ide-rename-symbol.c | 63 ++++++++++++++++++++++++++++++++++------------------- main.c | 11 ++++++---- menu.c | 15 +++++++------ ted.h | 57 +++++++++++++++++++++++------------------------- 6 files changed, 135 insertions(+), 72 deletions(-) diff --git a/ide-highlights.c b/ide-highlights.c index a2d149f..ae79053 100644 --- a/ide-highlights.c +++ b/ide-highlights.c @@ -2,8 +2,24 @@ #include "ted.h" +struct Highlights { + LSPServerRequestID last_request; + LSPDocumentPosition requested_position; + LSPHighlight *highlights; +}; + +void highlights_init(Ted *ted) { + ted->highlights = calloc(1, sizeof *ted->highlights); +} + +void highlights_quit(Ted *ted) { + highlights_close(ted); + free(ted->highlights); + ted->highlights = NULL; +} + void highlights_close(Ted *ted) { - Highlights *hls = &ted->highlights; + Highlights *hls = ted->highlights; arr_clear(hls->highlights); ted_cancel_lsp_request(ted, &hls->last_request); hls->requested_position = (LSPDocumentPosition){0}; @@ -11,7 +27,7 @@ void highlights_close(Ted *ted) { static void highlights_send_request(Ted *ted) { TextBuffer *buffer = ted->active_buffer; - Highlights *hls = &ted->highlights; + Highlights *hls = ted->highlights; if (!buffer) { highlights_close(ted); return; @@ -32,7 +48,7 @@ static void highlights_send_request(Ted *ted) { void highlights_process_lsp_response(Ted *ted, const LSPResponse *response) { - Highlights *hls = &ted->highlights; + Highlights *hls = ted->highlights; if (response->request.type != LSP_REQUEST_HIGHLIGHT) return; // not a highlight request if (response->request.id != hls->last_request.id) @@ -45,7 +61,7 @@ void highlights_process_lsp_response(Ted *ted, const LSPResponse *response) { } void highlights_frame(Ted *ted) { - Highlights *hls = &ted->highlights; + Highlights *hls = ted->highlights; TextBuffer *buffer = ted->active_buffer; if (!buffer) { highlights_close(ted); diff --git a/ide-hover.c b/ide-hover.c index d0f99c1..91eb185 100644 --- a/ide-hover.c +++ b/ide-hover.c @@ -2,14 +2,43 @@ #include "ted.h" +struct Hover { + LSPServerRequestID last_request; + /// is some hover info being displayed? + bool open; + /// text to display + char *text; + /// where the hover data is coming from. + /// we use this to check if we need to refresh it. + LSPDocumentPosition requested_position; + /// range in document to highlight + LSPRange range; + /// how long the cursor has been hovering for + double time; +}; + +void hover_init(Ted *ted) { + ted->hover = calloc(1, sizeof *ted->hover); +} + +void hover_quit(Ted *ted) { + hover_close(ted); + free(ted->hover); + ted->hover = NULL; +} + void hover_close(Ted *ted) { - Hover *hover = &ted->hover; + Hover *hover = ted->hover; hover->open = false; free(hover->text); hover->text = NULL; ted_cancel_lsp_request(ted, &hover->last_request); } +void hover_reset_timer(Ted *ted) { + ted->hover->time = 0.0; +} + static bool get_hover_position(Ted *ted, LSPDocumentPosition *pos, TextBuffer **pbuffer, LSP **lsp) { BufferPos mouse_pos = {0}; TextBuffer *buffer = NULL; @@ -26,7 +55,7 @@ static bool get_hover_position(Ted *ted, LSPDocumentPosition *pos, TextBuffer ** } static void hover_send_request(Ted *ted) { - Hover *hover = &ted->hover; + Hover *hover = ted->hover; ted_cancel_lsp_request(ted, &hover->last_request); LSPRequest request = {.type = LSP_REQUEST_HOVER}; @@ -42,7 +71,7 @@ void hover_process_lsp_response(Ted *ted, const LSPResponse *response) { if (!response) return; if (response->request.type != LSP_REQUEST_HOVER) return; - Hover *hover = &ted->hover; + Hover *hover = ted->hover; if (response->request.id != hover->last_request.id) { // stale request return; @@ -87,7 +116,7 @@ void hover_frame(Ted *ted, double dt) { const Settings *settings = ted_active_settings(ted); if (!settings->hover_enabled) return; - Hover *hover = &ted->hover; + Hover *hover = ted->hover; bool key_down = ted_is_key_combo_down(ted, settings->hover_key); diff --git a/ide-rename-symbol.c b/ide-rename-symbol.c index 88ae5bd..cef2005 100644 --- a/ide-rename-symbol.c +++ b/ide-rename-symbol.c @@ -1,39 +1,55 @@ #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); +struct RenameSymbol { + LSPServerRequestID request_id; +}; + +void rename_symbol_init(Ted *ted) { + ted->rename_symbol = calloc(1, sizeof *ted->rename_symbol); } -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; - } +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->new_name && !rs->request_id.id) { + if (!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); + data->new_name = str_dup(new_name); rs->request_id = lsp_send_request(lsp, &request); } - // we're just waitin' - ted->cursor = ted->cursor_wait; +} + +bool rename_symbol_is_loading(Ted *ted) { + return ted->rename_symbol->request_id.id != 0; +} + +void rename_symbol_clear(Ted *ted) { + RenameSymbol *rs = ted->rename_symbol; + ted_cancel_lsp_request(ted, &rs->request_id); +} + +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; + } } void rename_symbol_process_lsp_response(Ted *ted, const LSPResponse *response) { - RenameSymbol *rs = &ted->rename_symbol; + RenameSymbol *rs = ted->rename_symbol; if (response->request.type != LSP_REQUEST_RENAME || response->request.id != rs->request_id.id) { return; @@ -46,8 +62,6 @@ void rename_symbol_process_lsp_response(Ted *ted, const LSPResponse *response) { return; } - assert(rs->new_name); - bool perform_changes = true; arr_foreach_ptr(data->changes, const LSPWorkspaceChange, change) { if (change->type != LSP_CHANGE_EDIT) { @@ -108,4 +122,7 @@ void rename_symbol_process_lsp_response(Ted *ted, const LSPResponse *response) { } rename_symbol_clear(ted); + printf("aeiou\n"); + if (ted->menu == MENU_RENAME_SYMBOL) + menu_close(ted); } diff --git a/main.c b/main.c index cae3b05..93d8a00 100644 --- a/main.c +++ b/main.c @@ -503,6 +503,9 @@ int main(int argc, char **argv) { autocomplete_init(ted); signature_help_init(ted); usages_init(ted); + highlights_init(ted); + hover_init(ted); + rename_symbol_init(ted); PROFILE_TIME(gl_end) @@ -732,7 +735,7 @@ int main(int argc, char **argv) { buffer_pixels_to_pos(ted->drag_buffer, Vec2(x, y), &pos); buffer_select_to_pos(ted->drag_buffer, pos); } - ted->hover.time = 0.0; + hover_reset_timer(ted); } break; case SDL_KEYDOWN: { SDL_Keycode keycode = event.key.keysym.sym; @@ -1185,13 +1188,13 @@ int main(int argc, char **argv) { build_stop(ted); if (ted->menu) menu_close(ted); - hover_close(ted); + hover_quit(ted); signature_help_quit(ted); autocomplete_quit(ted); + highlights_quit(ted); usages_quit(ted); - highlights_close(ted); session_write(ted); - rename_symbol_clear(ted); + rename_symbol_quit(ted); document_link_clear(ted); for (int i = 0; i < TED_LSP_MAX; ++i) { diff --git a/menu.c b/menu.c index 026913d..70d9e3a 100644 --- a/menu.c +++ b/menu.c @@ -42,6 +42,7 @@ static void menu_close_with_next(Ted *ted, Menu next) { buffer_clear(&ted->line_buffer); break; case MENU_RENAME_SYMBOL: + rename_symbol_clear(ted); buffer_clear(&ted->line_buffer); break; } @@ -326,12 +327,13 @@ void menu_update(Ted *ted) { build_start_with_command(ted, command); } break; - case MENU_RENAME_SYMBOL: { - RenameSymbol *rs = &ted->rename_symbol; - if (line_buffer->line_buffer_submitted && !rs->new_name) { - rs->new_name = str32_to_utf8_cstr(buffer_get_line(line_buffer, 0)); + case MENU_RENAME_SYMBOL: + if (line_buffer->line_buffer_submitted) { + 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); } - } break; + break; } } @@ -469,8 +471,7 @@ void menu_render(Ted *ted) { menu_close(ted); return; } - RenameSymbol *rs = &ted->rename_symbol; - if (rs->new_name) { + if (rename_symbol_is_loading(ted)) { // already entered a new name return; } diff --git a/ted.h b/ted.h index cee978d..6524a25 100644 --- a/ted.h +++ b/ted.h @@ -630,26 +630,10 @@ typedef struct { } DocumentLinks; /// information for symbol rename (LSP) -typedef struct { - char *new_name; - LSPServerRequestID request_id; -} RenameSymbol; +typedef struct RenameSymbol RenameSymbol; /// "hover" information from LSP server -typedef struct { - LSPServerRequestID last_request; - /// is some hover info being displayed? - bool open; - /// text to display - char *text; - /// where the hover data is coming from. - /// we use this to check if we need to refresh it. - LSPDocumentPosition requested_position; - /// range in document to highlight - LSPRange range; - /// how long the cursor has been hovering for - double time; -} Hover; +typedef struct Hover Hover; /// symbol information for the definitions menu typedef struct { @@ -674,11 +658,7 @@ typedef struct { } Definitions; /// "highlight" information from LSP server -typedef struct { - LSPServerRequestID last_request; - LSPDocumentPosition requested_position; - LSPHighlight *highlights; -} Highlights; +typedef struct Highlights Highlights; typedef struct { Command command; @@ -781,11 +761,11 @@ struct Ted { Autocomplete *autocomplete; SignatureHelp *signature_help; DocumentLinks document_links; - Hover hover; + Hover *hover; Definitions definitions; - Highlights highlights; + Highlights *highlights; Usages *usages; - RenameSymbol rename_symbol; + RenameSymbol *rename_symbol; FILE *log; @@ -1513,19 +1493,36 @@ const char *document_link_at_buffer_pos(Ted *ted, BufferPos pos); void document_link_clear(Ted *ted); // === ide-highlights.c === -void highlights_close(Ted *ted); -void highlights_process_lsp_response(Ted *ted, const LSPResponse *response); +#if !TED_PLUGIN +void highlights_init(Ted *ted); +void highlights_quit(Ted *ted); void highlights_frame(Ted *ted); +void highlights_process_lsp_response(Ted *ted, const LSPResponse *response); +#endif +void highlights_close(Ted *ted); // === ide-hover.c === +#if !TED_PLUGIN +void hover_init(Ted *ted); +void hover_frame(Ted *ted, double dt); +void hover_quit(Ted *ted); +#endif +/// called for example whenever the mouse moves to reset the timer before hover info is displayed +void hover_reset_timer(Ted *ted); void hover_close(Ted *ted); void hover_process_lsp_response(Ted *ted, const LSPResponse *response); -void hover_frame(Ted *ted, double dt); // === ide-rename-symbol.c === -void rename_symbol_clear(Ted *ted); +#if !TED_PLUGIN +void rename_symbol_init(Ted *ted); +void rename_symbol_quit(Ted *ted); void rename_symbol_frame(Ted *ted); void rename_symbol_process_lsp_response(Ted *ted, const LSPResponse *response); +#endif +void rename_symbol_at_cursor(Ted *ted, TextBuffer *buffer, const char *new_name); +/// returns true if we are currently waiting for the LSP to send us a response +bool rename_symbol_is_loading(Ted *ted); +void rename_symbol_clear(Ted *ted); // === ide-signature-help.c === #if !TED_PLUGIN -- cgit v1.2.3