Skip to content

Support formatting of entire documents #769

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

Merged
merged 1 commit into from
Jan 24, 2024

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jul 13, 2023

Depend on the swift-format library to discover and write the swift-format configuration file. Invoke swift-format from the toolchain to actually format a document. This makes sure that the formatting of SourceKit-LSP and the swift-format executable in the toolchain never get out of sync.

Fixes #576
rdar://96159694

@ahoppen
Copy link
Member Author

ahoppen commented Jul 13, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jul 13, 2023

CC @allevato, I’d appreciate your review of how I’m calling swift-format’s API.

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

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

Looks good to me—excited to see this integration finally happening!

@@ -0,0 +1,4 @@
This file is intentionally an invalid swift-format configuration file. It is used to test
the behavior when program cannot find a valid file. We cannot just left this directory blank,
Copy link
Contributor

Choose a reason for hiding this comment

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

s/left/leave

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

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

Woo, formatting!

Comment on lines 1498 to 1507
// Unsupported options
if req.options.trimTrailingWhitespace == false {
throw ResponseError(code: .requestFailed, message: "swift-format does not support keeping trailing whitespace; set format option to trim trailing trivia to run the formatter")
}
if req.options.insertFinalNewline == false {
throw ResponseError(code: .requestFailed, message: "swift-format always inserts a final newline to the file; set option to insert a final newline to run the formatter")
}
if req.options.trimFinalNewlines == false {
throw ResponseError(code: .requestFailed, message: "swift-format always trims final newlines; set option to trim final newlines to run the formatter")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

These messages are all a little confusing to me, especially the first. Is the ; set option... really needed? If it is, can we name it explicitly with the actual value (true)?

@ahoppen
Copy link
Member Author

ahoppen commented Jul 14, 2023

@swift-ci Please test Windows

@MaxDesiatov
Copy link
Contributor

@swift-ci test

@ahoppen
Copy link
Member Author

ahoppen commented Aug 14, 2023

Putting on hold until we can build swift-format on Windows.

@ahoppen ahoppen marked this pull request as draft August 14, 2023 20:24
@IOOI-SqAR
Copy link

Putting on hold until we can build swift-format on Windows.

Is there already an issue for this in swift-format? Doing a cursory search there I only found https://github.com/apple/swift-format/issues?q=is%3Aissue+is%3Aopen+windows

@ahoppen
Copy link
Member Author

ahoppen commented Sep 15, 2023

No, but I think it's on @compnerd mental to do list

@ahoppen ahoppen force-pushed the ahoppen/document-formatting branch from 0baa53d to 4916151 Compare January 18, 2024 23:08
@ahoppen ahoppen requested a review from bnbarham January 18, 2024 23:09
@ahoppen ahoppen marked this pull request as ready for review January 18, 2024 23:09
@ahoppen
Copy link
Member Author

ahoppen commented Jan 18, 2024

I changed the implementation to invoke swift-format as an executable instead of having SourceKit-LSP link against SwiftFormat.

@ahoppen
Copy link
Member Author

ahoppen commented Jan 18, 2024

@swift-ci Please test

ahoppen added a commit to ahoppen/swift that referenced this pull request Jan 19, 2024
With swiftlang/sourcekit-lsp#769, sourcekit-lsp supports formatting that invokes swift-format from the toolchain and tests that test this behavior. For those to pass, we need to install swift-format into the toolchain on all jobs that test sourcekit-lsp.
@ahoppen
Copy link
Member Author

ahoppen commented Jan 19, 2024

swiftlang/swift#71003

@swift-ci Please test

ahoppen added a commit to ahoppen/swift that referenced this pull request Jan 19, 2024
With swiftlang/sourcekit-lsp#769, sourcekit-lsp supports formatting that invokes swift-format from the toolchain and tests that test this behavior. For those to pass, we need to install swift-format into the toolchain on all jobs that test sourcekit-lsp.
@ahoppen
Copy link
Member Author

ahoppen commented Jan 19, 2024

swiftlang/swift#71003

@swift-ci Please test

README.md Outdated
@@ -46,7 +46,7 @@ SourceKit-LSP is still in early development, so you may run into rough edges wit
| Workspace Symbols | ✅ | |
| Rename | ❌ | |
| Local Refactoring | ✅ | |
| Formatting | ❌ | |
| Formatting | ✅ | Whole file at once only. |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
| Formatting || Whole file at once only. |
| Formatting || Whole file only |

// Most likely, the editor inferred them from the current document and thus the options
// passed by the editor are most likely less correct than those in .swift-format.
return try await body(configFile)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer no else. Does swift-format allow setting up options without a file so there would be no need for the temporary file?

Copy link
Member

Choose a reason for hiding this comment

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

We added the ability to pass a raw JSON string in the --configuration flag a while back: swiftlang/swift-format#634

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, nice. I didn’t know that. That makes the implementation a lot nicer. 😍

}

let formattedBytes = try await withSwiftFormatConfiguration(for: req.textDocument.uri, options: req.options) {
(formatConfigUrl) -> [UInt8] in
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a bug open in swift-format for this? The formatting for closures makes very little sense IMO. It would be better to make the for:options: on newlines and then keep the arguments with the { (which could either be on a newline, or with the last argument to the call).

Copy link
Member Author

Choose a reason for hiding this comment

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

Depend on the swift-format library to discover and write the swift-format configuration file. Invoke swift-format from the toolchain to actually format a document. This makes sure that the formatting of SourceKit-LSP and the swift-format executable in the toolchain never get out of sync.

Fixes swiftlang#576
rdar://96159694
@ahoppen ahoppen force-pushed the ahoppen/document-formatting branch from 4916151 to 79cc4f9 Compare January 24, 2024 06:31
@ahoppen
Copy link
Member Author

ahoppen commented Jan 24, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge January 24, 2024 06:32
@ahoppen
Copy link
Member Author

ahoppen commented Jan 24, 2024

@swift-ci Please test Windows

@ahoppen ahoppen merged commit a9242f8 into swiftlang:main Jan 24, 2024
@ahoppen ahoppen deleted the ahoppen/document-formatting branch January 25, 2024 03:14
carlos4242 pushed a commit to carlos4242/swift that referenced this pull request May 31, 2024
With swiftlang/sourcekit-lsp#769, sourcekit-lsp supports formatting that invokes swift-format from the toolchain and tests that test this behavior. For those to pass, we need to install swift-format into the toolchain on all jobs that test sourcekit-lsp.
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.

Add support for the "textDocument/formatting" verb
6 participants