Skip to content

Commit 42f57ab

Browse files
committed
Format comments with topiary
1 parent c669aa1 commit 42f57ab

12 files changed

+121
-4
lines changed

format_zeek/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,25 @@ fn format_zeek(m: &Bound<'_, PyModule>) -> PyResult<()> {
9292

9393
#[cfg(test)]
9494
mod test {
95+
use insta::assert_debug_snapshot;
96+
9597
use crate::FormatError;
9698

9799
fn format(input: &str) -> Result<String, FormatError> {
98100
crate::format(input, false, false)
99101
}
100102

101103
#[test]
102-
fn expression() {
103-
assert_eq!(format("1+1;").unwrap(), "1+1;\n");
104+
fn comments() {
105+
assert_debug_snapshot!(format("# foo\n;1;"));
106+
assert_debug_snapshot!(format("##! foo\n;1;"));
107+
assert_debug_snapshot!(format("## foo\n1;"));
108+
assert_debug_snapshot!(format("##< foo\n1;"));
109+
110+
assert_debug_snapshot!(format("1;# foo\n;1;"));
111+
assert_debug_snapshot!(format("1;##! foo\n;1;"));
112+
assert_debug_snapshot!(format("1;## foo\n1;"));
113+
assert_debug_snapshot!(format("1;##< foo"));
114+
assert_debug_snapshot!(format("1;##< foo\n##< bar"));
104115
}
105116
}

format_zeek/src/query.scm

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; Rules for formatting Spicy.
2+
;
3+
; Formatting is specified here in terms of tree-sitter nodes. We select nodes
4+
; with tree-sitter queries[^1] and then attach topiary formatting rules[^2] in
5+
; the captures.
6+
;
7+
; See the Development section in README.md for a workflow on how to modify or
8+
; extend these rules.
9+
10+
; [^1]: https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries
11+
; [^2]: https://github.com/tweag/topiary#design
12+
13+
; Comments are always followed by a linebreak.
14+
[
15+
(minor_comment)
16+
(zeekygen_head_comment)
17+
(zeekygen_prev_comment)
18+
(zeekygen_next_comment)
19+
] @append_hardline
20+
21+
; Comments are preceeded by a space.
22+
(
23+
[
24+
(_)
25+
(nl) @do_nothing
26+
]
27+
.
28+
[
29+
(minor_comment)
30+
(zeekygen_head_comment)
31+
(zeekygen_prev_comment)
32+
(zeekygen_next_comment)
33+
] @prepend_space
34+
)
35+
36+
; If we have multiple comments documenting an item with `##<` align them all.
37+
(zeekygen_prev_comment) @multi_line_indent_all
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/lib.rs
3+
expression: "format(\"##! foo\\n;1;\")"
4+
---
5+
Ok(
6+
"##! foo\n;1;\n",
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/lib.rs
3+
expression: "format(\"## foo\\n1;\")"
4+
---
5+
Ok(
6+
"## foo\n1;\n",
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/lib.rs
3+
expression: "format(\"##< foo\\n1;\")"
4+
---
5+
Ok(
6+
"##< foo\n1;\n",
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/lib.rs
3+
expression: "format(\"1;# foo\\n;1;\")"
4+
---
5+
Ok(
6+
"1; # foo\n;1;\n",
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/lib.rs
3+
expression: "format(\"1;##! foo\\n;1;\")"
4+
---
5+
Ok(
6+
"1; ##! foo\n;1;\n",
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/lib.rs
3+
expression: "format(\"1;## foo\\n1;\")"
4+
---
5+
Ok(
6+
"1; ## foo\n1;\n",
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/lib.rs
3+
expression: "format(\"1;##< foo\")"
4+
---
5+
Ok(
6+
"1; ##< foo\n",
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: src/lib.rs
3+
expression: "format(\"1;##< foo\\n##< bar\")"
4+
---
5+
Ok(
6+
"1; ##< foo\n##< bar\n",
7+
)

0 commit comments

Comments
 (0)