From 1fd4455adb346b3d1f854c6604dd548bac2748d9 Mon Sep 17 00:00:00 2001 From: Travis Staloch <1562827+travisstaloch@users.noreply.github.com> Date: Sun, 12 Jan 2025 14:00:48 -0800 Subject: [PATCH 01/12] 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. --- src/Project.zig | 112 ++++++++++++++++++++++++++++++++++ src/keybind/builtin/flow.json | 3 +- src/project_manager.zig | 16 +++++ src/tui/editor.zig | 16 +++++ src/tui/mainview.zig | 26 ++++++++ 5 files changed, 172 insertions(+), 1 deletion(-) diff --git a/src/Project.zig b/src/Project.zig index 1c79f9e..adb1e24 100644 --- a/src/Project.zig +++ b/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); diff --git a/src/keybind/builtin/flow.json b/src/keybind/builtin/flow.json index f4aa868..e95f9fb 100644 --- a/src/keybind/builtin/flow.json +++ b/src/keybind/builtin/flow.json @@ -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"], diff --git a/src/project_manager.zig b/src/project_manager.zig index 257bf00..d65c9bb 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -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(); diff --git a/src/tui/editor.zig b/src/tui/editor.zig index f91f9a8..5a71db9 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -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) })) diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 7510578..076c372 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -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; From 1c37de6c290500add11c0ac1c5c5cedb69ceb784 Mon Sep 17 00:00:00 2001 From: Travis Staloch <1562827+travisstaloch@users.noreply.github.com> Date: Sun, 12 Jan 2025 17:32:05 -0800 Subject: [PATCH 02/12] feat(lsp): buffer renames in order to send a single, atomic message --- src/Project.zig | 68 +++++++++++++++++++++++++++++--------------- src/tui/editor.zig | 17 +++++++---- src/tui/mainview.zig | 48 ++++++++++++++++++------------- 3 files changed, 84 insertions(+), 49 deletions(-) diff --git a/src/Project.zig b/src/Project.zig index adb1e24..bba8197 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -782,6 +782,12 @@ fn send_completion_item(_: *Self, to: tp.pid_ref, file_path: []const u8, row: us }) 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 { const lsp = try self.get_language_server(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; defer self.allocator.free(response.buf); 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, .{ tp.any, tp.any, tp.any, tp.extract_cbor(&result) })) - try self.send_rename_symbol_map(from, result); + if (try cbor.match(response.buf, .{ tp.any, tp.any, tp.any, tp.extract_cbor(&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 -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 len = cbor.decodeMapHeader(&iter) catch return error.InvalidMessage; 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 (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; + try self.decode_rename_symbol_changes(changes, renames); + return; } 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; + try self.decode_rename_symbol_doc_changes(changes, renames); + return; } else { 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; } -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", .{}); +fn decode_rename_symbol_changes(self: *Self, changes: []const u8, renames: *std.ArrayList(Rename)) (ClientError || InvalidMessageError || cbor.Error)!void { 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); + 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 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; + var file_uri: []const u8 = ""; 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")) { @@ -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")) { 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 -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; +fn decode_rename_symbol_item(_: *Self, file_uri: []const u8, _iter: []const u8, renames: *std.ArrayList(Rename)) (ClientError || InvalidMessageError || cbor.Error)!void { + var iter = _iter; 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; @@ -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; - - 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; + try renames.append(.{ .uri = file_uri, .range = range, .new_text = new_text }); } } diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 5a71db9..7c54a58 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -4323,13 +4323,18 @@ pub const Editor = struct { 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; - 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(); + 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 { diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 076c372..1895879 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -555,26 +555,34 @@ 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 + // because the incoming message is an array of Renames, we manuallly + // parse instead of using ctx.args.match() which doesn't seem to return + // the parsed length needed to correctly advance iter. + var iter = ctx.args.buf; + var len = try cbor.decodeArrayHeader(&iter); + var mroot: ?@import("Buffer").Root = null; + while (len != 0) { + var file_uri: []const u8 = undefined; + var sel: ed.Selection = .{}; + var new_text: []const u8 = undefined; + len -= 1; + std.debug.assert(try cbor.decodeArrayHeader(&iter) == 6); + if (!try cbor.matchString(&iter, &file_uri)) return error.MissingArgument; + if (!try cbor.matchInt(usize, &iter, &sel.begin.row)) return error.MissingArgument; + if (!try cbor.matchInt(usize, &iter, &sel.begin.col)) return error.MissingArgument; + if (!try cbor.matchInt(usize, &iter, &sel.end.row)) return error.MissingArgument; + if (!try cbor.matchInt(usize, &iter, &sel.end.col)) return error.MissingArgument; + if (!try cbor.matchString(&iter, &new_text)) return error.MissingArgument; + file_uri = project_manager.normalize_file_path(file_uri); + 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", .{}); + } } } } From 860c36266cc0513622a69e45b0e1b2c482a0ce49 Mon Sep 17 00:00:00 2001 From: Travis Staloch <1562827+travisstaloch@users.noreply.github.com> Date: Mon, 13 Jan 2025 15:44:32 -0800 Subject: [PATCH 03/12] keybinds: changes related to lsp rename * vim mode: bind 'grn' to rename_symbol * flow mode: bind F4 to toggle_input_mode --- src/keybind/builtin/flow.json | 4 ++-- src/keybind/builtin/vim.json | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/keybind/builtin/flow.json b/src/keybind/builtin/flow.json index e95f9fb..33e5ed6 100644 --- a/src/keybind/builtin/flow.json +++ b/src/keybind/builtin/flow.json @@ -129,7 +129,7 @@ ["shift+backspace", "delete_backward"], ["shift+tab", "unindent"], ["f2", "rename_symbol"], - ["ctrl+shift+f2", "toggle_input_mode"], + ["f4", "toggle_input_mode"], ["ctrl+f2", "insert_command_name"], ["f3", "goto_next_match"], ["f15", "goto_prev_match"], @@ -231,7 +231,7 @@ ["alt+i", "toggle_inputview"], ["alt+x", "open_command_palette"], ["f1", "open_help"], - ["f2", "toggle_input_mode"], + ["f4", "toggle_input_mode"], ["ctrl+f2", "insert_command_name"], ["f6", "open_config"], ["f9", "theme_prev"], diff --git a/src/keybind/builtin/vim.json b/src/keybind/builtin/vim.json index c6dff63..16c1d30 100644 --- a/src/keybind/builtin/vim.json +++ b/src/keybind/builtin/vim.json @@ -44,6 +44,7 @@ ["gi", "goto_implementation"], ["gy", "goto_type_definition"], ["gg", "move_buffer_begin"], + ["grn", "rename_symbol"], ["g", "goto_declaration"], ["", "move_buffer_end"], From bfdeffc70fca958a4526a0757d4891a6d026fd4e Mon Sep 17 00:00:00 2001 From: Travis Staloch <1562827+travisstaloch@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:15:02 -0800 Subject: [PATCH 04/12] lsp-rename: minor cosmetic cleanups --- src/Project.zig | 8 ++++---- src/command.zig | 1 + src/tui/mainview.zig | 15 +++++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Project.zig b/src/Project.zig index bba8197..f0b0e1e 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -807,9 +807,9 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row: if (try cbor.match(response.buf, .{ tp.any, tp.any, tp.any, tp.extract_cbor(&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(); + var msg_buf = std.ArrayList(u8).init(self.allocator); + defer msg_buf.deinit(); + const w = msg_buf.writer(); try cbor.writeArrayHeader(w, 3); try cbor.writeValue(w, "cmd"); try cbor.writeValue(w, "rename_symbol_item"); @@ -824,7 +824,7 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row: rename.new_text, }); } - from.send_raw(.{ .buf = m.items }) catch return error.ClientFailed; + from.send_raw(.{ .buf = msg_buf.items }) catch return error.ClientFailed; } } } diff --git a/src/command.zig b/src/command.zig index f2d32d3..a01145a 100644 --- a/src/command.zig +++ b/src/command.zig @@ -35,6 +35,7 @@ pub const ArgumentType = enum { integer, float, object, + array, }; pub fn Closure(comptime T: type) type { diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 1895879..707378f 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -562,18 +562,18 @@ const cmds = struct { var len = try cbor.decodeArrayHeader(&iter); var mroot: ?@import("Buffer").Root = null; while (len != 0) { - var file_uri: []const u8 = undefined; - var sel: ed.Selection = .{}; - var new_text: []const u8 = undefined; len -= 1; std.debug.assert(try cbor.decodeArrayHeader(&iter) == 6); + var file_uri: []const u8 = undefined; if (!try cbor.matchString(&iter, &file_uri)) return error.MissingArgument; + var sel: ed.Selection = .{}; if (!try cbor.matchInt(usize, &iter, &sel.begin.row)) return error.MissingArgument; if (!try cbor.matchInt(usize, &iter, &sel.begin.col)) return error.MissingArgument; if (!try cbor.matchInt(usize, &iter, &sel.end.row)) return error.MissingArgument; 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; - file_uri = project_manager.normalize_file_path(file_uri); + 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://' @@ -581,12 +581,15 @@ const cmds = struct { 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", .{}); + const logger = log.logger("LSP"); + defer logger.deinit(); + logger.print("TODO perform renames in other files\n", .{}); } } } } - pub const rename_symbol_item_meta = .{ .arguments = &.{ .string, .integer, .integer, .integer, .integer, .string } }; + pub const rename_symbol_item_meta = .{ .arguments = &.{.array} }; + pub const rename_symbol_item_elem_meta = .{ .arguments = &.{ .string, .integer, .integer, .integer, .integer, .string } }; pub fn clear_diagnostics(self: *Self, ctx: Ctx) Result { var file_path: []const u8 = undefined; From e597fee2e594ee6099a666cde501290752a93fcd Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 16 Jan 2025 21:45:46 +0100 Subject: [PATCH 05/12] feat(lsp): rename_symbol: decode and normalize file uri --- src/Project.zig | 4 +++- src/tui/mainview.zig | 24 ++++++++++-------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Project.zig b/src/Project.zig index f0b0e1e..cee242f 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -815,8 +815,10 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row: try cbor.writeValue(w, "rename_symbol_item"); try cbor.writeArrayHeader(w, renames.items.len); 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..]); try cbor.writeValue(w, .{ - rename.uri, + file_path_, rename.range.start.line, rename.range.start.character, rename.range.end.line, diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 707378f..611cac5 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -564,8 +564,8 @@ const cmds = struct { while (len != 0) { len -= 1; std.debug.assert(try cbor.decodeArrayHeader(&iter) == 6); - var file_uri: []const u8 = undefined; - if (!try cbor.matchString(&iter, &file_uri)) return error.MissingArgument; + var file_path: []const u8 = undefined; + if (!try cbor.matchString(&iter, &file_path)) return error.MissingArgument; var sel: ed.Selection = .{}; if (!try cbor.matchInt(usize, &iter, &sel.begin.row)) return error.MissingArgument; if (!try cbor.matchInt(usize, &iter, &sel.begin.col)) return error.MissingArgument; @@ -574,18 +574,14 @@ const cmds = struct { var new_text: []const u8 = undefined; if (!try cbor.matchString(&iter, &new_text)) return error.MissingArgument; - 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 { - const logger = log.logger("LSP"); - defer logger.deinit(); - logger.print("TODO perform renames in other files\n", .{}); - } - } + 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 "")) { + try editor.rename_symbol_item(sel, new_text, &mroot, len == 0); + } else { + const logger = log.logger("LSP"); + defer logger.deinit(); + logger.print("TODO perform renames in other files\n", .{}); + }; } } pub const rename_symbol_item_meta = .{ .arguments = &.{.array} }; From a449e0ec979c0cac9c43a4814cfb24928e22adbd Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 16 Jan 2025 22:23:03 +0100 Subject: [PATCH 06/12] feat(lsp): rename_symbol: add cursors at rename points for interactive rename --- src/Project.zig | 2 +- src/tui/editor.zig | 22 ++++++++-------------- src/tui/mainview.zig | 10 ++++++---- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/Project.zig b/src/Project.zig index cee242f..997e8aa 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -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", .{ .textDocument = .{ .uri = uri }, .position = .{ .line = row, .character = col }, - .newName = "foobar", + .newName = "PLACEHOLDER", }) catch return error.LspFailed; defer self.allocator.free(response.buf); var result: []const u8 = undefined; diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 7c54a58..dbd38c5 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -4229,6 +4229,14 @@ pub const Editor = struct { } 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 { const primary = self.get_primary(); return self.hover_at(primary.cursor.row, primary.cursor.col); @@ -4323,20 +4331,6 @@ pub const Editor = struct { 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 { 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) })) diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 611cac5..a7e2420 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -555,12 +555,13 @@ 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 { + const editor = self.get_active_editor() orelse return; // because the incoming message is an array of Renames, we manuallly // parse instead of using ctx.args.match() which doesn't seem to return // the parsed length needed to correctly advance iter. var iter = ctx.args.buf; var len = try cbor.decodeArrayHeader(&iter); - var mroot: ?@import("Buffer").Root = null; + var first = true; while (len != 0) { len -= 1; 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; 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 "")) { - try editor.rename_symbol_item(sel, new_text, &mroot, len == 0); + 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", .{}); - }; + } } } pub const rename_symbol_item_meta = .{ .arguments = &.{.array} }; From 155c1f663d3d3450d51f0b7c72ce9c35f2e762a5 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 16 Jan 2025 23:02:52 +0100 Subject: [PATCH 07/12] feat(lsp): rename_symbol: treat out-of-file edits as references --- src/Project.zig | 6 ++++-- src/project_manager.zig | 2 +- src/tui/mainview.zig | 17 +++++++++++++---- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Project.zig b/src/Project.zig index 997e8aa..68563e2 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -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; diff --git a/src/project_manager.zig b/src/project_manager.zig index d65c9bb..9a71da8 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -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; diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index a7e2420..4852169 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -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, + ); } } } From 00efee6c027625d5e50c39bbdfee363c07160123 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 16 Jan 2025 23:24:48 +0100 Subject: [PATCH 08/12] fix(lsp): rename_symbol: fix error.InvalidMessage on multifile responses --- src/Project.zig | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Project.zig b/src/Project.zig index 68563e2..910bb7b 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -863,7 +863,7 @@ fn decode_rename_symbol_changes(self: *Self, changes: []const u8, renames: *std. while (files_len > 0) : (files_len -= 1) { var file_uri: []const u8 = undefined; if (!(try cbor.matchString(&iter, &file_uri))) return error.InvalidMessage; - try decode_rename_symbol_item(self, file_uri, iter, renames); + try decode_rename_symbol_item(self, file_uri, &iter, renames); } } @@ -887,31 +887,30 @@ fn decode_rename_symbol_doc_changes(self: *Self, changes: []const u8, renames: * } } else if (std.mem.eql(u8, field_name, "edits")) { if (file_uri.len == 0) return error.InvalidMessage; - try decode_rename_symbol_item(self, file_uri, iter, renames); + try decode_rename_symbol_item(self, file_uri, &iter, renames); } } } } // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit -fn decode_rename_symbol_item(_: *Self, file_uri: []const u8, _iter: []const u8, renames: *std.ArrayList(Rename)) (ClientError || InvalidMessageError || cbor.Error)!void { - var iter = _iter; - var text_edits_len = cbor.decodeArrayHeader(&iter) catch return error.InvalidMessage; +fn decode_rename_symbol_item(_: *Self, file_uri: []const u8, iter: *[]const u8, renames: *std.ArrayList(Rename)) (ClientError || InvalidMessageError || cbor.Error)!void { + 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; + 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 (!(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; + 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; + if (!(try cbor.matchString(iter, &new_text))) return error.InvalidMessageField; } else { - try cbor.skipValue(&iter); + try cbor.skipValue(iter); } } From f659fcf58634a813fa4895895bf56d1e05dd0a51 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 16 Jan 2025 23:40:10 +0100 Subject: [PATCH 09/12] feat(lsp): rename_symbol: add checks to file uri decoding --- src/Project.zig | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Project.zig b/src/Project.zig index 910bb7b..3750025 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -815,8 +815,15 @@ pub fn rename_symbol(self: *Self, from: tp.pid_ref, file_path: []const u8, row: try cbor.writeValue(w, "rename_symbol_item"); try cbor.writeArrayHeader(w, renames.items.len); for (renames.items) |rename| { + if (!std.mem.eql(u8, rename.uri[0..7], "file://")) return error.InvalidTargetURI; var file_path_buf: [std.fs.max_path_bytes]u8 = undefined; const file_path_ = std.Uri.percentDecodeBackwards(&file_path_buf, rename.uri[7..]); + if (builtin.os.tag == .windows) { + if (file_path[0] == '/') file_path = file_path[1..]; + for (file_path, 0..) |c, i| if (c == '/') { + file_path[i] = '\\'; + }; + } const line = try self.get_line_of_file(self.allocator, file_path, rename.range.start.line); try cbor.writeValue(w, .{ file_path_, From 61d9f583a83bac906a1cf6ceafe80c1ccf0f1d35 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 17 Jan 2025 12:33:02 +0100 Subject: [PATCH 10/12] feat(emacs): add lsp-mode emacs keybindings --- src/keybind/builtin/emacs.json | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/keybind/builtin/emacs.json b/src/keybind/builtin/emacs.json index be5f851..4865b10 100644 --- a/src/keybind/builtin/emacs.json +++ b/src/keybind/builtin/emacs.json @@ -37,7 +37,25 @@ ["ctrl+x ctrl+c", "quit"], ["ctrl+x b", "open_recent"], ["alt+x", "open_command_palette"], - ["ctrl+space", "enter_mode", "select"] + ["ctrl+space", "enter_mode", "select"], + + ["ctrl+c l = =", "format"], + ["ctrl+c l = r", "format"], + ["ctrl+c l g g", "goto_definition"], + ["ctrl+c l g i", "goto_implementation"], + ["ctrl+c l g d", "goto_declaration"], + ["ctrl+c l g r", "references"], + ["ctrl+c l h h", "hover"], + ["ctrl+c l r r", "rename_symbol"], + + ["super+l = =", "format"], + ["super+l = r", "format"], + ["super+l g g", "goto_definition"], + ["super+l g i", "goto_implementation"], + ["super+l g d", "goto_declaration"], + ["super+l g r", "references"], + ["super+l h h", "hover"], + ["super+l r r", "rename_symbol"] ] }, "select": { From 04b77b4d28e92edf9b13836392219b8389379517 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 17 Jan 2025 15:56:46 +0100 Subject: [PATCH 11/12] fix(lsp): rename_symbol: convert columns to byte offsets and back --- src/tui/editor.zig | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index dbd38c5..ce6b9b1 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -4224,13 +4224,26 @@ pub const Editor = struct { pub fn rename_symbol(self: *Self, _: Context) Result { const file_path = self.file_path orelse return; + const root = self.buf_root() catch return; const primary = self.get_primary(); - return project_manager.rename_symbol(file_path, primary.cursor.row, primary.cursor.col); + const col = try root.get_line_width_to_pos(primary.cursor.row, primary.cursor.col, self.metrics); + return project_manager.rename_symbol(file_path, primary.cursor.row, col); } pub const rename_symbol_meta = .{ .description = "Language: Rename symbol at cursor" }; - pub fn add_rename_symbol_cursor(self: *Self, sel: Selection, first: bool) !void { + 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 root = self.buf_root() catch return; + const sel: Selection = .{ + .begin = .{ + .row = sel_.begin.row, + .col = try root.pos_to_width(sel_.begin.row, sel_.begin.col, self.metrics), + }, + .end = .{ + .row = sel_.end.row, + .col = try root.pos_to_width(sel_.end.row, sel_.end.col, self.metrics), + }, + }; const primary = self.get_primary(); primary.selection = sel; primary.cursor = sel.end; From 4277f0f57bfbeebfa1a6ac166a209b6ea2cd3696 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sat, 18 Jan 2025 20:34:14 +0100 Subject: [PATCH 12/12] feat(lsp): rename_symbol: add support for LSPs that do send document updates for renames --- src/tui/editor.zig | 74 ++++++++++++++++++++++++++++++++++++++++++-- src/tui/mainview.zig | 7 +++-- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index ce6b9b1..51309f5 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -4231,8 +4231,11 @@ pub const Editor = struct { } 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(); + pub fn add_cursor_from_selection(self: *Self, sel_: Selection, op: enum { cancel, push }) !void { + switch (op) { + .cancel => self.cancel_all_selections(), + .push => try self.push_cursor(), + } const root = self.buf_root() catch return; const sel: Selection = .{ .begin = .{ @@ -4250,6 +4253,73 @@ pub const Editor = struct { self.need_render(); } + pub fn add_cursors_from_content_diff(self: *Self, new_content: []const u8) !void { + const frame = tracy.initZone(@src(), .{ .name = "editor diff syntax" }); + defer frame.deinit(); + + var content_ = std.ArrayList(u8).init(self.allocator); + defer content_.deinit(); + const root = self.buf_root() catch return; + const eol_mode = self.buf_eol_mode() catch return; + try root.store(content_.writer(), eol_mode); + const content = content_.items; + var last_begin_row: usize = 0; + var last_begin_col_pos: usize = 0; + var last_end_row: usize = 0; + var last_end_col_pos: usize = 0; + + const diffs = try @import("diff").diff(self.allocator, new_content, content); + defer self.allocator.free(diffs); + var first = true; + for (diffs) |diff| { + switch (diff.kind) { + .delete => { + var begin_row, var begin_col_pos = count_lines(content[0..diff.start]); + const end_row, const end_col_pos = count_lines(content[0 .. diff.start + diff.bytes.len]); + if (begin_row == last_end_row and begin_col_pos == last_end_col_pos) { + begin_row = last_begin_row; + begin_col_pos = last_begin_col_pos; + } else { + if (first) { + self.cancel_all_selections(); + } else { + try self.push_cursor(); + } + first = false; + } + const begin_col = try root.pos_to_width(begin_row, begin_col_pos, self.metrics); + const end_col = try root.pos_to_width(end_row, end_col_pos, self.metrics); + + last_begin_row = begin_row; + last_begin_col_pos = begin_col_pos; + last_end_row = end_row; + last_end_col_pos = end_col_pos; + + const sel: Selection = .{ + .begin = .{ .row = begin_row, .col = begin_col }, + .end = .{ .row = end_row, .col = end_col }, + }; + const primary = self.get_primary(); + primary.selection = sel; + primary.cursor = sel.end; + self.need_render(); + }, + else => {}, + } + } + } + + fn count_lines(content: []const u8) struct { usize, usize } { + var pos = content; + var offset = content.len; + var lines: usize = 0; + while (pos.len > 0) : (pos = pos[1..]) if (pos[0] == '\n') { + offset = pos.len - 1; + lines += 1; + }; + return .{ lines, offset }; + } + pub fn hover(self: *Self, _: Context) Result { const primary = self.get_primary(); return self.hover_at(primary.cursor.row, primary.cursor.col); diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 4852169..4eb251e 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -562,8 +562,7 @@ const cmds = struct { var iter = ctx.args.buf; var len = try cbor.decodeArrayHeader(&iter); var first = true; - while (len != 0) { - len -= 1; + while (len != 0) : (len -= 1) { 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; @@ -579,7 +578,9 @@ const cmds = struct { 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); + if (len == 1 and sel.begin.row == 0 and sel.begin.col == 0 and sel.end.row > 0) //probably a full file edit + return editor.add_cursors_from_content_diff(new_text); + try editor.add_cursor_from_selection(sel, if (first) .cancel else .push); first = false; } else { try self.add_find_in_files_result(