diff options
-rw-r--r-- | ide-usages.c | 71 | ||||
-rw-r--r-- | lsp-parse.c | 2 | ||||
-rw-r--r-- | main.c | 1 |
3 files changed, 70 insertions, 4 deletions
diff --git a/ide-usages.c b/ide-usages.c index 73fb725..70a44f1 100644 --- a/ide-usages.c +++ b/ide-usages.c @@ -37,14 +37,79 @@ void usages_process_lsp_response(Ted *ted, LSPResponse *response) { TextBuffer *buffer = &ted->build_buffer; build_setup_buffer(ted); ted->build_shown = true; + char last_path[TED_PATH_MAX] = {0}; + TextBuffer *last_buffer = NULL; + FILE *last_file = NULL; + u32 last_line = 0; + arr_foreach_ptr(refs->locations, LSPLocation, location) { + const char *path = lsp_document_path(lsp, location->document); + + if (!paths_eq(path, last_path)) { + // it's a new file! + strbuf_cpy(last_path, path); + if (last_file) { + fclose(last_file); + last_file = NULL; + } + last_buffer = ted_get_buffer_with_file(ted, path); + if (!last_buffer) { + last_file = fopen(path, "rb"); + last_line = 0; + } + } + + u32 line = location->range.start.line; + + char *line_text = NULL; + if (last_buffer) { + // read the line from the buffer + if (line < last_buffer->nlines) { + BufferPos pos = {.line = line, .index = 0}; + line_text = buffer_get_utf8_text_at_pos(last_buffer, pos, last_buffer->lines[line].len); + } + } else if (last_file) { + // read the line from the file + while (last_line < line) { + int c = getc(last_file); + if (c == '\n') ++last_line; + if (c == EOF) { + fclose(last_file); + last_file = NULL; + break; + } + } + + line_text = calloc(1, 1024); + if (last_file && last_line == line) { + char *p = line_text; + for (u32 i = 0; i < 1023; ++i, ++p) { + int c = getc(last_file); + if (c == '\n') { + ++last_line; + break; + } + if (c == EOF) { + fclose(last_file); + last_file = NULL; + break; + } + line_text[i] = (char)c; + } + } + } + char text[1024]; - strbuf_printf(text, "%s:%u: \n", - lsp_document_path(lsp, location->document), - location->range.start.line + 1); + strbuf_printf(text, "%s:%u: %s\n", + path, + line + 1, + line_text ? line_text + strspn(line_text, "\t ") : ""); + free(line_text); buffer_insert_utf8_at_cursor(buffer, text); buffer_cursor_move_to_end_of_file(buffer); } + if (last_file) + fclose(last_file); buffer->view_only = true; // the build_dir doesn't really matter since we're using absolute paths diff --git a/lsp-parse.c b/lsp-parse.c index cf590d3..5a1da29 100644 --- a/lsp-parse.c +++ b/lsp-parse.c @@ -799,6 +799,8 @@ static bool parse_highlight(LSP *lsp, const JSON *json, LSPResponse *response) { } static int references_location_cmp(void *context, const void *av, const void *bv) { + // IMPORTANT: don't change this comparison function. + // it matters in ide-usages.c LSP *lsp = context; const LSPLocation *a = av, *b = bv; const char *a_path = lsp_document_path(lsp, a->document); @@ -1,6 +1,5 @@ /* @TODO: -- show line containing usage - hover-auto - handle multiple symbols with same name in go-to-definition menu - :go-to-cursor-definition |