Added encoding offset aware documentRange

This commit is contained in:
Alexandros Naskos 2020-07-03 12:39:58 +03:00
parent 28b3cafc3f
commit 6bf2c9a1c7
3 changed files with 47 additions and 22 deletions

View File

@ -1245,7 +1245,7 @@ fn formattingHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req:
.result = .{ .result = .{
.TextEdits = &[1]types.TextEdit{ .TextEdits = &[1]types.TextEdit{
.{ .{
.range = handle.document.range(), .range = try offsets.documentRange(handle.document, offset_encoding),
.newText = stdout_bytes, .newText = stdout_bytes,
}, },
}, },

View File

@ -113,3 +113,49 @@ pub fn tokenLength(tree: *std.zig.ast.Tree, token: std.zig.ast.TokenIndex, encod
} }
return utf16_len; return utf16_len;
} }
pub fn documentRange(doc: types.TextDocument, encoding: Encoding) !types.Range {
var line_idx: i64 = 0;
var curr_line: []const u8 = doc.text;
var split_iterator = std.mem.split(doc.text, "\n");
while (split_iterator.next()) |line| : (line_idx += 1) {
curr_line = line;
}
if (encoding == .utf8) {
return types.Range{
.start = .{
.line = 0,
.character = 0,
},
.end = .{
.line = line_idx,
.character = @intCast(i64, curr_line.len),
},
};
} else {
var utf16_len: usize = 0;
var line_utf8_idx: usize = 0;
while (line_utf8_idx < curr_line.len) {
const n = try std.unicode.utf8ByteSequenceLength(curr_line[line_utf8_idx]);
const codepoint = try std.unicode.utf8Decode(curr_line[line_utf8_idx .. line_utf8_idx + n]);
if (codepoint < 0x10000) {
utf16_len += 1;
} else {
utf16_len += 2;
}
line_utf8_idx += n;
}
return types.Range{
.start = .{
.line = 0,
.character = 0,
},
.end = .{
.line = line_idx,
.character = @intCast(i64, utf16_len),
},
};
}
}

View File

@ -138,27 +138,6 @@ pub const TextDocument = struct {
text: String, text: String,
// This holds the memory that we have actually allocated. // This holds the memory that we have actually allocated.
mem: []u8, mem: []u8,
pub fn range(self: TextDocument) Range {
var line_idx: i64 = 0;
var curr_line: []const u8 = self.text;
var split_iterator = std.mem.split(self.text, "\n");
while (split_iterator.next()) |line| : (line_idx += 1) {
curr_line = line;
}
return .{
.start = .{
.line = 0,
.character = 0,
},
.end = .{
.line = line_idx,
.character = @intCast(i64, curr_line.len),
},
};
}
}; };
pub const WorkspaceEdit = struct { pub const WorkspaceEdit = struct {