From 92e8db6a364b8d1c38f3c15b8e040f6fb7c564f0 Mon Sep 17 00:00:00 2001 From: pommicket Date: Wed, 28 Dec 2022 12:08:36 -0500 Subject: nice signature help (still needs work) --- lsp-parse.c | 4 ++++ main.c | 6 +++--- math.c | 6 ++++++ signature-help.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- ted.h | 2 +- 5 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lsp-parse.c b/lsp-parse.c index 2932302..e780ee7 100644 --- a/lsp-parse.c +++ b/lsp-parse.c @@ -118,10 +118,14 @@ static void parse_capabilities(LSP *lsp, const JSON *json, JSONObject capabiliti if (signature_help_value.type == JSON_OBJECT) { cap->signature_help_support = true; JSONObject signature_help = signature_help_value.val.object; + json_debug_print_object(json, signature_help); JSONArray trigger_chars = json_object_get_array(json, signature_help, "triggerCharacters"); lsp->signature_help_trigger_chars = parse_trigger_characters(json, trigger_chars); JSONArray retrigger_chars = json_object_get_array(json, signature_help, "retriggerCharacters"); lsp->signature_help_retrigger_chars = parse_trigger_characters(json, retrigger_chars); + // rust-analyzer doesn't have ) or > as a retrigger char which is really weird + arr_add(lsp->signature_help_retrigger_chars, ')'); + arr_add(lsp->signature_help_retrigger_chars, '>'); } JSONObject workspace = json_object_get_object(json, capabilities, "workspace"); diff --git a/main.c b/main.c index ffc8610..4e06699 100644 --- a/main.c +++ b/main.c @@ -787,7 +787,7 @@ int main(int argc, char **argv) { } } - if (ted->signature_help.open) { + if (signature_help_is_open(ted)) { arr_foreach_ptr(lsp->signature_help_retrigger_chars, char32_t, c) { if (*c == last_char) { signature_help = true; @@ -1010,9 +1010,9 @@ int main(int argc, char **argv) { if (ted->nodes_used[0]) { float y1 = padding; node_frame(ted, node, rect4(x1, y1, x2, y)); - if (ted->autocomplete.open) { + if (ted->autocomplete.open) autocomplete_frame(ted); - } + signature_help_frame(ted); } else { autocomplete_close(ted); text_utf8_anchored(font, "Press Ctrl+O to open a file or Ctrl+N to create a new one.", diff --git a/math.c b/math.c index 80ef10e..dd1408d 100644 --- a/math.c +++ b/math.c @@ -687,6 +687,12 @@ static Rect rect4(float x1, float y1, float x2, float y2) { return rect(V2(x1,y1), V2(x2-x1, y2-y1)); } +static Rect rect_xywh(float x, float y, float w, float h) { + assert(w >= 0); + assert(h >= 0); + return rect(V2(x, y), V2(w, h)); +} + static Rect rect_centered(v2 center, v2 size) { Rect r; r.pos = v2_sub(center, v2_scale(size, 0.5f)); diff --git a/signature-help.c b/signature-help.c index b6a1105..4b7e847 100644 --- a/signature-help.c +++ b/signature-help.c @@ -11,6 +11,10 @@ void signature_help_open(Ted *ted, char32_t trigger) { lsp_send_request(lsp, &request); } +bool signature_help_is_open(Ted *ted) { + return ted->signature_help.signature_count > 0; +} + static void signature_help_clear(SignatureHelp *help) { for (int i = 0; i < help->signature_count; ++i) { Signature sig = help->signatures[i]; @@ -21,6 +25,10 @@ static void signature_help_clear(SignatureHelp *help) { memset(help->signatures, 0, sizeof help->signatures); } +void signature_help_close(Ted *ted) { + signature_help_clear(&ted->signature_help); +} + void signature_help_process_lsp_response(Ted *ted, const LSPResponse *response) { if (response->request.type != LSP_REQUEST_SIGNATURE_HELP) return; @@ -51,8 +59,60 @@ void signature_help_process_lsp_response(Ted *ted, const LSPResponse *response) signature->label_pre = strn_dup(label, active_start); signature->label_active = strn_dup(label + active_start, active_end - active_start); signature->label_post = str_dup(label + active_end); - printf("%s*%s*%s\n",signature->label_pre,signature->label_active,signature->label_post); } help->signature_count = (u16)signature_count; } + +void signature_help_frame(Ted *ted) { + SignatureHelp *help = &ted->signature_help; + u16 signature_count = help->signature_count; + if (!signature_count) + return; + Font *font = ted->font; + Font *font_bold = ted->font_bold; + TextBuffer *buffer = ted->active_buffer; + if (!buffer) + return; + Settings *settings = buffer_settings(buffer); + u32 *colors = settings->colors; + float border = settings->border_thickness; + + float width = buffer->x2 - buffer->x1; + float height = FLT_MAX; + // make sure signature help doesn't take up too much space + while (1) { + height = font->char_height * signature_count; + if (height < (buffer->y2 - buffer->y1) * 0.25f) + break; + --signature_count; + if (signature_count == 0) return; + } + float x = buffer->x1, y = buffer->y2 - height; + gl_geometry_rect(rect_xywh(x, y - border, width, border), + colors[COLOR_AUTOCOMPLETE_BORDER]); + gl_geometry_rect(rect_xywh(x, y, width, height), + colors[COLOR_AUTOCOMPLETE_BG]); + + // draw the signatures + for (int s = 0; s < signature_count; ++s) { + const Signature *signature = &help->signatures[s]; + TextRenderState state = text_render_state_default; + state.x = x; + state.y = y; + state.min_x = x; + state.min_y = y; + state.max_x = buffer->x2; + state.max_y = buffer->y2; + rgba_u32_to_floats(colors[COLOR_TEXT], state.color); + + text_utf8_with_state(font, &state, signature->label_pre); + text_utf8_with_state(font_bold, &state, signature->label_active); + text_utf8_with_state(font, &state, signature->label_post); + y += font->char_height; + } + + gl_geometry_draw(); + text_render(font); + text_render(font_bold); +} diff --git a/ted.h b/ted.h index aa475f8..0f6bcdf 100644 --- a/ted.h +++ b/ted.h @@ -430,7 +430,7 @@ typedef struct { // "signature help" (LSP) is thing that shows the current parameter, etc. typedef struct { - bool open; + // if signature_count = 0, signature help is closed u16 signature_count; Signature signatures[SIGNATURE_HELP_MAX]; } SignatureHelp; -- cgit v1.2.3