-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Allow defining alignment in indent queries #5355
Conversation
I guess my questions here are... can this be used to align struct members in C, escapes in multiline macros, and not indent ifdefs and such. Some complex C macros confuse even emacs which is annoying, for example: But otherwise alignment of not only the first item on the line but also alignment of the escape for multi-line macros is quite nice to have (githubs viewer fails to show reality, but this macro has its backslashes aligned given an appropriate tab spacing) https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/rtio/rtio.h#L410 |
Aligning struct members should work; for example with the following query:
Setting the indent for ifdefs to 0 is not yet possible. The issue is that (in this implementation) we can only align to other tree-sitter nodes. One could write queries that align Aligning escapes in multiline macros is also not possible and definitely the trickiest to implement: Currently the indentation system only handles leading whitespace in lines, so we would need to add a way to specify alignment inside a line (probably a new capture type). The issue is that at the moment the indentation queries are only run when inserting a new line. To allow for alignment, they would have to be run every time the syntax tree changes in a significant way. I've thought about doing something that in order to retroactively update incorrect indentation but there's some challenges to this. At the moment, it would probably be easier to implement the alignment of macro escapes in an external formatter. |
c93883a
to
7f8e826
Compare
class foo {
// Assume that this is indented with a single tab
void bar(int parm1,
int parm2);
} We want If we also want this to work when the previous line starts with an arbitrary sequence of tabs and spaces, we could change the type of |
Ah then I would go with a |
The new commits should now align correctly even for arbitrary sequences of tabs & spaces. This requires us to take |
except for one minor comment this looks pretty good now to me 👍 Btw. I am a bit hesitant about including copies of commands.rs and similar as testcases. Couldn't we just check that we indent the entire helix codebase correctly without keeping a copy of that file? RA has a similar testcases where they make sure that RA cad parse itself. |
Both approaches have advantages and disadvantages imo. Copying the file is of course a bit redundant because we store almost the same file twice. On the other hand, if we use files for testing without copying them, this means that anyone changing these files can get test failures if the indent queries are incorrect on the new version (even if their changes are perfectly reasonable). Since we'll probably never get 100% accuracy on the indent queries, this is likely to happen at some point. In fact, this has already happened since there are some test failures in the new version of |
Yeah that's definitely a concern too. Since we have git as a built requirement you could instead read the files from git: Simply read the stdout from Having a large test case like this that runs on a large evolving real-world codebase has the potential to nicely flush out edge cases. We can bump the pinned commit from time to time and if there are some code that truly can't be indented correctly we could probably add a way some known-bad lines |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really great, thanks for going the extra mile with the testing! Looks like you found quite a few edgecases in the rust queries
Thanks for the reviews 😄. The combination of I just saw #7817 and added a commit that fixes it, since it needed only one simple pattern. |
58ed4bf
to
d9aa8c9
Compare
Rebased it on top of #6768 to fix the conflicts in |
It looks like some of the yaml tests are still failing in CI |
For some reason, `cargo fmt` does not change the indentation in these places (maybe it isn't sure about what the correct formatting should be).
Add C++ indent test file.
d9aa8c9
to
268c4ee
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! LGTM
Add an
@align
indent capture that aligns everything inside some node to a given@anchor
. I added indent queries for aligning function parameters in C/C++ and python. In C, this makes the following possible (in python, it's essentially the same):This should (partially) resolve #3196. I tried using the
@align
capture to write indent queries for haskell but this is a lot more complicated than for other languages. It should be possible without adding anything else to the indentation system but there's still some work to do. It's probably best to do that in a separate PR.I also tweaked the rust indent queries a bit so that they now correctly guess the indentation for
helix-term/src/commands.rs
(which is nice since that's a large and fairly complicated file). It doesn't seem to require any alignment any more, probably duecommands.rs
having been changed since I last tested the indent queries on it. The changes to the queries are not too complicated but if it makes it easier to review, I can of course split them into a separate PR.