-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add a format guard #1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a format guard #1033
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -729,15 +729,37 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void { | |
}; | ||
|
||
for (flags.positionals.toSliceConst()) |file_path| { | ||
var file = try os.File.openRead(allocator, file_path); | ||
defer file.close(); | ||
|
||
const source_code = io.readFileAlloc(allocator, file_path) catch |err| { | ||
var file = os.File.openRead(allocator, file_path) catch |err| { | ||
try stderr.print("unable to open '{}': {}", file_path, err); | ||
continue; | ||
}; | ||
defer file.close(); | ||
const size = try file.getEndPos(); | ||
|
||
var adaptater = io.FileInStream.init(&file); | ||
|
||
var source_code: []u8 = undefined; | ||
var start_index: usize = 0; | ||
|
||
if(size < 16){ | ||
source_code = try allocator.alloc(u8, size); | ||
} | ||
else { | ||
source_code = try allocator.alloc(u8, 16); | ||
start_index = try adaptater.stream.read(source_code); | ||
if(mem.eql(u8, source_code, "// zig fmt: skip")){ | ||
try stderr.print("won't format {}\n", file_path); | ||
allocator.free(source_code); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this free? Won't it happen at line 756? If you want to handle the potential There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it is a fixed size, one thing you could do here is read into a local scope There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I actually planned on doing that but couldn't find a seek function. Well, I checked again and found |
||
continue; | ||
} | ||
else{ | ||
source_code = try allocator.realloc(u8, source_code, size); | ||
} | ||
} | ||
defer allocator.free(source_code); | ||
|
||
_ = try adaptater.stream.read(source_code[start_index..]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this an error if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
var tree = std.zig.parse(allocator, source_code) catch |err| { | ||
try stderr.print("error parsing file '{}': {}\n", file_path, err); | ||
continue; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you meant
adapter
.