Skip to content

Commit 71134e0

Browse files
committed
[Commands] Prototype swift fixit — a subcommand to deserialize diagnostics and apply fix-its
1 parent 68d55b1 commit 71134e0

File tree

11 files changed

+1168
-0
lines changed

11 files changed

+1168
-0
lines changed

Package.swift

+26
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,21 @@ let package = Package(
304304
]
305305
),
306306

307+
.target(
308+
/** The backing API for `SwiftFixItCommand` */
309+
name: "SwiftFixIt",
310+
dependencies: [
311+
"Basics",
312+
.product(name: "TSCBasic", package: "swift-tools-support-core"),
313+
] + swiftSyntaxDependencies(
314+
["SwiftDiagnostics", "SwiftIDEUtils", "SwiftParser", "SwiftSyntax"]
315+
),
316+
exclude: ["CMakeLists.txt"],
317+
swiftSettings: commonExperimentalFeatures + [
318+
.unsafeFlags(["-static"]),
319+
]
320+
),
321+
307322
// MARK: Project Model
308323

309324
.target(
@@ -573,6 +588,7 @@ let package = Package(
573588
"Workspace",
574589
"XCBuildSupport",
575590
"SwiftBuildSupport",
591+
"SwiftFixIt",
576592
] + swiftSyntaxDependencies(["SwiftIDEUtils"]),
577593
exclude: ["CMakeLists.txt", "README.md"],
578594
swiftSettings: swift6CompatibleExperimentalFeatures + [
@@ -730,6 +746,12 @@ let package = Package(
730746
"Workspace",
731747
]
732748
),
749+
.executableTarget(
750+
/** Deserializes diagnostics and applies fix-its */
751+
name: "swift-fixit",
752+
dependencies: ["Commands"],
753+
exclude: ["CMakeLists.txt"]
754+
),
733755

734756
// MARK: Support for Swift macros, should eventually move to a plugin-based solution
735757

@@ -916,6 +938,10 @@ let package = Package(
916938
dependencies: ["SourceControl", "_InternalTestSupport"],
917939
exclude: ["Inputs/TestRepo.tgz"]
918940
),
941+
.testTarget(
942+
name: "SwiftFixItTests",
943+
dependencies: ["SwiftFixIt", "_InternalTestSupport"]
944+
),
919945
.testTarget(
920946
name: "XCBuildSupportTests",
921947
dependencies: ["XCBuildSupport", "_InternalTestSupport", "_InternalBuildTestSupport"],

Sources/Commands/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ add_library(Commands
4040
Snippets/Card.swift
4141
Snippets/Colorful.swift
4242
SwiftBuildCommand.swift
43+
SwiftFixItCommand.swift
4344
SwiftRunCommand.swift
4445
SwiftTestCommand.swift
4546
CommandWorkspaceDelegate.swift
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import struct ArgumentParser.Argument
14+
import protocol ArgumentParser.AsyncParsableCommand
15+
import struct ArgumentParser.CommandConfiguration
16+
import struct ArgumentParser.OptionGroup
17+
import protocol ArgumentParser.ParsableArguments
18+
19+
import struct Basics.AbsolutePath
20+
import var Basics.localFileSystem
21+
import struct Basics.SwiftVersion
22+
23+
import struct CoreCommands.LoggingOptions
24+
25+
import struct SwiftFixIt.SwiftFixIt
26+
27+
private struct Options: ParsableArguments {
28+
@OptionGroup(title: "Logging")
29+
var logging: LoggingOptions
30+
31+
@Argument(
32+
help: "",
33+
completion: .file(extensions: [".dia"])
34+
)
35+
var diagnosticFiles: [AbsolutePath] = []
36+
}
37+
38+
/// Deserializes `.dia` files and applies all fix-its in place.
39+
package struct SwiftFixitCommand: AsyncParsableCommand {
40+
package static var configuration = CommandConfiguration(
41+
commandName: "fixit",
42+
_superCommandName: "swift",
43+
abstract: "Deserialize diagnostics and apply fix-its",
44+
version: SwiftVersion.current.completeDisplayString,
45+
helpNames: [
46+
.short,
47+
.long,
48+
.customLong("help", withSingleDash: true),
49+
]
50+
)
51+
52+
@OptionGroup
53+
fileprivate var options: Options
54+
55+
package init() {}
56+
57+
package func run() async throws {
58+
if self.options.diagnosticFiles.isEmpty {
59+
return
60+
}
61+
62+
let swiftFixIt = try SwiftFixIt(diagnosticFiles: options.diagnosticFiles, fileSystem: localFileSystem)
63+
64+
try swiftFixIt.applyFixIts()
65+
}
66+
}

Sources/SwiftFixIt/CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This source file is part of the Swift open source project
2+
#
3+
# Copyright (c) 2025 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_library(SwiftFixIt STATIC
10+
SwiftFixIt.swift)
11+
target_link_libraries(SwiftFixIt PUBLIC
12+
Basics
13+
14+
SwiftSyntax::SwiftDiagnostics
15+
SwiftSyntax::SwiftIDEUtils
16+
SwiftSyntax::SwiftParser
17+
SwiftSyntax::SwiftSyntax
18+
19+
TSCBasic
20+
TSCUtility
21+
)
22+
23+
set_target_properties(SwiftFixIt PROPERTIES
24+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

0 commit comments

Comments
 (0)