@@ -134,25 +134,34 @@ not worthwhile due to uniformity being desirable, but it is a useful goal).
134
134
135
135
### Architecture details
136
136
137
- We use the AST from libsyntax. We use libsyntax's visit module to walk the AST
138
- to find starting points for reformatting. Eventually, we should reformat everything
139
- and we shouldn't need the visit module. We keep track of the last formatted
140
- position in the code, and when we reformat the next piece of code we make sure
141
- to output the span for all the code in between (handled by missed_spans.rs).
137
+ We use the AST from [ syntex_syntax] , an export of rustc's libsyntax. We use
138
+ syntex_syntax's visit module to walk the AST to find starting points for
139
+ reformatting. Eventually, we should reformat everything and we shouldn't need
140
+ the visit module. We keep track of the last formatted position in the code, and
141
+ when we reformat the next piece of code we make sure to output the span for all
142
+ the code in between (handled by missed_spans.rs).
143
+
144
+ [ syntex_syntax ] : https://crates.io/crates/syntex_syntax
145
+
146
+ We read in formatting configuration from a ` rustfmt.toml ` file if there is one.
147
+ The options and their defaults are defined in ` config.rs ` . A ` Config ` object is
148
+ passed throughout the formatting code, and each formatting routine looks there
149
+ for its configuration.
142
150
143
151
Our visitor keeps track of the desired current indent due to blocks (
144
- ` block_indent ` ). Each ` visit_* ` method reformats code according to this indent
145
- and ` IDEAL_WIDTH ` and ` MAX_WIDTH ` (which should one day be supplied from a
146
- config file). Most reformatting done in the ` visit_* ` methods is a bit hackey
147
- and is meant to be temporary until it can be done properly.
152
+ ` block_indent ` ). Each ` visit_* ` method reformats code according to this indent,
153
+ ` config.ideal_width ` and ` config.max_width ` . Most reformatting done in the
154
+ ` visit_* ` methods is a bit hackey and is meant to be temporary until it can be
155
+ done properly.
148
156
149
157
There are a bunch of methods called ` rewrite_* ` . There do the bulk of the
150
158
reformatting. These take the AST node to be reformatted (this may not literally
151
- be an AST node from libsyntax, there might be multiple parameters describing a
152
- logical node), the current indent, and the current width budget. They return a
153
- ` String ` (or sometimes an ` Option<String> ` ) which formats the code in the box
154
- given by the indent and width budget. If the method fails, it returns ` None ` and
155
- the calling method then has to fallback in some way to give the callee more space.
159
+ be an AST node from syntex_syntax: there might be multiple parameters
160
+ describing a logical node), the current indent, and the current width budget.
161
+ They return a ` String ` (or sometimes an ` Option<String> ` ) which formats the
162
+ code in the box given by the indent and width budget. If the method fails, it
163
+ returns ` None ` and the calling method then has to fallback in some way to give
164
+ the callee more space.
156
165
157
166
So, in summary to format a node, we calculate the width budget and then walk down
158
167
the tree from the node. At a leaf, we generate an actual string and then unwind,
0 commit comments