@@ -109,6 +109,25 @@ private FormattingOptions fo() = formattingOptions();
109109data FileSystemChange (bool needsConfirmation = false );
110110
111111@synopsis {Generates a file formatter that immediately applies the new style to the input.}
112+ @description {
113+ The formatter function this generates will produce formatted string directly and replace the original file with it.
114+
115+ * `grammar` should be the grammar for the entire language (a start non-terminal preferably)
116+ * `style` is typically an extended version of ((toBox))
117+ * `opts` provides the ((FormattingOptions))
118+ }
119+ @benefits {
120+ * can be used to seemlessly integrate a box-based formatter in an IDE for "format file"-purposes.
121+ * will do nothing if the formatter breaks syntax rules or introduces ambiguity.
122+ * will never lose any comments.
123+ * is parameterized by a ((Style)) function to support different styles of formatting for the same language
124+ * can be used to normalize case-insensitive literals in one go
125+ }
126+ @pitfalls {
127+ * only applicable to entire files. For selection-based formatting see ((subTreeEdits)) and ((subStringEdits)).
128+ * in rare case the recovered comments are positioned in awkward places.
129+ * can be slow for very very large input
130+ }
112131void (loc ) fileFormatter (type [&G <: Tree ] grammar , Style style , FormattingOptions opts =fo ()) {
113132 list [TextEdit ](loc ) toEdits = fileEdits (grammar , style , opts =opts );
114133
@@ -118,6 +137,25 @@ void(loc) fileFormatter(type[&G <: Tree] grammar, Style style, FormattingOptions
118137}
119138
120139@synopsis {Generates a string formatter}
140+ @description {
141+ The formatter function this generates will produce formatted string directly.
142+
143+ * `grammar` should be the grammar for the entire language (a start non-terminal preferably)
144+ * `style` is typically an extended version of ((toBox))
145+ * `opts` provides the ((FormattingOptions))
146+ }
147+ @benefits {
148+ * can be used to seemlessly integrate a box-based formatter in an IDE for "format selection"-purposes.
149+ * will do nothing if the formatter breaks syntax rules or introduces ambiguity.
150+ * will never lose any comments.
151+ * is parameterized by a ((Style)) function to support different styles of formatting for the same language
152+ * can be used to normalize case-insensitive literals in one go
153+ }
154+ @pitfalls {
155+ * only applicable to entire files. For selection-based formatting see ((subTreeEdits)) and ((subStringEdits)).
156+ * in rare case the recovered comments are positioned in awkward places.
157+ * can be slow for very very large input
158+ }
121159str (str ) stringFormatter (type [&G <: Tree ] grammar , Style style , FormattingOptions opts =fo ()) {
122160 list [TextEdit ](str ) toEdits = stringEdits (grammar , style , opts =opts );
123161
@@ -127,6 +165,34 @@ str(str) stringFormatter(type[&G <: Tree] grammar, Style style, FormattingOption
127165}
128166
129167@synopsis {Generates a substring formatter}
168+ @description {
169+ The formatter function this generates will produce a formatted substring.
170+
171+ * `grammar` should be the grammar for the entire language (a start non-terminal preferably)
172+ * `style` is typically an extended version of ((toBox))
173+ * `opts` provides the ((FormattingOptions))
174+
175+ The subStringFormatter does not have to be called on a top-level parse tree (with a start non-terminal), but
176+ rather works on any sub-sentence:
177+ 1. first the given sub-sentence will be parsed and formatted with its left-most margin at column 0.
178+ 2. then the original indentation of the old sub-string will be computed.
179+ 3. then the formatted output will be indented (expect the first line).
180+ 4. then the whitespace differences between the old and the new string will be computed. Also lost comments will be re-inserted.
181+ 5. finally the resulting string is produced.
182+ }
183+ @benefits {
184+ * can be used to seemlessly integrate a box-based formatter in an IDE for "format selection"-purposes.
185+ * learns the appropriate indentation level for the sub-tree from the input.
186+ * learns the non-terminal to use for parsing from the input sub-tree.
187+ * will do nothing if the formatter breaks syntax rules or introduces ambiguity.
188+ * will never lose any comments.
189+ * is parameterized by a ((Style)) function to support different styles of formatting for the same language
190+ * can be used to normalize case-insensitive literals in one go
191+ }
192+ @pitfalls {
193+ * in rare case the recovered comments are positioned in awkward places.
194+ * can be slow for very very large input
195+ }
130196str (type [Tree ], str ) subStringFormatter (type [&G <: Tree ] grammar , Style style , FormattingOptions opts =fo ()) {
131197 list [TextEdit ](type [Tree ], str ) toEdits = subStringEdits (grammar , style , opts =opts );
132198
@@ -136,6 +202,26 @@ str(type[Tree], str) subStringFormatter(type[&G <: Tree] grammar, Style style, F
136202}
137203
138204@synopsis {Generates a file formatter that produces ((TextEdit))s for downstream processing.}
205+ @description {
206+ The formatter function this generates will produce a list of ((TextEdit))s. The edits change the original
207+ input to the formatted output.
208+
209+ * `grammar` should be the grammar for the entire language (a start non-terminal preferably)
210+ * `style` is typically an extended version of ((toBox))
211+ * `opts` provides the ((FormattingOptions))
212+ }
213+ @benefits {
214+ * can be used to seemlessly integrate a box-based formatter in an IDE for "format selection"-purposes.
215+ * will do nothing if the formatter breaks syntax rules or introduces ambiguity.
216+ * will never lose any comments.
217+ * is parameterized by a ((Style)) function to support different styles of formatting for the same language
218+ * can be used to normalize case-insensitive literals in one go
219+ }
220+ @pitfalls {
221+ * only applicable to entire files. For selection-based formatting see ((subTreeEdits)) and ((subStringEdits)).
222+ * in rare case the recovered comments are positioned in awkward places.
223+ * can be slow for very very large input
224+ }
139225list [TextEdit ](loc ) fileEdits (type [&G <: Tree ] grammar , Style style , FormattingOptions opts =fo ()) {
140226 &G (value , loc ) p = parser (grammar );
141227
@@ -158,6 +244,26 @@ list[TextEdit](loc) fileEdits(type[&G <: Tree] grammar, Style style, FormattingO
158244}
159245
160246@synopsis {Generates a file formatter that produces ((TextEdit))s from a ((Tree)) for downstream processing.}
247+ @description {
248+ The formatter function this generates will produce a list of ((TextEdit))s. The edits change the original
249+ input to the formatted output.
250+
251+ * `grammar` should be the grammar for the entire language (a start non-terminal preferably)
252+ * `style` is typically an extended version of ((toBox))
253+ * `opts` provides the ((FormattingOptions))
254+ }
255+ @benefits {
256+ * can be used to seemlessly integrate a box-based formatter in an IDE for "format string"-purposes.
257+ * will do nothing if the formatter breaks syntax rules or introduces ambiguity.
258+ * will never lose any comments.
259+ * is parameterized by a ((Style)) function to support different styles of formatting for the same language
260+ * can be used to normalize case-insensitive literals in one go
261+ }
262+ @pitfalls {
263+ * only applicable to entire files. For selection-based formatting see ((subTreeEdits)) and ((subStringEdits)).
264+ * in rare case the recovered comments are positioned in awkward places.
265+ * can be slow for very very large input
266+ }
161267list [TextEdit ](&G <: Tree ) treeEdits (type [&G <: Tree ] grammar , Style style , FormattingOptions opts =fo ()) {
162268 &G (value , loc ) p = parser (grammar );
163269
@@ -178,13 +284,44 @@ list[TextEdit](&G <: Tree) treeEdits(type[&G <: Tree] grammar, Style style, Form
178284 };
179285}
180286
181- @synopsis {Generates a file formatter that produces ((TextEdit))s from sub((Trees)s for downstream processing.}
287+ @synopsis {Generates a file formatter that produces ((TextEdit))s from sub((Trees)s}
288+ @description {
289+ The formatter function this generates will produce a list of ((TextEdit))s. The edits change the original
290+ input to the formatted output.
291+
292+ * `grammar` should be the grammar for the entire language (a start non-terminal preferably)
293+ * `style` is typically an extended version of ((toBox))
294+ * `opts` provides the ((FormattingOptions))
295+
296+ The subTreeEdits formatter does not have to be called on a top-level parse tree (with a start non-terminal), but
297+ rather works on any sub-tree:
298+ 1. first the given sub-tree will be formatted with its left-most margin at column 0.
299+ 2. then the original indentation of the old sub-tree will be computed.
300+ 3. then the formatted output will be indented (expect the first line).
301+ 4. then the whitespace differences between the old and the new tree will be computed. Also lost comments will be re-inserted.
302+ 5. finally the resulting edits are produced.
303+ }
304+ @benefits {
305+ * can be used to seemlessly integrate a box-based formatter in an IDE for "format selection"-purposes.
306+ * learns the appropriate indentation level for the sub-tree from the input.
307+ * learns the non-terminal to use for parsing from the input sub-tree.
308+ * will do nothing if the formatter breaks syntax rules or introduces ambiguity.
309+ * will never lose any comments.
310+ * is parameterized by a ((Style)) function to support different styles of formatting for the same language
311+ * can be used to normalize case-insensitive literals in one go
312+ }
313+ @pitfalls {
314+ * in rare case the recovered comments are positioned in awkward places.
315+ * can be slow for very very large input
316+ }
182317list [TextEdit ](Tree ) subTreeEdits (type [&G <: Tree ] grammar , Style style , FormattingOptions opts =fo ()) {
183318 Tree (type [Tree ], value , loc ) p = parsers (grammar );
184319
185320 return list [TextEdit ] (Tree tree ) {
186321 try {
187- return layoutDiff (tree , p (type (delabel (tree .prod .def ), ()), format (style (tree ), opts =opts [ci =asIs ()]), tree @\loc ), ci =opts .caseInsensitivity );
322+ str formatted = format (style (tree ));
323+ str indented = subIndent (tree @\loc , "<formatted > " , input );
324+ return layoutDiff (tree , p (type (delabel (tree .prod .def ), ()), indented , tree @\loc ), ci =opts .caseInsensitivity );
188325 }
189326 catch ParseError (loc place ): {
190327 writeFile (|tmp :///<tree@\loc.top.file>|, format(style(tree), opts=opts[ci=asIs()]));
@@ -204,6 +341,26 @@ private Symbol delabel(conditional(Symbol s, _)) = delabel(s);
204341private default Symbol delabel (Symbol s ) = s ;
205342
206343@synopsis {Generates a string formatter that produces ((TextEdit))s for downstream processing.}
344+ @description {
345+ The formatter function this generates will produce a list of ((TextEdit))s. The edits change the original
346+ input to the formatted output.
347+
348+ * `grammar` should be the grammar for the entire language (a start non-terminal preferably)
349+ * `style` is typically an extended version of ((toBox))
350+ * `opts` provides the ((FormattingOptions))
351+ }
352+ @benefits {
353+ * can be used to seemlessly integrate a box-based formatter in an IDE for "format-string"-purposes.
354+ * will do nothing if the formatter breaks syntax rules or introduces ambiguity.
355+ * will never lose any comments.
356+ * is parameterized by a ((Style)) function to support different styles of formatting for the same language
357+ * can be used to normalize case-insensitive literals in one go
358+ }
359+ @pitfalls {
360+ * in rare case the recovered comments are positioned in awkward places.
361+ * can be slow for very very large input
362+ }
363+
207364list [TextEdit ](str ) stringEdits (type [&G <: Tree ] grammar , Style style , FormattingOptions opts =fo ()) {
208365 &G (str , loc ) p = parser (grammar );
209366
@@ -236,10 +393,33 @@ list[TextEdit](str) stringEdits(type[&G <: Tree] grammar, Style style, Formattin
236393
237394@synopsis {Generates a string formatter that produces ((TextEdit))s, from substrings with a given nonterminal, for downstream processing.}
238395@description {
239- * `grammar` should be the entire grammar for the language
240- * `style` should be the entire `toBox` function for this formatter
241- * the function returns a binary function that asks for the right non-terminal of the language to use to parse the second
242- parameter (a substring). For example: `#Statement` and `if (true) false;`.
396+ The formatter function this generates will produce a list of ((TextEdit))s. The edits change the original
397+ input to the formatted output.
398+
399+ * `grammar` should be the grammar for the entire language (a start non-terminal preferably)
400+ * `style` is typically an extended version of ((toBox))
401+ * `opts` provides the ((FormattingOptions))
402+
403+ The subTreeEdits formatter does not have to be called on a top-level parse tree (with a start non-terminal), but
404+ rather works on any sub-tree:
405+ 1. first the given sub-tree will be formatted with its left-most margin at column 0.
406+ 2. then the original indentation of the old sub-tree will be computed.
407+ 3. then the formatted output will be indented (expect the first line).
408+ 4. then the whitespace differences between the old and the new tree will be computed. Also lost comments will be re-inserted.
409+ 5. finally the resulting edits are produced.
410+ }
411+ @benefits {
412+ * can be used to seemlessly integrate a box-based formatter in an IDE for "format selection"-purposes.
413+ * learns the appropriate indentation level for the sub-tree from the input.
414+ * learns the non-terminal to use for parsing from the input sub-tree.
415+ * will do nothing if the formatter breaks syntax rules or introduces ambiguity.
416+ * will never lose any comments.
417+ * is parameterized by a ((Style)) function to support different styles of formatting for the same language
418+ * can be used to normalize case-insensitive literals in one go
419+ }
420+ @pitfalls {
421+ * in rare case the recovered comments are positioned in awkward places.
422+ * can be slow for very very large input
243423}
244424list [TextEdit ](type [Tree ], str ) subStringEdits (type [&G <: Tree ] grammar , Style style , FormattingOptions opts =fo ()) {
245425 Tree (type [Tree ], str , loc ) p = parsers (grammar );
0 commit comments