Skip to content

Commit

Permalink
allow files with only preprocessor directives
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanderc committed Dec 6, 2023
1 parent f5694d1 commit 353f62e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ zig-out
zig-cache
stderr.log
__pycache__
.venv
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ run *ARGS:
zig build run --summary none {{flags}} -- {{ARGS}}

watch *ARGS:
#!/usr/bin/bash
watchexec -e zig,py,vert,frag,comp -c -- "just ${*@Q} && echo ok"

test:
Expand Down
2 changes: 1 addition & 1 deletion src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ fn findIncludedDocuments(
const directive = parse.parsePreprocessorDirective(line) orelse continue;
switch (directive) {
.include => |include| {
var include_path = line[include.path.start..include.path.end];
const include_path = line[include.path.start..include.path.end];

const is_relative = !std.fs.path.isAbsolute(include_path);

Expand Down
23 changes: 22 additions & 1 deletion src/format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn Writer(comptime ChildWriter: type) type {
self.preceded_by_space = last == ' ' or last == '\n';
}

fn emitLeadingTokens(self: *Self, until: u32) !void {
fn emitLeadingTokens(self: *Self, until: usize) !void {
while (self.last_emit < until) {
if (self.last_ignored >= self.ignored_tokens.len) break;

Expand Down Expand Up @@ -180,6 +180,7 @@ fn formatNode(tree: parse.Tree, current: usize, writer: anytype) !void {
for (children.start..children.end) |child| {
try formatNode(tree, child, writer);
}
try writer.emitLeadingTokens(writer.source.len);
try writer.emitLeadingWhitespace(writer.source.len, .{ .min = 1, .max = 1 });
},

Expand Down Expand Up @@ -617,6 +618,26 @@ test "format array declaration" {
);
}

test "format only preprocessor" {
try expectIsFormatted(
\\#version 330
\\
);
try expectIsFormatted(
\\#version 330
\\#define FOO
\\#define BAR
\\
);
}

test "format only comments" {
try expectIsFormatted(
\\// hello world
\\
);
}

fn expectIsFormatted(source: []const u8) !void {
try expectFormat(source, source);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ pub const Dispatch = struct {
return state.success(request.id, null);
}

var locations = try state.allocator.alloc(lsp.Location, references.items.len);
const locations = try state.allocator.alloc(lsp.Location, references.items.len);
defer state.allocator.free(locations);

for (references.items, locations) |reference, *location| {
Expand Down
2 changes: 1 addition & 1 deletion src/syntax.zig
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub fn Extractor(comptime expected_tag: Tag, comptime T: type) type {
pub fn extract(tree: Tree, node: u32, _: void) @This() {
var matches = MatchFields{};

var children = tree.children(node);
const children = tree.children(node);
var current = children.start;

inline for (fields) |field| {
Expand Down

0 comments on commit 353f62e

Please sign in to comment.