Skip to content

In nodesDescription only merge tokens if they occur consecutively in the source code #1651

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 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Sources/SwiftParserDiagnostics/MissingNodesError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ fileprivate enum NodesDescriptionPart {
}

static func descriptionParts(for nodes: some Sequence<Syntax>) -> [NodesDescriptionPart] {
// For each token that we modified (e.g. made it present), map to the original token in `nodes`
var originalTokens: [TokenSyntax: TokenSyntax] = [:]

// Accumulates the result
var parts: [NodesDescriptionPart] = []

for missingNode in nodes {
if let token = missingNode.as(TokenSyntax.self) {
let newPart: NodesDescriptionPart
Expand All @@ -77,10 +82,12 @@ fileprivate enum NodesDescriptionPart {
let (rawKind, text) = token.tokenKind.decomposeToRaw()
if let text = text, !text.isEmpty {
let presentToken = token.with(\.presence, .present)
originalTokens[presentToken] = token
newPart = .tokensWithDefaultText([presentToken])
} else if let defaultText = rawKind.defaultText {
let newKind = TokenKind.fromRaw(kind: rawKind, text: String(syntaxText: defaultText))
let presentToken = token.with(\.tokenKind, newKind).with(\.presence, .present)
originalTokens[presentToken] = token
newPart = .tokensWithDefaultText([presentToken])
} else {
newPart = .tokenWithoutDefaultText(token)
Expand All @@ -89,7 +96,15 @@ fileprivate enum NodesDescriptionPart {

switch (parts.last, newPart) {
case (.tokensWithDefaultText(let previousTokens), .tokensWithDefaultText(let newTokens)):
parts[parts.count - 1] = .tokensWithDefaultText(previousTokens + newTokens)
// Merge `tokensWithDefaultText` if they occur consecutively in the tree
if let lastPrevious = previousTokens.last,
let firstNew = newTokens.first,
originalTokens[lastPrevious, default: lastPrevious].nextToken(viewMode: .all) == originalTokens[firstNew, default: firstNew]
{
parts[parts.count - 1] = .tokensWithDefaultText(previousTokens + newTokens)
} else {
parts.append(newPart)
}
default:
parts.append(newPart)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ final class DeclarationTests: XCTestCase {
diagnostics: [
DiagnosticSpec(
message: "expected 'set' in modifier",
fixIts: ["remove 'get, , didSet'"]
fixIts: ["remove 'get,' and ', didSet'"]
)
],
fixedSource: """
Expand Down
10 changes: 5 additions & 5 deletions Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,7 @@ final class RecoveryTests: XCTestCase {
diagnostics: [
DiagnosticSpec(
message: "expected ':' to begin inheritance clause",
fixIts: ["replace '()' with ':'"]
fixIts: ["replace '(' and ')' with ':'"]
)
],
fixedSource: """
Expand All @@ -2034,7 +2034,7 @@ final class RecoveryTests: XCTestCase {
diagnostics: [
DiagnosticSpec(
message: "expected ':' to begin inheritance clause",
fixIts: ["replace '()' with ':'"]
fixIts: ["replace '(' and ')' with ':'"]
)
],
fixedSource: """
Expand All @@ -2051,7 +2051,7 @@ final class RecoveryTests: XCTestCase {
diagnostics: [
DiagnosticSpec(
message: "expected ':' to begin inheritance clause",
fixIts: ["replace '()' with ':'"]
fixIts: ["replace '(' and ')' with ':'"]
)
],
fixedSource: """
Expand All @@ -2068,7 +2068,7 @@ final class RecoveryTests: XCTestCase {
diagnostics: [
DiagnosticSpec(
message: "expected ':' to begin inheritance clause",
fixIts: ["replace '()' with ':'"]
fixIts: ["replace '(' and ')' with ':'"]
)
],
fixedSource: """
Expand All @@ -2085,7 +2085,7 @@ final class RecoveryTests: XCTestCase {
diagnostics: [
DiagnosticSpec(
message: "expected ':' to begin inheritance clause",
fixIts: ["replace '()' with ':'"]
fixIts: ["replace '(' and ')' with ':'"]
)
],
fixedSource: """
Expand Down