diff options
Diffstat (limited to 'core/lsp')
| -rw-r--r-- | core/lsp/Ask.zig | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/core/lsp/Ask.zig b/core/lsp/Ask.zig index bb1eccb6..f10ddf26 100644 --- a/core/lsp/Ask.zig +++ b/core/lsp/Ask.zig @@ -38,9 +38,10 @@ wait: ?struct { arg: Effect.Buf(128), row: i32 = 0, col: i32 = 0, - /// A hover asked from the keyboard where jumps walk (Pick.asked, when - /// asked: a 9P script's answer comes after its write): drawn as ghost - /// lines, no +Hover pane. + /// A hover or code actions asked from the keyboard where jumps walk + /// (Pick.asked, when asked: a 9P script's answer comes after its write): + /// a hover drawn as ghost lines, no +Hover pane; the actions a picker + /// in the pane's tag, no +Lsp. ghost: bool = false, } = null, /// A hover asked from the keyboard (where the walk is on), drawn as ghost @@ -125,7 +126,7 @@ pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) .arg = .from(arg), .row = pane.body.cur_row, .col = pane.body.cur_col, - .ghost = kind == .hover and pardes.Pick.asked(p), + .ghost = (kind == .hover or kind == .code_action) and pardes.Pick.asked(p), }; p.emit(.{ .lsp = .{ .id = p.lsp_ask.seq, @@ -136,11 +137,12 @@ pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) } }); } -const LspEdit = struct { start: usize, end: usize, text: []const u8 }; +pub const LspEdit = struct { start: usize, end: usize, text: []const u8 }; -fn parseLspEdits(p: *Pardes, bytes: []const u8, fallback: ?[]const u8) ?[]LspEdit { +/// The client's edit records (`@put`, or `@edit` with `fallback`'s text), +/// in `a`; null for anything else or a malformed one. +pub fn parseLspEdits(a: std.mem.Allocator, bytes: []const u8, fallback: ?[]const u8) ?[]LspEdit { if (bytes.len == 0 or bytes[bytes.len - 1] != '\n') return null; - const a = p.scratch.allocator(); const edits = a.alloc(LspEdit, std.mem.count(u8, bytes, "\n")) catch return null; var lines = std.mem.splitScalar(u8, bytes, '\n'); var n: usize = 0; @@ -205,6 +207,14 @@ fn mapLspEditOffset(edits: []const LspEdit, old: usize) usize { return new_at + (old - old_at); } +/// A code action's edit records (Pick.zig's actions) made in pane `pane`, +/// whose text they are of at `revision`: one undo step; false when they +/// are not (malformed, of another revision, changing nothing). +pub fn applyAction(p: *Pardes, pane: *Pane, revision: u32, records: []const u8) bool { + const edits = parseLspEdits(p.scratch.allocator(), records, null) orelse return false; + return applyLspEdits(p, pane, revision, edits); +} + /// The server's edits, of the file's text (its offsets; the `Changes` /// layer's lines are none of it), made as the file's new text (File.load). fn applyLspEdits(p: *Pardes, pane: *Pane, revision: u32, edits: []const LspEdit) bool { @@ -357,7 +367,7 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { } if (w.kind == .rename or w.kind == .format) { const fallback: ?[]const u8 = if (w.kind == .rename) w.arg.slice() else null; - if (parseLspEdits(p, rows, fallback)) |edits| { + if (parseLspEdits(p.scratch.allocator(), rows, fallback)) |edits| { if (applyLspEdits(p, pane, w.revision, edits)) { var buf: pardes.Kind.Buf(64) = .{}; const verb: []const u8 = if (w.kind == .rename) "renamed" else "formatted"; @@ -397,8 +407,25 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { p.lsp_ask.hover = .{ .serial = pane.serial, .row = w.row, .revision = w.revision, .text = .from(rows[0..cut]) }; return; } + // Code actions asked from the keyboard: a picker of them in the pane's + // own tag, each previewed as its edit and applied by Enter or its digit. + if (w.kind == .code_action and w.ghost) { + pardes.Pick.open(p, asked, .actions, "", "", rows); + if (pardes.Pick.live(p)) |pk| pk.revision = w.revision; + return; + } + // Anyone else's: their titles alone, in a +Lsp. + const listed_rows = if (w.kind == .code_action) titles: { + var kept: std.ArrayList(u8) = .empty; + var lines = std.mem.splitScalar(u8, rows, '\n'); + while (lines.next()) |ln| if (ln.len > 0 and ln[0] != '@') { + kept.appendSlice(p.scratch.allocator(), ln) catch |err| return p.reportError(w.pane, lspWord(w.kind), err); + kept.append(p.scratch.allocator(), '\n') catch |err| return p.reportError(w.pane, lspWord(w.kind), err); + }; + break :titles kept.items; + } else rows; const from: panes.Output.Origin = .{ .query = w.kind }; - const nrows = std.mem.count(u8, rows, "\n"); + const nrows = std.mem.count(u8, listed_rows, "\n"); 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]; @@ -452,7 +479,7 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { return; }; const dir = if (pane.file) |f| panes.Dir.ofName(f.path) else pane.cwdSlice(); - const content = p.gpa.dupe(u8, rows) catch |err| return p.reportError(w.pane, lspWord(w.kind), err); + const content = p.gpa.dupe(u8, listed_rows) catch |err| return p.reportError(w.pane, lspWord(w.kind), err); const serial_before = p.next_serial; p.fs.refilled = null; panes.Output.fillResults(p, asked, dir, from, w.arg.slice(), content, null) catch |err| { |
