sourcekit-lsp
                                
                                
                                
                                    sourcekit-lsp copied to clipboard
                            
                            
                            
                        Add local refactoring code actions based on swift-syntax
Introduce new local refactoring code actions based on the Swift syntax tree, without going through SourceKit.
This change includes a number of new refactoring code actions. Most of them adapt the syntax refactorings from the SwiftRefactor module of swift-syntax:
- Add digit separators to an integer literal, e.g., 
100000->1_000_000. - Remove digit separators from an integer literal, e.g., 
1_000_000->1000000. (from SwiftRefactor) - Format a raw string literal, e.g., 
"Hello \#(world)"->##"Hello \#(world)"## - Migrate to new if let syntax, e.g., 
if let x = x { ... }->if let x { ... } - Replace opaque parameters with generic parameters, e.g., 
func f(p: some P)-->func f<T1: P>(p: T1). 
This is generally easy to do, requiring one conformance to provide a name for the refactoring:
extension AddSeparatorsToIntegerLiteral: SyntaxRefactoringCodeActionProvider {
  public static var title: String { "Add digit separators" }
}
There are also a few new refactoring code actions implemented here. These could be sunk down into SwiftRefactor to be made more widely available, or stay here if they're too IDE-centric to live in swift-refactor:
- Convert an integer literal from one base to another (e.g., 
172387->0x2a163). - Convert JSON into a Codable struct.
 - Apply Demorgan's law, e.g., 
!(a || b)-->!a && !b. 
There is a small amount of overlap between the refactoring code actions added here and those SourceKit provides. For example, SourceKit provides a "Simplify long number literal" operation that is similar to "Add digit separators". We have several directions we could go with those:
- Don't add the new refactoring code actions if they conflict. This would be sad.
 - Filter out the SourceKit ones so the user doesn't see them. We can keep the list in SourceKit-LSP.
 - Remove the redundant actions from SourceKit. Clients of SourceKit (not -LSP) would lose access to them.
 - Replace the SourceKit implementation with the ones from SwiftRefactor to server SourceKit clients, and filter them out (like option 2) for LSP clients.
 
Most of the code here is actually from @CodaFi, and I've given it a light dusting to bring it up to modern times.
@swift-ci please test
@swift-ci please test
@swift-ci please test windows
@swift-ci please test Windows
Love the JSON-to-Codable conversion, that looks super useful!
@swift-ci please test
@swift-ci please test Windows
@swift-ci please test Windows
I'm tackling the first part of this, exposing swift-syntax-based actions, via the PR at https://github.com/apple/sourcekit-lsp/pull/1179. It drops Demorgans and the JSON-to-structs refactoring actions and addresses most of the feedback here. (Not all of it, yet)
Everything but DeMorgan's has merged via other pull requests, so I'm going to close this out. DeMorgan's needs a some work that I'm not up for just now.