Merge pull request #149 from travisstaloch/lsp-rename
implement lsp rename
This commit is contained in:
commit
9f29853cd6
8 changed files with 328 additions and 4 deletions
|
@ -4247,6 +4247,104 @@ 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 root = self.buf_root() catch return;
|
||||
const primary = self.get_primary();
|
||||
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_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 = .{
|
||||
.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;
|
||||
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);
|
||||
|
|
|
@ -554,6 +554,51 @@ 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 first = true;
|
||||
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;
|
||||
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;
|
||||
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 "")) {
|
||||
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(
|
||||
.references,
|
||||
file_path,
|
||||
sel.begin.row + 1,
|
||||
sel.begin.col,
|
||||
sel.end.row + 1,
|
||||
sel.end.col,
|
||||
line_text,
|
||||
.Information,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
if (!try ctx.args.match(.{tp.extract(&file_path)})) return error.InvalidClearDiagnosticsArgument;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue