Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatter for .talon files to pre-commit #947

Merged
merged 2 commits into from
Sep 14, 2022
Merged

Conversation

wenkokke
Copy link
Collaborator

@wenkokke wenkokke commented Aug 1, 2022

This PR adds talonfmt, as well a tabs-to-spaces conversion, to pre-commit.

The changes in the .talon should absolutely be reviewed.

Note that talonfmt supports alignment of context headers and one-line commands, but I've not enabled those options.

@rntz
Copy link
Collaborator

rntz commented Aug 1, 2022

This appears to remove all empty lines from .talon files, which I'm strongly against - it should be possible to separate commands into groups by using empty lines.

It also appears to make all single-line commands into multi-line-with-indent, which I also dislike. I'd be okay with doing this for long lines (and/or collapsing commands into a single line when it would be short enough).

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 1, 2022

It also appears to make all single-line commands into multi-line-with-indent, which I also dislike. I'd be okay with doing this for long lines (and/or collapsing commands into a single line when it would be short enough).

That’s just the default. I've set a max line width of 80 so talonfmt doesn't just pick whatever gives the shortest line widths.

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 1, 2022

This appears to remove all empty lines from .talon files, which I'm strongly against - it should be possible to separate commands into groups by using empty lines.

What do you feel should happen with blank lines?

@pokey
Copy link
Collaborator

pokey commented Aug 2, 2022

What do you feel should happen with blank lines?

I would argue that any sequence of blank lines should be collapsed into a single blank line. And any whitespace on the blank line should be removed

@pokey
Copy link
Collaborator

pokey commented Aug 2, 2022

Super minor, but it would be nice if comments had a space after the #. Prob not worth fixing if that's going to take much time tho

Copy link
Collaborator

@rntz rntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if empty lines were simply left alone (with whitespace removed), but combining adjacent empty lines would also be ok.

I'm against changing comments by adding a leading space, though. Comments are for expressing human intent so I think that autoformatters should leave them alone.

The PR looks much better now that it tries to put things on a single line where possible. It is removing the nice horizontal alignment some .talon files have, though. I remember there was some discussion on the corresponding issue of the right way to do this to avoid large diffs - how does talonfmt handle alignment?

apps/i3wm/i3wm.talon Show resolved Hide resolved
apps/generic_debugger.talon Outdated Show resolved Hide resolved
@pokey
Copy link
Collaborator

pokey commented Aug 2, 2022

I'd prefer if empty lines were simply left alone (with whitespace removed), but combining adjacent empty lines would also be ok.

I think it's pretty common for autoformatters to collapse adjacent empty lines, unless the spec calls for them (eg before a function in Python)

One place I'd be tempted to remove empty lines is before / after the - context separator line. I think the autoformatter should have an opinion there.

I would also be tempted to remove empty lines if they appear after the rule but before the body of a command

I'm against changing comments by adding a leading space, though. Comments are for expressing human intent so I think that autoformatters should leave them alone.

I guess I don't feel too strongly here. I'm pretty sure both other autoformatters that we use do this, though. And I'm not sure I see what intent one would be expressing by not including a space between the # and the start of the comment.

But again, I don't feel strongly, and it's easier to leave them untouched

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 2, 2022

What do you feel should happen with blank lines?

I would argue that any sequence of blank lines should be collapsed into a single blank line. And any whitespace on the blank line should be removed

@pokey @rntz What about "1 blank line is left alone, 2 or more blank lines become 2 blank lines" at the top level, but all blank lines in the context header, after the "-", and at the start of a command body are removed? That way you can group commands but there's still some consistency.

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 2, 2022

I'm against changing comments by adding a leading space, though. Comments are for expressing human intent so I think that autoformatters should leave them alone.

@rntz I think that while all code formatting expresses human intent, the purpose of a formatter is to enforce some measure of consistency. However, I've added a "--no-format-comments" flag, so we can at least not hardcode this behaviour.

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 2, 2022

I guess I don't feel too strongly here. I'm pretty sure both other autoformatters that we use do this, though. And I'm not sure I see what intent one would be expressing by not including a space between the # and the start of the comment.

But again, I don't feel strongly, and it's easier to leave them untouched

I think the best option would be to use, e.g., comment_formatter on blocks of comments. Outsource it, but ideally find something that respects indented blocks.

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 2, 2022

Changes:

  • Set --no-format-comments to preserve comments as-is.
  • Added logic to keep either 1 or 2 blank lines intact, and condense 3 or more blank lines down to 2.

@phillco
Copy link
Collaborator

phillco commented Aug 2, 2022

This looks great. I find the horizontal alignment inside of existing Talon files to be a bit distracting, so I'm fan of the way they currently look in the PR.

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 2, 2022

This looks great. I find the horizontal alignment inside of existing Talon files to be a bit distracting, so I'm fan of the way they currently look in the PR.

@phillco do you have any ideas how to resolve the issues with pre-commit.ci? It currently doesn't have C++ build tools, which breaks tree_sitter.

@phillco
Copy link
Collaborator

phillco commented Aug 2, 2022

I'm not sure. If they don't support it out of the box I'm guessing we might have to move to Github Actions.

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 2, 2022

I'm not sure. If they don't support it out of the box I'm guessing we might have to move to Github Actions.

Works locally, it's just not working with the CI.

