diff options
Diffstat (limited to 'core/lsp')
| -rw-r--r-- | core/lsp/Ask.zig | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/core/lsp/Ask.zig b/core/lsp/Ask.zig index 500f02b5..4d57f21e 100644 --- a/core/lsp/Ask.zig +++ b/core/lsp/Ask.zig @@ -62,15 +62,18 @@ pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) (!zig_buf and std.mem.indexOfAny(u8, arg, " \t\r\n\"\\") != null)) return p.reportFailure(id, said.print("Rename: {s} is no name", .{arg})); } - var off = if (pane.file) |f| modal.offsetAt(f.content, .{ + // A place in the file's text, as the server has it (File.fileText: the + // `Changes` layer's lines are none of it; one in them is after the file + // line above). + var off = if (pane.file) |*f| panes.File.fileOffset(f, modal.offsetAt(f.content, .{ .row = @intCast(@max(0, pane.body.cur_row)), .col = @intCast(@max(0, pane.body.cur_col)), - }) else 0; + })) else 0; // Def or Refs given a symbol (a 2-1 chord's selection) asks at its // occurrence nearest the cursor, which a sweep leaves at one end of // what it selected; the server is asked at a place, never a name. if ((kind == .definition or kind == .references) and arg.len > 0) { - const content = pane.file.?.content; + const content = panes.File.fileText(&pane.file.?, p.scratch.allocator()) catch |err| return p.reportError(.at(id), lspWord(kind), err); var nearest: ?usize = null; var best: usize = 0; var from: usize = 0; @@ -188,48 +191,51 @@ fn mapLspEditOffset(edits: []const LspEdit, old: usize) usize { return new_at + (old - old_at); } +/// 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 { const f = if (pane.file) |*file| file else return false; if (f.revision != revision) return false; + const text = panes.File.fileText(f, p.scratch.allocator()) catch return false; var removed: usize = 0; var added: usize = 0; var previous_end: usize = 0; var same = true; for (edits) |e| { - if (e.start < previous_end or e.start > e.end or e.end > f.content.len) return false; + if (e.start < previous_end or e.start > e.end or e.end > text.len) return false; if (e.start == e.end and e.text.len == 0) return false; // an empty insert is a malformed record removed = std.math.add(usize, removed, e.end - e.start) catch return false; added = std.math.add(usize, added, e.text.len) catch return false; previous_end = e.end; - if (same and !std.mem.eql(u8, f.content[e.start..e.end], e.text)) same = false; + if (same and !std.mem.eql(u8, text[e.start..e.end], e.text)) same = false; } if (same) return false; // a no-op response earns no undo entry - const final_len = std.math.add(usize, f.content.len - removed, added) catch return false; + const final_len = std.math.add(usize, text.len - removed, added) catch return false; const replacement = p.gpa.alloc(u8, final_len) catch return false; - const old_cursor = modal.offsetAt(f.content, .{ + const old_cursor = panes.File.fileOffset(f, modal.offsetAt(f.content, .{ .row = @intCast(@max(0, pane.body.cur_row)), .col = @intCast(@max(0, pane.body.cur_col)), - }); + })); const mapped_cursor = mapLspEditOffset(edits, old_cursor); var read_at: usize = 0; var write_at: usize = 0; for (edits) |e| { - @memcpy(replacement[write_at .. write_at + (e.start - read_at)], f.content[read_at..e.start]); + @memcpy(replacement[write_at .. write_at + (e.start - read_at)], text[read_at..e.start]); write_at += e.start - read_at; @memcpy(replacement[write_at .. write_at + e.text.len], e.text); write_at += e.text.len; read_at = e.end; } - @memcpy(replacement[write_at..], f.content[read_at..]); + @memcpy(replacement[write_at..], text[read_at..]); edit.pushUndo(p.editor(), &pane.body) catch { p.gpa.free(replacement); return false; }; - panes.File.setContent(p, f, replacement); - const cursor = modal.positionAt(f.content, mapped_cursor); + panes.File.load(p, f, replacement); + const cursor = modal.positionAt(f.content, panes.File.bufferOffset(f, mapped_cursor, false)); pane.body.cur_row = @intCast(cursor.row); pane.body.cur_col = @intCast(cursor.col); pane.body.vsel.active = false; @@ -336,7 +342,8 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { // and n and N step it. const spot = look.parsePathLine(place); if (!p.look_missed and spot.at.line > 0) if (p.panes[v.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); + // The server's line is the file's (File.bufferRow). + const ls = panes.File.lineStart(p.gpa, tf, panes.File.bufferRow(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. |
