add more goto commands

This commit is contained in:
xdBronch 2024-08-24 20:31:08 -04:00 committed by CJ van den Berg
parent 0758838cce
commit 9e59e808ba
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 146 additions and 1 deletions

View file

@ -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 { fn navigate_to_location_link(_: *Self, from: tp.pid_ref, location_link: []const u8) !void {
var iter = location_link; var iter = location_link;
var targetUri: ?[]const u8 = null; var targetUri: ?[]const u8 = null;

View file

@ -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 }); 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 { pub fn references(file_path: []const u8, row: usize, col: usize) !void {
const project = tp.env.get().str("project"); const project = tp.env.get().str("project");
if (project.len == 0) 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()); 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) })) { } 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()); 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) })) { } 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()); 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) })) { } 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); 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 { 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" }); const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".references" });
defer frame.deinit(); defer frame.deinit();

View file

@ -3428,6 +3428,24 @@ pub const Editor = struct {
return project_manager.goto_definition(file_path, primary.cursor.row, primary.cursor.col); 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 { pub fn references(self: *Self, _: Context) Result {
const file_path = self.file_path orelse return; const file_path = self.file_path orelse return;
const primary = self.get_primary(); const primary = self.get_primary();

View file

@ -373,6 +373,13 @@ fn mapFollower(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
'G' => switch (modifiers) { 'G' => switch (modifiers) {
0 => switch (keypress) { 0 => switch (keypress) {
'G' => self.cmd("move_buffer_begin", .{}), '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 => {},
}, },
else => {}, else => {},
@ -523,7 +530,10 @@ const hints = tui.KeybindHints.initComptime(.{
.{ "filter", "A-s" }, // self.cmd("filter", command.fmt(.{"sort"})), .{ "filter", "A-s" }, // self.cmd("filter", command.fmt(.{"sort"})),
// .{ "filter", "S-A-s" }, // self.cmd("filter", command.fmt(.{ "sort", "-u" })), // .{ "filter", "S-A-s" }, // self.cmd("filter", command.fmt(.{ "sort", "-u" })),
.{ "format", "S-A-f" }, .{ "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_file_or_diagnostic", "A-n" },
.{ "goto_next_match", "C-n, F3, n" }, .{ "goto_next_match", "C-n, F3, n" },
.{ "goto_prev_file_or_diagnostic", "A-p" }, .{ "goto_prev_file_or_diagnostic", "A-p" },