fixw
| (require fixw) | package: fixw |
A Racket formatter that only fixes whitespace and keeps newlines.
It provides a command line tool and a library.
1 Examples
|
|
2 raco fixw
raco fixw reads text from stdin and outputs formatted code to stdout.
raco fixw ‹files or dirs› ... formats ‹files› or ‹dirs› recursively. For ‹files›, fixw formats them regardless of extension. For ‹dirs›, fixw formats all "*.rkt" files recursively.
It accepts the following flags:
-t —
Use time to time the entire formatting process and output the result. -n or --newline —
Enforce a trailing empty line (two newlines) at the end of the file.
3 API
If interactive? is #t, every empty line will be indented with the appropriate amount of whitespace as if there were a visible atom at that line. This is designed for use during editing.
fixw preserves existing trailing empty lines.
The built-in rules are always used.
procedure
(fixw/trailing-newline in rules [ #:interactive? interactive?]) → string? in : input-port? rules : (or/c (hash/c string? integer?) #f) interactive? : boolean? = #f
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 does not remove extra trailing empty lines.
The built-in rules are always used.
4 Features
You might want to know what fixw exactly does with your code:
Runs a lexer on the code, removing whitespace (except newlines) unless in (fixw off) disabled region.
Regenerates the code, adding whitespace between tokens (with exceptions) and indenting tokens that follow a #\newline.
Preserves trailing empty lines (use fixw/trailing-newline to enforce a single trailing newline).
Any other behavior should be considered a bug.
5 Indent rules
The fixw indenter has a basic assumption: user-defined procedures are more common than macros. Therefore, it performs procedure indentation by default.
Procedure indentation looks like this:
(fn arg1 arg2 ...)
Macros are treated as special cases. They are assumed to be like this:
(macro parg_1 parg_2 ... parg_n body ...)
The number of ‹parg› arguments for a macro is specified by a rule.
For example, a rule ("func" 2) specifies a form starting with func, followed by 2 aligned arguments, and then the body. So fixw formats func like this:
(func (function-name args ...) (types ...) body ...)
Besides these two strategies, fixw also uses some heuristics. Here are the full details:
- If the head element of the form needs indentation, it receives 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 have the same indentation.
(1 2 3 4) #[v1 v2 v3 v4] - If the opening parenthesis ends with #\[, the second element of the form shares the same indentation.
[a (expt 2 10)] - If the head element is a list, the second element shares the same indentation.
([a 1] [b 2]) - If the head element is not a list, the second element is indented by 2 extra spaces.
If a rule applies, it is followed.
Otherwise, elements follow the indentation of the second element.
6 Config file
fixw supports reading user-defined rules from a ".lispwords" configuration file, compatible with scmindent’s config. Here are some examples; they are equivalent:
(lambda 1) (define 1) ;; or (1 define lambda) ;; or ((define lambda) 1)
When formatting a file, fixw attempts to read the ".lispwords" file in the same directory. If not found, it checks the parent directory, continuing up to the root directory.
The built-in rules are always used. User-defined rules override them.
7 Enable/Disable in code
Use (fixw off) in a comment to temporarily disable fixw. Use (fixw on) to re-enable it.
For example,
;; (fixw off) ;; your code ;; (fixw on)