From ecca6666861f590ac17c68ea0b81ba0672667e65 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 31 Dec 2022 16:04:26 -0500 Subject: goto-definition-at-cursor command --- buffer.c | 23 ++++++++++++++--------- command.c | 15 ++++++++++----- command.h | 6 ++++-- main.c | 3 --- ted.cfg | 2 ++ 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/buffer.c b/buffer.c index 0ef1ec5..353f565 100644 --- a/buffer.c +++ b/buffer.c @@ -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: diff --git a/command.c b/command.c index 1df0363..7a72d0f 100644 --- a/command.c +++ b/command.c @@ -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; diff --git a/command.h b/command.h index 000b050..0eef7aa 100644 --- a/command.h +++ b/command.h @@ -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}, diff --git a/main.c b/main.c index e393cc8..d770d70 100644 --- a/main.c +++ b/main.c @@ -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? diff --git a/ted.cfg b/ted.cfg index 7c0079e..6f78426 100644 --- a/ted.cfg +++ b/ted.cfg @@ -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 -- cgit v1.2.3