summaryrefslogtreecommitdiff
path: root/ide-usages.c
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 /ide-usages.c
parent2a9f03edf21f8d6d0392a74eb0a6b2507c261a55 (diff)
show line containing usage
Diffstat (limited to 'ide-usages.c')
-rw-r--r--ide-usages.c71
1 files changed, 68 insertions, 3 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