format with Ast.render
instead of zig fmt
(#755)
This commit is contained in:
parent
68ab004bb1
commit
662b560861
@ -2043,69 +2043,48 @@ 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);
|
return try send(writer, server.arena.allocator(), types.Response{
|
||||||
|
.id = id,
|
||||||
var edits = diff.edits(server.arena.allocator(), handle.text, stdout_bytes) catch {
|
.result = .{
|
||||||
const range = offsets.locToRange(handle.text, .{ .start = 0, .end = handle.text.len }, server.offset_encoding);
|
.TextEdits = &[1]types.TextEdit{.{
|
||||||
// If there was an error trying to diff the text, return the formatted response
|
.range = offsets.locToRange(handle.text, .{ .start = 0, .end = handle.text.len }, server.offset_encoding),
|
||||||
// as the new text for the entire range of the document
|
.newText = formatted,
|
||||||
return try send(writer, server.arena.allocator(), types.Response{
|
}},
|
||||||
.id = id,
|
|
||||||
.result = .{
|
|
||||||
.TextEdits = &[1]types.TextEdit{
|
|
||||||
.{
|
|
||||||
.range = range,
|
|
||||||
.newText = stdout_bytes,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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 send(
|
|
||||||
writer,
|
|
||||||
server.arena.allocator(),
|
|
||||||
types.Response{
|
|
||||||
.id = id,
|
|
||||||
.result = .{ .TextEdits = text_edits.items },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
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 {
|
fn didChangeConfigurationHandler(server: *Server, writer: anytype, id: types.RequestId, req: Config.DidChangeConfigurationParams) !void {
|
||||||
|
Loading…
Reference in New Issue
Block a user