vscode-swift icon indicating copy to clipboard operation
vscode-swift copied to clipboard

Add support for swift-format

Open ScottRobbins opened this issue 3 years ago • 8 comments

Apple has had a formatter under their GitHub organization for a while now that is actively maintained.

It would be great if support for this formatter was added to this plugin so that you could format files using it while writing code.

I did find another plugin that does this already, but the community may benefit from this functionality being included in this plugin. Perhaps use of the formatter itself may gain greater adoption due to better integration into VSCode.

ScottRobbins avatar Jan 20 '22 14:01 ScottRobbins

Sounds like a plan... we've been in this weird chicken and egg with the usage of that formatter. Starting to use it here more would be a good step forward probably 👍

ktoso avatar Jan 20 '22 23:01 ktoso

Could we do this by adding more general support for the swift package manager command plugins. This means we aren't specifying the formatting package people should use. Many people prefer Nick Lockwood's SwiftFormat.

adam-fowler avatar Jan 21 '22 10:01 adam-fowler

Looking at package manager command plugins, it seems that source code formatting is an example given.

However, I don't quite understand from that document if this would require every user to define their formatter plugin with all of that source code, or if swift-format itself could provide it. It would be unfortunate if every codebase had to write their own SPM command plugin that would work with VSCode and their chosen formatter.

Side question: Am I right in assuming that this isn't available in a currently released version of SwiftPM and Apple has not given a date for its release?

ScottRobbins avatar Jan 21 '22 21:01 ScottRobbins

Looking at package manager command plugins, it seems that source code formatting is an example given.

However, I don't quite understand from that document if this would require every user to define their formatter plugin with all of that source code, or if swift-format itself could provide it. It would be unfortunate if every codebase had to write their own SPM command plugin that would work with VSCode and their chosen formatter.

correct, this is an example and test-case for the new SwiftPM plugin APIs.

the idea is that someone would create a plugin - ie publish it as a package for anyone to use, such that other Swift users can apply that plugin in their own projects. this would work in a similar way to the recently published docc plugin: https://github.com/apple/swift-docc-plugin

Side question: Am I right in assuming that this isn't available in a currently released version of SwiftPM and Apple has not given a date for its release?

the new SwiftPM plugin functionality is available in the nightly 5.6 toolchains and subject to the general release Swift schedule

tomerd avatar Jan 21 '22 23:01 tomerd

Thank you @tomerd! I realize making the plugin a product was in the proposal and I missed it. I appreciate this information.

Could we do this by adding more general support for the swift package manager command plugins. This means we aren't specifying the formatting package people should use. Many people prefer Nick Lockwood's SwiftFormat.

Using the command plugin makes sense to me. I think that the VSCode extension would still want to register itself as a formatting editor provider.

I am curious what you had in mind in terms of general support for command plugins? Would the VSCode extension make an attempt to automatically register SPM command plugins as VSCode commands?

ScottRobbins avatar Jan 22 '22 01:01 ScottRobbins

cc @abertelrud

the command plugins API has a construct called "intent" in which a plugin author can define the functional goal of the plugin. the idea is that while some plugins are more unique in their goal, some are addressing common needs like formatting, linting, etc. in that context, different plugins could offer the same intent while integrating with a different formatting tools. the intent is then useful to provide a consistent user experience when invoking plugins that share the same goal, for example on the CLI invoking all plugins that define "formatting" as their intent would be using swift package format even tho they may invoke a different formatting tool with different configuration options. this also helps with IDE integration where the IDE could query which plugins / intents are available and invoke a plugin with a "formatting" intent regardless of the underlying formatting tool

hth

tomerd avatar Jan 22 '22 01:01 tomerd

@ScottRobbins. just curious, why this should be added into vscode-swift? if you installed a formatter for swift files for VSCode AND you can config when to format this file. like on editing or saving

this may the one you need https://github.com/vknabel/vscode-swiftformat https://github.com/nicklockwood/SwiftFormat

aelam avatar Jul 16 '22 11:07 aelam

@aelam Super late responding to this

Yes, those solutions are already available, and I use them myself.

why this should be added into vscode-swift?

I think it would make these formatters a little easier to use/more popular, especially in open source projects, if it was available in this plugin.

One of the attractive things about vscode-swift is that it covers a lot of general capabilities everyone wants for their swift projects and isn't split into multiple focused plugins. It removes the friction of having to set up another thing.

ScottRobbins avatar Aug 12 '22 20:08 ScottRobbins

I think this should be fixed now that sourcekit-lsp supports the formatting LSP request (https://github.com/apple/sourcekit-lsp/pull/769). Note that this requires a recent Swift 6.0 or main development snapshot from https://www.swift.org/download/

ahoppen avatar Apr 12 '24 21:04 ahoppen

This is available in the editor context menu, or option+shift+f with Swift 6

adam-fowler avatar Apr 13 '24 14:04 adam-fowler