Skip to content
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

Format previous line on 'Enter' key #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vinocher-bc
Copy link
Contributor

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

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
Contributor Author

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

@vknabel
Copy link
Owner

vknabel commented Jun 22, 2024

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 vknabel and MahdiBM June 22, 2024 10:04
Copy link
Collaborator

@MahdiBM MahdiBM left a comment

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.

src/SwiftFormatEditProvider.ts Outdated Show resolved Hide resolved
@vinocher-bc
Copy link
Contributor Author

vinocher-bc commented Jun 24, 2024

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
Copy link
Collaborator

MahdiBM commented Jun 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants