diff options
-rw-r--r-- | lsp-parse.c | 15 | ||||
-rw-r--r-- | lsp.h | 1 | ||||
-rw-r--r-- | ted.c | 27 |
3 files changed, 34 insertions, 9 deletions
diff --git a/lsp-parse.c b/lsp-parse.c index 719b8c6..cf590d3 100644 --- a/lsp-parse.c +++ b/lsp-parse.c @@ -798,6 +798,20 @@ static bool parse_highlight(LSP *lsp, const JSON *json, LSPResponse *response) { return true; } +static int references_location_cmp(void *context, const void *av, const void *bv) { + LSP *lsp = context; + const LSPLocation *a = av, *b = bv; + const char *a_path = lsp_document_path(lsp, a->document); + const char *b_path = lsp_document_path(lsp, b->document); + int cmp = strcmp(a_path, b_path); + if (cmp) return cmp; + u32 a_line = a->range.start.line; + u32 b_line = b->range.start.line; + if (a_line < b_line) return -1; + if (a_line > b_line) return +1; + return 0; +} + static bool parse_references(LSP *lsp, const JSON *json, LSPResponse *response) { LSPResponseReferences *refs = &response->data.references; JSONArray result = json_force_array(json_get(json, "result")); @@ -807,6 +821,7 @@ static bool parse_references(LSP *lsp, const JSON *json, LSPResponse *response) if (!parse_location(lsp, json, location_in, location_out)) return false; } + qsort_with_context(refs->locations, arr_len(refs->locations), sizeof *refs->locations, references_location_cmp, lsp); return true; } @@ -359,6 +359,7 @@ typedef struct { } LSPResponseHighlight; typedef struct { + // these will be sorted by path (alphabetically), then by line number LSPLocation *locations; } LSPResponseReferences; @@ -361,23 +361,32 @@ static Status ted_open_buffer(Ted *ted, u16 *buffer_idx, u16 *tab) { } } - -// Returns true on success -static bool ted_open_file(Ted *ted, char const *filename) { - char path[TED_PATH_MAX]; - ted_path_full(ted, filename, path, sizeof path); - - // first, check if file is already open +// Returns the buffer containing the file at `path`, or NULL if there is none. +static TextBuffer *ted_get_buffer_with_file(Ted *ted, const char *path) { bool *buffers_used = ted->buffers_used; TextBuffer *buffers = ted->buffers; for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) { if (buffers_used[i]) { if (buffers[i].filename && paths_eq(path, buffers[i].filename)) { - ted_switch_to_buffer(ted, &buffers[i]); - return true; + return &buffers[i]; } } } + return NULL; +} + + +// Returns true on success +static bool ted_open_file(Ted *ted, char const *filename) { + char path[TED_PATH_MAX]; + ted_path_full(ted, filename, path, sizeof path); + + // first, check if file is already open + TextBuffer *already_open = ted_get_buffer_with_file(ted, path); + if (already_open) { + ted_switch_to_buffer(ted, already_open); + return true; + } // not open; we need to load it u16 buffer_idx, tab_idx; |