From f1e2e8382cc09bacb5da9e3c9463fea983a9e146 Mon Sep 17 00:00:00 2001 From: pommicket Date: Thu, 29 Dec 2022 12:32:26 -0500 Subject: more hover --- buffer.c | 6 +++++ hover.c | 78 ++++++++++++++++++++++++++++++++++++++++------------------------ main.c | 2 +- math.c | 7 ++++++ ted.h | 2 ++ 5 files changed, 65 insertions(+), 30 deletions(-) diff --git a/buffer.c b/buffer.c index 6e32770..70483a8 100644 --- a/buffer.c +++ b/buffer.c @@ -1453,6 +1453,12 @@ LSPDocumentPosition buffer_pos_to_lsp_document_position(TextBuffer *buffer, Buff return docpos; } +BufferPos buffer_pos_from_lsp(TextBuffer *buffer, LSPPosition lsp_pos) { + BufferPos pos = {.line = lsp_pos.line}; + abort(); // @TODO + return pos; +} + LSPPosition buffer_cursor_pos_as_lsp_position(TextBuffer *buffer) { return buffer_pos_to_lsp_position(buffer, buffer->cursor_pos); } diff --git a/hover.c b/hover.c index d22f513..708e009 100644 --- a/hover.c +++ b/hover.c @@ -7,7 +7,7 @@ void hover_close(Ted *ted) { hover->text = NULL; } -static bool get_hover_position(Ted *ted, LSPDocumentPosition *pos, LSP **lsp) { +static bool get_hover_position(Ted *ted, LSPDocumentPosition *pos, TextBuffer **pbuffer, LSP **lsp) { // find the buffer where the mouse is for (int i = 0; i < TED_MAX_BUFFERS; ++i) { TextBuffer *buffer = &ted->buffers[i]; @@ -16,8 +16,9 @@ static bool get_hover_position(Ted *ted, LSPDocumentPosition *pos, LSP **lsp) { if (!l) continue; BufferPos mouse_pos = {0}; if (buffer_pixels_to_pos(buffer, ted->mouse_pos, &mouse_pos)) { - *pos = buffer_pos_to_lsp_document_position(buffer, mouse_pos); - *lsp = l; + if (pos) *pos = buffer_pos_to_lsp_document_position(buffer, mouse_pos); + if (pbuffer) *pbuffer = buffer; + if (lsp) *lsp = l; return true; } } @@ -28,7 +29,7 @@ void hover_send_request(Ted *ted) { LSPRequest request = {.type = LSP_REQUEST_HOVER}; LSPRequestHover *h = &request.data.hover; LSP *lsp = NULL; - if (get_hover_position(ted, &h->position, &lsp)) { + if (get_hover_position(ted, &h->position, NULL, &lsp)) { hover->requested_position = h->position; hover->requested_lsp = lsp->id; lsp_send_request(lsp, &request); @@ -42,8 +43,15 @@ void hover_process_lsp_response(Ted *ted, LSPResponse *response) { Hover *hover = &ted->hover; LSPResponseHover *hover_response = &response->data.hover; free(hover->text); - hover->text = NULL; + TextBuffer *buffer=0; + get_hover_position(ted, NULL, &buffer, NULL); + + hover->text = NULL; + if (buffer) { + hover->range_start = buffer_pos_from_lsp(buffer, hover_response->range.start); + hover->range_end = buffer_pos_from_lsp(buffer, hover_response->range.end); + } const char *contents = lsp_response_string(response, hover_response->contents); if (*contents) { hover->text = str_dup(contents); @@ -74,10 +82,11 @@ void hover_frame(Ted *ted, double dt) { return; } + TextBuffer *buffer=0; { LSPDocumentPosition pos={0}; LSP *lsp=0; - if (get_hover_position(ted, &pos, &lsp)) { + if (get_hover_position(ted, &pos, &buffer, &lsp)) { if (lsp->id != hover->requested_lsp || !lsp_document_position_eq(pos, hover->requested_position)) { // refresh hover @@ -89,8 +98,7 @@ void hover_frame(Ted *ted, double dt) { } } - if (!hover->text) - return; + const Settings *settings = ted_active_settings(ted); const float padding = settings->padding; @@ -99,29 +107,41 @@ void hover_frame(Ted *ted, double dt) { const char *text = hover->text; Font *font = ted->font; float x = ted->mouse_pos.x, y = ted->mouse_pos.y; - TextRenderState state = text_render_state_default; - state.x = state.min_x = x; - state.y = state.min_y = y; - state.render = false; - state.wrap = true; - state.max_x = x + 400; - state.max_y = ted->window_height; - text_utf8_with_state(font, &state, text); - float width = (float)(state.x_largest - x); - float height = (float)(state.y_largest - y) + font->char_height; - if (height > 300) { - height = 300; + + if (hover->text) { + TextRenderState state = text_render_state_default; + state.x = state.min_x = x; + state.y = state.min_y = y; + state.render = false; + state.wrap = true; + state.max_x = x + 400; + state.max_y = ted->window_height; + text_utf8_with_state(font, &state, text); + float width = (float)(state.x_largest - x); + float height = (float)(state.y_largest - y) + font->char_height; + if (height > 300) { + height = 300; + } + state.x = x; + state.y = y; + state.render = true; + state.max_y = y + height; + + Rect rect = rect_xywh(x - padding, y - padding, width + 2*padding, height + 2*padding); + gl_geometry_rect(rect, colors[COLOR_HOVER_BG]); + gl_geometry_rect_border(rect, border, colors[COLOR_HOVER_BORDER]); + rgba_u32_to_floats(colors[COLOR_HOVER_TEXT], state.color); + text_utf8_with_state(font, &state, text); + } + if (!buffer_pos_eq(hover->range_start, hover->range_end)) { + // draw the highlight + v2 range_start = buffer_pos_to_pixels(buffer, hover->range_start); + v2 range_end = buffer_pos_to_pixels(buffer, hover->range_end); + range_end.y += font->char_height; + Rect rect = rect_endpoints(range_start, range_end); + gl_geometry_rect(rect, colors[COLOR_HOVER_BG]);//@TODO: HOVER_HL color } - state.x = x; - state.y = y; - state.render = true; - state.max_y = y + height; - Rect rect = rect_xywh(x - padding, y - padding, width + 2*padding, height + 2*padding); - gl_geometry_rect(rect, colors[COLOR_HOVER_BG]); - gl_geometry_rect_border(rect, border, colors[COLOR_HOVER_BORDER]); - rgba_u32_to_floats(colors[COLOR_HOVER_TEXT], state.color); - text_utf8_with_state(font, &state, text); gl_geometry_draw(); text_render(font); } diff --git a/main.c b/main.c index fac2c4e..2087c8e 100644 --- a/main.c +++ b/main.c @@ -3,8 +3,8 @@ - more LSP stuff: - go to definition using LSP - find usages +- test full unicode position handling - highlight hover range -- check mouse position in hover_process_lsp_response - hover-enabled, hover-time settings - check if there are any other non-optional/nice-to-have-support-for server-to-client requests - better non-error window/showMessage(Request) diff --git a/math.c b/math.c index dd1408d..db8e4f2 100644 --- a/math.c +++ b/math.c @@ -681,6 +681,13 @@ static Rect rect(v2 pos, v2 size) { return r; } +static Rect rect_endpoints(v2 e1, v2 e2) { + Rect r; + r.pos = e1; + r.size = v2_sub(e2, e1); + return r; +} + static Rect rect4(float x1, float y1, float x2, float y2) { assert(x2 >= x1); assert(y2 >= y1); diff --git a/ted.h b/ted.h index 64fb993..f081c46 100644 --- a/ted.h +++ b/ted.h @@ -408,6 +408,8 @@ typedef struct { // we use this to check if we need to refresh it. LSPDocumentPosition requested_position; LSPID requested_lsp; + BufferPos range_start; + BufferPos range_end; } Hover; -- cgit v1.2.3