feat(lsp): initial support for textDocument/rename
flow keybinds: changes f2 from toggle_input_mode to rename_symbol and moves toggle_input_mode command to ctrl+shift+f2 (since ctrl+f2 is already bound to insert_command_name) the replacement text is hard coded for now. i've checked that replace works with zls and pylsp which send WorkspaceEdit response messages in different shapes - zls sends shape `{"changes": {}}` while pylsp sends `{"documentChanges": []}`. currently the 'rename_symbol_item' commands are sent one at a time. however they should be buffered and be performed between one buf_for_update, update_buf pair. this will be addressed in a follow up.
This commit is contained in:
parent
7558a63819
commit
1fd4455adb
5 changed files with 172 additions and 1 deletions
112
src/Project.zig
112
src/Project.zig
|
@ -782,6 +782,118 @@ fn send_completion_item(_: *Self, to: tp.pid_ref, file_path: []const u8, row: us
|
|||
}) catch error.ClientFailed;
|
||||
}
|
||||
|
||||
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 uri = try self.make_URI(file_path);
|
||||
defer self.allocator.free(uri);
|
||||
const response = lsp.send_request(self.allocator, "textDocument/rename", .{
|
||||
.textDocument = .{ .uri = uri },
|
||||
.position = .{ .line = row, .character = col },
|
||||
.newName = "foobar",
|
||||
}) catch return error.LspFailed;
|
||||
defer self.allocator.free(response.buf);
|
||||
var result: []const u8 = undefined;
|
||||
|
||||
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) }))
|
||||
try self.send_rename_symbol_map(from, result);
|
||||
}
|
||||
}
|
||||
|
||||
// parse a WorkspaceEdit record which may have shape {"changes": {}} or {"documentChanges": []}
|
||||
// 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 {
|
||||
var iter = result;
|
||||
var len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage;
|
||||
var changes: []const u8 = "";
|
||||
while (len > 0) : (len -= 1) {
|
||||
var field_name: []const u8 = undefined;
|
||||
if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage;
|
||||
if (std.mem.eql(u8, field_name, "changes")) {
|
||||
if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&changes)))) return error.InvalidMessageField;
|
||||
return self.send_rename_symbol_changes(to, changes) catch error.ClientFailed;
|
||||
} else if (std.mem.eql(u8, field_name, "documentChanges")) {
|
||||
if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&changes)))) return error.InvalidMessageField;
|
||||
return self.send_rename_symbol_doc_changes(to, changes) catch error.ClientFailed;
|
||||
} else {
|
||||
try cbor.skipValue(&iter);
|
||||
}
|
||||
}
|
||||
return error.ClientFailed;
|
||||
}
|
||||
|
||||
fn send_rename_symbol_changes(self: *Self, to: tp.pid_ref, changes: []const u8) (ClientError || InvalidMessageError || cbor.Error)!void {
|
||||
// log.logger("lsp").print("send_rename_symbol_changes()\n", .{});
|
||||
var iter = changes;
|
||||
var files_len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage;
|
||||
while (files_len > 0) : (files_len -= 1) {
|
||||
var file_uri: []const u8 = undefined;
|
||||
if (!(try cbor.matchString(&iter, &file_uri))) return error.InvalidMessage;
|
||||
try send_rename_symbol_item(self, to, file_uri, iter);
|
||||
}
|
||||
}
|
||||
|
||||
fn send_rename_symbol_doc_changes(self: *Self, to: tp.pid_ref, changes: []const u8) (ClientError || InvalidMessageError || cbor.Error)!void {
|
||||
var iter = changes;
|
||||
var changes_len = cbor.decodeArrayHeader(&iter) catch return error.InvalidMessage;
|
||||
while (changes_len > 0) : (changes_len -= 1) {
|
||||
var dc_fields_len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage;
|
||||
while (dc_fields_len > 0) : (dc_fields_len -= 1) {
|
||||
var file_uri: []const u8 = "";
|
||||
var field_name: []const u8 = undefined;
|
||||
if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage;
|
||||
if (std.mem.eql(u8, field_name, "textDocument")) {
|
||||
var td_fields_len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage;
|
||||
while (td_fields_len > 0) : (td_fields_len -= 1) {
|
||||
var td_field_name: []const u8 = undefined;
|
||||
if (!(try cbor.matchString(&iter, &td_field_name))) return error.InvalidMessage;
|
||||
if (std.mem.eql(u8, td_field_name, "uri")) {
|
||||
if (!(try cbor.matchString(&iter, &file_uri))) return error.InvalidMessage;
|
||||
} else try cbor.skipValue(&iter); // skip "version": 1
|
||||
}
|
||||
} else if (std.mem.eql(u8, field_name, "edits")) {
|
||||
if (file_uri.len == 0) return error.InvalidMessage;
|
||||
try send_rename_symbol_item(self, to, file_uri, iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
var iter = bytes;
|
||||
var text_edits_len = cbor.decodeArrayHeader(&iter) catch return error.InvalidMessage;
|
||||
while (text_edits_len > 0) : (text_edits_len -= 1) {
|
||||
var m_range: ?Range = null;
|
||||
var new_text: []const u8 = "";
|
||||
var edits_len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage;
|
||||
while (edits_len > 0) : (edits_len -= 1) {
|
||||
var field_name: []const u8 = undefined;
|
||||
if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage;
|
||||
if (std.mem.eql(u8, field_name, "range")) {
|
||||
var range: []const u8 = undefined;
|
||||
if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&range)))) return error.InvalidMessageField;
|
||||
m_range = try read_range(range);
|
||||
} else if (std.mem.eql(u8, field_name, "newText")) {
|
||||
if (!(try cbor.matchString(&iter, &new_text))) return error.InvalidMessageField;
|
||||
} else {
|
||||
try cbor.skipValue(&iter);
|
||||
}
|
||||
}
|
||||
|
||||
const range = m_range orelse return error.InvalidMessageField;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hover(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 uri = try self.make_URI(file_path);
|
||||
|
|
|
@ -128,7 +128,8 @@
|
|||
["shift+enter", "smart_insert_line_before"],
|
||||
["shift+backspace", "delete_backward"],
|
||||
["shift+tab", "unindent"],
|
||||
["f2", "toggle_input_mode"],
|
||||
["f2", "rename_symbol"],
|
||||
["ctrl+shift+f2", "toggle_input_mode"],
|
||||
["ctrl+f2", "insert_command_name"],
|
||||
["f3", "goto_next_match"],
|
||||
["f15", "goto_prev_match"],
|
||||
|
|
|
@ -176,6 +176,13 @@ pub fn completion(file_path: []const u8, row: usize, col: usize) (ProjectManager
|
|||
return send(.{ "completion", project, file_path, row, col });
|
||||
}
|
||||
|
||||
pub fn rename_symbol(file_path: []const u8, row: usize, col: usize) (ProjectManagerError || ProjectError)!void {
|
||||
const project = tp.env.get().str("project");
|
||||
if (project.len == 0)
|
||||
return error.NoProject;
|
||||
return send(.{ "rename_symbol", project, file_path, row, col });
|
||||
}
|
||||
|
||||
pub fn hover(file_path: []const u8, row: usize, col: usize) (ProjectManagerError || ProjectError)!void {
|
||||
const project = tp.env.get().str("project");
|
||||
if (project.len == 0)
|
||||
|
@ -339,6 +346,8 @@ const Process = struct {
|
|||
self.references(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
|
||||
} else if (try cbor.match(m.buf, .{ "completion", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) {
|
||||
self.completion(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
|
||||
} else if (try cbor.match(m.buf, .{ "rename_symbol", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) {
|
||||
self.rename_symbol(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
|
||||
} else if (try cbor.match(m.buf, .{ "hover", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) {
|
||||
self.hover(from, project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
|
||||
} else if (try cbor.match(m.buf, .{ "get_mru_position", tp.extract(&project_directory), tp.extract(&path) })) {
|
||||
|
@ -492,6 +501,13 @@ 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 {
|
||||
const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".rename_symbol" });
|
||||
defer frame.deinit();
|
||||
const project = self.projects.get(project_directory) orelse return error.NoProject;
|
||||
return project.rename_symbol(from, file_path, row, col);
|
||||
}
|
||||
|
||||
fn hover(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 {
|
||||
const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".hover" });
|
||||
defer frame.deinit();
|
||||
|
|
|
@ -4222,6 +4222,13 @@ pub const Editor = struct {
|
|||
}
|
||||
pub const completion_meta = .{ .description = "Language: Show completions at cursor" };
|
||||
|
||||
pub fn rename_symbol(self: *Self, _: Context) Result {
|
||||
const file_path = self.file_path orelse return;
|
||||
const primary = self.get_primary();
|
||||
return project_manager.rename_symbol(file_path, primary.cursor.row, primary.cursor.col);
|
||||
}
|
||||
pub const rename_symbol_meta = .{ .description = "Language: Rename symbol at cursor" };
|
||||
|
||||
pub fn hover(self: *Self, _: Context) Result {
|
||||
const primary = self.get_primary();
|
||||
return self.hover_at(primary.cursor.row, primary.cursor.col);
|
||||
|
@ -4316,6 +4323,15 @@ pub const Editor = struct {
|
|||
self.need_render();
|
||||
}
|
||||
|
||||
pub fn rename_symbol_item(self: *Self, sel: Selection, new_text: []const u8) Result {
|
||||
self.get_primary().selection = sel;
|
||||
const buf = try self.buf_for_update();
|
||||
const r1 = try self.delete_selection(buf.root, self.get_primary(), self.allocator);
|
||||
const r2 = try self.insert(r1, self.get_primary(), new_text, self.allocator);
|
||||
try self.update_buf(r2);
|
||||
self.need_render();
|
||||
}
|
||||
|
||||
pub fn select(self: *Self, ctx: Context) Result {
|
||||
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) }))
|
||||
|
|
|
@ -554,6 +554,32 @@ const cmds = struct {
|
|||
}
|
||||
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 {
|
||||
var file_uri: []const u8 = undefined;
|
||||
var sel: ed.Selection = .{};
|
||||
var new_text: []const u8 = undefined;
|
||||
if (!try ctx.args.match(.{
|
||||
tp.extract(&file_uri),
|
||||
tp.extract(&sel.begin.row),
|
||||
tp.extract(&sel.begin.col),
|
||||
tp.extract(&sel.end.row),
|
||||
tp.extract(&sel.end.col),
|
||||
tp.extract(&new_text),
|
||||
})) return error.InvalidRenameSymbolArgument;
|
||||
file_uri = project_manager.normalize_file_path(file_uri);
|
||||
if (self.get_active_editor()) |editor| {
|
||||
// TODO match 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);
|
||||
} else {
|
||||
// TODO perform renames in other files
|
||||
}
|
||||
}
|
||||
}
|
||||
pub const rename_symbol_item_meta = .{ .arguments = &.{ .string, .integer, .integer, .integer, .integer, .string } };
|
||||
|
||||
pub fn clear_diagnostics(self: *Self, ctx: Ctx) Result {
|
||||
var file_path: []const u8 = undefined;
|
||||
if (!try ctx.args.match(.{tp.extract(&file_path)})) return error.InvalidClearDiagnosticsArgument;
|
||||
|
|
Loading…
Add table
Reference in a new issue