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