-
Notifications
You must be signed in to change notification settings - Fork 23
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
Syntax aware formatting #3
Comments
Maybe you could parse the code into an AST using |
Worth a try. Not sure how the performance will be impacted though |
It's designed for "performance-critical applications" - whatever that may mean:
|
Right. Thanks for the suggestion, I'll do some experimenting this week! |
Yeah, because of assumptions the parsing makes, it can format things in an unexpected way so you have to be fairly specific about what you highlight sometimes. I find myself needing to hit CtrlI afterward which then indents the results correctly. Adopting It would be hugely beneficial to be adding unit tests to ensure formatting continues to work as expected as the extension is updated over time, since there's so many edge cases and variations in code formatting. (I would be game for helping out with writing unit tests at some point.) |
One major benefit to using a formal AST is that you could format an entire file in one go. I often come across code I inherit or old code I wrote where parameters were all on single lines. It would be amazing to just CmdA and run the formatter. I did a bit of snooping and it looks like some linters/formatters are already capable of this: apple/swift-format:
SwiftFormat:
|
@orchetect ah, so both SwiftFormats can kind of replace Multiliner. Nice investigating! I guess if you want full file formatting it's better to go with one of the above. Multiliner was originally meant to be a quick shortcut to expand whatever line you're currently on. This makes it pretty fast actually (since it doesn't need to parse the whole file, it formats instantly even if your file is huge). I've thought about whole file formatting, but the SwiftFormats can do this already and are great at it. Maybe it would be better to keep Multiliner lightweight and focus more on small shortcuts. I'll still look into AST though |
I don't think it's a replacement necessarily, after testing them out. It's also not exactly the same as Multiliner, you have to tweak the rules to get them to force it and it only works under certain circumstances. Whereas Multiliner does it every time. So I'd still probably use both depending on circumstances. What I was thinking is it might be possible to learn from their source code how they're doing it. |
Currently when you select a chunk of code that contains parameters nested inside, formatting is weird:
This is because Multiliner splits parameters by commas
,
— this includes commas inside nested inits.Multiliner/Sources/MultilinerExtension/SourceEditorCommand.swift
Lines 120 to 123 in 150c4a9
Possibly a split using Regex?
The text was updated successfully, but these errors were encountered: