|
| 1 | +//===--- KeyPathCompletion.cpp --------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 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/IDE/KeyPathCompletion.h" |
| 14 | +#include "swift/IDE/CodeCompletion.h" |
| 15 | +#include "swift/IDE/CompletionLookup.h" |
| 16 | +#include "swift/Sema/ConstraintSystem.h" |
| 17 | + |
| 18 | +using namespace swift; |
| 19 | +using namespace swift::constraints; |
| 20 | +using namespace swift::ide; |
| 21 | + |
| 22 | +void KeyPathTypeCheckCompletionCallback::sawSolution( |
| 23 | + const constraints::Solution &S) { |
| 24 | + // Determine the code completion. |
| 25 | + size_t ComponentIndex = 0; |
| 26 | + for (auto &Component : KeyPath->getComponents()) { |
| 27 | + if (Component.getKind() == KeyPathExpr::Component::Kind::CodeCompletion) { |
| 28 | + break; |
| 29 | + } else { |
| 30 | + ComponentIndex++; |
| 31 | + } |
| 32 | + } |
| 33 | + assert(ComponentIndex < KeyPath->getComponents().size() && |
| 34 | + "Didn't find a code compleiton component?"); |
| 35 | + |
| 36 | + Type BaseType; |
| 37 | + if (ComponentIndex == 0) { |
| 38 | + // We are completing on the root and need to extract the key path's root |
| 39 | + // type. |
| 40 | + if (KeyPath->getRootType()) { |
| 41 | + BaseType = S.getResolvedType(KeyPath->getRootType()); |
| 42 | + } else { |
| 43 | + // The key path doesn't have a root TypeRepr set, so we can't look the key |
| 44 | + // path's root up through it. Build a constraint locator and look the |
| 45 | + // root type up through it. |
| 46 | + // FIXME: Improve the linear search over S.typeBindings when it's possible |
| 47 | + // to look up type variables by their locators. |
| 48 | + auto RootLocator = |
| 49 | + S.getConstraintLocator(KeyPath, {ConstraintLocator::KeyPathRoot}); |
| 50 | + auto BaseVariableTypeBinding = |
| 51 | + llvm::find_if(S.typeBindings, [&RootLocator](const auto &Entry) { |
| 52 | + return Entry.first->getImpl().getLocator() == RootLocator; |
| 53 | + }); |
| 54 | + if (BaseVariableTypeBinding != S.typeBindings.end()) { |
| 55 | + BaseType = S.simplifyType(BaseVariableTypeBinding->getSecond()); |
| 56 | + } |
| 57 | + } |
| 58 | + } else { |
| 59 | + // We are completing after a component. Get the previous component's result |
| 60 | + // type. |
| 61 | + BaseType = S.simplifyType(S.getType(KeyPath, ComponentIndex - 1)); |
| 62 | + } |
| 63 | + if (BaseType.isNull()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // If ExpectedTy is a duplicate of any other result, ignore this solution. |
| 68 | + if (llvm::any_of(Results, [&](const Result &R) { |
| 69 | + return R.BaseType->isEqual(BaseType); |
| 70 | + })) { |
| 71 | + return; |
| 72 | + } |
| 73 | + Results.push_back({BaseType, /*OnRoot=*/(ComponentIndex == 0)}); |
| 74 | +} |
| 75 | + |
| 76 | +void swift::ide::deliverKeyPathResults( |
| 77 | + ArrayRef<KeyPathTypeCheckCompletionCallback::Result> Results, |
| 78 | + DeclContext *DC, SourceLoc DotLoc, |
| 79 | + ide::CodeCompletionContext &CompletionCtx, |
| 80 | + CodeCompletionConsumer &Consumer) { |
| 81 | + ASTContext &Ctx = DC->getASTContext(); |
| 82 | + CompletionLookup Lookup(CompletionCtx.getResultSink(), Ctx, DC, |
| 83 | + &CompletionCtx); |
| 84 | + |
| 85 | + if (DotLoc.isValid()) { |
| 86 | + Lookup.setHaveDot(DotLoc); |
| 87 | + } |
| 88 | + Lookup.shouldCheckForDuplicates(Results.size() > 1); |
| 89 | + |
| 90 | + for (auto &Result : Results) { |
| 91 | + Lookup.setIsSwiftKeyPathExpr(Result.OnRoot); |
| 92 | + Lookup.getValueExprCompletions(Result.BaseType); |
| 93 | + } |
| 94 | + |
| 95 | + deliverCompletionResults(CompletionCtx, Lookup, DC, Consumer); |
| 96 | +} |
0 commit comments