|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "RefactoringActions.h" |
| 14 | + |
| 15 | +using namespace swift::refactoring; |
| 16 | + |
| 17 | +bool RefactoringActionMoveMembersToExtension::isApplicable( |
| 18 | + const ResolvedRangeInfo &Info, DiagnosticEngine &Diag) { |
| 19 | + switch (Info.Kind) { |
| 20 | + case RangeKind::SingleDecl: |
| 21 | + case RangeKind::MultiTypeMemberDecl: { |
| 22 | + DeclContext *DC = Info.RangeContext; |
| 23 | + |
| 24 | + // The the common decl context is not a nomial type, we cannot create an |
| 25 | + // extension for it |
| 26 | + if (!DC || !DC->getInnermostDeclarationDeclContext() || |
| 27 | + !isa<NominalTypeDecl>(DC->getInnermostDeclarationDeclContext())) |
| 28 | + return false; |
| 29 | + |
| 30 | + // Members of types not declared at top file level cannot be extracted |
| 31 | + // to an extension at top file level |
| 32 | + if (DC->getParent()->getContextKind() != DeclContextKind::FileUnit) |
| 33 | + return false; |
| 34 | + |
| 35 | + // Check if contained nodes are all allowed decls. |
| 36 | + for (auto Node : Info.ContainedNodes) { |
| 37 | + Decl *D = Node.dyn_cast<Decl *>(); |
| 38 | + if (!D) |
| 39 | + return false; |
| 40 | + |
| 41 | + if (isa<AccessorDecl>(D) || isa<DestructorDecl>(D) || |
| 42 | + isa<EnumCaseDecl>(D) || isa<EnumElementDecl>(D)) |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + // We should not move instance variables with storage into the extension |
| 47 | + // because they are not allowed to be declared there |
| 48 | + for (auto DD : Info.DeclaredDecls) { |
| 49 | + if (auto ASD = dyn_cast<AbstractStorageDecl>(DD.VD)) { |
| 50 | + // Only disallow storages in the common decl context, allow them in |
| 51 | + // any subtypes |
| 52 | + if (ASD->hasStorage() && ASD->getDeclContext() == DC) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return true; |
| 59 | + } |
| 60 | + case RangeKind::SingleExpression: |
| 61 | + case RangeKind::PartOfExpression: |
| 62 | + case RangeKind::SingleStatement: |
| 63 | + case RangeKind::MultiStatement: |
| 64 | + case RangeKind::Invalid: |
| 65 | + return false; |
| 66 | + } |
| 67 | + llvm_unreachable("unhandled kind"); |
| 68 | +} |
| 69 | + |
| 70 | +bool RefactoringActionMoveMembersToExtension::performChange() { |
| 71 | + DeclContext *DC = RangeInfo.RangeContext; |
| 72 | + |
| 73 | + auto CommonTypeDecl = |
| 74 | + dyn_cast<NominalTypeDecl>(DC->getInnermostDeclarationDeclContext()); |
| 75 | + assert(CommonTypeDecl && "Not applicable if common parent is no nomial type"); |
| 76 | + |
| 77 | + SmallString<64> Buffer; |
| 78 | + llvm::raw_svector_ostream OS(Buffer); |
| 79 | + OS << "\n\n"; |
| 80 | + OS << "extension " << CommonTypeDecl->getName() << " {\n"; |
| 81 | + OS << RangeInfo.ContentRange.str().trim(); |
| 82 | + OS << "\n}"; |
| 83 | + |
| 84 | + // Insert extension after the type declaration |
| 85 | + EditConsumer.insertAfter(SM, CommonTypeDecl->getEndLoc(), Buffer); |
| 86 | + EditConsumer.remove(SM, RangeInfo.ContentRange); |
| 87 | + |
| 88 | + return false; |
| 89 | +} |
0 commit comments