diff options
Diffstat (limited to 'core/normal.zig')
| -rw-r--r-- | core/normal.zig | 84 |
1 files changed, 54 insertions, 30 deletions
diff --git a/core/normal.zig b/core/normal.zig index 981a3f3b..e1af647d 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -22,6 +22,7 @@ const sel_slot = pardes.sel_slot; const Pardes = pardes.Pardes; const Macro = pardes.Macro; const Editor = @import("Editor.zig"); +const Prompt = @import("Prompt.zig"); fn isPrefix(key: Key, cp: u21) bool { return key.cp == cp and !key.ctrl and !key.alt; @@ -53,15 +54,19 @@ pub fn selRegexArmed(pane: *Pane) ?struct { pat: []const u8, mode: modal.Normal. /// Select (or split on, keep or remove by) `pat` inside the selections `t` /// had when s/S/K/Alt-K was pressed; the prompt's snapshot of them lives on -/// `pane`. -pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: modal.Normal.RegexMode) void { - selectByRegex(p.editor(), t, pane.sel_snap[0..pane.nsel_snap], pane.sel_snap_pri, pane.sel_snap_expl, pat, mode); +/// `pane`. The ranges it found: none, the snapshot's back. +pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: modal.Normal.RegexMode) usize { + return selectByRegex(p.editor(), t, pane.sel_snap[0..pane.nsel_snap], pane.sel_snap_pri, pane.sel_snap_expl, pat, mode); } /// Select (or split on, keep or remove by) `pat` inside `snap`, the -/// ranges there were (primary `pri`) when s/S/K/Alt-K was pressed. -pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, expl: bool, pat: []const u8, mode: modal.Normal.RegexMode) void { - const text = edit.flatSurface(e, t) catch |err| return e.reportError(err); +/// ranges there were (primary `pri`) when s/S/K/Alt-K was pressed; the +/// ranges it found. +pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, expl: bool, pat: []const u8, mode: modal.Normal.RegexMode) usize { + const text = edit.flatSurface(e, t) catch |err| { + e.reportError(err); + return 0; + }; var out: [Text.max_selections]modal.Selection = undefined; var m: usize = 0; if (regexp.Regex.compile(pat)) |compiled| { @@ -69,7 +74,10 @@ pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, e var hay_all = text; if (for (pat) |c| { if (std.ascii.isUpper(c)) break false; - } else true) hay_all = std.ascii.allocLowerString(e.scratch, text) catch |err| return e.reportError(err); + } else true) hay_all = std.ascii.allocLowerString(e.scratch, text) catch |err| { + e.reportError(err); + return 0; + }; for (snap) |r| { const from = @min(r.anchor, r.head); const to = @min(@max(r.anchor, r.head), text.len); @@ -116,9 +124,11 @@ pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, e r.anchor = @min(r.anchor, text.len); r.head = @min(r.head, text.len); } - return t.setRanges(e.tab, text, snap, &.{}, pri, expl); + t.setRanges(e.tab, text, snap, &.{}, pri, expl); + return 0; } t.setRanges(e.tab, text, out[0..m], &.{}, 0, true); // helix keeps primary 0 (its own TODO) + return m; } fn lastMatch(re: *SearchRegex, hay: []const u8, from: usize, to: usize) ?regexp.Regex.Match { @@ -249,7 +259,7 @@ fn objectJump(e: Editor, t: *Text, text: []const u8, range: modal.Selection, obj const tree = nodeTree(e, t, text) orelse return; var last = range; for (0..count) |_| { - const s = syntax.objectNext(tree.path, tree.root, tree.text, object, forward, tree.in(modal.selectionCursor(text, last))) orelse break; + const s = syntax.objectNext(tree.path, tree.root, tree.text, object, forward, tree.in(modal.selectionCursor(text, last)), null) orelse break; const next: modal.Selection = .{ .anchor = tree.out(s.from, false), .head = tree.out(s.to, s.to > s.from) }; if (std.meta.eql(next, last)) break; last = next; @@ -600,6 +610,9 @@ fn actionPass(e: Editor, t: *Text, action: modal.Normal.Action) edit.Error!void } fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void { + // A body's prompt is its tag's (Prompt.zig); a tag's or a header's asks + // on the notice band. + if (t.what == .body) return Prompt.open(p, pane, Prompt.ofMode(mode)); const text = try edit.flatSurface(p.editor(), t); const got = t.ranges(text, 0, &pane.sel_snap); pane.nsel_snap = @intCast(got.n); @@ -1075,7 +1088,7 @@ fn shrinkSelToLineBounds(tab: u16, t: *Text, text: []const u8, range: modal.Sele pub fn textRole(role: modal.Normal.Role, tree: bool) bool { return switch (role) { .all_children, .all_siblings, .expand_node, .goto_class, .goto_comment, .goto_entry, .goto_function, .goto_parameter, .goto_test, .goto_xml_element, .next_sibling, .parent_node_end, .parent_node_start, .prev_sibling, .shrink_node => tree, - .append_output, .command_line, .format, .goto_callstack, .goto_change, .goto_declaration, .goto_definition, .goto_diagnostic, .goto_diagnostic_end, .goto_implementation, .goto_references, .goto_type_definition, .goto_view_bottom, .goto_view_top, .goto_word, .half_page_down, .half_page_up, .insert_output, .keep_pipe, .keep_regex, .leader, .macro_record, .macro_replay, .page_down, .page_up, .pipe_selection, .pipe_selection_to, .remove_regex, .repeat_insert, .search, .search_next, .search_prev, .select_regex, .split_regex, .view_bottom, .view_center, .view_middle, .view_scroll_down, .view_scroll_up, .view_top => false, + .append_output, .command_line, .format, .goto_callstack, .goto_change, .goto_declaration, .goto_definition, .goto_diagnostic, .goto_diagnostic_end, .goto_implementation, .goto_references, .goto_type_definition, .goto_view_bottom, .goto_view_top, .goto_word, .half_page_down, .half_page_up, .insert_output, .keep_pipe, .keep_regex, .leader, .macro_record, .macro_replay, .page_down, .page_up, .pipe_selection, .pipe_selection_to, .remove_regex, .repeat_insert, .search, .search_back, .search_next, .search_prev, .select_regex, .split_regex, .view_bottom, .view_center, .view_middle, .view_scroll_down, .view_scroll_up, .view_top => false, else => true, }; } @@ -1095,8 +1108,9 @@ pub fn textAction(e: Editor, t: *Text, a: modal.Normal.Action) edit.Error!void { // typed again by a macro, Esc is only helix's if (e.p.macro.replaying > 0) return; // At rest a thing at a time (pick-ui SPEC §10.4): a search's - // tints, then the warnings and errors, then the selections - // (the primary kept); with none of them left, the previous pane. + // tints (its word stays in the trail, dimmed: §7.3), then the + // warnings and errors, then the selections (the primary kept); + // with none of them left, the previous pane. if (e.p.panes[e.p.view().active]) |pane| if (t == &pane.body and (pane.places.pattern.len > 0 or pane.places.hits.items.len > 0)) { pane.places.clear(e.p.gpa); e.p.needs_frame = true; @@ -1253,14 +1267,13 @@ pub fn textAction(e: Editor, t: *Text, a: modal.Normal.Action) edit.Error!void { .decrement => return edit.normalAdjustNumber(e, t, -@as(i64, @intCast(a.count))), .search_selection, .search_selection_word => { try searchSelection(e, t, text, a.role == .search_selection_word); - // The pattern is line 1 of this text's query pane, made or - // reused, the keyboard staying here: n and N step its places. + // The pattern lights its places and is the pane's search word, + // no prompt asked (Prompt.zig): n and N step them. const pane = t.pane() orelse return; if (t.what == .body and pane.file != null and pane.query == null) { const name = if (e.register.* != 0) e.register.* else '/'; const pat = e.registers.value(name, 0) orelse return; - panes.Query.open(e.p, e.p.view().active, .text, pat, false); - pane.places.provenance = .search; + Prompt.star(e.p, pane, pat); } }, // prefix keys: what they begin is the command @@ -1330,7 +1343,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .view_scroll_down => if (t.what == .body) return t.scrollViewMove(p.settings.tab, text, range, 1), .view_scroll_up => if (t.what == .body) return t.scrollViewMove(p.settings.tab, text, range, -1), // The next and the previous of the changes this view shows (jj.zig). - .goto_change => if (@import("jj.zig").nextChange(p, pane, @intCast(@max(0, t.cur_row)), a.forward)) |line| + .goto_change => if (@import("jj.zig").nextChange(p, pane, @intCast(@max(0, t.cur_row)), a.forward, std.math.maxInt(usize))) |line| return pointMove(p.settings.tab, t, text, range, modal.lineStartOffset(text, line)), .goto_diagnostic, .goto_diagnostic_end => { // Once a Diagnostics query fed them, the diagnostics are this @@ -1373,15 +1386,23 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .column, .workspace => tagline.exitHeader(p), .body, .input => tagline.enterTag(p, pane), }, - .pipe_selection => return panes.startPrompt(p, pane, t, .{ .pipe = .replace }), - .pipe_selection_to => return panes.startPrompt(p, pane, t, .{ .pipe = .ignore }), - .insert_output => return panes.startPrompt(p, pane, t, .{ .pipe = .insert }), - .append_output => return panes.startPrompt(p, pane, t, .{ .pipe = .append }), - .keep_pipe => return panes.startPrompt(p, pane, t, .{ .pipe = .keep }), - // `/` in a text is its query pane's line 1 (Query.zig); a - // terminal's still asks on the notice band. - .search => if (pane.file != null and t.what == .body) - panes.Query.open(p, v.active, .text, "", true) + inline .pipe_selection, .pipe_selection_to, .insert_output, .append_output, .keep_pipe => |role| { + const how: modal.Normal.PipeBehavior = switch (role) { + .pipe_selection => .replace, + .pipe_selection_to => .ignore, + .insert_output => .insert, + .append_output => .append, + else => .keep, + }; + // A body's in its tag (Prompt.zig), else on the notice band. + if (t.what == .body) return Prompt.open(p, pane, Prompt.ofBehavior(how)); + return panes.startPrompt(p, pane, t, .{ .pipe = how }); + }, + // `/` and `?` in a file's text search it in its tag (Prompt.zig), + // Alt-Enter keeping the places as its query pane's rows (Query.zig); + // a terminal's still asks on the notice band. + .search, .search_back => if (pane.file != null and t.what == .body) + return Prompt.open(p, pane, if (a.role == .search) .search else .search_back) else return panes.startPrompt(p, pane, &pane.body, .{ .search = config.search_marker }), // In a file's text, n and N step its places while they answer for @@ -1390,9 +1411,12 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! // places the rows hold, never the disk (a motion). .search_next, .search_prev => if (t.what == .body and pane.addon != null) return look.lookWalk(p, if (a.role == .search_next) 1 else -1, false) - else if (t.what == .body and pane.file != null and pane.places.answersN(p, pane)) - return pane.places.step(p, pane, a.role == .search_next, a.count, t.select) - else if (t.what == .body and panes.Pick.walkList(p, v.active, a.role == .search_next)) + else if (t.what == .body and pane.file != null and pane.places.answersN(p, pane)) { + // The pane's search word says where it went (Prompt.zig). + const was = pane.places.current; + pane.places.step(p, pane, a.role == .search_next, a.count, t.select); + return Prompt.stepped(p, v.active, a.role == .search_next, was); + } else if (t.what == .body and panes.Pick.walkList(p, v.active, a.role == .search_next)) return else return searchStep(p.editor(), t, text, a.role == .search_next, a.count), @@ -1672,7 +1696,7 @@ test "s and S search a multi-line selection as addr does: ^ and $ per line, . wi }) |c| { pane.sel_snap[0] = .{ .anchor = 0, .head = text.len }; pane.nsel_snap = 1; - applySelRegex(p, pane, &pane.body, c.pat, c.mode); + _ = applySelRegex(p, pane, &pane.body, c.pat, c.mode); var out: [Text.max_selections]modal.Selection = undefined; const got = pane.body.ranges(text, 0, &out); try std.testing.expectEqual(c.want.len, got.n); |
