@@ -8754,18 +8754,53 @@ getMacroExpansionBuffers(SourceManager &sourceMgr, ResolvedCursorInfoPtr Info) {
87548754 return {};
87558755}
87568756
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);
87648794 if (bufferIDs.empty ())
87658795 return true ;
87668796
8797+ SourceFile *containingSF = cursorInfo->getSourceFile ();
8798+ if (!containingSF)
8799+ return true ;
8800+
87678801 // Send all of the rewritten buffer snippets.
87688802 CustomAttr *attachedMacroAttr = nullptr ;
8803+ SmallString<64 > scratchBuffer;
87698804 for (auto bufferID: bufferIDs) {
87708805 auto generatedInfo = SM.getGeneratedSourceInfo (bufferID);
87718806 if (!generatedInfo || generatedInfo->originalSourceRange .isInvalid ())
@@ -8779,8 +8814,12 @@ bool RefactoringActionExpandMacro::performChange() {
87798814 rewrittenBuffer.empty ())
87808815 continue ;
87818816
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:
87848823 // ```
87858824 // // m.swift
87868825 // @AddMemberAttributes
@@ -8799,14 +8838,14 @@ bool RefactoringActionExpandMacro::performChange() {
87998838 // expansion.
88008839 auto originalSourceRange = generatedInfo->originalSourceRange ;
88018840 SourceFile *originalFile =
8802- MD ->getSourceFileContainingLocation (originalSourceRange.getStart ());
8841+ containingSF-> getParentModule () ->getSourceFileContainingLocation (originalSourceRange.getStart ());
88038842 StringRef originalPath;
88048843 if (originalFile->getBufferID ().hasValue () &&
8805- TheFile ->getBufferID () != originalFile->getBufferID ()) {
8844+ containingSF ->getBufferID () != originalFile->getBufferID ()) {
88068845 originalPath = SM.getIdentifierForBuffer (*originalFile->getBufferID ());
88078846 }
88088847
8809- EditConsumer .accept (SM, {originalPath,
8848+ editConsumer .accept (SM, {originalPath,
88108849 originalSourceRange,
88118850 SM.getIdentifierForBuffer (bufferID),
88128851 rewrittenBuffer,
@@ -8821,12 +8860,31 @@ bool RefactoringActionExpandMacro::performChange() {
88218860 if (attachedMacroAttr) {
88228861 SourceRange range = attachedMacroAttr->getRangeWithAt ();
88238862 auto charRange = Lexer::getCharSourceRangeFromSourceRange (SM, range);
8824- EditConsumer .accept (SM, charRange, StringRef ());
8863+ editConsumer .accept (SM, charRange, StringRef ());
88258864 }
88268865
88278866 return false ;
88288867}
88298868
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+
88308888} // end of anonymous namespace
88318889
88328890StringRef swift::ide::
@@ -8938,8 +8996,8 @@ swift::ide::collectRefactorings(ResolvedCursorInfoPtr CursorInfo,
89388996
89398997 // Only macro expansion is available within generated buffers
89408998 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 ,
89439001 RefactorAvailableKind::Available);
89449002 }
89459003 return Infos;
@@ -9017,6 +9075,7 @@ refactorSwiftModule(ModuleDecl *M, RefactoringOptions Opts,
90179075case RefactoringKind::KIND: { \
90189076 RefactoringAction##KIND Action (M, Opts, EditConsumer, DiagConsumer); \
90199077 if (RefactoringKind::KIND == RefactoringKind::LocalRename || \
9078+ RefactoringKind::KIND == RefactoringKind::ExpandMacro || \
90209079 Action.isApplicable ()) \
90219080 return Action.performChange (); \
90229081 return true ; \
0 commit comments