feat(lsp): rename_symbol: treat out-of-file edits as references

This commit is contained in:
CJ van den Berg 2025-01-16 23:02:52 +01:00
parent a449e0ec97
commit 155c1f663d
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 18 additions and 7 deletions

View file

@ -788,7 +788,7 @@ const Rename = struct {
range: Range,
};
pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row: usize, col: usize) (LspOrClientError || InvalidMessageError || cbor.Error)!void {
pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row: usize, col: usize) (LspOrClientError || GetLineOfFileError || InvalidMessageError || cbor.Error)!void {
const lsp = try self.get_language_server(file_path);
const uri = try self.make_URI(file_path);
defer self.allocator.free(uri);
@ -817,6 +817,7 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row:
for (renames.items) |rename| {
var file_path_buf: [std.fs.max_path_bytes]u8 = undefined;
const file_path_ = std.Uri.percentDecodeBackwards(&file_path_buf, rename.uri[7..]);
const line = try self.get_line_of_file(self.allocator, file_path, rename.range.start.line);
try cbor.writeValue(w, .{
file_path_,
rename.range.start.line,
@ -824,6 +825,7 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row:
rename.range.end.line,
rename.range.end.character,
rename.new_text,
line,
});
}
from.send_raw(.{ .buf = msg_buf.items }) catch return error.ClientFailed;
@ -1532,7 +1534,7 @@ fn format_lsp_name_func(
const eol = '\n';
const GetLineOfFileError = (OutOfMemoryError || std.fs.File.OpenError || std.fs.File.Reader.Error);
pub const GetLineOfFileError = (OutOfMemoryError || std.fs.File.OpenError || std.fs.File.Reader.Error);
fn get_line_of_file(self: *Self, allocator: std.mem.Allocator, file_path: []const u8, line_: usize) GetLineOfFileError![]const u8 {
const line = line_ + 1;

View file

@ -501,7 +501,7 @@ const Process = struct {
return project.completion(from, file_path, row, col);
}
fn rename_symbol(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8, row: usize, col: usize) (ProjectError || Project.InvalidMessageError || Project.LspOrClientError || cbor.Error)!void {
fn rename_symbol(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8, row: usize, col: usize) (ProjectError || Project.InvalidMessageError || Project.LspOrClientError || Project.GetLineOfFileError || cbor.Error)!void {
const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".rename_symbol" });
defer frame.deinit();
const project = self.projects.get(project_directory) orelse return error.NoProject;

View file

@ -564,7 +564,7 @@ const cmds = struct {
var first = true;
while (len != 0) {
len -= 1;
std.debug.assert(try cbor.decodeArrayHeader(&iter) == 6);
if (try cbor.decodeArrayHeader(&iter) != 7) return error.InvalidRenameSymbolItemArgument;
var file_path: []const u8 = undefined;
if (!try cbor.matchString(&iter, &file_path)) return error.MissingArgument;
var sel: ed.Selection = .{};
@ -574,15 +574,24 @@ const cmds = struct {
if (!try cbor.matchInt(usize, &iter, &sel.end.col)) return error.MissingArgument;
var new_text: []const u8 = undefined;
if (!try cbor.matchString(&iter, &new_text)) return error.MissingArgument;
var line_text: []const u8 = undefined;
if (!try cbor.matchString(&iter, &line_text)) return error.MissingArgument;
file_path = project_manager.normalize_file_path(file_path);
if (std.mem.eql(u8, file_path, editor.file_path orelse "")) {
try editor.add_rename_symbol_cursor(sel, first);
first = false;
} else {
const logger = log.logger("LSP");
defer logger.deinit();
logger.print("TODO perform renames in other files\n", .{});
try self.add_find_in_files_result(
.references,
file_path,
sel.begin.row + 1,
sel.begin.col,
sel.end.row + 1,
sel.end.col,
line_text,
.Information,
);
}
}
}