Skip to content

Commit

Permalink
Switch to Diff Match Patch (diffz) (#982)
Browse files Browse the repository at this point in the history
* Add tests, note about correctness issue

* Use diffz (DiffMatchPatch)
  • Loading branch information
SuperAuguste authored Feb 11, 2023
1 parent 73d6264 commit bf19ed3
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 342 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "src/tres"]
path = src/tres
url = https://github.com/ziglibs/tres.git
[submodule "src/diffz"]
path = src/diffz
url = https://github.com/ziglibs/diffz
8 changes: 8 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,15 @@ pub fn build(b: *std.build.Builder) !void {
const tres_module = b.createModule(.{ .source_file = .{ .path = tres_path } });
exe.addModule("tres", tres_module);

const DIFFZ_DEFAULT_PATH = "src/diffz/DiffMatchPatch.zig";
const diffz_path = b.option([]const u8, "diffz", "Path to diffz package (default: " ++ DIFFZ_DEFAULT_PATH ++ ")") orelse DIFFZ_DEFAULT_PATH;
const diffz_module = b.createModule(.{ .source_file = .{ .path = diffz_path } });
exe.addModule("diffz", diffz_module);

const check_submodules_step = CheckSubmodulesStep.init(b, &.{
known_folders_path,
tres_path,
diffz_path,
});
b.getInstallStep().dependOn(&check_submodules_step.step);

Expand Down Expand Up @@ -201,11 +207,13 @@ pub fn build(b: *std.build.Builder) !void {
.dependencies = &.{
.{ .name = "known-folders", .module = known_folders_module },
.{ .name = "tres", .module = tres_module },
.{ .name = "diffz", .module = diffz_module },
.{ .name = "build_options", .module = build_options_module },
},
});
tests.addModule("zls", zls_module);
tests.addModule("tres", tres_module);
tests.addModule("diffz", diffz_module);

test_step.dependOn(&tests.step);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,7 @@ fn changeDocumentHandler(server: *Server, notification: types.DidChangeTextDocum

const handle = server.document_store.getHandle(notification.textDocument.uri) orelse return;

const new_text = try diff.applyTextEdits(server.allocator, handle.text, notification.contentChanges, server.offset_encoding);
const new_text = try diff.applyContentChanges(server.allocator, handle.text, notification.contentChanges, server.offset_encoding);

try server.document_store.refreshDocument(handle.uri, new_text);

Expand Down Expand Up @@ -2372,7 +2372,7 @@ fn formattingHandler(server: *Server, request: types.DocumentFormattingParams) E
return text_edits;
}

return if (diff.edits(allocator, handle.text, formatted)) |text_edits| text_edits.items else |_| null;
return if (diff.edits(allocator, handle.text, formatted, server.offset_encoding)) |text_edits| text_edits.items else |_| null;
}

fn didChangeConfigurationHandler(server: *Server, request: configuration.DidChangeConfigurationParams) Error!void {
Expand Down
Loading

0 comments on commit bf19ed3

Please sign in to comment.