feat(lsp): rename_symbol: add cursors at rename points for interactive rename

This commit is contained in:
CJ van den Berg 2025-01-16 22:23:03 +01:00
parent e597fee2e5
commit a449e0ec97
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 15 additions and 19 deletions

View file

@ -795,7 +795,7 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row:
const response = lsp.send_request(self.allocator, "textDocument/rename", .{ const response = lsp.send_request(self.allocator, "textDocument/rename", .{
.textDocument = .{ .uri = uri }, .textDocument = .{ .uri = uri },
.position = .{ .line = row, .character = col }, .position = .{ .line = row, .character = col },
.newName = "foobar", .newName = "PLACEHOLDER",
}) catch return error.LspFailed; }) catch return error.LspFailed;
defer self.allocator.free(response.buf); defer self.allocator.free(response.buf);
var result: []const u8 = undefined; var result: []const u8 = undefined;

View file

@ -4229,6 +4229,14 @@ pub const Editor = struct {
} }
pub const rename_symbol_meta = .{ .description = "Language: Rename symbol at cursor" }; pub const rename_symbol_meta = .{ .description = "Language: Rename symbol at cursor" };
pub fn add_rename_symbol_cursor(self: *Self, sel: Selection, first: bool) !void {
if (first) self.cancel_all_selections() else try self.push_cursor();
const primary = self.get_primary();
primary.selection = sel;
primary.cursor = sel.end;
self.need_render();
}
pub fn hover(self: *Self, _: Context) Result { pub fn hover(self: *Self, _: Context) Result {
const primary = self.get_primary(); const primary = self.get_primary();
return self.hover_at(primary.cursor.row, primary.cursor.col); return self.hover_at(primary.cursor.row, primary.cursor.col);
@ -4323,20 +4331,6 @@ pub const Editor = struct {
self.need_render(); self.need_render();
} }
pub fn rename_symbol_item(
self: *Self,
sel: Selection,
new_text: []const u8,
root_prev: *?Buffer.Root,
final: bool,
) !void {
self.get_primary().selection = sel;
if (root_prev.* == null) root_prev.* = (try self.buf_for_update()).root;
const root = try self.delete_selection(root_prev.*.?, self.get_primary(), self.allocator);
root_prev.* = try self.insert(root, self.get_primary(), new_text, self.allocator);
if (final) try self.update_buf(root_prev.*.?);
}
pub fn select(self: *Self, ctx: Context) Result { pub fn select(self: *Self, ctx: Context) Result {
var sel: Selection = .{}; var sel: Selection = .{};
if (!try ctx.args.match(.{ tp.extract(&sel.begin.row), tp.extract(&sel.begin.col), tp.extract(&sel.end.row), tp.extract(&sel.end.col) })) if (!try ctx.args.match(.{ tp.extract(&sel.begin.row), tp.extract(&sel.begin.col), tp.extract(&sel.end.row), tp.extract(&sel.end.col) }))

View file

@ -555,12 +555,13 @@ const cmds = struct {
pub const add_diagnostic_meta = .{ .arguments = &.{ .string, .string, .string, .string, .integer, .integer, .integer, .integer, .integer } }; pub const add_diagnostic_meta = .{ .arguments = &.{ .string, .string, .string, .string, .integer, .integer, .integer, .integer, .integer } };
pub fn rename_symbol_item(self: *Self, ctx: Ctx) Result { pub fn rename_symbol_item(self: *Self, ctx: Ctx) Result {
const editor = self.get_active_editor() orelse return;
// because the incoming message is an array of Renames, we manuallly // because the incoming message is an array of Renames, we manuallly
// parse instead of using ctx.args.match() which doesn't seem to return // parse instead of using ctx.args.match() which doesn't seem to return
// the parsed length needed to correctly advance iter. // the parsed length needed to correctly advance iter.
var iter = ctx.args.buf; var iter = ctx.args.buf;
var len = try cbor.decodeArrayHeader(&iter); var len = try cbor.decodeArrayHeader(&iter);
var mroot: ?@import("Buffer").Root = null; var first = true;
while (len != 0) { while (len != 0) {
len -= 1; len -= 1;
std.debug.assert(try cbor.decodeArrayHeader(&iter) == 6); std.debug.assert(try cbor.decodeArrayHeader(&iter) == 6);
@ -575,13 +576,14 @@ const cmds = struct {
if (!try cbor.matchString(&iter, &new_text)) return error.MissingArgument; if (!try cbor.matchString(&iter, &new_text)) return error.MissingArgument;
file_path = project_manager.normalize_file_path(file_path); file_path = project_manager.normalize_file_path(file_path);
if (self.get_active_editor()) |editor| if (std.mem.eql(u8, file_path, editor.file_path orelse "")) { if (std.mem.eql(u8, file_path, editor.file_path orelse "")) {
try editor.rename_symbol_item(sel, new_text, &mroot, len == 0); try editor.add_rename_symbol_cursor(sel, first);
first = false;
} else { } else {
const logger = log.logger("LSP"); const logger = log.logger("LSP");
defer logger.deinit(); defer logger.deinit();
logger.print("TODO perform renames in other files\n", .{}); logger.print("TODO perform renames in other files\n", .{});
}; }
} }
} }
pub const rename_symbol_item_meta = .{ .arguments = &.{.array} }; pub const rename_symbol_item_meta = .{ .arguments = &.{.array} };