feat(lsp): buffer renames in order to send a single, atomic message

This commit is contained in:
Travis Staloch 2025-01-12 17:32:05 -08:00 committed by CJ van den Berg
parent 1fd4455adb
commit 1c37de6c29
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 84 additions and 49 deletions

View file

@ -782,6 +782,12 @@ fn send_completion_item(_: *Self, to: tp.pid_ref, file_path: []const u8, row: us
}) catch error.ClientFailed; }) catch error.ClientFailed;
} }
const Rename = struct {
uri: []const u8,
new_text: []const u8,
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 || InvalidMessageError || cbor.Error)!void {
const lsp = try self.get_language_server(file_path); const lsp = try self.get_language_server(file_path);
const uri = try self.make_URI(file_path); const uri = try self.make_URI(file_path);
@ -793,16 +799,39 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row:
}) 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;
// buffer the renames in order to send as a single, atomic message
var renames = std.ArrayList(Rename).init(self.allocator);
defer renames.deinit();
if (try cbor.match(response.buf, .{ "child", tp.string, "result", tp.map })) { if (try cbor.match(response.buf, .{ "child", tp.string, "result", tp.map })) {
if (try cbor.match(response.buf, .{ tp.any, tp.any, tp.any, tp.extract_cbor(&result) })) if (try cbor.match(response.buf, .{ tp.any, tp.any, tp.any, tp.extract_cbor(&result) })) {
try self.send_rename_symbol_map(from, result); try self.decode_rename_symbol_map(result, &renames);
// write the renames message manually since there doesn't appear to be an array helper
var m = std.ArrayList(u8).init(self.allocator);
const w = m.writer();
defer m.deinit();
try cbor.writeArrayHeader(w, 3);
try cbor.writeValue(w, "cmd");
try cbor.writeValue(w, "rename_symbol_item");
try cbor.writeArrayHeader(w, renames.items.len);
for (renames.items) |rename| {
try cbor.writeValue(w, .{
rename.uri,
rename.range.start.line,
rename.range.start.character,
rename.range.end.line,
rename.range.end.character,
rename.new_text,
});
}
from.send_raw(.{ .buf = m.items }) catch return error.ClientFailed;
}
} }
} }
// parse a WorkspaceEdit record which may have shape {"changes": {}} or {"documentChanges": []} // decode a WorkspaceEdit record which may have shape {"changes": {}} or {"documentChanges": []}
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspaceEdit // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspaceEdit
fn send_rename_symbol_map(self: *Self, to: tp.pid_ref, result: []const u8) (ClientError || InvalidMessageError || cbor.Error)!void { fn decode_rename_symbol_map(self: *Self, result: []const u8, renames: *std.ArrayList(Rename)) (ClientError || InvalidMessageError || cbor.Error)!void {
var iter = result; var iter = result;
var len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage; var len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage;
var changes: []const u8 = ""; var changes: []const u8 = "";
@ -811,10 +840,12 @@ fn send_rename_symbol_map(self: *Self, to: tp.pid_ref, result: []const u8) (Clie
if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage; if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage;
if (std.mem.eql(u8, field_name, "changes")) { if (std.mem.eql(u8, field_name, "changes")) {
if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&changes)))) return error.InvalidMessageField; if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&changes)))) return error.InvalidMessageField;
return self.send_rename_symbol_changes(to, changes) catch error.ClientFailed; try self.decode_rename_symbol_changes(changes, renames);
return;
} else if (std.mem.eql(u8, field_name, "documentChanges")) { } else if (std.mem.eql(u8, field_name, "documentChanges")) {
if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&changes)))) return error.InvalidMessageField; if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&changes)))) return error.InvalidMessageField;
return self.send_rename_symbol_doc_changes(to, changes) catch error.ClientFailed; try self.decode_rename_symbol_doc_changes(changes, renames);
return;
} else { } else {
try cbor.skipValue(&iter); try cbor.skipValue(&iter);
} }
@ -822,24 +853,23 @@ fn send_rename_symbol_map(self: *Self, to: tp.pid_ref, result: []const u8) (Clie
return error.ClientFailed; return error.ClientFailed;
} }
fn send_rename_symbol_changes(self: *Self, to: tp.pid_ref, changes: []const u8) (ClientError || InvalidMessageError || cbor.Error)!void { fn decode_rename_symbol_changes(self: *Self, changes: []const u8, renames: *std.ArrayList(Rename)) (ClientError || InvalidMessageError || cbor.Error)!void {
// log.logger("lsp").print("send_rename_symbol_changes()\n", .{});
var iter = changes; var iter = changes;
var files_len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage; var files_len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage;
while (files_len > 0) : (files_len -= 1) { while (files_len > 0) : (files_len -= 1) {
var file_uri: []const u8 = undefined; var file_uri: []const u8 = undefined;
if (!(try cbor.matchString(&iter, &file_uri))) return error.InvalidMessage; if (!(try cbor.matchString(&iter, &file_uri))) return error.InvalidMessage;
try send_rename_symbol_item(self, to, file_uri, iter); try decode_rename_symbol_item(self, file_uri, iter, renames);
} }
} }
fn send_rename_symbol_doc_changes(self: *Self, to: tp.pid_ref, changes: []const u8) (ClientError || InvalidMessageError || cbor.Error)!void { fn decode_rename_symbol_doc_changes(self: *Self, changes: []const u8, renames: *std.ArrayList(Rename)) (ClientError || InvalidMessageError || cbor.Error)!void {
var iter = changes; var iter = changes;
var changes_len = cbor.decodeArrayHeader(&iter) catch return error.InvalidMessage; var changes_len = cbor.decodeArrayHeader(&iter) catch return error.InvalidMessage;
while (changes_len > 0) : (changes_len -= 1) { while (changes_len > 0) : (changes_len -= 1) {
var dc_fields_len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage; var dc_fields_len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage;
var file_uri: []const u8 = "";
while (dc_fields_len > 0) : (dc_fields_len -= 1) { while (dc_fields_len > 0) : (dc_fields_len -= 1) {
var file_uri: []const u8 = "";
var field_name: []const u8 = undefined; var field_name: []const u8 = undefined;
if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage; if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage;
if (std.mem.eql(u8, field_name, "textDocument")) { if (std.mem.eql(u8, field_name, "textDocument")) {
@ -853,15 +883,15 @@ fn send_rename_symbol_doc_changes(self: *Self, to: tp.pid_ref, changes: []const
} }
} else if (std.mem.eql(u8, field_name, "edits")) { } else if (std.mem.eql(u8, field_name, "edits")) {
if (file_uri.len == 0) return error.InvalidMessage; if (file_uri.len == 0) return error.InvalidMessage;
try send_rename_symbol_item(self, to, file_uri, iter); try decode_rename_symbol_item(self, file_uri, iter, renames);
} }
} }
} }
} }
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit
fn send_rename_symbol_item(_: *Self, to: tp.pid_ref, file_uri: []const u8, bytes: []const u8) (ClientError || InvalidMessageError || cbor.Error)!void { fn decode_rename_symbol_item(_: *Self, file_uri: []const u8, _iter: []const u8, renames: *std.ArrayList(Rename)) (ClientError || InvalidMessageError || cbor.Error)!void {
var iter = bytes; var iter = _iter;
var text_edits_len = cbor.decodeArrayHeader(&iter) catch return error.InvalidMessage; var text_edits_len = cbor.decodeArrayHeader(&iter) catch return error.InvalidMessage;
while (text_edits_len > 0) : (text_edits_len -= 1) { while (text_edits_len > 0) : (text_edits_len -= 1) {
var m_range: ?Range = null; var m_range: ?Range = null;
@ -882,15 +912,7 @@ fn send_rename_symbol_item(_: *Self, to: tp.pid_ref, file_uri: []const u8, bytes
} }
const range = m_range orelse return error.InvalidMessageField; const range = m_range orelse return error.InvalidMessageField;
try renames.append(.{ .uri = file_uri, .range = range, .new_text = new_text });
to.send(.{ "cmd", "rename_symbol_item", .{
file_uri,
range.start.line,
range.start.character,
range.end.line,
range.end.character,
new_text,
} }) catch return error.ClientFailed;
} }
} }

