diff options
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/gui/gui.zig | 7 | ||||
| -rw-r--r-- | dev/lsp/lsp_client.zig | 85 | ||||
| -rw-r--r-- | dev/tui/tui.zig | 39 |
3 files changed, 107 insertions, 24 deletions
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 6cd97b69..9f6e4876 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -1555,10 +1555,13 @@ const StdinFeed = struct { // finger;<kind>;<id>;<x>;<y>;<pressure> // close: the window's close button // One the read cut short, or what may yet begin one, waits for - // the next read. + // the next read; but a read that ends in a lone ESC ends in the + // Escape key, as the tty's reader has a read of one ESC (a driver + // writes each key and event whole): held, it waited for the next + // write, which then came as Alt on its first key. const rest = f.buf[seq_start..len]; const osc = "\x1b]777;"; - if (std.mem.startsWith(u8, rest, osc) or std.mem.startsWith(u8, osc, rest)) { + if (std.mem.startsWith(u8, rest, osc) or (rest.len > 1 and std.mem.startsWith(u8, osc, rest))) { const bel = std.mem.indexOfScalar(u8, rest, 0x07) orelse { std.mem.copyForwards(u8, f.buf[0..rest.len], rest); f.fill = rest.len; diff --git a/dev/lsp/lsp_client.zig b/dev/lsp/lsp_client.zig index fdd4af55..a53ff57f 100644 --- a/dev/lsp/lsp_client.zig +++ b/dev/lsp/lsp_client.zig @@ -247,6 +247,8 @@ const Caps = struct { pull_workspace: bool = false, call_hier: bool = false, type_hier: bool = false, + /// codeAction/resolve: an action that came without its edit is asked for it + resolve_actions: bool = false, /// workspace/didChangeWorkspaceFolders is worth sending folders: bool = false, }; @@ -1038,10 +1040,19 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, var b = try docParams(arena, uri.items); try b.print(arena, ",\"range\":{{\"start\":{{\"line\":{d},\"character\":{d}}},\"end\":{{\"line\":{d},\"character\":{d}}}}},\"context\":{{\"diagnostics\":[]}}", .{ pos.line, pos.ch, pos.line, pos.ch }); const result = (try call(c, io, arena, "textDocument/codeAction", b.items, deadline)) orelse return; - for (items(result)) |ca| { - const title = str(get(ca, "title")) orelse continue; + // Each action a row, its edit of this file after it as `@put` + // records (the core's picker previews and applies them, Pick.zig); + // one the server left to resolve asked for its edit first. + for (items(result)) |listed| { + const title = str(get(listed, "title")) orelse continue; try lsp.row(cx.out, lsp.rel(cx.base, cx.cur_path), pos.line, 0, flat(arena, title)); cx.rows += 1; + var ca = listed; + if (get(ca, "edit") == null and get(ca, "data") != null and c.caps.resolve_actions and ca == .object) { + const whole = try std.json.Stringify.valueAlloc(arena, ca, .{}); + if (try call(c, io, arena, "codeAction/resolve", whole[1 .. whole.len - 1], deadline)) |resolved| ca = resolved; + } + try actionEdits(&cx, c.caps.enc, uri.items, get(ca, "edit"), req.source); } }, .completion => { @@ -1246,24 +1257,8 @@ fn byteSpan(src: []const u8, range: ?std.json.Value, enc: Enc) ?struct { start: /// applying a fraction of a workspace rename would be worse than either. fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value, src: []const u8, old: []const u8, also: []const []const u8) Err!void { var edits: std.ArrayList(PutEdit) = .empty; - var foreign = false; - - // (uri, edits) per file, from either shape of a WorkspaceEdit - var files: std.ArrayList(struct { []const u8, ?std.json.Value }) = .empty; - if (get(result, "documentChanges")) |dcs| { - for (items(dcs)) |dc| { - if (get(dc, "kind") != null) { - foreign = true; // create/rename/delete file operations - continue; - } - const u = str(get(get(dc, "textDocument"), "uri")) orelse continue; - try files.append(cx.arena, .{ u, get(dc, "edits") }); - } - } else if (get(result, "changes")) |ch| if (ch == .object) { - var it = ch.object.iterator(); - while (it.next()) |e| try files.append(cx.arena, .{ e.key_ptr.*, e.value_ptr.* }); - }; - for (files.items) |f| { + const files, var foreign = try editFiles(cx.arena, result); + for (files) |f| { const in_self = std.mem.eql(u8, f[0], self_uri); if (!in_self) foreign = true; for (items(f[1])) |ed| { @@ -1299,6 +1294,55 @@ fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value, for (edits.items) |ed| try lsp.put(cx.out, ed.start, ed.end, ed.text); } +/// A file a WorkspaceEdit edits: its uri and its TextEdit[]. +const EditFile = struct { []const u8, ?std.json.Value }; + +/// A WorkspaceEdit's files, from either of its shapes, and whether it also +/// creates, renames or deletes a file. +fn editFiles(arena: std.mem.Allocator, we: std.json.Value) Err!struct { []const EditFile, bool } { + var files: std.ArrayList(EditFile) = .empty; + var ops = false; + if (get(we, "documentChanges")) |dcs| { + for (items(dcs)) |dc| { + if (get(dc, "kind") != null) { + ops = true; + continue; + } + const u = str(get(get(dc, "textDocument"), "uri")) orelse continue; + try files.append(arena, .{ u, get(dc, "edits") }); + } + } else if (get(we, "changes")) |ch| if (ch == .object) { + var it = ch.object.iterator(); + while (it.next()) |e| try files.append(arena, .{ e.key_ptr.*, e.value_ptr.* }); + }; + return .{ files.items, ops }; +} + +/// A code action's WorkspaceEdit (none for a command's): its edits of this +/// file as sorted `@put` records; one that reaches another file or makes +/// one is `@elsewhere`, never a part of it applied; a malformed edit leaves +/// the action none. +fn actionEdits(cx: *Cx, enc: Enc, self_uri: []const u8, we: ?std.json.Value, src: []const u8) Err!void { + const v = we orelse return; + const files, const ops = try editFiles(cx.arena, v); + var edits: std.ArrayList(PutEdit) = .empty; + var elsewhere = ops; + for (files) |f| { + if (!std.mem.eql(u8, f[0], self_uri)) { + elsewhere = true; + continue; + } + for (items(f[1])) |ed| { + const span = byteSpan(src, get(ed, "range"), enc) orelse return; + const text = str(get(ed, "newText")) orelse return; + try edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text }); + } + } + if (elsewhere) return cx.out.writeAll("@elsewhere\n"); + sortEdits(edits.items); + for (edits.items) |ed| try lsp.put(cx.out, ed.start, ed.end, ed.text); +} + /// TextEdit[] from formatting is by definition about the current document: /// straight to sorted `@put` records. fn formatEdits(cx: *Cx, enc: Enc, result: std.json.Value, src: []const u8) Err!void { @@ -2111,6 +2155,7 @@ fn handshake(c: *Conn, io: std.Io, arena: std.mem.Allocator, root: []const u8) E } c.caps.call_hier = provider(get(caps, "callHierarchyProvider")); c.caps.type_hier = provider(get(caps, "typeHierarchyProvider")); + c.caps.resolve_actions = if (get(get(caps, "codeActionProvider"), "resolveProvider")) |r| r == .bool and r.bool else false; c.caps.folders = if (get(get(get(caps, "workspace"), "workspaceFolders"), "supported")) |s| s == .bool and s.bool else false; try send(c, io, "{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}", deadline); diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 542ef59a..aa1f8849 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -179,6 +179,9 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { var carried: usize = 0; // The sequence carried is past max_sequence, and said so. var dropping = false; + // The ESC the last read ended in, carried because more came while it + // waited for a sequence's rest. + var esc_held = false; while (true) { // stopInput cancels the task; the read then fails with Canceled if (carried == buf.len and buf.len < max_sequence) { if (gpa.realloc(buf, @min(2 * buf.len, max_sequence))) |grown| buf = grown else |_| {} @@ -227,6 +230,12 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { if (scalar_len > end - (lead - 1)) parse_end = lead - 1; } var consumed: usize = 0; + // That ESC is the Escape key unless this read goes on with a + // sequence's rest (an ssh packet cut it after its ESC): its + // introducer, CSI's `[`, SS3's `O`, a string's, or another ESC. A + // key typed just after Escape is its own, not Alt on it. + const lone_esc = esc_held and end > 1 and buf[0] == 0x1b and std.mem.indexOfScalar(u8, "[O]P_^\\\x1b", buf[1]) == null; + esc_held = false; // A read that is one ESC and nothing else is the Escape key: a // terminal writes each key's sequence whole and ssh carries each of // its writes whole, so no sequence arrives cut right after its ESC. @@ -247,9 +256,12 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { std.mem.startsWith(u8, FrameAck.answer, rest)) break; if (!waited and parse_end == end and rest[0] == 0x1b and (rest.len == 1 or (rest.len == 2 and rest[1] == 0x1b))) { waited = true; - if (try inputFollows(tty, loop.io, if (vx.caps.kitty_keyboard) 1000 else 50)) break; + if (try inputFollows(tty, loop.io, if (vx.caps.kitty_keyboard) 1000 else 50)) { + esc_held = rest.len == 1; + break; + } } - const result = try parser.parse(buf[consumed..parse_end], vx.opts.system_clipboard_allocator); + const result = try parser.parse(buf[consumed..if (lone_esc and consumed == 0) 1 else parse_end], vx.opts.system_clipboard_allocator); if (result.n == 0) break; consumed += result.n; // A modifier pressed on its own, which the kitty keyboard @@ -2668,6 +2680,29 @@ test "an ESC read by itself is the Escape key at once" { t.vx.caps.kitty_keyboard = false; } +test "an ESC that ends a read, a key read after it within the wait, is the Escape key and then that key" { + var t: TestInput = undefined; + try t.init(); + defer t.deinit(); + // Typed faster than read (a burst over a link): `x` and Escape in one + // read, `A` in the next while the ESC waits for a sequence's rest. Joined + // to it, it was Alt-A and the Escape was lost (insert mode kept, `A` + // typed nothing). A sequence cut after its ESC still joins (the test + // above: `[`, `O` and the strings' introducers, another ESC). + var reader: TestReader = .{ .parts = &.{ "x\x1b", "A" } }; + try readInput(&t.loop, &reader, &t.vx, &t.feed); + try std.testing.expect(reader.asked); + try std.testing.expectEqual(.winsize, std.meta.activeTag((try t.loop.tryEvent()).?)); + try std.testing.expectEqual(@as(u21, 'x'), (try t.loop.tryEvent()).?.key_press.codepoint); + const escape = (try t.loop.tryEvent()).?.key_press; + try std.testing.expectEqual(vaxis.Key.escape, escape.codepoint); + try std.testing.expect(!escape.mods.alt); + const a = (try t.loop.tryEvent()).?.key_press; + try std.testing.expectEqual(@as(u21, 'A'), a.codepoint); + try std.testing.expect(!a.mods.alt); + try std.testing.expect(try t.loop.tryEvent() == null); +} + test "an Escape read with the click after it is the Escape key, then the click" { var t: TestInput = undefined; try t.init(); |
