diff options
author | pommicket <pommicket@gmail.com> | 2022-12-31 16:04:26 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-31 16:04:26 -0500 |
commit | ecca6666861f590ac17c68ea0b81ba0672667e65 (patch) | |
tree | 984e898163635e4fb5b8f81692d1d44946f15978 | |
parent | fd617d8d9723f76dcdb51b2fa54ef960be4edccc (diff) |
goto-definition-at-cursor command
-rw-r--r-- | buffer.c | 23 | ||||
-rw-r--r-- | command.c | 15 | ||||
-rw-r--r-- | command.h | 6 | ||||
-rw-r--r-- | main.c | 3 | ||||
-rw-r--r-- | ted.cfg | 2 |
5 files changed, 30 insertions, 19 deletions
@@ -1366,6 +1366,10 @@ String32 buffer_word_at_cursor(TextBuffer *buffer) { return buffer_word_at_pos(buffer, buffer->cursor_pos); } +char *buffer_word_at_cursor_utf8(TextBuffer *buffer) { + return str32_to_utf8_cstr(buffer_word_at_cursor(buffer)); +} + // Returns the position corresponding to the start of the given line. BufferPos buffer_pos_start_of_line(TextBuffer *buffer, u32 line) { (void)buffer; @@ -2471,6 +2475,15 @@ u32 buffer_last_rendered_line(TextBuffer *buffer) { return clamp_u32(line, 0, buffer->nlines); } +void buffer_goto_word_at_cursor(TextBuffer *buffer) { + char *word = buffer_word_at_cursor_utf8(buffer); + if (*word) { + LSPDocumentPosition pos = buffer_pos_to_lsp_document_position(buffer, buffer->cursor_pos); + definition_goto(buffer->ted, buffer_lsp(buffer), word, pos); + } + free(word); +} + // returns true if the buffer "used" this event bool buffer_handle_click(Ted *ted, TextBuffer *buffer, v2 click, u8 times) { BufferPos buffer_pos; @@ -2495,15 +2508,7 @@ bool buffer_handle_click(Ted *ted, TextBuffer *buffer, v2 click, u8 times) { if (!buffer->is_line_buffer) { // go to definition buffer_cursor_move_to_pos(buffer, buffer_pos); - String32 word = buffer_word_at_cursor(buffer); - if (word.len) { - char *tag = str32_to_utf8_cstr(word); - if (tag) { - LSPDocumentPosition pos = buffer_pos_to_lsp_document_position(buffer, buffer_pos); - definition_goto(buffer->ted, buffer_lsp(buffer), tag, pos); - free(tag); - } - } + buffer_goto_word_at_cursor(buffer); } break; case 0: @@ -292,7 +292,15 @@ void command_execute(Ted *ted, Command c, i64 argument) { case CMD_AUTOCOMPLETE_BACK: if (ted->autocomplete.open) autocomplete_prev(ted); - break; + break; + case CMD_GOTO_DEFINITION: + menu_open(ted, MENU_GOTO_DEFINITION); + break; + case CMD_GOTO_DEFINITION_AT_CURSOR: { + if (buffer && buffer_is_named_file(buffer)) { + buffer_goto_word_at_cursor(buffer); + } + } break; case CMD_FIND_USAGES: usages_find(ted); break; @@ -439,10 +447,7 @@ void command_execute(Ted *ted, Command c, i64 argument) { case CMD_GENERATE_TAGS: tags_generate(ted, true); break; - - case CMD_GOTO_DEFINITION: - menu_open(ted, MENU_GOTO_DEFINITION); - break; + case CMD_GOTO_LINE: menu_open(ted, MENU_GOTO_LINE); break; @@ -64,6 +64,8 @@ ENUM_U16 { CMD_AUTOCOMPLETE, CMD_AUTOCOMPLETE_BACK, CMD_FIND_USAGES, + CMD_GOTO_DEFINITION, // "go to definition of..." menu + CMD_GOTO_DEFINITION_AT_CURSOR, CMD_COPY, CMD_CUT, @@ -89,7 +91,6 @@ ENUM_U16 { CMD_SHELL, CMD_GENERATE_TAGS, - CMD_GOTO_DEFINITION, // "go to definition of..." CMD_GOTO_LINE, // open "goto line..." menu CMD_SPLIT_HORIZONTAL, @@ -163,6 +164,8 @@ static CommandName const command_names[] = { {"autocomplete", CMD_AUTOCOMPLETE}, {"autocomplete-back", CMD_AUTOCOMPLETE_BACK}, {"find-usages", CMD_FIND_USAGES}, + {"goto-definition", CMD_GOTO_DEFINITION}, + {"goto-definition-at-cursor", CMD_GOTO_DEFINITION_AT_CURSOR}, {"find", CMD_FIND}, {"find-replace", CMD_FIND_REPLACE}, {"tab-close", CMD_TAB_CLOSE}, @@ -179,7 +182,6 @@ static CommandName const command_names[] = { {"build-next-error", CMD_BUILD_NEXT_ERROR}, {"shell", CMD_SHELL}, {"generate-tags", CMD_GENERATE_TAGS}, - {"goto-definition", CMD_GOTO_DEFINITION}, {"goto-line", CMD_GOTO_LINE}, {"split-horizontal", CMD_SPLIT_HORIZONTAL}, {"split-vertical", CMD_SPLIT_VERTICAL}, @@ -1,11 +1,8 @@ /* @TODO: - handle multiple symbols with same name in go-to-definition menu -- :go-to-cursor-definition -- test full unicode position handling - better non-error window/showMessage(Request) - document lsp.h and lsp.c. -- add last_request_id checking to autocomplete_process_lsp_response - maximum queue size for requests/responses just in case? - idea: configurable timeout - what to do if initialize request takes a long time? @@ -261,6 +261,8 @@ Ctrl+! = :shell Ctrl+t = :generate-tags Ctrl+d = :goto-definition +# alternative to ctrl+click +Ctrl+Shift+d = :goto-definition-at-cursor Ctrl+g = :goto-line Ctrl+\ = :split-horizontal |