Skip to content

Commit b879a97

Browse files
authored
Merge pull request swiftlang#63640 from DougGregor/expand-macro-whitespace-adjust
2 parents e800217 + 3b42b96 commit b879a97

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

lib/Refactoring/Refactoring.cpp

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8595,6 +8595,50 @@ bool RefactoringActionExpandMacro::isApplicable(const ResolvedCursorInfo &Info,
85958595
return !getMacroExpansionBuffers(Diag.SourceMgr, Info).empty();
85968596
}
85978597

8598+
/// Given the expanded code for a particular macro, perform whitespace
8599+
/// adjustments to make the refactoring more.
8600+
static StringRef adjustMacroExpansionWhitespace(
8601+
GeneratedSourceInfo::Kind kind, StringRef expandedCode,
8602+
llvm::SmallString<64> &scratch
8603+
) {
8604+
scratch.clear();
8605+
8606+
switch (kind) {
8607+
case GeneratedSourceInfo::ExpressionMacroExpansion:
8608+
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
8609+
return expandedCode;
8610+
8611+
case GeneratedSourceInfo::AccessorMacroExpansion:
8612+
// For accessor macros, wrap curly braces around the buffer contents.
8613+
scratch += "{\n";
8614+
scratch += expandedCode;
8615+
scratch += "\n}";
8616+
return scratch;
8617+
8618+
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
8619+
// For member-attribute macros, add a space at the end.
8620+
scratch += expandedCode;
8621+
scratch += " ";
8622+
return scratch;
8623+
8624+
case GeneratedSourceInfo::PeerMacroExpansion:
8625+
// For peers, add a newline to create some separation.
8626+
scratch += "\n";
8627+
LLVM_FALLTHROUGH;
8628+
8629+
case GeneratedSourceInfo::MemberMacroExpansion:
8630+
// For members, add a newline.
8631+
scratch += "\n";
8632+
scratch += expandedCode;
8633+
scratch += "\n";
8634+
return scratch;
8635+
8636+
case GeneratedSourceInfo::ReplacedFunctionBody:
8637+
case GeneratedSourceInfo::PrettyPrinted:
8638+
return expandedCode;
8639+
}
8640+
}
8641+
85988642
bool RefactoringActionExpandMacro::performChange() {
85998643
auto bufferIDs = getMacroExpansionBuffers(SM, CursorInfo);
86008644
if (bufferIDs.empty())
@@ -8637,15 +8681,11 @@ bool RefactoringActionExpandMacro::performChange() {
86378681

86388682
auto afterLeftBraceLoc = Lexer::getLocForEndOfToken(SM, leftBraceLoc);
86398683
originalSourceRange = CharSourceRange(afterLeftBraceLoc, 0);
8640-
} else if (generatedInfo->kind ==
8641-
GeneratedSourceInfo::AccessorMacroExpansion) {
8642-
// For accessor macros, wrap curly braces around the buffer contents.
8643-
scratchBuffer += "{\n";
8644-
scratchBuffer += rewrittenBuffer;
8645-
scratchBuffer += "\n}";
8646-
rewrittenBuffer = scratchBuffer;
86478684
}
86488685

8686+
rewrittenBuffer = adjustMacroExpansionWhitespace(
8687+
generatedInfo->kind, rewrittenBuffer, scratchBuffer);
8688+
86498689
EditConsumer.accept(SM, originalSourceRange, rewrittenBuffer);
86508690

86518691
if (generatedInfo->attachedMacroCustomAttr && !attachedMacroAttr)

test/SourceKit/Macros/macro_basic.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ struct S3 {
106106
//##-- Refactoring expanding the attached macro
107107
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=21:2 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=ATTACHED_EXPAND %s
108108
// ATTACHED_EXPAND: source.edit.kind.active:
109-
// ATTACHED_EXPAND: 23:3-23:3 "@accessViaStorage"
109+
// ATTACHED_EXPAND: 23:3-23:3 "@accessViaStorage "
110110
// ATTACHED_EXPAND: source.edit.kind.active:
111-
// ATTACHED_EXPAND: 24:3-24:3 "@accessViaStorage"
111+
// ATTACHED_EXPAND: 24:3-24:3 "@accessViaStorage "
112112
// ATTACHED_EXPAND: source.edit.kind.active:
113-
// ATTACHED_EXPAND: 22:11-22:11 "private var _storage = _Storage()"
113+
// ATTACHED_EXPAND: 22:11-22:11 "
114+
// ATTACHED_EXPAND: private var _storage = _Storage()
114115
// ATTACHED_EXPAND: source.edit.kind.active:
115116
// ATTACHED_EXPAND: 21:1-21:15 ""
116117

@@ -137,10 +138,11 @@ struct S3 {
137138
//##-- Refactoring expanding the second accessor macro
138139
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=42:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=PEER_EXPAND %s
139140
// PEER_EXPAND: source.edit.kind.active:
140-
// PEER_EXPAND: 45:4-45:4 "func f(a: Int, for b: String, _ value: Double, completionHandler: (String) -> Void) {
141+
// PEER_EXPAND: 45:4-45:4 "
142+
// PEER_EXPAND: func f(a: Int, for b: String, _ value: Double, completionHandler: (String) -> Void) {
141143
// PEER_EXPAND: Task {
142144
// PEER_EXPAND: completionHandler(await f(a: a, for: b, value))
143145
// PEER_EXPAND: }
144-
// PEER_EXPAND: }"
146+
// PEER_EXPAND: }
145147
// PEER_EXPAND: source.edit.kind.active:
146148
// PEER_EXPAND: 42:3-42:24 ""

0 commit comments

Comments
 (0)