From 4b10cb8f0892a720bd8d7f61b39f5faffc43107c Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 5 Aug 2023 15:08:45 -0400 Subject: internalize signature help, usages --- ide-autocomplete.c | 6 ++++++ ide-highlights.c | 2 +- ide-hover.c | 4 ++-- ide-rename-symbol.c | 12 ++++++------ ide-signature-help.c | 41 ++++++++++++++++++++++++++++++++++------ ide-usages.c | 21 +++++++++++++++++---- main.c | 7 +++++-- ted.h | 53 +++++++++++++++++++++------------------------------- 8 files changed, 93 insertions(+), 53 deletions(-) diff --git a/ide-autocomplete.c b/ide-autocomplete.c index c024e30..6b98526 100644 --- a/ide-autocomplete.c +++ b/ide-autocomplete.c @@ -765,3 +765,9 @@ void autocomplete_frame(Ted *ted) { gl_geometry_draw(); text_render(font); } + +void autocomplete_quit(Ted *ted) { + autocomplete_close(ted); + free(ted->autocomplete); + ted->autocomplete = NULL; +} diff --git a/ide-highlights.c b/ide-highlights.c index 8c58657..a2d149f 100644 --- a/ide-highlights.c +++ b/ide-highlights.c @@ -31,7 +31,7 @@ static void highlights_send_request(Ted *ted) { } -void highlights_process_lsp_response(Ted *ted, LSPResponse *response) { +void highlights_process_lsp_response(Ted *ted, const LSPResponse *response) { Highlights *hls = &ted->highlights; if (response->request.type != LSP_REQUEST_HIGHLIGHT) return; // not a highlight request diff --git a/ide-hover.c b/ide-hover.c index 4c6c5f2..d0f99c1 100644 --- a/ide-hover.c +++ b/ide-hover.c @@ -38,7 +38,7 @@ static void hover_send_request(Ted *ted) { } } -void hover_process_lsp_response(Ted *ted, LSPResponse *response) { +void hover_process_lsp_response(Ted *ted, const LSPResponse *response) { if (!response) return; if (response->request.type != LSP_REQUEST_HOVER) return; @@ -49,7 +49,7 @@ void hover_process_lsp_response(Ted *ted, LSPResponse *response) { } hover->last_request.id = 0; - LSPResponseHover *hover_response = &response->data.hover; + const LSPResponseHover *hover_response = &response->data.hover; TextBuffer *buffer=0; LSPDocumentPosition pos={0}; diff --git a/ide-rename-symbol.c b/ide-rename-symbol.c index 4df948e..88ae5bd 100644 --- a/ide-rename-symbol.c +++ b/ide-rename-symbol.c @@ -32,14 +32,14 @@ void rename_symbol_frame(Ted *ted) { ted->cursor = ted->cursor_wait; } -void rename_symbol_process_lsp_response(Ted *ted, LSPResponse *response) { +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; } - LSPResponseRename *data = &response->data.rename; + const LSPResponseRename *data = &response->data.rename; LSP *lsp = ted_get_lsp_by_id(ted, rs->request_id.lsp); if (!lsp) { // LSP crashed or something @@ -49,7 +49,7 @@ void rename_symbol_process_lsp_response(Ted *ted, LSPResponse *response) { assert(rs->new_name); bool perform_changes = true; - arr_foreach_ptr(data->changes, LSPWorkspaceChange, change) { + arr_foreach_ptr(data->changes, const LSPWorkspaceChange, change) { if (change->type != LSP_CHANGE_EDIT) { // TODO(eventually) : support these ted_error(ted, "rename is too complicated for ted to perform."); @@ -59,10 +59,10 @@ void rename_symbol_process_lsp_response(Ted *ted, LSPResponse *response) { if (perform_changes) { - arr_foreach_ptr(data->changes, LSPWorkspaceChange, change) { + arr_foreach_ptr(data->changes, const LSPWorkspaceChange, change) { switch (change->type) { case LSP_CHANGE_EDIT: { - LSPWorkspaceChangeEdit *change_data = &change->data.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; @@ -80,7 +80,7 @@ void rename_symbol_process_lsp_response(Ted *ted, LSPResponse *response) { } - LSPTextEdit *edit = &change_data->edit; + 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); diff --git a/ide-signature-help.c b/ide-signature-help.c index 87eef7d..a18eea0 100644 --- a/ide-signature-help.c +++ b/ide-signature-help.c @@ -3,6 +3,29 @@ #include "ted.h" +/// a single signature in the signature help. +typedef struct { + /// displayed normal + char *label_pre; + /// displayed bold + char *label_active; + /// displayed normal + char *label_post; +} Signature; + +struct SignatureHelp { + LSPServerRequestID last_request; + /// should we resend a signature help request this frame? + bool retrigger; + /// if signature_count = 0, signature help is closed + u16 signature_count; + Signature signatures[SIGNATURE_HELP_MAX]; +}; + +void signature_help_init(Ted *ted) { + ted->signature_help = calloc(1, sizeof *ted->signature_help); +} + static void signature_help_clear(SignatureHelp *help) { for (int i = 0; i < help->signature_count; ++i) { Signature sig = help->signatures[i]; @@ -15,7 +38,7 @@ static void signature_help_clear(SignatureHelp *help) { } static void signature_help_send_request(Ted *ted) { - SignatureHelp *help = &ted->signature_help; + SignatureHelp *help = ted->signature_help; Settings *settings = ted_active_settings(ted); if (!settings->signature_help_enabled) { signature_help_clear(help); @@ -42,7 +65,7 @@ static void signature_help_send_request(Ted *ted) { void signature_help_retrigger(Ted *ted) { // don't just send the request here -- we don't want to send more than // one request per frame. - ted->signature_help.retrigger = true; + ted->signature_help->retrigger = true; } void signature_help_open(Ted *ted, uint32_t trigger) { @@ -51,18 +74,18 @@ void signature_help_open(Ted *ted, uint32_t trigger) { } bool signature_help_is_open(Ted *ted) { - return ted->signature_help.signature_count > 0; + return ted->signature_help->signature_count > 0; } void signature_help_close(Ted *ted) { - SignatureHelp *help = &ted->signature_help; + SignatureHelp *help = ted->signature_help; signature_help_clear(help); ted_cancel_lsp_request(ted, &help->last_request); } void signature_help_process_lsp_response(Ted *ted, const LSPResponse *response) { - SignatureHelp *help = &ted->signature_help; + SignatureHelp *help = ted->signature_help; Settings *settings = ted_active_settings(ted); if (!settings->signature_help_enabled) return; @@ -110,7 +133,7 @@ void signature_help_frame(Ted *ted) { if (!settings->signature_help_enabled) return; - SignatureHelp *help = &ted->signature_help; + SignatureHelp *help = ted->signature_help; if (help->retrigger) signature_help_send_request(ted); u16 signature_count = help->signature_count; @@ -164,3 +187,9 @@ void signature_help_frame(Ted *ted) { text_render(font); text_render(font_bold); } + +void signature_help_quit(Ted *ted) { + signature_help_close(ted); + free(ted->signature_help); + ted->signature_help = NULL; +} diff --git a/ide-usages.c b/ide-usages.c index b9c2085..0547b5c 100644 --- a/ide-usages.c +++ b/ide-usages.c @@ -1,14 +1,27 @@ // find usages of symbol #include "ted.h" +struct Usages { + LSPServerRequestID last_request; + double last_request_time; +}; + +void usages_init(Ted *ted) { + ted->usages = calloc(1, sizeof *ted->usages); +} + +void usages_quit(Ted *ted) { + free(ted->usages); + ted->usages = NULL; +} void usages_cancel_lookup(Ted *ted) { - Usages *usages = &ted->usages; + Usages *usages = ted->usages; ted_cancel_lsp_request(ted, &usages->last_request); } void usages_find(Ted *ted) { - Usages *usages = &ted->usages; + Usages *usages = ted->usages; TextBuffer *buffer = ted->active_buffer; if (!buffer) return; LSP *lsp = buffer_lsp(buffer); @@ -26,7 +39,7 @@ void usages_find(Ted *ted) { void usages_process_lsp_response(Ted *ted, const LSPResponse *response) { - Usages *usages = &ted->usages; + Usages *usages = ted->usages; if (response->request.type != LSP_REQUEST_REFERENCES) return; // not for us if (response->request.id != usages->last_request.id) @@ -125,7 +138,7 @@ void usages_process_lsp_response(Ted *ted, const LSPResponse *response) { } void usages_frame(Ted *ted) { - Usages *usages = &ted->usages; + Usages *usages = ted->usages; if (usages->last_request.id && ted->frame_time - usages->last_request_time > 0.2) ted->cursor = ted->cursor_wait; // this request is takin a while } diff --git a/main.c b/main.c index ec8af1e..cae3b05 100644 --- a/main.c +++ b/main.c @@ -501,6 +501,8 @@ int main(int argc, char **argv) { gl_geometry_init(); text_init(); autocomplete_init(ted); + signature_help_init(ted); + usages_init(ted); PROFILE_TIME(gl_end) @@ -1184,8 +1186,9 @@ int main(int argc, char **argv) { if (ted->menu) menu_close(ted); hover_close(ted); - signature_help_close(ted); - autocomplete_close(ted); + signature_help_quit(ted); + autocomplete_quit(ted); + usages_quit(ted); highlights_close(ted); session_write(ted); rename_symbol_clear(ted); diff --git a/ted.h b/ted.h index 36d5ce7..cee978d 100644 --- a/ted.h +++ b/ted.h @@ -612,33 +612,13 @@ typedef enum { typedef struct Autocomplete Autocomplete; /// data needed for finding usages -typedef struct { - LSPServerRequestID last_request; - double last_request_time; -} Usages; - -/// a single signature in the signature help. -typedef struct { - /// displayed normal - char *label_pre; - /// displayed bold - char *label_active; - /// displayed normal - char *label_post; -} Signature; +typedef struct Usages Usages; /// max number of signatures to display at a time. #define SIGNATURE_HELP_MAX 5 /// "signature help" (LSP) is thing that shows the current parameter, etc. -typedef struct { - LSPServerRequestID last_request; - /// should we resend a signature help request this frame? - bool retrigger; - /// if signature_count = 0, signature help is closed - u16 signature_count; - Signature signatures[SIGNATURE_HELP_MAX]; -} SignatureHelp; +typedef struct SignatureHelp SignatureHelp; typedef struct DocumentLink DocumentLink; @@ -799,12 +779,12 @@ struct Ted { /// is the build process running? bool building; Autocomplete *autocomplete; - SignatureHelp signature_help; + SignatureHelp *signature_help; DocumentLinks document_links; Hover hover; Definitions definitions; Highlights highlights; - Usages usages; + Usages *usages; RenameSymbol rename_symbol; FILE *log; @@ -1479,6 +1459,8 @@ GLuint gl_load_texture_from_image(const char *path); // === ide-autocomplete.c === #if !TED_PLUGIN void autocomplete_init(Ted *ted); +void autocomplete_quit(Ted *ted); +void autocomplete_process_lsp_response(Ted *ted, const LSPResponse *response); #endif /// is the autocomplete box open? bool autocomplete_is_open(Ted *ted); @@ -1489,7 +1471,6 @@ bool autocomplete_box_contains_point(Ted *ted, vec2 point); /// open autocomplete /// trigger should either be a character (e.g. '.') or one of the TRIGGER_* constants. void autocomplete_open(Ted *ted, uint32_t trigger); -void autocomplete_process_lsp_response(Ted *ted, const LSPResponse *response); /// select the completion the cursor is on, /// or select the phantom completion if there is one. void autocomplete_select_completion(Ted *ted); @@ -1533,20 +1514,26 @@ void document_link_clear(Ted *ted); // === ide-highlights.c === void highlights_close(Ted *ted); -void highlights_process_lsp_response(Ted *ted, LSPResponse *response); +void highlights_process_lsp_response(Ted *ted, const LSPResponse *response); void highlights_frame(Ted *ted); // === ide-hover.c === void hover_close(Ted *ted); -void hover_process_lsp_response(Ted *ted, LSPResponse *response); +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); void rename_symbol_frame(Ted *ted); -void rename_symbol_process_lsp_response(Ted *ted, LSPResponse *response); +void rename_symbol_process_lsp_response(Ted *ted, const LSPResponse *response); // === ide-signature-help.c === +#if !TED_PLUGIN +void signature_help_init(Ted *ted); +void signature_help_quit(Ted *ted); +void signature_help_frame(Ted *ted); +void signature_help_process_lsp_response(Ted *ted, const LSPResponse *response); +#endif /// figure out new signature help void signature_help_retrigger(Ted *ted); /// open signature help. `trigger` should either be the trigger character (e.g. ',') @@ -1554,16 +1541,18 @@ void signature_help_retrigger(Ted *ted); void signature_help_open(Ted *ted, uint32_t trigger); bool signature_help_is_open(Ted *ted); void signature_help_close(Ted *ted); -void signature_help_process_lsp_response(Ted *ted, const LSPResponse *response); -void signature_help_frame(Ted *ted); // === ide-usages.c === +#if !TED_PLUGIN +void usages_init(Ted *ted); +void usages_process_lsp_response(Ted *ted, const LSPResponse *response); +void usages_frame(Ted *ted); +void usages_quit(Ted *ted); +#endif /// cancel the last "find usages" request void usages_cancel_lookup(Ted *ted); /// find usages for word under the cursor in the active buffer. void usages_find(Ted *ted); -void usages_process_lsp_response(Ted *ted, const LSPResponse *response); -void usages_frame(Ted *ted); // === macro.c === #if !TED_PLUGIN -- cgit v1.2.3