|
| 1 | +//===--- CompletionContextFinder.cpp --------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 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 "swift/Sema/CompletionContextFinder.h" |
| 14 | + |
| 15 | +using namespace swift; |
| 16 | +using Fallback = CompletionContextFinder::Fallback; |
| 17 | + |
| 18 | +std::pair<bool, Expr *> CompletionContextFinder::walkToExprPre(Expr *E) { |
| 19 | + if (auto *closure = dyn_cast<ClosureExpr>(E)) { |
| 20 | + Contexts.push_back({closure->hasSingleExpressionBody() |
| 21 | + ? ContextKind::SingleStmtClosure |
| 22 | + : ContextKind::MultiStmtClosure, |
| 23 | + closure}); |
| 24 | + } |
| 25 | + |
| 26 | + if (isa<InterpolatedStringLiteralExpr>(E)) { |
| 27 | + Contexts.push_back({ContextKind::StringInterpolation, E}); |
| 28 | + } |
| 29 | + |
| 30 | + if (isa<ApplyExpr>(E) || isa<SequenceExpr>(E)) { |
| 31 | + Contexts.push_back({ContextKind::FallbackExpression, E}); |
| 32 | + } |
| 33 | + |
| 34 | + if (auto *Error = dyn_cast<ErrorExpr>(E)) { |
| 35 | + Contexts.push_back({ContextKind::ErrorExpression, E}); |
| 36 | + if (auto *OrigExpr = Error->getOriginalExpr()) { |
| 37 | + OrigExpr->walk(*this); |
| 38 | + if (hasCompletionExpr()) |
| 39 | + return std::make_pair(false, nullptr); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (auto *CCE = dyn_cast<CodeCompletionExpr>(E)) { |
| 44 | + CompletionNode = CCE; |
| 45 | + return std::make_pair(false, nullptr); |
| 46 | + } |
| 47 | + if (auto *KeyPath = dyn_cast<KeyPathExpr>(E)) { |
| 48 | + for (auto &component : KeyPath->getComponents()) { |
| 49 | + if (component.getKind() == KeyPathExpr::Component::Kind::CodeCompletion) { |
| 50 | + CompletionNode = KeyPath; |
| 51 | + return std::make_pair(false, nullptr); |
| 52 | + } |
| 53 | + } |
| 54 | + // Code completion in key paths is modelled by a code completion component |
| 55 | + // Don't walk the key path's parsed expressions. |
| 56 | + return std::make_pair(false, E); |
| 57 | + } |
| 58 | + |
| 59 | + return std::make_pair(true, E); |
| 60 | +} |
| 61 | + |
| 62 | +Expr *CompletionContextFinder::walkToExprPost(Expr *E) { |
| 63 | + if (isa<ClosureExpr>(E) || isa<InterpolatedStringLiteralExpr>(E) || |
| 64 | + isa<ApplyExpr>(E) || isa<SequenceExpr>(E) || isa<ErrorExpr>(E)) { |
| 65 | + assert(Contexts.back().E == E); |
| 66 | + Contexts.pop_back(); |
| 67 | + } |
| 68 | + return E; |
| 69 | +} |
| 70 | + |
| 71 | +size_t CompletionContextFinder::getKeyPathCompletionComponentIndex() const { |
| 72 | + assert(hasCompletionKeyPathComponent()); |
| 73 | + size_t ComponentIndex = 0; |
| 74 | + auto Components = getKeyPathContainingCompletionComponent()->getComponents(); |
| 75 | + for (auto &Component : Components) { |
| 76 | + if (Component.getKind() == KeyPathExpr::Component::Kind::CodeCompletion) { |
| 77 | + break; |
| 78 | + } else { |
| 79 | + ComponentIndex++; |
| 80 | + } |
| 81 | + } |
| 82 | + assert(ComponentIndex < Components.size() && |
| 83 | + "No completion component in the key path?"); |
| 84 | + return ComponentIndex; |
| 85 | +} |
| 86 | + |
| 87 | +Optional<Fallback> CompletionContextFinder::getFallbackCompletionExpr() const { |
| 88 | + if (!hasCompletionExpr()) { |
| 89 | + // Creating a fallback expression only makes sense if we are completing in |
| 90 | + // an expression, not when we're completing in a key path. |
| 91 | + return None; |
| 92 | + } |
| 93 | + |
| 94 | + Optional<Fallback> fallback; |
| 95 | + bool separatePrecheck = false; |
| 96 | + DeclContext *fallbackDC = InitialDC; |
| 97 | + |
| 98 | + // Find the outermost fallback expression within the innermost error |
| 99 | + // expression or multi-statement closure, keeping track of its decl context. |
| 100 | + for (auto context : Contexts) { |
| 101 | + switch (context.Kind) { |
| 102 | + case ContextKind::StringInterpolation: |
| 103 | + LLVM_FALLTHROUGH; |
| 104 | + case ContextKind::FallbackExpression: |
| 105 | + if (!fallback && context.E != InitialExpr) |
| 106 | + fallback = Fallback{context.E, fallbackDC, separatePrecheck}; |
| 107 | + continue; |
| 108 | + |
| 109 | + case ContextKind::SingleStmtClosure: |
| 110 | + if (!fallback && context.E != InitialExpr) |
| 111 | + fallback = Fallback{context.E, fallbackDC, separatePrecheck}; |
| 112 | + fallbackDC = cast<AbstractClosureExpr>(context.E); |
| 113 | + continue; |
| 114 | + |
| 115 | + case ContextKind::MultiStmtClosure: |
| 116 | + fallbackDC = cast<AbstractClosureExpr>(context.E); |
| 117 | + LLVM_FALLTHROUGH; |
| 118 | + case ContextKind::ErrorExpression:; |
| 119 | + fallback = None; |
| 120 | + separatePrecheck = true; |
| 121 | + continue; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (fallback) |
| 126 | + return fallback; |
| 127 | + |
| 128 | + if (getCompletionExpr()->getBase() && getCompletionExpr() != InitialExpr) |
| 129 | + return Fallback{getCompletionExpr(), fallbackDC, separatePrecheck}; |
| 130 | + return None; |
| 131 | +} |
0 commit comments