@pokey
Copy link
Collaborator

pokey commented Aug 3, 2022

Maybe you could have a branch of talonfmt where you check in the compiled code so that it doesn't need to compile?

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 3, 2022

Maybe you could have a branch of talonfmt where you check in the compiled code so that it doesn't need to compile?

Was considering that, yes. The downside is that it's part of a different package, tree_sitter_talon. Guess I could build OS-specific versions of that package which include the compiles library? Anyone here know how to do that with setuptools?

@pokey
Copy link
Collaborator

pokey commented Aug 3, 2022

Hmm. Looks like OS-specific native code builds for Python are still pretty painful today

Could maybe use wasm with wasmer?

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 3, 2022

Hmm. Looks like OS-specific native code builds for Python are still pretty painful today

Could maybe use wasm with wasmer?

Unfortunately, the tree_sitter package relies hard on the C bindings.

@wenkokke wenkokke force-pushed the talonfmt branch 3 times, most recently from 873e7d8 to 6a49b97 Compare August 3, 2022 17:41
@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 3, 2022

Set pre-commit to skip talonfmt on pre-commit.ci. Added pre-commit to GitHub Actions.

@auscompgeek
Copy link
Collaborator

auscompgeek commented Aug 4, 2022

It currently doesn't have C++ build tools, which breaks tree_sitter.

I took a look at a pre-commit.ci run, the build tools run fine, but the package tries to write to the installation directory at runtime, which is a big no-no since the virtual environment is immutable once installed.

If this will require a C++ compiler at runtime, this will be very annoying for people on Windows, as having Visual Studio installed is a lot more rare than having a C++ toolchain on UNIX systems.

apps/web/github.talon Outdated Show resolved Hide resolved
misc/extensions.talon Outdated Show resolved Hide resolved
@wenkokke wenkokke requested a review from pokey August 13, 2022 15:54
@wenkokke
Copy link
Collaborator Author

Added blank lines after the match context, as requested.

@rntz
Copy link
Collaborator

rntz commented Aug 18, 2022 via email

@wenkokke
Copy link
Collaborator Author

wenkokke commented Aug 23, 2022

Latest version:

  • supports key-binding declarations;
  • checks that formatting is idempotent and preserves the AST at runtime (the latter was suggested by @rntz);
  • resolves the ambiguity issues around match contexts in the parser, by using arbitrary lookahead in the lexer (suggested by @pokey and @rntz); and
  • simplifies the parsing of matches, no longer attempting to parse match contexts into logical formulas (suggested by @pokey).

I've also switched all the defaults over to the settings used here.

- id: remove-tabs
types: [file]
files: \.talon$
args: ["--whitespaces-count=4"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, it does seem like most of our .talon files use 4 space indents. We should fix the .editorconfig to do the same.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #970 for this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be cool if remove-tabs respected .editorconfig. I believe prettier does

@wenkokke
Copy link
Collaborator Author

wenkokke commented Sep 4, 2022

Can we merge this? I don’t think there’s still any blocking issues.

@rntz
Copy link
Collaborator

rntz commented Sep 4, 2022

We should probably decide at this point whether we're standardizing on 2 or 4 spaces for indentation in talon files. Per #970 we mostly use 4 spaces but the editorconfig file is set up to use 2 spaces. I prefer 2 spaces but I reckon I'm probably outvoted here :P. Still:

@rntz
Copy link
Collaborator

rntz commented Sep 4, 2022

upvote for 2 spaces

@rntz
Copy link
Collaborator

rntz commented Sep 4, 2022

upvote for 4 spaces

@wenkokke
Copy link
Collaborator Author

Okay, it's been open to a vote for 9 days. Can we merge this now?

@phillco
Copy link
Collaborator

phillco commented Sep 13, 2022

I'd naturally wait for @rntz or @pokey, but this looks good to me. Might be worth to do in the grooming session this Saturday.

@pokey pokey merged commit 446ec76 into talonhub:main Sep 14, 2022
@wenkokke
Copy link
Collaborator Author

🎉🎉🎉

@wenkokke wenkokke deleted the talonfmt branch September 14, 2022 21:41
@phillco
Copy link
Collaborator

phillco commented Sep 15, 2022

Awesome! Thanks a bunch for this change, Wen.

Now to merge up my fork :D

rntz pushed a commit that referenced this pull request Sep 16, 2022
This omits the changes from
#947
(446ec76) from blame for people who
have the configuration set, so the reformatting changes don't affect
blame.
nickvisut pushed a commit to nickvisut/knausj_talon that referenced this pull request Nov 1, 2022
This PR adds `talonfmt`, as well a tabs-to-spaces conversion, to
pre-commit.

The changes in the .talon should _absolutely_ be reviewed.

Note that `talonfmt` supports alignment of context headers and one-line
commands, but I've not enabled those options.
nickvisut pushed a commit to nickvisut/knausj_talon that referenced this pull request Nov 1, 2022
This omits the changes from
talonhub#947
(446ec76) from blame for people who
have the configuration set, so the reformatting changes don't affect
blame.
code-n-cool added a commit to code-n-cool/social-community that referenced this pull request May 10, 2024
This omits the changes from
talonhub/community#947
(446ec764c9caa98973eacd7f792b6a087a1b635f) from blame for people who
have the configuration set, so the reformatting changes don't affect
blame.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants