diff options
author | pommicket <pommicket@gmail.com> | 2022-12-29 12:12:36 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-29 12:12:36 -0500 |
commit | 18fbb21e6b95139a8890ba4a65f4402df128ac84 (patch) | |
tree | e72f8f462465d14bf48a88372f90d2d347f025c9 /hover.c | |
parent | 948decc409bac1e3671afc5d0212b4b35c669b4a (diff) |
working hover
Diffstat (limited to 'hover.c')
-rw-r--r-- | hover.c | 87 |
1 files changed, 65 insertions, 22 deletions
@@ -2,29 +2,37 @@ void hover_close(Ted *ted) { Hover *hover = &ted->hover; - hover->time = 0.0; hover->open = false; free(hover->text); hover->text = NULL; } -void hover_send_request(Ted *ted) { +static bool get_hover_position(Ted *ted, LSPDocumentPosition *pos, LSP **lsp) { // find the buffer where the mouse is for (int i = 0; i < TED_MAX_BUFFERS; ++i) { TextBuffer *buffer = &ted->buffers[i]; if (!buffer->filename) continue; - LSP *lsp = buffer_lsp(buffer); - if (!lsp) continue; + LSP *l = buffer_lsp(buffer); + if (!l) continue; BufferPos mouse_pos = {0}; if (buffer_pixels_to_pos(buffer, ted->mouse_pos, &mouse_pos)) { - // send the request - LSPRequest request = {.type = LSP_REQUEST_HOVER}; - LSPRequestHover *h = &request.data.hover; - h->position = buffer_pos_to_lsp_document_position(buffer, mouse_pos); - lsp_send_request(lsp, &request); - break; + *pos = buffer_pos_to_lsp_document_position(buffer, mouse_pos); + *lsp = l; + return true; } } + return false; +} +void hover_send_request(Ted *ted) { + Hover *hover = &ted->hover; + LSPRequest request = {.type = LSP_REQUEST_HOVER}; + LSPRequestHover *h = &request.data.hover; + LSP *lsp = NULL; + if (get_hover_position(ted, &h->position, &lsp)) { + hover->requested_position = h->position; + hover->requested_lsp = lsp->id; + lsp_send_request(lsp, &request); + } } void hover_process_lsp_response(Ted *ted, LSPResponse *response) { @@ -50,30 +58,65 @@ void hover_process_lsp_response(Ted *ted, LSPResponse *response) { void hover_frame(Ted *ted, double dt) { Hover *hover = &ted->hover; - if (ted->autocomplete.open) { + bool shift_down = SDL_GetKeyboardState(NULL)[SDL_SCANCODE_LSHIFT] + || SDL_GetKeyboardState(NULL)[SDL_SCANCODE_RSHIFT]; + + if (!shift_down) { hover_close(ted); } + (void)dt; if (!hover->open) { - hover->time += dt; - if (hover->time > 1.0) { + if (shift_down) { hover_send_request(ted); hover->open = true; } return; } + { + LSPDocumentPosition pos={0}; + LSP *lsp=0; + if (get_hover_position(ted, &pos, &lsp)) { + if (lsp->id != hover->requested_lsp + || !lsp_document_position_eq(pos, hover->requested_position)) { + // refresh hover + hover_send_request(ted); + } + } else { + hover_close(ted); + return; + } + } + if (!hover->text) return; + const Settings *settings = ted_active_settings(ted); + const u32 *colors = settings->colors; const char *text = hover->text; - u16 lines = 0; // number of lines of text - for (int i = 0; text[i]; ++i) - if (text[i] == '\n') - ++lines; - - //Font *font = ted->font; - //float width = 200, height = lines * font->char_height; - - + 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; + } + state.x = x; + state.y = y; + state.render = true; + state.max_y = y + height; + gl_geometry_rect(rect_xywh(x, y, width, height), colors[COLOR_AUTOCOMPLETE_BG]); + rgba_u32_to_floats(colors[COLOR_TEXT], state.color); + text_utf8_with_state(font, &state, text); + gl_geometry_draw(); + text_render(font); } |