diff options
Diffstat (limited to 'ide-rename-symbol.c')
-rw-r--r-- | ide-rename-symbol.c | 93 |
1 files changed, 85 insertions, 8 deletions
diff --git a/ide-rename-symbol.c b/ide-rename-symbol.c index b3ef52b..7d1e2ad 100644 --- a/ide-rename-symbol.c +++ b/ide-rename-symbol.c @@ -4,10 +4,6 @@ struct RenameSymbol { LSPServerRequestID request_id; }; -void rename_symbol_init(Ted *ted) { - ted->rename_symbol = calloc(1, sizeof *ted->rename_symbol); -} - void rename_symbol_quit(Ted *ted) { rename_symbol_clear(ted); free(ted->rename_symbol); @@ -31,10 +27,6 @@ void rename_symbol_at_cursor(Ted *ted, TextBuffer *buffer, const char *new_name) } -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); @@ -48,6 +40,79 @@ void rename_symbol_frame(Ted *ted) { } } +static void rename_symbol_menu_open(Ted *ted) { + ted_switch_to_buffer(ted, &ted->line_buffer); +} + +static void rename_symbol_menu_update(Ted *ted) { + TextBuffer *line_buffer = &ted->line_buffer; + 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); + } +} + +static void rename_symbol_menu_render(Ted *ted) { + RenameSymbol *rs = ted->rename_symbol; + // highlight symbol + TextBuffer *buffer = ted->prev_active_buffer; + if (!buffer) { + menu_close(ted); + return; + } + if (rs->request_id.id) { + // already entered a new name + return; + } + const Settings *settings = buffer_settings(buffer); + const float padding = settings->padding; + const u32 *colors = settings->colors; + const float line_buffer_height = ted_line_buffer_height(ted); + + u32 sym_start=0, sym_end=0; + BufferPos cursor_pos = buffer->cursor_pos; + buffer_word_span_at_pos(buffer, cursor_pos, &sym_start, &sym_end); + BufferPos bpos0 = { + .line = cursor_pos.line, + .index = sym_start + }; + BufferPos bpos1 = { + .line = cursor_pos.line, + .index = sym_end + }; + // symbol should span from pos0 to pos1 + vec2 p0 = buffer_pos_to_pixels(buffer, bpos0); + vec2 p1 = buffer_pos_to_pixels(buffer, bpos1); + p1.y += text_font_char_height(buffer_font(buffer)); + Rect highlight = rect_endpoints(p0, p1); + gl_geometry_rect_border(highlight, settings->border_thickness, colors[COLOR_BORDER]); + gl_geometry_rect(highlight, colors[COLOR_HOVER_HL]); + + const float width = ted_get_menu_width(ted); + const float height = line_buffer_height + 2 * padding; + Rect bounds = { + .pos = {(ted->window_width - width) / 2, padding}, + .size = {width, height}, + }; + gl_geometry_rect(bounds, colors[COLOR_MENU_BG]); + gl_geometry_rect_border(bounds, settings->border_thickness, colors[COLOR_BORDER]); + gl_geometry_draw(); + bounds = rect_shrink(bounds, padding); + const char *text = "Rename symbol to..."; + text_utf8(ted->font_bold, text, bounds.pos.x, bounds.pos.y, colors[COLOR_TEXT]); + bounds = rect_shrink_left(bounds, text_get_size_vec2(ted->font_bold, text).x + padding); + text_render(ted->font_bold); + + buffer_render(&ted->line_buffer, bounds); +} + +static bool rename_symbol_menu_close(Ted *ted) { + rename_symbol_clear(ted); + buffer_clear(&ted->line_buffer); + return true; +} + void rename_symbol_process_lsp_response(Ted *ted, const LSPResponse *response) { RenameSymbol *rs = ted->rename_symbol; if (response->request.type != LSP_REQUEST_RENAME @@ -125,3 +190,15 @@ void rename_symbol_process_lsp_response(Ted *ted, const LSPResponse *response) { if (menu_is_open(ted, MENU_RENAME_SYMBOL)) menu_close(ted); } + +void rename_symbol_init(Ted *ted) { + ted->rename_symbol = calloc(1, sizeof *ted->rename_symbol); + MenuInfo menu = { + .open = rename_symbol_menu_open, + .close = rename_symbol_menu_close, + .update = rename_symbol_menu_update, + .render = rename_symbol_menu_render, + }; + strbuf_cpy(menu.name, MENU_RENAME_SYMBOL); + menu_register(ted, &menu); +} |