diff --git a/src/Project.zig b/src/Project.zig index 9c3a1bc..ebf4701 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -422,6 +422,75 @@ pub fn goto_definition(self: *Self, from: tp.pid_ref, file_path: []const u8, row } } +pub fn goto_declaration(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/declaration", .{ + .textDocument = .{ .uri = uri }, + .position = .{ .line = row, .character = col }, + }); + defer self.a.free(response.buf); + var link: []const u8 = undefined; + if (try response.match(.{ "child", tp.string, "result", tp.array })) { + if (try response.match(.{ tp.any, tp.any, tp.any, .{ tp.extract_cbor(&link), tp.more } })) { + try self.navigate_to_location_link(from, link); + } else if (try response.match(.{ tp.any, tp.any, tp.any, .{tp.extract_cbor(&link)} })) { + try self.navigate_to_location_link(from, link); + } + } else if (try response.match(.{ "child", tp.string, "result", tp.null_ })) { + return; + } else if (try response.match(.{ "child", tp.string, "result", tp.extract_cbor(&link) })) { + try self.navigate_to_location_link(from, link); + } +} + +pub fn goto_implementation(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/implementation", .{ + .textDocument = .{ .uri = uri }, + .position = .{ .line = row, .character = col }, + }); + defer self.a.free(response.buf); + var link: []const u8 = undefined; + if (try response.match(.{ "child", tp.string, "result", tp.array })) { + if (try response.match(.{ tp.any, tp.any, tp.any, .{ tp.extract_cbor(&link), tp.more } })) { + try self.navigate_to_location_link(from, link); + } else if (try response.match(.{ tp.any, tp.any, tp.any, .{tp.extract_cbor(&link)} })) { + try self.navigate_to_location_link(from, link); + } + } else if (try response.match(.{ "child", tp.string, "result", tp.null_ })) { + return; + } else if (try response.match(.{ "child", tp.string, "result", tp.extract_cbor(&link) })) { + try self.navigate_to_location_link(from, link); + } +} + +pub fn goto_type_definition(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/typeDefinition", .{ + .textDocument = .{ .uri = uri }, + .position = .{ .line = row, .character = col }, + }); + defer self.a.free(response.buf); + var link: []const u8 = undefined; + if (try response.match(.{ "child", tp.string, "result", tp.array })) { + if (try response.match(.{ tp.any, tp.any, tp.any, .{ tp.extract_cbor(&link), tp.more } })) { + try self.navigate_to_location_link(from, link); + } else if (try response.match(.{ tp.any, tp.any, tp.any, .{tp.extract_cbor(&link)} })) { + try self.navigate_to_location_link(from, link); + } + } else if (try response.match(.{ "child", tp.string, "result", tp.null_ })) { + return; + } else if (try response.match(.{ "child", tp.string, "result", tp.extract_cbor(&link) })) { + try self.navigate_to_location_link(from, link); + } +} + fn navigate_to_location_link(_: *Self, from: tp.pid_ref, location_link: []const u8) !void { var iter = location_link; var targetUri: ?[]const u8 = null; diff --git a/src/project_manager.zig b/src/project_manager.zig index e02b5bb..d52d76f 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -110,6 +110,27 @@ 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 goto_declaration(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(.{ "goto_declaration", project, file_path, row, col }); +} + +pub fn goto_implementation(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(.{ "goto_implementation", project, file_path, row, col }); +} + +pub fn goto_type_definition(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(.{ "goto_type_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) @@ -254,6 +275,12 @@ 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(.{ "goto_declaration", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) { + self.goto_declaration(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()); + } else if (try m.match(.{ "goto_implementation", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) { + self.goto_implementation(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()); + } else if (try m.match(.{ "goto_type_definition", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) { + self.goto_type_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) })) { @@ -367,6 +394,27 @@ const Process = struct { return project.goto_definition(from, file_path, row, col); } + fn goto_declaration(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 ++ ".goto_declaration" }); + defer frame.deinit(); + const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project"); + return project.goto_declaration(from, file_path, row, col); + } + + fn goto_implementation(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 ++ ".goto_implementation" }); + defer frame.deinit(); + const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project"); + return project.goto_implementation(from, file_path, row, col); + } + + fn goto_type_definition(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 ++ ".goto_type_definition" }); + defer frame.deinit(); + const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project"); + return project.goto_type_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(); diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 13da539..b0def65 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -3428,6 +3428,24 @@ pub const Editor = struct { return project_manager.goto_definition(file_path, primary.cursor.row, primary.cursor.col); } + pub fn goto_declaration(self: *Self, _: Context) Result { + const file_path = self.file_path orelse return; + const primary = self.get_primary(); + return project_manager.goto_declaration(file_path, primary.cursor.row, primary.cursor.col); + } + + pub fn goto_implementation(self: *Self, _: Context) Result { + const file_path = self.file_path orelse return; + const primary = self.get_primary(); + return project_manager.goto_implementation(file_path, primary.cursor.row, primary.cursor.col); + } + + pub fn goto_type_definition(self: *Self, _: Context) Result { + const file_path = self.file_path orelse return; + const primary = self.get_primary(); + return project_manager.goto_type_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(); diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index 7ad80f7..da266a4 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -373,6 +373,13 @@ fn mapFollower(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { 'G' => switch (modifiers) { 0 => switch (keypress) { 'G' => self.cmd("move_buffer_begin", .{}), + 'D' => self.cmd("goto_definition", .{}), + 'I' => self.cmd("goto_implementation", .{}), + 'Y' => self.cmd("goto_type_definition", .{}), + else => {}, + }, + mod.SHIFT => switch (keypress) { + 'D' => self.cmd("goto_declaration", .{}), else => {}, }, else => {}, @@ -523,7 +530,10 @@ const hints = tui.KeybindHints.initComptime(.{ .{ "filter", "A-s" }, // self.cmd("filter", command.fmt(.{"sort"})), // .{ "filter", "S-A-s" }, // self.cmd("filter", command.fmt(.{ "sort", "-u" })), .{ "format", "S-A-f" }, - .{ "goto_definition", "F12" }, + .{ "goto_definition", "F12, g d" }, + .{ "goto_declaration", "g D" }, + .{ "goto_implementation", "g i" }, + .{ "goto_type_definition", "g y" }, .{ "goto_next_file_or_diagnostic", "A-n" }, .{ "goto_next_match", "C-n, F3, n" }, .{ "goto_prev_file_or_diagnostic", "A-p" },