@@ -8756,18 +8756,53 @@ getMacroExpansionBuffers(SourceManager &sourceMgr, ResolvedCursorInfoPtr Info) {
87568756 return {};
87578757}
87588758
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);
87668796 if (bufferIDs.empty ())
87678797 return true ;
87688798
8799+ SourceFile *containingSF = cursorInfo->getSourceFile ();
8800+ if (!containingSF)
8801+ return true ;
8802+
87698803 // Send all of the rewritten buffer snippets.
87708804 CustomAttr *attachedMacroAttr = nullptr ;
8805+ SmallString<64 > scratchBuffer;
87718806 for (auto bufferID: bufferIDs) {
87728807 auto generatedInfo = SM.getGeneratedSourceInfo (bufferID);
87738808 if (!generatedInfo || generatedInfo->originalSourceRange .isInvalid ())
@@ -8781,8 +8816,12 @@ bool RefactoringActionExpandMacro::performChange() {
87818816 rewrittenBuffer.empty ())
87828817 continue ;
87838818
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:
87868825 // ```
87878826 // // m.swift
87888827 // @AddMemberAttributes
@@ -8801,14 +8840,14 @@ bool RefactoringActionExpandMacro::performChange() {
88018840 // expansion.
88028841 auto originalSourceRange = generatedInfo->originalSourceRange ;
88038842 SourceFile *originalFile =
8804- MD ->getSourceFileContainingLocation (originalSourceRange.getStart ());
8843+ containingSF-> getParentModule () ->getSourceFileContainingLocation (originalSourceRange.getStart ());
88058844 StringRef originalPath;
88068845 if (originalFile->getBufferID ().hasValue () &&
8807- TheFile ->getBufferID () != originalFile->getBufferID ()) {
8846+ containingSF ->getBufferID () != originalFile->getBufferID ()) {
88088847 originalPath = SM.getIdentifierForBuffer (*originalFile->getBufferID ());
88098848 }
88108849
8811- EditConsumer .accept (SM, {originalPath,
8850+ editConsumer .accept (SM, {originalPath,
88128851 originalSourceRange,
88138852 SM.getIdentifierForBuffer (bufferID),
88148853 rewrittenBuffer,
@@ -8823,12 +8862,31 @@ bool RefactoringActionExpandMacro::performChange() {
88238862 if (attachedMacroAttr) {
88248863 SourceRange range = attachedMacroAttr->getRangeWithAt ();
88258864 auto charRange = Lexer::getCharSourceRangeFromSourceRange (SM, range);
8826- EditConsumer .accept (SM, charRange, StringRef ());
8865+ editConsumer .accept (SM, charRange, StringRef ());
88278866 }
88288867
88298868 return false ;
88308869}
88318870
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+
88328890} // end of anonymous namespace
88338891
88348892StringRef swift::ide::
@@ -8940,8 +8998,8 @@ swift::ide::collectRefactorings(ResolvedCursorInfoPtr CursorInfo,
89408998
89418999 // Only macro expansion is available within generated buffers
89429000 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 ,
89459003 RefactorAvailableKind::Available);
89469004 }
89479005 return Infos;
@@ -9019,6 +9077,7 @@ refactorSwiftModule(ModuleDecl *M, RefactoringOptions Opts,
90199077case RefactoringKind::KIND: { \
90209078 RefactoringAction##KIND Action (M, Opts, EditConsumer, DiagConsumer); \
90219079 if (RefactoringKind::KIND == RefactoringKind::LocalRename || \
9080+ RefactoringKind::KIND == RefactoringKind::ExpandMacro || \
90229081 Action.isApplicable ()) \
90239082 return Action.performChange (); \
90249083 return true ; \
0 commit comments