@@ -39,6 +39,9 @@ Topiary has been created with the following goals in mind:
39
39
* Use [ Tree-sitter] for parsing, to avoid writing yet another grammar
40
40
for a formatter.
41
41
42
+ * Expect idempotency. That is, formatting of already-formatted code
43
+ doesn't change anything.
44
+
42
45
* For bundled formatting styles to meet the following constraints:
43
46
44
47
* Be compatible with attested formatting styles used for that language
@@ -53,11 +56,8 @@ Topiary has been created with the following goals in mind:
53
56
won't force you to make later, cosmetic changes when you modify your
54
57
code.
55
58
56
- * Be idempotent. That is, formatting of already-formatted code doesn't
57
- change anything.
58
-
59
- * Code and formatting styles must be well-tested and robust, so that the
60
- formatter can be used in large projects.
59
+ * Be well-tested and robust, so that the formatter can be trusted in
60
+ large projects.
61
61
62
62
* For end users -- i.e., not formatting style authors -- the formatter
63
63
should:
@@ -165,7 +165,7 @@ Options:
165
165
166
166
* ` -v ` , ` --visualise ` \
167
167
Visualise the syntax tree, rather than format [ possible values: ` json `
168
- (default)] .
168
+ (default), ` dot ` ] .
169
169
170
170
* ` -s ` , ` --skip-idempotence ` \
171
171
Do not check that formatting twice gives the same output.
@@ -836,22 +836,26 @@ suggested way to work:
836
836
837
837
4 . Run ` RUST_LOG=debug cargo test ` .
838
838
839
- 5 . Provided it works, it should output a lot of log messages. Copy that
839
+ Provided it works, it should output a lot of log messages. Copy that
840
840
output to a text editor. You are particularly interested in the CST
841
841
output that starts with a line like this: `CST node: {Node
842
842
compilation_unit (0, 0) - (5942, 0)} - Named: true`.
843
843
844
- 6 . The test run will output all the differences between the actual
844
+ :bulb : As an alternative to using the debugging output, the
845
+ ` --visualise ` command line option exists to output the Tree-sitter
846
+ syntax tree in a variety of formats.
847
+
848
+ 5 . The test run will output all the differences between the actual
845
849
output and the expected output, e.g. missing spaces between tokens.
846
850
Pick a difference you would like to fix, and find the line number and
847
851
column in the input file.
848
852
849
- 7 . Keep in mind that the CST output uses 0-based line and column
853
+ : bulb : Keep in mind that the CST output uses 0-based line and column
850
854
numbers, so if your editor reports line 40, column 37, you probably
851
855
want line 39, column 36.
852
856
853
- 8 . In the CST debug output, find the nodes in this region, such as the
854
- following:
857
+ 6 . In the CST debug or visualisation output, find the nodes in this
858
+ region, such as the following:
855
859
856
860
```
857
861
[DEBUG atom_collection] CST node: {Node constructed_type (39, 15) - (39, 42)} - Named: true
@@ -861,35 +865,47 @@ suggested way to work:
861
865
[DEBUG atom_collection] CST node: {Node type_constructor (39, 36) - (39, 42)} - Named: true
862
866
```
863
867
864
- 9 . This may indicate that you would like spaces after all
868
+ 7 . This may indicate that you would like spaces after all
865
869
` type_constructor_path ` nodes:
866
870
867
871
``` scheme
868
872
(type_constructor_path) @append_space
869
873
```
870
874
871
- 10 . Or, more likely, you just want spaces between pairs of them:
875
+ Or, more likely, you just want spaces between pairs of them:
876
+
877
+ ``` scheme
878
+ (
879
+ (type_constructor_path) @append_space
880
+ .
881
+ (type_constructor_path)
882
+ )
883
+ ```
884
+
885
+ Or maybe you want spaces between all children of ` constructed_type ` :
886
+
887
+ ``` scheme
888
+ (constructed_type
889
+ (_) @append_space
890
+ .
891
+ (_)
892
+ )
893
+ ```
872
894
873
- ``` scheme
874
- (
875
- (type_constructor_path) @append_space
876
- .
877
- (type_constructor_path)
878
- )
879
- ```
895
+ 8 . Run ` cargo test ` again, to see if the output is better now, and then
896
+ return to step 5.
880
897
881
- 11. Or maybe you want spaces between all children of `constructed_type`:
898
+ ### Syntax Tree Visualisation
882
899
883
- ```scheme
884
- (constructed_type
885
- (_) @append_space
886
- .
887
- (_)
888
- )
889
- ```
900
+ To support the development of formatting queries, the Tree-sitter syntax
901
+ tree for a given input can be produced using the ` --visualise ` CLI
902
+ option.
890
903
891
- 12. Run `cargo test` again, to see if the output is better now, and then
892
- return to step 6.
904
+ This currently supports JSON output, covering the same information as
905
+ the debugging output, as well as GraphViz DOT output, which is useful
906
+ for generating syntax diagrams. (Note that the text position
907
+ serialisation in the visualisation output is 1-based, unlike the
908
+ debugging output's 0-based position.)
893
909
894
910
### Terminal-Based Playground
895
911
@@ -921,6 +937,8 @@ of choice open in another.
921
937
language.
922
938
* [ Neovim Treesitter Playground] [ nvim-treesitter ] : A Tree-sitter
923
939
playground plugin for Neovim.
940
+ * [ Difftastic] : A tool that utilises Tree-sitter to perform syntactic
941
+ diffing.
924
942
925
943
### Meta and Multi-Language Formatters
926
944
@@ -948,6 +966,7 @@ of choice open in another.
948
966
[ bash ] : https://www.gnu.org/software/bash
949
967
[ ci-badge ] : https://github.com/tweag/topiary/actions/workflows/ci.yml/badge.svg
950
968
[ contributing ] : CONTRIBUTING.md
969
+ [ difftastic ] : https://difftastic.wilfred.me.uk
951
970
[ format-all ] : https://melpa.org/#/format-all
952
971
[ gofmt-slides ] : https://go.dev/talks/2015/gofmt-en.slide#1
953
972
[ gofmt ] : https://pkg.go.dev/cmd/gofmt
0 commit comments