-
Notifications
You must be signed in to change notification settings - Fork 439
Add edits to FixIt for sourcekit-lsp to access #2314
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A textual edit to the original source represented by a range and a | ||
/// replacement. | ||
public struct SourceEdit: Equatable { | ||
/// The half-open range that this edit applies to. | ||
public let range: Range<AbsolutePosition> | ||
/// The text to replace the original range with. Empty for a deletion. | ||
public let replacement: String | ||
|
||
/// Length of the original source range that this edit applies to. Zero if | ||
/// this is an addition. | ||
public var length: SourceLength { | ||
return SourceLength(utf8Length: range.lowerBound.utf8Offset - range.upperBound.utf8Offset) | ||
} | ||
|
||
/// Create an edit to replace `range` in the original source with | ||
/// `replacement`. | ||
public init(range: Range<AbsolutePosition>, replacement: String) { | ||
self.range = range | ||
self.replacement = replacement | ||
} | ||
|
||
/// Convenience function to create a textual addition after the given node | ||
/// and its trivia. | ||
public static func insert(_ newText: String, after node: some SyntaxProtocol) -> SourceEdit { | ||
return SourceEdit(range: node.endPosition..<node.endPosition, replacement: newText) | ||
} | ||
|
||
/// Convenience function to create a textual addition before the given node | ||
/// and its trivia. | ||
public static func insert(_ newText: String, before node: some SyntaxProtocol) -> SourceEdit { | ||
return SourceEdit(range: node.position..<node.position, replacement: newText) | ||
} | ||
|
||
/// Convenience function to create a textual replacement of the given node, | ||
/// including its trivia. | ||
public static func replace(_ node: some SyntaxProtocol, with replacement: String) -> SourceEdit { | ||
return SourceEdit(range: node.position..<node.endPosition, replacement: replacement) | ||
} | ||
|
||
/// Convenience function to create a textual deletion the given node and its | ||
/// trivia. | ||
public static func remove(_ node: some SyntaxProtocol) -> SourceEdit { | ||
return SourceEdit(range: node.position..<node.endPosition, replacement: "") | ||
} | ||
} | ||
|
||
extension SourceEdit: CustomDebugStringConvertible { | ||
public var debugDescription: String { | ||
let hasNewline = replacement.contains { $0.isNewline } | ||
if hasNewline { | ||
return #""" | ||
\#(range.lowerBound.utf8Offset)-\#(range.upperBound.utf8Offset) | ||
""" | ||
\#(replacement) | ||
""" | ||
"""# | ||
} | ||
return "\(range.lowerBound.utf8Offset)-\(range.upperBound.utf8Offset) \"\(replacement)\"" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftParser | ||
import SwiftParserDiagnostics | ||
import SwiftSyntax | ||
import XCTest | ||
import _SwiftSyntaxTestSupport | ||
|
||
final class FixItTests: XCTestCase { | ||
func testEditsForFixIt() throws { | ||
let markedSource = "protocol 1️⃣Multi 2️⃣ident 3️⃣{}" | ||
let (markers, source) = extractMarkers(markedSource) | ||
let positions = markers.mapValues { AbsolutePosition(utf8Offset: $0) } | ||
XCTAssertEqual(positions.count, 3) | ||
|
||
let expectedEdits = [ | ||
SourceEdit(range: positions["1️⃣"]!..<positions["2️⃣"]!, replacement: "Multiident "), | ||
SourceEdit(range: positions["2️⃣"]!..<positions["3️⃣"]!, replacement: ""), | ||
] | ||
let tree = Parser.parse(source: source) | ||
let diags = ParseDiagnosticsGenerator.diagnostics(for: tree) | ||
XCTAssertEqual(diags.count, 1) | ||
let diag = try XCTUnwrap(diags.first) | ||
XCTAssertEqual(diag.fixIts.count, 2) | ||
|
||
let fixIt = try XCTUnwrap(diag.fixIts.first) | ||
let changes = fixIt.changes | ||
let edits = fixIt.edits | ||
XCTAssertNotEqual(changes.count, edits.count) | ||
XCTAssertEqual(expectedEdits, edits) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.