Skip to content

Commit 664e881

Browse files
committed
Rename FixIt.Change.textualReplacement to replaceText(range:with:in:)
Rename according to the discussion. Additionally, only require a Syntax node (not a SourceFileSyntax) as the place we're rewriting from.
1 parent b0aee79 commit 664e881

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

Release Notes/602.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22

33
## New APIs
44

5-
- `DiagnosticMessage` has a new optional property, `category`, that providesa category name and documentation URL for a diagnostic.
6-
- Description: Tools often have many different diagnostics. Diagnostic categories allow tools to group several diagnostics together with documentation that can help users understand what the diagnostics mean and how to address them. This API allows diagnostics to provide this category information. The diagnostic renderer will provide the category at the end of the diagnostic message in the form `[#CategoryName]`, and can print categories as "footnotes" with its `categoryFootnotes` method.
7-
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2981
8-
- Migration steps: None required. The new `category` property has optional type, and there is a default implementation that returns `nil`. Types that conform to `DiagnosticMessage` can choose to implement this property and provide a category when appropriate.
9-
105
- `SwiftLexicalLookup` - A new Swift unqualified lookup library
116
- Description: The library provides a new Swift unqualified lookup implementation detached from the compiler and accessible to outside clients. The query is stateless and can be directly run on swift-syntax syntax tree, with any syntax node functioning as an entry point. It produces an enum-based data structure as a result that partitions collected names based on the lexical scope of introduction.
127
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2952
138
- Notes: The library follows the behavior of the compiler implementation with some minor differences, such as a different way of handling dollar identifiers `$x` and generic parameters inside extensions. Furthermore, in the future, once the compiler adopts `SwiftLexicalLookup` and it becomes the canonical implementation, results produced by the query will be guaranteed to be correct.
149

10+
- `DiagnosticMessage` has a new optional property, `category`, that provides a category name and documentation URL for a diagnostic.
11+
- Description: Tools often have many different diagnostics. Diagnostic categories allow tools to group several diagnostics together with documentation that can help users understand what the diagnostics mean and how to address them. This API allows diagnostics to provide this category information. The diagnostic renderer will provide the category at the end of the diagnostic message in the form `[#CategoryName]`, and can print categories as "footnotes" with its `categoryFootnotes` method.
12+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2981
13+
- Migration steps: None required. The new `category` property has optional type, and there is a default implementation that returns `nil`. Types that conform to `DiagnosticMessage` can choose to implement this property and provide a category when appropriate.
14+
15+
- `FixIt.Change` has a new case `replaceText` that performs a textual replacement of a range of text with another string.
16+
- Description: The other `FixIt.Change` cases provide structured
17+
modifications to syntax trees, such as replacing specific notes. Some
18+
clients provide Fix-Its that don't fit well into this structured model. The
19+
`replaceText` case makes it possible for such clients to express Fix-Its.
20+
- Pull request: https://github.com/swiftlang/swift-syntax/pull/3030
21+
- Migration stems: None required.
22+
1523
## API Behavior Changes
1624

1725
## Deprecations

Sources/SwiftCompilerPluginMessageHandling/Diagnostics.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ extension PluginMessage.Diagnostic {
150150
case .replaceChild(let replaceChildData):
151151
range = sourceManager.range(replaceChildData.replacementRange, in: replaceChildData.parent)
152152
text = replaceChildData.newChild.description
153-
case .textualReplacement(
154-
replacementRange: let replacementRange,
155-
sourceFile: let sourceFile,
156-
newText: let newText
153+
case .replaceText(
154+
range: let replacementRange,
155+
with: let newText,
156+
in: let syntax
157157
):
158-
range = sourceManager.range(replacementRange, in: sourceFile)
158+
range = sourceManager.range(replacementRange, in: syntax)
159159
text = newText
160160
#if RESILIENT_LIBRARIES
161161
@unknown default:

Sources/SwiftDiagnostics/FixIt.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ public struct FixIt: Sendable {
7777
case replaceTrailingTrivia(token: TokenSyntax, newTrivia: Trivia)
7878
/// Replace the child node of the given parent node at the given replacement range with the given new child node
7979
case replaceChild(data: any ReplacingChildData)
80-
/// Replace the text within the given range in a source file with new text.
80+
/// Replace the text within the given range in a syntax node with new text.
8181
///
8282
/// Generally, one should use other cases to replace specific syntax nodes
8383
/// or trivia, because it more easily leads to a correct result. However,
8484
/// this case provides a fallback for textual replacement that ignores
8585
/// syntactic structure. After applying a textual replacement, there is no
8686
/// way to get back to a syntax tree without reparsing.
87-
case textualReplacement(replacementRange: Range<AbsolutePosition>, sourceFile: SourceFileSyntax, newText: String)
87+
case replaceText(range: Range<AbsolutePosition>, with: String, in: Syntax)
8888
}
8989

9090
/// A description of what this Fix-It performs.
@@ -166,7 +166,7 @@ private extension FixIt.Change {
166166
replacement: replacingChildData.newChild.description
167167
)
168168

169-
case .textualReplacement(replacementRange: let replacementRange, sourceFile: _, newText: let newText):
169+
case .replaceText(range: let replacementRange, with: let newText, in: _):
170170
return SourceEdit(range: replacementRange, replacement: newText)
171171
}
172172
}

Sources/SwiftSyntaxMacrosGenericTestSupport/Assertions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,9 @@ fileprivate extension FixIt.Change {
633633
replacement: replacingChildData.newChild.description
634634
)
635635

636-
case .textualReplacement(replacementRange: let range, sourceFile: let sourceFile, newText: let newText):
637-
let start = expansionContext.position(of: range.lowerBound, anchoredAt: sourceFile)
638-
let end = expansionContext.position(of: range.upperBound, anchoredAt: sourceFile)
636+
case .replaceText(range: let range, with: let newText, in: let syntax):
637+
let start = expansionContext.position(of: range.lowerBound, anchoredAt: syntax)
638+
let end = expansionContext.position(of: range.upperBound, anchoredAt: syntax)
639639
return SourceEdit(range: start..<end, replacement: newText)
640640
}
641641
}

Tests/SwiftDiagnosticsTest/FixItTests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ final class FixItTests: XCTestCase {
5555
let change = FixIt(
5656
message: SimpleFixItMessage(message: "fix it please"),
5757
changes: [
58-
.textualReplacement(replacementRange: five..<fifteen, sourceFile: "func myFunction() { }", newText: "yours")
58+
.replaceText(
59+
range: five..<fifteen,
60+
with: "yours",
61+
in: Syntax("func myFunction() { }" as SourceFileSyntax)
62+
)
5963
]
6064
)
6165
XCTAssertEqual(change.edits.count, 1)

0 commit comments

Comments
 (0)