summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Query.zig95
-rw-r--r--src/pardes.zig13
2 files changed, 103 insertions, 5 deletions
diff --git a/src/Query.zig b/src/Query.zig
index 98419c7b..2ab687ee 100644
--- a/src/Query.zig
+++ b/src/Query.zig
@@ -1201,9 +1201,25 @@ pub fn fillList(p: *Pardes, owner_id: usize, source: Source, rows: []const u8) v
const line = lineOne(rp.file.?.content);
const a = parse(line) orelse return;
makeListRows(p, qid, a.pat) catch |err| p.reportError(qid, "search", err);
- // A list to pick from, typed into; diagnostics are a jump (]d): the
- // keyboard stays on the place in the text.
- if (source != .diagnostics) toLineOne(p, qid) else p.active = owner_id;
+ // Diagnostics are a jump (]d): the keyboard stays on the place in the
+ // text. The others are a list answered, not a query typed: the keyboard
+ // on the current row in normal mode (j, k, n, N step, Enter lands, Esc
+ // goes back), line 1 left as text for whoever wants to edit it.
+ if (source == .diagnostics) {
+ p.active = owner_id;
+ return;
+ }
+ tagline.exitHeader(p);
+ p.active = qid;
+ if (layout.findPane(p, qid)) |at| p.setActiveColumn(at.col);
+ rp.focus = .body;
+ const t = &rp.body;
+ if (t.mode == .insert) edit.exitInsert(p, t);
+ t.vsel.active = false;
+ t.nsel = 0;
+ t.cur_row = @intCast((q.current orelse 0) + 1);
+ showCurrent(rp);
+ showTarget(p, qid);
}
fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void {
@@ -1334,12 +1350,25 @@ fn clearPlaces(p: *Pardes, q: *Query, serial: u32, owner_id: ?usize, asked: anyt
/// is current. Scrolled into view, line 1 kept when it can be.
fn showCurrent(rp: *Pane) void {
const q = rp.query orelse return;
- if (rp.body.cur_row != 0 and rp.focus == .body) return;
+ const on_rows = rp.body.cur_row != 0 and rp.focus == .body;
+ if (on_rows and rp.body.mode != .normal) return;
const c = q.current orelse {
rp.body.msel.active = false;
return;
};
const r: i32 = @intCast(c + 1);
+ if (on_rows) {
+ // The keyboard on the rows is the current row: n and N take it there.
+ rp.body.msel.active = false;
+ rp.body.cur_row = r;
+ rp.body.cur_col = 0;
+ rp.body.cur_pinned = true;
+ q.seen_row = r;
+ const off = rp.scroll();
+ const shown = off + @as(i32, rp.rows) - 1;
+ if (r > shown) rp.scrollBy(r - shown) else if (r < off) rp.scrollBy(r - off);
+ return;
+ }
rp.body.msel = .{ .active = true, .r0 = r, .r1 = r };
// Typed into, line 1 stays in view whatever the current row: what you
// type is never scrolled away (the live rows start just before the
@@ -1821,6 +1850,12 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool {
step(p, id, if (k.cp == 'n') 1 else -1);
return true;
}
+ // A language server's list, live: Esc is back to the text it was
+ // asked in, every view as it was.
+ if (hit(k, config.escape) and !q.frozen and q.lsp_rows.len > 0 and t.nsel == 0 and !t.vsel.active) {
+ cancel(p, id);
+ return true;
+ }
if (hit(k, config.look_key) and t.cur_row > 0) {
const i: usize = @intCast(t.cur_row - 1);
if (i >= q.items.items.len or q.items.items[i].kind != .hit) return false;
@@ -2341,3 +2376,55 @@ fn queryPane2(p: *Pardes, not: usize) ?usize {
for (p.panes, 0..) |slot, i| if (slot) |pane| if (pane.query != null and i != not) return i;
return null;
}
+
+test "a language server's list: one answer lands with no pane, several open the list in normal mode on the current row, none is a message, Esc goes back" {
+ const lsp = pardes.lsp;
+ if (!lsp.supports.contains(.references) or !lsp.supports.contains(.diagnostics)) return;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 });
+ defer p.deinit();
+ const pane = try p.setTestFile("alpha one\nbeta\nalpha two\ngamma\nalpha three\n");
+ const npanes = struct {
+ fn of(pp: *Pardes) usize {
+ var n: usize = 0;
+ for (pp.panes) |slot| n += @intFromBool(slot != null);
+ return n;
+ }
+ }.of;
+ // One: the keyboard on it, no pane, the place tinted and n's.
+ p.lspRequest(0, .references, "");
+ p.update(.{ .lsp_resp = .{ .id = p.lsp_wait.?.id, .rows = "/test.txt:3:1-5 alpha two\n" } });
+ try std.testing.expectEqual(@as(usize, 1), npanes(p));
+ try std.testing.expectEqual(@as(usize, 0), p.active);
+ try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row);
+ try std.testing.expectEqual(@as(usize, 1), pane.places.hits.items.len);
+ try std.testing.expectEqual(Places.Hit{ .start = 15, .end = 20 }, pane.places.hits.items[0]);
+ // None: a message, no pane.
+ p.lspRequest(0, .diagnostics, "");
+ p.update(.{ .lsp_resp = .{ .id = p.lsp_wait.?.id, .rows = "" } });
+ try std.testing.expectEqual(@as(usize, 1), npanes(p));
+ try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "none") != null);
+ // Several: the list open, the keyboard in it in normal mode on the
+ // current row, which the text shows; line 1 as text.
+ pane.body.cur_row = 0;
+ pane.body.cur_col = 0;
+ p.lspRequest(0, .references, "");
+ p.update(.{ .lsp_resp = .{ .id = p.lsp_wait.?.id, .rows = "/test.txt:1:1-5 alpha one\n/test.txt:3:1-5 alpha two\n/test.txt:5:1-5 alpha three\n" } });
+ const qid = queryPane(p).?;
+ const qp = p.panes[qid].?;
+ const q = qp.query.?;
+ try std.testing.expectEqual(qid, p.active);
+ try std.testing.expect(qp.body.mode == .normal);
+ try std.testing.expect(std.mem.startsWith(u8, qp.file.?.content, "Refs"));
+ try std.testing.expectEqual(@as(i32, @intCast(q.current.? + 1)), qp.body.cur_row);
+ try std.testing.expectEqual(@as(usize, 3), pane.places.hits.items.len);
+ const first = pane.places.current.?;
+ // n steps the rows, the caret with them, the text following.
+ typeKeys(p, "n");
+ try std.testing.expectEqual(@as(i32, @intCast(q.current.? + 1)), qp.body.cur_row);
+ try std.testing.expectEqual(first + 1, pane.places.current.?);
+ // Esc: back to the text, as it was, the list gone.
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expect(p.panes[qid] == null);
+ try std.testing.expectEqual(@as(usize, 0), p.active);
+ try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row);
+}
diff --git a/src/pardes.zig b/src/pardes.zig
index a23039ef..a35d1604 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -7093,11 +7093,22 @@ pub const Pardes = struct {
const from: panes.Output.Origin = .{ .query = w.kind };
const nrows = std.mem.count(u8, rows, "\n");
- if (panes.Output.traits(from).jumps and nrows == 1) {
+ if ((panes.Output.traits(from).jumps or w.kind == .diagnostics) and nrows == 1) {
const ln = std.mem.trimEnd(u8, rows, "\n");
const place = ln[0..panes.Output.location(ln).end];
p.look_missed = false;
look.lookAt(p, w.pane, place);
+ // The one place is the text's places, as a list's would be: tinted,
+ // and n and N step it.
+ const spot = look.parsePathLine(place);
+ if (!p.look_missed and spot.at.line > 0) if (p.panes[p.active]) |there| if (there.file) |*tf| if (spot.at.line <= panes.File.nlines(p.gpa, tf)) {
+ const ls = panes.File.lineStart(p.gpa, tf, spot.at.line - 1);
+ const line = tf.content[ls .. std.mem.indexOfScalarPos(u8, tf.content, ls, '\n') orelse tf.content.len];
+ const col = @min(spot.at.col -| 1, line.len);
+ // To its end when the server said it, else the word there.
+ const end = if (spot.at.end_line == spot.at.line and spot.at.end_col > col) @min(spot.at.end_col, line.len) else if (look.expandedWord(line, col)) |word| word.hi else col + 1;
+ if (ls + end <= tf.content.len) there.places.feed(p, there, 0, &.{.{ .start = ls + col, .end = ls + end }}, ls + col);
+ };
// A word that asks for a list (Callers, Refs) with one answer
// jumps to it with no list: where it went is said, as the list
// would have shown it. A definition's jump is the answer itself.