View file

@ -4323,13 +4323,18 @@ pub const Editor = struct {
self.need_render(); self.need_render();
} }
pub fn rename_symbol_item(self: *Self, sel: Selection, new_text: []const u8) Result { 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; self.get_primary().selection = sel;
const buf = try self.buf_for_update(); if (root_prev.* == null) root_prev.* = (try self.buf_for_update()).root;
const r1 = try self.delete_selection(buf.root, self.get_primary(), self.allocator); const root = try self.delete_selection(root_prev.*.?, self.get_primary(), self.allocator);
const r2 = try self.insert(r1, self.get_primary(), new_text, self.allocator); root_prev.* = try self.insert(root, self.get_primary(), new_text, self.allocator);
try self.update_buf(r2); if (final) try self.update_buf(root_prev.*.?);
self.need_render();
} }
pub fn select(self: *Self, ctx: Context) Result { pub fn select(self: *Self, ctx: Context) Result {

View file

@ -555,26 +555,34 @@ 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 {
var file_uri: []const u8 = undefined; // because the incoming message is an array of Renames, we manuallly
var sel: ed.Selection = .{}; // parse instead of using ctx.args.match() which doesn't seem to return
var new_text: []const u8 = undefined; // the parsed length needed to correctly advance iter.
if (!try ctx.args.match(.{ var iter = ctx.args.buf;
tp.extract(&file_uri), var len = try cbor.decodeArrayHeader(&iter);
tp.extract(&sel.begin.row), var mroot: ?@import("Buffer").Root = null;
tp.extract(&sel.begin.col), while (len != 0) {
tp.extract(&sel.end.row), var file_uri: []const u8 = undefined;
tp.extract(&sel.end.col), var sel: ed.Selection = .{};
tp.extract(&new_text), var new_text: []const u8 = undefined;
})) return error.InvalidRenameSymbolArgument; len -= 1;
file_uri = project_manager.normalize_file_path(file_uri); std.debug.assert(try cbor.decodeArrayHeader(&iter) == 6);
if (self.get_active_editor()) |editor| { if (!try cbor.matchString(&iter, &file_uri)) return error.MissingArgument;
// TODO match correctly. endsWith() isn't correct because path is a if (!try cbor.matchInt(usize, &iter, &sel.begin.row)) return error.MissingArgument;
// short, relative path while file_uri is an absolute path starting with 'file://' if (!try cbor.matchInt(usize, &iter, &sel.begin.col)) return error.MissingArgument;
const match = if (editor.file_path) |path| std.mem.endsWith(u8, file_uri, path) else false; if (!try cbor.matchInt(usize, &iter, &sel.end.row)) return error.MissingArgument;
if (match) { if (!try cbor.matchInt(usize, &iter, &sel.end.col)) return error.MissingArgument;
try editor.rename_symbol_item(sel, new_text); if (!try cbor.matchString(&iter, &new_text)) return error.MissingArgument;
} else { file_uri = project_manager.normalize_file_path(file_uri);
// TODO perform renames in other files if (self.get_active_editor()) |editor| {
// TODO match file_uri correctly. endsWith() isn't correct because 'path' is a
// short, relative path while 'file_uri' is an absolute path starting with 'file://'
const match = if (editor.file_path) |path| std.mem.endsWith(u8, file_uri, path) else false;
if (match) {
try editor.rename_symbol_item(sel, new_text, &mroot, len == 0);
} else {
log.logger("LSP").print("TODO perform renames in other files\n", .{});
}
} }
} }
} }