From 78489e31f6fada3f3cd868cf2de77f9a4b5ad54c Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 16 Aug 2024 00:02:42 +0200 Subject: [PATCH] feat: add find all references LSP command --- src/Project.zig | 75 +++++++++++++++++++++++++++++++++++++ src/project_manager.zig | 16 ++++++++ src/tui/editor.zig | 6 +++ src/tui/mode/input/flow.zig | 2 + 4 files changed, 99 insertions(+) diff --git a/src/Project.zig b/src/Project.zig index ebc41a1..4d39a23 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -469,6 +469,81 @@ fn navigate_to_location_link(_: *Self, from: tp.pid_ref, location_link: []const } } +pub fn references(self: *Self, from: tp.pid_ref, file_path: []const u8, row: usize, col: usize) !void { + const lsp = try self.get_file_lsp(file_path); + const uri = try self.make_URI(file_path); + defer self.a.free(uri); + const response = try lsp.send_request(self.a, "textDocument/references", .{ + .textDocument = .{ .uri = uri }, + .position = .{ .line = row, .character = col }, + .context = .{ .includeDeclaration = true }, + }); + defer self.a.free(response.buf); + var locations: []const u8 = undefined; + if (try response.match(.{ "child", tp.string, "result", tp.null_ })) { + return; + } else if (try response.match(.{ "child", tp.string, "result", tp.extract_cbor(&locations) })) { + try self.send_location_list(from, locations); + } +} + +fn send_location_list(self: *Self, to: tp.pid_ref, locations: []const u8) !void { + var iter = locations; + var len = try cbor.decodeArrayHeader(&iter); + while (len > 0) : (len -= 1) { + var location: []const u8 = undefined; + if (try cbor.matchValue(&iter, cbor.extract_cbor(&location))) { + try self.send_location(to, location); + } else return error.InvalidMessageField; + } +} + +fn send_location(_: *Self, to: tp.pid_ref, location: []const u8) !void { + var iter = location; + var targetUri: ?[]const u8 = null; + var targetRange: ?Range = null; + var targetSelectionRange: ?Range = null; + var len = try cbor.decodeMapHeader(&iter); + while (len > 0) : (len -= 1) { + var field_name: []const u8 = undefined; + if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage; + if (std.mem.eql(u8, field_name, "targetUri") or std.mem.eql(u8, field_name, "uri")) { + var value: []const u8 = undefined; + if (!(try cbor.matchValue(&iter, cbor.extract(&value)))) return error.InvalidMessageField; + targetUri = value; + } else if (std.mem.eql(u8, field_name, "targetRange") or std.mem.eql(u8, field_name, "range")) { + var range: []const u8 = undefined; + if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&range)))) return error.InvalidMessageField; + targetRange = try read_range(range); + } else if (std.mem.eql(u8, field_name, "targetSelectionRange")) { + var range: []const u8 = undefined; + if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&range)))) return error.InvalidMessageField; + targetSelectionRange = try read_range(range); + } else { + try cbor.skipValue(&iter); + } + } + if (targetUri == null or targetRange == null) return error.InvalidMessageField; + if (!std.mem.eql(u8, targetUri.?[0..7], "file://")) return error.InvalidTargetURI; + var file_path_buf: [std.fs.max_path_bytes]u8 = undefined; + var file_path = std.Uri.percentDecodeBackwards(&file_path_buf, targetUri.?[7..]); + if (builtin.os.tag == .windows) { + if (file_path[0] == '/') file_path = file_path[1..]; + for (file_path, 0..) |c, i| if (c == '/') { + file_path[i] = '\\'; + }; + } + try to.send(.{ + "FIF", + file_path, + targetRange.?.start.line + 1, + targetRange.?.start.character + 1, + targetRange.?.start.line + 1, + targetRange.?.start.character + 1, + "", + }); +} + pub fn completion(self: *Self, _: tp.pid_ref, file_path: []const u8, row: usize, col: usize) !void { const lsp = try self.get_file_lsp(file_path); const uri = try self.make_URI(file_path); diff --git a/src/project_manager.zig b/src/project_manager.zig index 5801a9e..1dec7a4 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -98,6 +98,13 @@ pub fn goto_definition(file_path: []const u8, row: usize, col: usize) !void { return (try get()).pid.send(.{ "goto_definition", project, file_path, row, col }); } +pub fn references(file_path: []const u8, row: usize, col: usize) !void { + const project = tp.env.get().str("project"); + if (project.len == 0) + return tp.exit("No project"); + return (try get()).pid.send(.{ "references", project, file_path, row, col }); +} + pub fn completion(file_path: []const u8, row: usize, col: usize) !void { const project = tp.env.get().str("project"); if (project.len == 0) @@ -227,6 +234,8 @@ const Process = struct { self.did_close(project_directory, path) catch |e| return from.forward_error(e, @errorReturnTrace()); } else if (try m.match(.{ "goto_definition", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) { self.goto_definition(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()); + } else if (try m.match(.{ "references", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) { + self.references(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()); } else if (try m.match(.{ "completion", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) { self.completion(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()); } else if (try m.match(.{ "get_mru_position", tp.extract(&project_directory), tp.extract(&path) })) { @@ -317,6 +326,13 @@ const Process = struct { return project.goto_definition(from, file_path, row, col); } + fn references(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8, row: usize, col: usize) !void { + const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".references" }); + defer frame.deinit(); + const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project"); + return project.references(from, file_path, row, col); + } + fn completion(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8, row: usize, col: usize) !void { const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".completion" }); defer frame.deinit(); diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 557237d..0f5dd5a 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -3425,6 +3425,12 @@ pub const Editor = struct { return project_manager.goto_definition(file_path, primary.cursor.row, primary.cursor.col); } + pub fn references(self: *Self, _: Context) Result { + const file_path = self.file_path orelse return; + const primary = self.get_primary(); + return project_manager.references(file_path, primary.cursor.row, primary.cursor.col); + } + pub fn completion(self: *Self, _: Context) Result { const file_path = self.file_path orelse return; const primary = self.get_primary(); diff --git a/src/tui/mode/input/flow.zig b/src/tui/mode/input/flow.zig index 27e58b2..6b55d53 100644 --- a/src/tui/mode/input/flow.zig +++ b/src/tui/mode/input/flow.zig @@ -175,6 +175,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { }, mod.SHIFT => switch (keypress) { key.F03 => self.cmd("goto_prev_match", .{}), + key.F12 => self.cmd("references", .{}), key.LEFT => self.cmd("select_left", .{}), key.RIGHT => self.cmd("select_right", .{}), key.UP => self.cmd("select_up", .{}), @@ -374,6 +375,7 @@ const hints = tui.KeybindHints.initComptime(.{ .{ "quit", "C-q" }, .{ "quit_without_saving", "C-S-q" }, .{ "redo", "C-S-z, C-y" }, + .{ "references", "S-F12" }, .{ "save_file", "C-s" }, .{ "scroll_view_bottom", "C-l" }, .{ "scroll_view_center", "C-l" },