@@ -8756,18 +8756,53 @@ getMacroExpansionBuffers(SourceManager &sourceMgr, ResolvedCursorInfoPtr Info) {
8756
8756
return {};
8757
8757
}
8758
8758
8759
- bool RefactoringActionExpandMacro::isApplicable (ResolvedCursorInfoPtr Info,
8760
- DiagnosticEngine &Diag) {
8761
- return !getMacroExpansionBuffers (Diag.SourceMgr , Info).empty ();
8762
- }
8763
-
8764
- bool RefactoringActionExpandMacro::performChange () {
8765
- auto bufferIDs = getMacroExpansionBuffers (SM, CursorInfo);
8759
+ // / Given the expanded code for a particular macro, perform whitespace
8760
+ // / adjustments to make the refactoring more suitable for inline insertion.
8761
+ static StringRef adjustMacroExpansionWhitespace (
8762
+ GeneratedSourceInfo::Kind kind, StringRef expandedCode,
8763
+ llvm::SmallString<64 > &scratch) {
8764
+ scratch.clear ();
8765
+
8766
+ switch (kind) {
8767
+ case GeneratedSourceInfo::MemberAttributeMacroExpansion:
8768
+ // Attributes are added to the beginning, add a space to separate from
8769
+ // any existing.
8770
+ scratch += expandedCode;
8771
+ scratch += " " ;
8772
+ return scratch;
8773
+
8774
+ case GeneratedSourceInfo::MemberMacroExpansion:
8775
+ case GeneratedSourceInfo::PeerMacroExpansion:
8776
+ case GeneratedSourceInfo::ConformanceMacroExpansion:
8777
+ // All added to the end. Note that conformances are always expanded as
8778
+ // extensions, hence treating them the same as peer.
8779
+ scratch += " \n\n " ;
8780
+ scratch += expandedCode;
8781
+ scratch += " \n " ;
8782
+ return scratch;
8783
+
8784
+ case GeneratedSourceInfo::ExpressionMacroExpansion:
8785
+ case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
8786
+ case GeneratedSourceInfo::AccessorMacroExpansion:
8787
+ case GeneratedSourceInfo::ReplacedFunctionBody:
8788
+ case GeneratedSourceInfo::PrettyPrinted:
8789
+ return expandedCode;
8790
+ }
8791
+ }
8792
+
8793
+ static bool expandMacro (SourceManager &SM, ResolvedCursorInfoPtr cursorInfo,
8794
+ SourceEditConsumer &editConsumer, bool adjustExpansion) {
8795
+ auto bufferIDs = getMacroExpansionBuffers (SM, cursorInfo);
8766
8796
if (bufferIDs.empty ())
8767
8797
return true ;
8768
8798
8799
+ SourceFile *containingSF = cursorInfo->getSourceFile ();
8800
+ if (!containingSF)
8801
+ return true ;
8802
+
8769
8803
// Send all of the rewritten buffer snippets.
8770
8804
CustomAttr *attachedMacroAttr = nullptr ;
8805
+ SmallString<64 > scratchBuffer;
8771
8806
for (auto bufferID: bufferIDs) {
8772
8807
auto generatedInfo = SM.getGeneratedSourceInfo (bufferID);
8773
8808
if (!generatedInfo || generatedInfo->originalSourceRange .isInvalid ())
@@ -8781,8 +8816,12 @@ bool RefactoringActionExpandMacro::performChange() {
8781
8816
rewrittenBuffer.empty ())
8782
8817
continue ;
8783
8818
8784
- // `TheFile` is the file of the actual expansion site, where as
8785
- // `OriginalFile` is the possibly enclosing buffer. Concretely:
8819
+ if (adjustExpansion) {
8820
+ rewrittenBuffer = adjustMacroExpansionWhitespace (generatedInfo->kind , rewrittenBuffer, scratchBuffer);
8821
+ }
8822
+
8823
+ // `containingFile` is the file of the actual expansion site, where as
8824
+ // `originalFile` is the possibly enclosing buffer. Concretely:
8786
8825
// ```
8787
8826
// // m.swift
8788
8827
// @AddMemberAttributes
@@ -8801,14 +8840,14 @@ bool RefactoringActionExpandMacro::performChange() {
8801
8840
// expansion.
8802
8841
auto originalSourceRange = generatedInfo->originalSourceRange ;
8803
8842
SourceFile *originalFile =
8804
- MD ->getSourceFileContainingLocation (originalSourceRange.getStart ());
8843
+ containingSF-> getParentModule () ->getSourceFileContainingLocation (originalSourceRange.getStart ());
8805
8844
StringRef originalPath;
8806
8845
if (originalFile->getBufferID ().hasValue () &&
8807
- TheFile ->getBufferID () != originalFile->getBufferID ()) {
8846
+ containingSF ->getBufferID () != originalFile->getBufferID ()) {
8808
8847
originalPath = SM.getIdentifierForBuffer (*originalFile->getBufferID ());
8809
8848
}
8810
8849
8811
- EditConsumer .accept (SM, {originalPath,
8850
+ editConsumer .accept (SM, {originalPath,
8812
8851
originalSourceRange,
8813
8852
SM.getIdentifierForBuffer (bufferID),
8814
8853
rewrittenBuffer,
@@ -8823,12 +8862,31 @@ bool RefactoringActionExpandMacro::performChange() {
8823
8862
if (attachedMacroAttr) {
8824
8863
SourceRange range = attachedMacroAttr->getRangeWithAt ();
8825
8864
auto charRange = Lexer::getCharSourceRangeFromSourceRange (SM, range);
8826
- EditConsumer .accept (SM, charRange, StringRef ());
8865
+ editConsumer .accept (SM, charRange, StringRef ());
8827
8866
}
8828
8867
8829
8868
return false ;
8830
8869
}
8831
8870
8871
+ bool RefactoringActionExpandMacro::isApplicable (ResolvedCursorInfoPtr Info,
8872
+ DiagnosticEngine &Diag) {
8873
+ // Never list in available refactorings. Only allow requesting directly.
8874
+ return false ;
8875
+ }
8876
+
8877
+ bool RefactoringActionExpandMacro::performChange () {
8878
+ return expandMacro (SM, CursorInfo, EditConsumer, /* adjustExpansion=*/ false );
8879
+ }
8880
+
8881
+ bool RefactoringActionInlineMacro::isApplicable (ResolvedCursorInfoPtr Info,
8882
+ DiagnosticEngine &Diag) {
8883
+ return !getMacroExpansionBuffers (Diag.SourceMgr , Info).empty ();
8884
+ }
8885
+
8886
+ bool RefactoringActionInlineMacro::performChange () {
8887
+ return expandMacro (SM, CursorInfo, EditConsumer, /* adjustExpansion=*/ true );
8888
+ }
8889
+
8832
8890
} // end of anonymous namespace
8833
8891
8834
8892
StringRef swift::ide::
@@ -8940,8 +8998,8 @@ swift::ide::collectRefactorings(ResolvedCursorInfoPtr CursorInfo,
8940
8998
8941
8999
// Only macro expansion is available within generated buffers
8942
9000
if (CursorInfo->getSourceFile ()->Kind == SourceFileKind::MacroExpansion) {
8943
- if (RefactoringActionExpandMacro ::isApplicable (CursorInfo, DiagEngine)) {
8944
- Infos.emplace_back (RefactoringKind::ExpandMacro ,
9001
+ if (RefactoringActionInlineMacro ::isApplicable (CursorInfo, DiagEngine)) {
9002
+ Infos.emplace_back (RefactoringKind::InlineMacro ,
8945
9003
RefactorAvailableKind::Available);
8946
9004
}
8947
9005
return Infos;
@@ -9019,6 +9077,7 @@ refactorSwiftModule(ModuleDecl *M, RefactoringOptions Opts,
9019
9077
case RefactoringKind::KIND: { \
9020
9078
RefactoringAction##KIND Action (M, Opts, EditConsumer, DiagConsumer); \
9021
9079
if (RefactoringKind::KIND == RefactoringKind::LocalRename || \
9080
+ RefactoringKind::KIND == RefactoringKind::ExpandMacro || \
9022
9081
Action.isApplicable ()) \
9023
9082
return Action.performChange (); \
9024
9083
return true ; \
0 commit comments