summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-31 15:34:24 -0500
committerpommicket <pommicket@gmail.com>2022-12-31 15:34:24 -0500
commit992315198b510e210a7791f21953bf0e27786108 (patch)
tree079286001f5ce96f04c507293316bb2dbbee2270
parent2a9f03edf21f8d6d0392a74eb0a6b2507c261a55 (diff)
show line containing usage
-rw-r--r--ide-usages.c71
-rw-r--r--lsp-parse.c2
-rw-r--r--main.c1
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);
diff --git a/main.c b/main.c
index 5576227..c2e4904 100644
--- a/main.c
+++ b/main.c
@@ -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