-
Notifications
You must be signed in to change notification settings - Fork 378
Expand file tree
/
Copy pathSwapBinaryOperands.swift
More file actions
171 lines (144 loc) · 6.62 KB
/
Copy pathSwapBinaryOperands.swift
File metadata and controls
171 lines (144 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2026 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
//
//===----------------------------------------------------------------------===//
internal import LanguageServerProtocol
internal import SourceKitLSP
import SwiftBasicFormat
import SwiftExtensions
import SwiftOperators
import SwiftRefactor
import SwiftSyntax
struct SwapBinaryOperands: SyntaxRefactoringCodeActionProvider {
package static let title: String = "Swap operands"
package static func nodeToRefactor(in scope: SyntaxCodeActionScope) -> ExprSyntax? {
guard let node = scope.innermostNodeContainingRange else {
return nil
}
guard
let opExpr = node.findParentOfSelf(
ofType: ExprSyntax.self,
stoppingIf: { $0.is(CodeBlockItemSyntax.self) },
matching: { $0.is(BinaryOperatorExprSyntax.self) }
)?.as(BinaryOperatorExprSyntax.self)
else {
return nil
}
// Only offer the refactoring when the operator participates in a
// binary expression that can be folded into an InfixOperatorExprSyntax.
guard
opExpr.parent?.is(InfixOperatorExprSyntax.self) == true
|| opExpr.parent?.as(ExprListSyntax.self)?.parent?.is(SequenceExprSyntax.self) == true
else {
return nil
}
let startPos = scope.snapshot.absolutePosition(of: scope.request.range.lowerBound)
let endPos = scope.snapshot.absolutePosition(of: scope.request.range.upperBound)
let selectionRange = startPos..<endPos
// Only offer the refactoring when the cursor or selection targets the
// operator token. This prevents offering the action when the cursor is placed on
// either operand or in surrounding trivia.
let tokenRange = opExpr.operator.trimmedRange
guard selectionRange.overlapsOrTouches(tokenRange) else {
return nil
}
return ExprSyntax(opExpr)
}
package static func textRefactor(syntax opExpr: ExprSyntax, in context: Void) throws -> [SourceEdit] {
guard let binOp = opExpr.as(BinaryOperatorExprSyntax.self) else {
throw RefactoringNotApplicableError("Selected expression is not a binary operator")
}
// Locate the smallest expression that may contain the operator under the cursor.
// SequenceExprSyntax needs to be folded before precedence-aware rewriting can occur.
let exprToFold: ExprSyntax
if let infixExpr = binOp.parent?.as(InfixOperatorExprSyntax.self) {
exprToFold = ExprSyntax(infixExpr)
} else if let seqExpr = binOp.parent?.as(ExprListSyntax.self)?.parent?.as(SequenceExprSyntax.self) {
exprToFold = ExprSyntax(seqExpr)
} else {
throw RefactoringNotApplicableError("Could not find an infix or sequence expression to fold")
}
let foldedExpr: ExprSyntax
// Fold operator precedence so nested expressions such as
// `1 + -2 * 5` become a structured tree where the selected
// infix operator can be identified reliably.
if exprToFold.is(SequenceExprSyntax.self) {
guard let folded = OperatorTable.standardOperators.foldAll(exprToFold, errorHandler: { _ in }).as(ExprSyntax.self)
else {
throw RefactoringNotApplicableError("Failed to fold operator sequence")
}
foldedExpr = folded
} else {
foldedExpr = exprToFold.detached
}
let currentOperatorText = binOp.operator.text
// Calculate the relative UTF-8 offset.
let targetRelativeOffset =
binOp.operator.positionAfterSkippingLeadingTrivia.utf8Offset - exprToFold.position.utf8Offset
let targetPosition = AbsolutePosition(utf8Offset: foldedExpr.position.utf8Offset + targetRelativeOffset)
guard
let token = foldedExpr.token(at: targetPosition),
let infixExpr = token.parent?.as(BinaryOperatorExprSyntax.self)?.parent?.as(InfixOperatorExprSyntax.self)
else {
throw RefactoringNotApplicableError("Could not locate the target operator in the folded tree")
}
// Avoid offering the refactoring for malformed expressions such as
// `a +` or `+ b` while the user is still typing.
guard !infixExpr.hasError else {
throw RefactoringNotApplicableError("Expression is malformed or incomplete")
}
let newOperatorText: String
// Comparison operators must be inverted when operands are swapped.
// Symmetric operators can be reused unchanged.
switch currentOperatorText {
case "<": newOperatorText = ">"
case ">": newOperatorText = "<"
case "<=": newOperatorText = ">="
case ">=": newOperatorText = "<="
case "+", "*", "==", "!=", "===", "!==", "&&", "||", "&", "|", "^":
newOperatorText = currentOperatorText
default:
throw RefactoringNotApplicableError("Operator '\(currentOperatorText)' cannot be swapped")
}
let leftOperand = infixExpr.leftOperand
let rightOperand = infixExpr.rightOperand
// Preserve operand trivia so whitespace and comments remain attached
// to the same side of the expression after swapping.
var newLeft = rightOperand
newLeft.leadingTrivia = leftOperand.leadingTrivia
newLeft.trailingTrivia = leftOperand.trailingTrivia
var newRight = leftOperand
newRight.leadingTrivia = rightOperand.leadingTrivia
newRight.trailingTrivia = rightOperand.trailingTrivia
guard let targetBinOp = infixExpr.operator.as(BinaryOperatorExprSyntax.self) else {
throw RefactoringNotApplicableError("Failed to cast operator to BinaryOperatorExprSyntax")
}
let newToken = targetBinOp.operator.with(\.tokenKind, .binaryOperator(newOperatorText))
let newOperatorExpr = ExprSyntax(targetBinOp.with(\.operator, newToken))
let newInfix =
infixExpr
.with(\.leftOperand, newLeft)
.with(\.operator, newOperatorExpr)
.with(\.rightOperand, newRight)
// Calculate the absolute range of the targeted expression in the original source
// by applying its relative offset from the detached folded tree.
let rootOffset = exprToFold.position.utf8Offset
let foldedRootOffset = foldedExpr.position.utf8Offset
let startOffset = rootOffset + (infixExpr.position.utf8Offset - foldedRootOffset)
let endOffset = rootOffset + (infixExpr.endPosition.utf8Offset - foldedRootOffset)
let editRange = AbsolutePosition(utf8Offset: startOffset)..<AbsolutePosition(utf8Offset: endOffset)
return [
SourceEdit(
range: editRange,
replacement: newInfix.description
)
]
}
}