Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Format previous line on 'Enter' key#43

Open
vinocher-bc wants to merge 2 commits into
vknabel:mainfrom
vinocher-bc:formatOnType
Open

Format previous line on 'Enter' key#43
vinocher-bc wants to merge 2 commits into
vknabel:mainfrom
vinocher-bc:formatOnType

Conversation

@vinocher-bc

@vinocher-bc vinocher-bc commented Jun 18, 2024

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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

@vknabel

vknabel commented Jun 22, 2024

Copy link
Copy Markdown
Owner

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.

@vknabel
vknabel requested review from MahdiBM and vknabel June 22, 2024 10:04

@MahdiBM MahdiBM left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some suggestions, other than @vknabel 's.

Comment thread src/SwiftFormatEditProvider.ts
@vinocher-bc

vinocher-bc commented Jun 24, 2024

Copy link
Copy Markdown
Contributor Author

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 nicklockwood/SwiftFormat#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

MahdiBM commented Jun 24, 2024

Copy link
Copy Markdown
Collaborator

@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?

@vknabel

vknabel commented Nov 21, 2024

Copy link
Copy Markdown
Owner

Is this ready to merge now as nicklockwood/SwiftFormat#1738 has been resolved?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants