Compare commits

...

6 commits

3 changed files with 51 additions and 46 deletions

View file

@ -19,6 +19,19 @@ pub fn from_cursor(cursor: *const Cursor) Self {
return .{ .begin = cursor.*, .end = cursor.* }; return .{ .begin = cursor.*, .end = cursor.* };
} }
pub fn from_pos(sel: Self, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Self {
return .{
.begin = .{
.row = sel.begin.row,
.col = try root.pos_to_width(sel.begin.row, sel.begin.col, metrics),
},
.end = .{
.row = sel.end.row,
.col = try root.pos_to_width(sel.end.row, sel.end.col, metrics),
},
};
}
pub fn line_from_cursor(cursor: Cursor, root: Buffer.Root, mtrx: Buffer.Metrics) Self { pub fn line_from_cursor(cursor: Cursor, root: Buffer.Root, mtrx: Buffer.Metrics) Self {
var begin = cursor; var begin = cursor;
var end = cursor; var end = cursor;

View file

@ -74,6 +74,10 @@ pub const Match = struct {
return .{ .begin = self.begin, .end = self.end }; return .{ .begin = self.begin, .end = self.end };
} }
pub fn from_pos(self: Self, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Self {
return from_selection(try self.to_selection().from_pos(root, metrics));
}
fn nudge_insert(self: *Self, nudge: Selection) void { fn nudge_insert(self: *Self, nudge: Selection) void {
self.begin.nudge_insert(nudge); self.begin.nudge_insert(nudge);
self.end.nudge_insert(nudge); self.end.nudge_insert(nudge);
@ -175,16 +179,10 @@ pub const CurSel = struct {
} }
fn selection_from_range(range: syntax.Range, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection { fn selection_from_range(range: syntax.Range, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection {
return .{ return Selection.from_pos(.{
.begin = .{ .begin = .{ .row = range.start_point.row, .col = range.start_point.column },
.row = range.start_point.row, .end = .{ .row = range.end_point.row, .col = range.end_point.column },
.col = try root.pos_to_width(range.start_point.row, range.start_point.column, metrics), }, root, metrics);
},
.end = .{
.row = range.end_point.row,
.col = try root.pos_to_width(range.end_point.row, range.end_point.column, metrics),
},
};
} }
pub fn selection_from_node(node: syntax.Node, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection { pub fn selection_from_node(node: syntax.Node, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection {
@ -5442,10 +5440,8 @@ pub const Editor = struct {
if (pos_type == .byte) { if (pos_type == .byte) {
column = root.pos_to_width(line - 1, column - 1, self.metrics) catch return; column = root.pos_to_width(line - 1, column - 1, self.metrics) catch return;
column += 1; column += 1;
if (have_sel) { if (have_sel)
sel.begin.col = root.pos_to_width(sel.begin.row, sel.begin.col, self.metrics) catch return; sel = sel.from_pos(root, self.metrics) catch return;
sel.end.col = root.pos_to_width(sel.end.row, sel.end.col, self.metrics) catch return;
}
// self.logger.print("goto_byte_pos: l:{d} c:{d} {any} {}", .{ line, column, sel, pos_type }); // self.logger.print("goto_byte_pos: l:{d} c:{d} {any} {}", .{ line, column, sel, pos_type });
} }
const primary = self.get_primary(); const primary = self.get_primary();
@ -5524,9 +5520,11 @@ pub const Editor = struct {
pub fn completion(self: *Self, _: Context) Result { pub fn completion(self: *Self, _: Context) Result {
const file_path = self.file_path orelse return; const file_path = self.file_path orelse return;
const root = self.buf_root() catch return;
const primary = self.get_primary(); const primary = self.get_primary();
const col = try root.get_line_width_to_pos(primary.cursor.row, primary.cursor.col, self.metrics);
self.completions.clearRetainingCapacity(); self.completions.clearRetainingCapacity();
return project_manager.completion(file_path, primary.cursor.row, primary.cursor.col); return project_manager.completion(file_path, primary.cursor.row, col);
} }
pub const completion_meta: Meta = .{ .description = "Language: Show completions at cursor" }; pub const completion_meta: Meta = .{ .description = "Language: Show completions at cursor" };
@ -5545,16 +5543,7 @@ pub const Editor = struct {
.push => try self.push_cursor(), .push => try self.push_cursor(),
} }
const root = self.buf_root() catch return; const root = self.buf_root() catch return;
const sel: Selection = .{ const sel = try sel_.from_pos(root, self.metrics);
.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(); const primary = self.get_primary();
primary.selection = sel; primary.selection = sel;
primary.cursor = sel.end; primary.cursor = sel.end;
@ -5595,18 +5584,16 @@ pub const Editor = struct {
} }
first = false; 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_row = begin_row;
last_begin_col_pos = begin_col_pos; last_begin_col_pos = begin_col_pos;
last_end_row = end_row; last_end_row = end_row;
last_end_col_pos = end_col_pos; last_end_col_pos = end_col_pos;
const sel: Selection = .{ const sel = try Selection.from_pos(.{
.begin = .{ .row = begin_row, .col = begin_col }, .begin = .{ .row = begin_row, .col = begin_col_pos },
.end = .{ .row = end_row, .col = end_col }, .end = .{ .row = end_row, .col = end_col_pos },
}; }, root, self.metrics);
const primary = self.get_primary(); const primary = self.get_primary();
primary.selection = sel; primary.selection = sel;
primary.cursor = sel.end; primary.cursor = sel.end;
@ -5617,6 +5604,11 @@ pub const Editor = struct {
} }
} }
pub fn selection_pos_to_width(self: *Self, sel_: Selection) ?Selection {
const root = self.buf_root() catch return null;
return sel_.from_pos(root, self.metrics) catch null;
}
fn count_lines(content: []const u8) struct { usize, usize } { fn count_lines(content: []const u8) struct { usize, usize } {
var pos = content; var pos = content;
var offset = content.len; var offset = content.len;
@ -5683,23 +5675,13 @@ pub const Editor = struct {
if (!std.mem.eql(u8, file_path, self.file_path orelse return)) return; if (!std.mem.eql(u8, file_path, self.file_path orelse return)) return;
const root = self.buf_root() catch return; const root = self.buf_root() catch return;
const sel: Selection = .{
.begin = .{
.row = sel_.begin.row,
.col = root.pos_to_width(sel_.begin.row, sel_.begin.col, self.metrics) catch return,
},
.end = .{
.row = sel_.end.row,
.col = root.pos_to_width(sel_.end.row, sel_.end.col, self.metrics) catch return,
},
};
(try self.diagnostics.addOne(self.allocator)).* = .{ (try self.diagnostics.addOne(self.allocator)).* = .{
.source = try self.allocator.dupe(u8, source), .source = try self.allocator.dupe(u8, source),
.code = try self.allocator.dupe(u8, code), .code = try self.allocator.dupe(u8, code),
.message = try self.allocator.dupe(u8, message), .message = try self.allocator.dupe(u8, message),
.severity = severity, .severity = severity,
.sel = sel, .sel = sel_.from_pos(root, self.metrics) catch return,
}; };
switch (Diagnostic.to_severity(severity)) { switch (Diagnostic.to_severity(severity)) {

View file

@ -40,9 +40,10 @@ pub fn load_entries(palette: *Type) !usize {
var max_label_len: usize = 0; var max_label_len: usize = 0;
for (palette.entries.items) |*item| { for (palette.entries.items) |*item| {
const label_, const sort_text, _, const replace = get_values(item.cbor); const label_, const sort_text, _, const maybe_replace = get_values(item.cbor);
if (palette.value.replace == null and !(replace.begin.row == 0 and replace.begin.col == 0 and replace.end.row == 0 and replace.end.col == 0)) if (get_replace_selection(maybe_replace)) |replace| {
palette.value.replace = replace; if (palette.value.replace == null) palette.value.replace = replace;
}
item.label = label_; item.label = label_;
item.sort_text = sort_text; item.sort_text = sort_text;
max_label_len = @max(max_label_len, item.label.len); max_label_len = @max(max_label_len, item.label.len);
@ -130,6 +131,15 @@ fn get_values(item_cbor: []const u8) struct { []const u8, []const u8, u8, Buffer
return .{ label_, sort_text, kind, replace }; return .{ label_, sort_text, kind, replace };
} }
fn get_replace_selection(replace: Buffer.Selection) ?Buffer.Selection {
return if (tui.get_active_editor()) |edt|
edt.selection_pos_to_width(replace)
else if (replace.empty())
null
else
replace;
}
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void { fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
const label_, _, _, _ = get_values(button.opts.label); const label_, _, _, _ = get_values(button.opts.label);
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e); tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
@ -140,7 +150,7 @@ pub fn updated(palette: *Type, button_: ?*Type.ButtonState) !void {
const button = button_ orelse return cancel(palette); const button = button_ orelse return cancel(palette);
_, _, _, const replace = get_values(button.opts.label); _, _, _, const replace = get_values(button.opts.label);
const editor = tui.get_active_editor() orelse return error.NotFound; const editor = tui.get_active_editor() orelse return error.NotFound;
editor.get_primary().selection = if (replace.empty()) null else replace; editor.get_primary().selection = get_replace_selection(replace);
} }
pub fn cancel(palette: *Type) !void { pub fn cancel(palette: *Type) !void {