summaryrefslogtreecommitdiff
path: root/core/lsp
diff options
context:
space:
mode:
Diffstat (limited to 'core/lsp')
-rw-r--r--core/lsp/Ask.zig149
1 files changed, 149 insertions, 0 deletions
diff --git a/core/lsp/Ask.zig b/core/lsp/Ask.zig
index 69a398c4..312743d0 100644
--- a/core/lsp/Ask.zig
+++ b/core/lsp/Ask.zig
@@ -18,6 +18,7 @@ const memory = @import("../memory.zig");
const Pardes = pardes.Pardes;
const Pane = panes.Pane;
const Effect = pardes.Effect;
+const TagState = @import("../TagState.zig");
const Ask = @This();
@@ -38,6 +39,12 @@ wait: ?struct {
row: i32 = 0,
col: i32 = 0,
} = null,
+/// A hover asked from the keyboard (where the walk is on), drawn as ghost
+/// lines under the line it was asked at in its own body (pick-ui SPEC §3.5,
+/// the coordinator's R3: never written): until Esc, an edit of that text,
+/// or the next hover. Its first `hover_max` bytes.
+hover: ?struct { serial: u32, row: i32, revision: u32, text: Effect.Buf(hover_max) } = null,
+pub const hover_max = 1024;
const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET;
pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) void {
@@ -247,6 +254,48 @@ fn applyLspEdits(p: *Pardes, pane: *Pane, revision: u32, edits: []const LspEdit)
return true;
}
+/// Rename typed in pane `id`'s own tag (SPEC §3.5): the field the name
+/// under its caret, which the body shows renamed as it is typed
+/// (body_layer.renameGhosts) until Enter asks the server, or Esc.
+pub fn openRename(p: *Pardes, id: usize) void {
+ const pane = p.panes[id] orelse return;
+ const old = renamedWord(pane) orelse return p.reportFailure(id, "Rename: nothing here to rename");
+ TagState.toRest(p);
+ const s = &p.view().tag_state;
+ s.kind = .rename;
+ s.host = pane.serial;
+ s.field.set(old);
+ s.previewing.set(id);
+}
+
+/// A key in Rename's field: Enter asks the server to rename to it (a name
+/// the same as the old is no question), Esc (the tag's) puts it away.
+pub fn renameKey(p: *Pardes, key: pardes.Key) void {
+ const s = &p.view().tag_state;
+ if (!key.ctrl and !key.alt and key.cp == pardes.Key.enter) {
+ const id = TagState.hostOf(p) orelse return TagState.toRest(p);
+ var name: Effect.Buf(128) = .{};
+ const typed = std.mem.trim(u8, s.field.text(), " ");
+ const same = std.mem.eql(u8, typed, renamedWord(p.panes[id].?) orelse "");
+ if (typed.len <= 128) name = .from(typed);
+ TagState.toRest(p);
+ if (same) return;
+ if (typed.len > 128) return p.reportFailure(id, "Rename: a name is at most 128 bytes");
+ return lspRequest(p, id, .rename, name.slice());
+ }
+ _ = s.field.key(key);
+}
+
+/// The name under pane `pane`'s caret, which a Rename renames.
+pub fn renamedWord(pane: *const Pane) ?[]const u8 {
+ const line = panes.File.sourceLine(pane, pane.body.cur_row);
+ var lo: usize = @min(@as(usize, @intCast(@max(0, pane.body.cur_col))), line.len);
+ var hi = lo;
+ while (lo > 0 and (std.ascii.isAlphanumeric(line[lo - 1]) or line[lo - 1] == '_')) lo -= 1;
+ while (hi < line.len and (std.ascii.isAlphanumeric(line[hi]) or line[hi] == '_')) hi += 1;
+ return if (hi > lo) line[lo..hi] else null;
+}
+
/// The builtin word that asks `kind`, for what it says.
fn lspWord(kind: lsp.Kind) []const u8 {
return switch (kind) {
@@ -333,6 +382,14 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void {
return;
}
+ // A hover asked from the keyboard: ghost lines in its own body, no
+ // +Hover pane (SPEC §3.5).
+ if (w.kind == .hover and pardes.Pick.asked(p)) {
+ var cut = @min(rows.len, hover_max);
+ while (cut < rows.len and cut > 0 and rows[cut] & 0xC0 == 0x80) cut -= 1;
+ p.lsp_ask.hover = .{ .serial = pane.serial, .row = w.row, .revision = w.revision, .text = .from(rows[0..cut]) };
+ return;
+ }
const from: panes.Output.Origin = .{ .query = w.kind };
const nrows = std.mem.count(u8, rows, "\n");
if ((panes.Output.traits(from).jumps or w.kind == .diagnostics) and nrows == 1) {
@@ -628,3 +685,95 @@ test "LSP failure clears only its matching wait without completion indentation"
try std.testing.expectEqualStrings("x ", pane.file.?.content);
try std.testing.expectEqual(@as(i32, 4), pane.body.cur_col);
}
+
+test "a hover asked from the keyboard is ghost lines under the line it was asked at, drawn and never written, no +Hover pane; Esc takes them away" {
+ if (!lsp.supports.contains(.hover)) return error.SkipZigTest;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 20 });
+ defer p.deinit();
+ const pane = try p.setTestFile("one\ntwo\nthree\nfour\n");
+ const id = p.view().active;
+ p.settings.jumps = .walk;
+ while (p.nextEffect()) |_| {}
+ pane.focus = .body;
+ pane.body.cur_row = 1;
+ var npanes: usize = 0;
+ for (p.panes) |slot| npanes += @intFromBool(slot != null);
+ p.lspRequest(id, .hover, "");
+ while (p.nextEffect()) |_| {}
+ lspResponse(p, p.lsp_ask.seq, "pub fn add(a: i32) i32\nadds them\n");
+ var now: usize = 0;
+ for (p.panes) |slot| now += @intFromBool(slot != null);
+ try std.testing.expectEqual(npanes, now);
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ p.sync();
+ var s = try p.render(frame.allocator());
+ const body = s.region(.body, id).?.rect;
+ var buf: [256]u8 = undefined;
+ try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 1, &buf), "two") != null);
+ try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 2, &buf), "pub fn add(a: i32) i32") != null);
+ try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "adds them") != null);
+ try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 4, &buf), "three") != null);
+ try std.testing.expectEqualStrings("one\ntwo\nthree\nfour\n", pane.file.?.content);
+ p.update(.{ .key = .{ .cp = pardes.Key.escape } });
+ try std.testing.expect(p.lsp_ask.hover == null);
+ try std.testing.expectEqual(id, p.view().active);
+ _ = frame.reset(.retain_capacity);
+ s = try p.render(frame.allocator());
+ try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 2, &buf), "three") != null);
+}
+
+/// Grid row `y` of `s` from column `x`, as text.
+fn rowText(s: *const pardes.Surface, x: u16, y: u16, buf: []u8) []const u8 {
+ var n: usize = 0;
+ for (x..s.cols) |col| {
+ const g = s.cells[@as(usize, y) * s.cols + col].grapheme();
+ if (n + g.len > buf.len) break;
+ @memcpy(buf[n..][0..g.len], g);
+ n += g.len;
+ }
+ return buf[0..n];
+}
+
+test "SPC l r types the new name in the pane's own tag, the body showing each place in view renamed as it is typed, never written; Enter asks the server for it, Esc puts it away" {
+ if (!lsp.supports.contains(.rename)) return error.SkipZigTest;
+ const Key = pardes.Key;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 20 });
+ defer p.deinit();
+ const pane = try p.setTestFile("const best = 1;\nconst b = best + best;\nconst bestow = 2;\n");
+ const id = p.view().active;
+ p.settings.jumps = .walk;
+ pane.focus = .body;
+ pane.body.cur_row = 0;
+ pane.body.cur_col = 7;
+ for (" lr") |c| p.update(.{ .key = .{ .cp = c } });
+ const s = &p.view().tag_state;
+ try std.testing.expectEqual(TagState.Kind.rename, s.kind);
+ try std.testing.expectEqualStrings("best", s.field.text());
+ for ("er") |c| p.update(.{ .key = .{ .cp = c } });
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const a = frame.allocator();
+ try std.testing.expect(std.mem.indexOf(u8, try pardes.tagline.tagText(p, a, pane), " Rename best → bester") != null);
+ p.sync();
+ const sf = try p.render(a);
+ const body = sf.region(.body, id).?.rect;
+ var buf: [256]u8 = undefined;
+ // Each `best` in view struck and followed by the new name; `bestow` is
+ // another word.
+ try std.testing.expect(std.mem.indexOf(u8, rowText(sf, body.x, body.y, &buf), "const bestbester = 1;") != null);
+ try std.testing.expect(std.mem.indexOf(u8, rowText(sf, body.x, body.y + 1, &buf), "const b = bestbester + bestbester;") != null);
+ try std.testing.expect(std.mem.indexOf(u8, rowText(sf, body.x, body.y + 2, &buf), "const bestow = 2;") != null);
+ try std.testing.expectEqualStrings("const best = 1;\nconst b = best + best;\nconst bestow = 2;\n", pane.file.?.content);
+ // Enter: the server asked to rename it, the tag at rest.
+ while (p.nextEffect()) |_| {}
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expectEqual(TagState.Kind.rest, s.kind);
+ try std.testing.expectEqualStrings("bester", p.lsp_ask.wait.?.arg.slice());
+ // Esc from the field asks nothing.
+ p.lsp_ask.wait = null;
+ for (" lr") |c| p.update(.{ .key = .{ .cp = c } });
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expectEqual(TagState.Kind.rest, s.kind);
+ try std.testing.expect(p.lsp_ask.wait == null);
+}