talonhub / community

Voice command set for Talon, community-supported.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.talon file auto formatter

rntz opened this issue · comments

Now that we have pre-commit hooks set up, it would be nice if we had a talon file auto formatter. The minimal feature set here is consistent indentation. I'd vote for 2 or 4 spaces.

I know @AndreasArvidsson has one of these, albeit in VScode rather than standalone. His also auto-aligns the right-hand-sides of all single-line commands in the whole file, which is nice for scannability but could result in very large diffs if that alignment needs to change. We could either disable this feature or try to tame it, eg, always align to column 36 and break lines whose LHS is too long for that.

I worry a bit about parsing .talon files correctly - it's a pretty simple format, but if we get it wrong the autoformatter will break stuff.

See also: #900 #666

It would also be useful to warn about problems in spoken forms, eg. literal numbers in spoken forms (#901).

@rntz My talon script formatter is now updated with a fixed column width. Those git diffs was really starting to bug me so it was about time.

@rntz It should be rather easy to write a python script that uses my talon parser (https://github.com/wenkokke/tree-sitter-talon) to do some basic linting such as non-alphabetic numbers and spoken forms when not specifying a different engine.

It should also be pretty simple to write a pretty printer from those syntax trees.

@rntz I've made good on my threats and wrote a little Talon file formatter: wenkokke/talonfmt

pip install talonfmt

It should be fairly feature-rich:

Usage: talonfmt [OPTIONS] [INPUT]

Options:
  --indent-size INTEGER           [default: 4]
  --align-match-context / --no-align-match-context
                                  [default: no-align-match-context]
  --align-match-context-at INTEGER
  --align-short-commands / --no-align-short-commands
                                  [default: no-align-short-commands]
  --align-short-commands-at INTEGER
  --in-place / --stdout           [default: stdout]
  --max-line-width INTEGER        [default: 80]
  --help                          Show this message and exit.

However, I could use some help setting it up with pre-commit.

It would also be useful to warn about problems in spoken forms, eg. literal numbers in spoken forms (#901).

Unfortunately, this isn't necessarily a problem in files which set the speech-engine option, so it's not trivial to check, but you could easily add this as a lint on the formatter, since it parses the full file into an AST.

@phillco Would you be able to help me setup talonfmt with precommit, so we can use it on this repository?

On it!

Done in #947.