vknabel / vscode-swiftformat

SwiftFormat for VS Code
https://marketplace.visualstudio.com/items?itemName=vknabel.vscode-swiftformat
MIT License
39 stars 7 forks source link

Format previous line on 'Enter' key #43

Open vinocher-bc opened 2 weeks ago

vinocher-bc commented 2 weeks ago

When the 'Enter' key is pressed, format the previous line because the user has usually moved to a new empty line. This is the most common scenario.

Also, format only the previous line instead of the entire document for better performance and because formatting the entire doc may conflict with "editor.formatOnSaveMode": "modifications" (See Only format modified text.)

Previously, the entire document was formatted if the new line was non-empty.

vinocher-bc commented 2 weeks ago

@vknabel, @MahdiBM, please review if possible. Thanks.

vknabel commented 2 weeks ago

Great start! Though when having the following code, formatting lead to losing the indentation:

SomeType()
    .doSomething (  )

// was formatted to
SomeType()
.doSomething()

I suppose for non-empty lines we should advance the range to the first non-whitespace character.

vinocher-bc commented 2 weeks ago

Great start! Though when having the following code, formatting lead to losing the indentation:

SomeType()
    .doSomething (  )

// was formatted to
SomeType()
.doSomething()

I suppose for non-empty lines we should advance the range to the first non-whitespace character.

This looks like a swiftformat issue -- I don't think there is a workaround. Added it to the end of https://github.com/nicklockwood/SwiftFormat/issues/1738. I don't think we can make this change in the extension until swiftformat fixes the corresponding issue, otherwise it may be too disruptive for users.

MahdiBM commented 2 weeks ago

@vinocher-bc ppssibly we can see how many spaces are at beginning of the line before format, and add them back after the format too each new line of the formatted text?