From 992315198b510e210a7791f21953bf0e27786108 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 31 Dec 2022 15:34:24 -0500 Subject: show line containing usage --- ide-usages.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 3 deletions(-) (limited to 'ide-usages.c') 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 -- cgit v1.2.3