Skip to content

Commit

Permalink
format with Ast.render instead of zig fmt (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix authored Nov 16, 2022
1 parent 68ab004 commit 662b560
Showing 1 changed file with 36 additions and 57 deletions.
93 changes: 36 additions & 57 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2043,69 +2043,48 @@ fn formattingHandler(server: *Server, writer: anytype, id: types.RequestId, req:
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();

if (server.config.zig_exe_path) |zig_exe_path| {
const handle = server.document_store.getHandle(req.params.textDocument.uri) orelse {
return try respondGeneric(writer, id, null_result_response);
};
const handle = server.document_store.getHandle(req.params.textDocument.uri) orelse {
return try respondGeneric(writer, id, null_result_response);
};

var process = std.ChildProcess.init(&[_][]const u8{ zig_exe_path, "fmt", "--stdin" }, server.allocator);
process.stdin_behavior = .Pipe;
process.stdout_behavior = .Pipe;
const formatted = try handle.tree.render(server.allocator);
defer server.allocator.free(formatted);

process.spawn() catch |err| {
log.warn("Failed to spawn zig fmt process, error: {}", .{err});
return try respondGeneric(writer, id, null_result_response);
};
try process.stdin.?.writeAll(handle.text);
process.stdin.?.close();
process.stdin = null;

const stdout_bytes = try process.stdout.?.reader().readAllAlloc(server.allocator, std.math.maxInt(usize));
defer server.allocator.free(stdout_bytes);

switch (try process.wait()) {
.Exited => |code| if (code == 0) {
if (std.mem.eql(u8, handle.text, stdout_bytes)) return try respondGeneric(writer, id, null_result_response);

var edits = diff.edits(server.arena.allocator(), handle.text, stdout_bytes) catch {
const range = offsets.locToRange(handle.text, .{ .start = 0, .end = handle.text.len }, server.offset_encoding);
// If there was an error trying to diff the text, return the formatted response
// as the new text for the entire range of the document
return try send(writer, server.arena.allocator(), types.Response{
.id = id,
.result = .{
.TextEdits = &[1]types.TextEdit{
.{
.range = range,
.newText = stdout_bytes,
},
},
},
});
};
if (std.mem.eql(u8, handle.text, formatted)) return try respondGeneric(writer, id, null_result_response);

// Convert from `[]diff.Edit` to `[]types.TextEdit`
var text_edits = try std.ArrayListUnmanaged(types.TextEdit).initCapacity(server.arena.allocator(), edits.items.len);
for (edits.items) |edit| {
text_edits.appendAssumeCapacity(.{
.range = edit.range,
.newText = edit.newText.items,
});
}
// avoid computing diffs if the output is small
const maybe_edits = if (formatted.len <= 512) null else diff.edits(server.arena.allocator(), handle.text, formatted) catch null;

return try send(
writer,
server.arena.allocator(),
types.Response{
.id = id,
.result = .{ .TextEdits = text_edits.items },
},
);
const edits = maybe_edits orelse {
// if edits have been computed we replace the entire file with the formatted text
return try send(writer, server.arena.allocator(), types.Response{
.id = id,
.result = .{
.TextEdits = &[1]types.TextEdit{.{
.range = offsets.locToRange(handle.text, .{ .start = 0, .end = handle.text.len }, server.offset_encoding),
.newText = formatted,
}},
},
else => {},
}
});
};

// Convert from `[]diff.Edit` to `[]types.TextEdit`
var text_edits = try std.ArrayListUnmanaged(types.TextEdit).initCapacity(server.arena.allocator(), edits.items.len);
for (edits.items) |edit| {
text_edits.appendAssumeCapacity(.{
.range = edit.range,
.newText = edit.newText.items,
});
}
return try respondGeneric(writer, id, null_result_response);

return try send(
writer,
server.arena.allocator(),
types.Response{
.id = id,
.result = .{ .TextEdits = text_edits.items },
},
);
}

fn didChangeConfigurationHandler(server: *Server, writer: anytype, id: types.RequestId, req: Config.DidChangeConfigurationParams) !void {
Expand Down

0 comments on commit 662b560

Please sign in to comment.