fixw
(require fixw) | package: fixw |
A Racket formatter that only fixes whitespaces and keep newlines.
It provides a command line tool and a library.
1 Examples
|
|
2 raco fixw
raco fixw reads text from stdin, and output formatted code to stdout.
raco fixw ‹files or dirs› ... format ‹files› or ‹dirs› recursively. For ‹files›, fixw will format it whatever its extension. For ‹dirs›, fixw will format all "*.rkt" files recursively.
It accepts these flags:
-t —
use time timing the whole formatting process and output.
3 API
If interactive? is #t, every empty line will be indented with appropriate amount of whitespace as if there are visible atom at that line. It is designed to be used when editing.
fixw also remove extra trailing empty lines to keep only one trailing empty line.
The builtin rules are always used.
procedure
(fixw/lines in rules [ start end #:interactive? interactive?]) → (listof string?) in : input-port? rules : (or/c (hash/c string? integer?) #f) start : exact-nonnegative-integer? = 0 end : exact-nonnegative-integer? = (length (port->lines in)) interactive? : boolean? = #f
fixw/lines don’t remove extra trailing empty lines.
The builtin rules are always used.
4 Features
You might want to know what fixw exactly do with your code:
running lexer on the code, remove whitespaces except newline or it’s in disabled part.
regenerate the code while add some whitespaces between two tokens except several exceptions, and indent for the tokens that following a #\newline.
remove extra trailing empty lines.
Any other behavior should be considered a bug.
5 Indent rules
fixw indenter has a basic assumption: user defined procedure is more common than macros. So it will perform procedure indent by default.
The procedure indent look like this:
(fn arg1 arg2 ...)
Macros as special cases. They are assumed to be like this:
(macro parg_1 parg_2 ... parg_n body ...)
The number of ‹parg› of a macro is specified by a rule.
For example, A rule ("func" 2) specifies a form whose first element is func, then 2 argument aligned, then body. So fixw will format func as this:
(func (function-name args ...) (types ...) body ...)
Except these two strategy, fixw also use some heuristics strategies. Here are the full details:
- If the head element of the form need indent, it would have 1 extra space.
( head) - If the form is considered a list literal whose head element is a string, boolean, number, character, keyword, "#&" or the opening parenthesis is not one of #\(, #\[, #\{, all elements in this list would have same indent.
(1 2 3 4) #[v1 v2 v3 v4] - If the opening parenthesis ends with #\[, the second element of this form would have same indent.
[a (expt 2 10)] - If the head element is a list, the second element of this form would have same indent.
([a 1] [b 2]) - If the head element is not a list, the second element would have 2 extra spaces indented.
If it hits a rule, it would follow the rule.
Otherwise, elements will follow the indent of the second element.
6 Config file
fixw support read user defined rules from config file ".lispwords" that compatible with scmindent’s config. Here are some examples, they are equivalent.
(lambda 1) (define 1) ;; or (1 define lambda) ;; or ((define lambda) 1)
For a file that need to be formatted, fixw will try to read the ".lispwords" file at the same directory. If not found, then its parent directory, ..., until the root directory.
The builtin rules are always used. User defined rules can override them.
7 Enable/Disable in code
Use (fixw off) in comment to disable fixw temporarily. And (fixw on) to enable it.
For example,
;; (fixw off) ;; your code ;; (fixw on)