From 6bf2c9a1c7ebc5b1cc0e32bb83bd7affc89becae Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Fri, 3 Jul 2020 12:39:58 +0300 Subject: [PATCH] Added encoding offset aware documentRange --- src/main.zig | 2 +- src/offsets.zig | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/types.zig | 21 --------------------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/main.zig b/src/main.zig index 71cc132..3dd9c3b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1245,7 +1245,7 @@ fn formattingHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: .result = .{ .TextEdits = &[1]types.TextEdit{ .{ - .range = handle.document.range(), + .range = try offsets.documentRange(handle.document, offset_encoding), .newText = stdout_bytes, }, }, diff --git a/src/offsets.zig b/src/offsets.zig index 9a689b8..8f5ef6a 100644 --- a/src/offsets.zig +++ b/src/offsets.zig @@ -113,3 +113,49 @@ pub fn tokenLength(tree: *std.zig.ast.Tree, token: std.zig.ast.TokenIndex, encod } 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), + }, + }; + } +} diff --git a/src/types.zig b/src/types.zig index 3280fa3..f39ff1e 100644 --- a/src/types.zig +++ b/src/types.zig @@ -138,27 +138,6 @@ pub const TextDocument = struct { text: String, // This holds the memory that we have actually allocated. 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 {