nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 640 forks source link

SwiftFormat without Unfolding Brackets #1452

Open shindgewongxj opened 1 year ago

nicklockwood commented 1 year ago

Can you clarify? I'm not sure what you mean.

shindgewongxj commented 1 year ago

Can you clarify? I'm not sure what you mean.

SwiftFormat will automatically unfold folded brackets, so it makes suffer from relocating the line being edited.

nicklockwood commented 1 year ago

Do you mean the Xcode extension?

shindgewongxj commented 1 year ago

YES!

On May 17, 2023, at 20:25, Nick Lockwood @.***> wrote:

Do you mean the Xcode extension?

— Reply to this email directly, view it on GitHub https://github.com/nicklockwood/SwiftFormat/issues/1452#issuecomment-1551299104, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOHEVZ3EYFXOV2YYW5CGEM3XGS7U7ANCNFSM6AAAAAAYE4PDJ4. You are receiving this because you authored the thread.

Detnator-Neo commented 1 year ago

Hi @nicklockwood. I just came here to report the same thing. Steps to reproduce the issue:

  1. Editor > Code Folding > Fold Methods & Functions

  2. Editor > Code Folding > Fold Comment Blocks Result: All comments, methods, functions, are folded up (obviously).

  3. Editor > SwiftFormat > Format File Result: File is formatted, but all comments, methods, functions are now unfolded.

Instead, desired result is: File should be formatted (as SwiftFormat does already), while all existing folded and unfolded levels should be left as is, not changed (or restored if the issue here is it all has to be unfolded to format it).

The above example is just one example, where I've folded up everything, but of course sometimes I'm manually folding/unfolding sections of code, so it's not all folded, and it's not all unfolded. In all cases though, as far as I can tell, SwiftFormat unfolds everything when it does its thing, when the desired result of course is it should leave all folding/unfolding as is and not change any of it.

I also noticed, if it makes any difference, that if I haven't edited anything that needs formatting, since I last formatted, then SwiftFormat basically doesn't do anything -- doesn't reformat anything (because it's not needed) and doesn't unfold anything (presumably because it's not doing anything?). But if I do something that requires a format - eg. add a tab in front of a class name - and then format, it fixes that (correctly as desired), and unfolds everything (undesired, consistent with the issue, obviously).

It's very frustrating. 😊 Not sure how hard this is to fix, but if you can fix it, that'd be fantastic, because everything else about this app/extension is awesome. If you do fix it, where can I make a donation? 😊

My setup: 2021 M1 Max MacBook Pro 16" 64GB RAM macOS Ventura 13.3.1 (a) Xcode 14.3 (14E222b) SwiftFormat 0.51.9 (also with 0.51.7 (1), before I updated today).

Hope this helps. Thanks!

Detnator-Neo commented 1 year ago

Hey again @nicklockwood. I just had a thought about this and wanted to ask...

Is the reason for this issue due to the fact that I think any edit in Xcode unfolds whatever is being edited, and since, presumably this plugin potentially edits the entire current file of code, that's the reason it unfolds everything?

And I'm guessing the current folding status of every line/block in an Xcode file is something an extension can't access, and if so does that make this unfolding issue impossible for this or any other editing extension to avoid?

I'm considering trying to fix this myself with a pull request, although it would be my first time working on an Xcode extension. If you already know it's impossible because of the above (or some other reason) it would help to know that. 😉

(PS. I figured out where to donate, and did. Regardless of this issue, this extension is awesome.)

nicklockwood commented 1 year ago

@Detnator-Neo hi, I haven't checked recently, but this was certainly the case when I last looked. It's possible there's some way to access code fold state that I'm not aware of, but otherwise yes, changing any folded line appears to unavoidably cause it to be expanded.

It's possible that the situation could be improved a bit by ensuring that only lines which actually need to change are replaced by the extension. If you'd like to have a go at implementing something like that I'd be happy to accept the PR.

Detnator-Neo commented 1 year ago

@nicklockwood Thanks for that. Unfortunately my job just hit me with a sudden new intense project, so it might be a while before I can do this now 😕 but I'll see what I can do. Thanks for the info!

crisanvlad commented 9 months ago

It'd be nice if we get the option to not unfold everything everytime the code formatting is applied by the Xcode extension :)