format with Ast.render instead of zig fmt (#755)

This commit is contained in:
Techatrix 2022-11-16 23:33:15 +01:00 committed by GitHub
parent 68ab004bb1
commit 662b560861
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2043,43 +2043,27 @@ fn formattingHandler(server: *Server, writer: anytype, id: types.RequestId, req:
const tracy_zone = tracy.trace(@src()); const tracy_zone = tracy.trace(@src());
defer tracy_zone.end(); defer tracy_zone.end();
if (server.config.zig_exe_path) |zig_exe_path| {
const handle = server.document_store.getHandle(req.params.textDocument.uri) orelse { const handle = server.document_store.getHandle(req.params.textDocument.uri) orelse {
return try respondGeneric(writer, id, null_result_response); return try respondGeneric(writer, id, null_result_response);
}; };
var process = std.ChildProcess.init(&[_][]const u8{ zig_exe_path, "fmt", "--stdin" }, server.allocator); const formatted = try handle.tree.render(server.allocator);
process.stdin_behavior = .Pipe; defer server.allocator.free(formatted);
process.stdout_behavior = .Pipe;
process.spawn() catch |err| { if (std.mem.eql(u8, handle.text, formatted)) return try respondGeneric(writer, id, null_result_response);
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)); // avoid computing diffs if the output is small
defer server.allocator.free(stdout_bytes); const maybe_edits = if (formatted.len <= 512) null else diff.edits(server.arena.allocator(), handle.text, formatted) catch null;
switch (try process.wait()) { const edits = maybe_edits orelse {
.Exited => |code| if (code == 0) { // if edits have been computed we replace the entire file with the formatted text
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{ return try send(writer, server.arena.allocator(), types.Response{
.id = id, .id = id,
.result = .{ .result = .{
.TextEdits = &[1]types.TextEdit{ .TextEdits = &[1]types.TextEdit{.{
.{ .range = offsets.locToRange(handle.text, .{ .start = 0, .end = handle.text.len }, server.offset_encoding),
.range = range, .newText = formatted,
.newText = stdout_bytes, }},
},
},
}, },
}); });
}; };
@ -2101,11 +2085,6 @@ fn formattingHandler(server: *Server, writer: anytype, id: types.RequestId, req:
.result = .{ .TextEdits = text_edits.items }, .result = .{ .TextEdits = text_edits.items },
}, },
); );
},
else => {},
}
}
return try respondGeneric(writer, id, null_result_response);
} }
fn didChangeConfigurationHandler(server: *Server, writer: anytype, id: types.RequestId, req: Config.DidChangeConfigurationParams) !void { fn didChangeConfigurationHandler(server: *Server, writer: anytype, id: types.RequestId, req: Config.DidChangeConfigurationParams) !void {