|
| 1 | +//===--- CodeCompletionContext.h ------------------------------------------===// |
| 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 | +#ifndef SWIFT_IDE_CODECOMPLETIONCONTEXT |
| 14 | +#define SWIFT_IDE_CODECOMPLETIONCONTEXT |
| 15 | + |
| 16 | +#include "swift/IDE/CodeCompletionResult.h" |
| 17 | +#include "swift/IDE/CodeCompletionResultSink.h" |
| 18 | + |
| 19 | +namespace swift { |
| 20 | +namespace ide { |
| 21 | + |
| 22 | +class CodeCompletionCache; |
| 23 | + |
| 24 | +class CodeCompletionContext { |
| 25 | + friend class CodeCompletionResultBuilder; |
| 26 | + |
| 27 | + /// A set of current completion results. |
| 28 | + CodeCompletionResultSink CurrentResults; |
| 29 | + |
| 30 | +public: |
| 31 | + CodeCompletionCache &Cache; |
| 32 | + CompletionKind CodeCompletionKind = CompletionKind::None; |
| 33 | + |
| 34 | + enum class TypeContextKind { |
| 35 | + /// There is no known contextual type. All types are equally good. |
| 36 | + None, |
| 37 | + |
| 38 | + /// There is a contextual type from a single-expression closure/function |
| 39 | + /// body. The context is a hint, and enables unresolved member completion, |
| 40 | + /// but should not hide any results. |
| 41 | + SingleExpressionBody, |
| 42 | + |
| 43 | + /// There are known contextual types, or there aren't but a nonvoid type is |
| 44 | + /// expected. |
| 45 | + Required, |
| 46 | + }; |
| 47 | + |
| 48 | + TypeContextKind typeContextKind = TypeContextKind::None; |
| 49 | + |
| 50 | + /// Whether there may be members that can use implicit member syntax, |
| 51 | + /// e.g. `x = .foo`. |
| 52 | + bool MayUseImplicitMemberExpr = false; |
| 53 | + |
| 54 | + /// Flag to indicate that the completion is happening reusing ASTContext |
| 55 | + /// from the previous completion. |
| 56 | + /// NOTE: Do not use this to change the behavior. This is only for debugging. |
| 57 | + bool ReusingASTContext = false; |
| 58 | + |
| 59 | + CodeCompletionContext(CodeCompletionCache &Cache) : Cache(Cache) {} |
| 60 | + |
| 61 | + void setAnnotateResult(bool flag) { CurrentResults.annotateResult = flag; } |
| 62 | + bool getAnnotateResult() const { return CurrentResults.annotateResult; } |
| 63 | + |
| 64 | + void setIncludeObjectLiterals(bool flag) { |
| 65 | + CurrentResults.includeObjectLiterals = flag; |
| 66 | + } |
| 67 | + bool includeObjectLiterals() const { |
| 68 | + return CurrentResults.includeObjectLiterals; |
| 69 | + } |
| 70 | + |
| 71 | + void setAddInitsToTopLevel(bool flag) { |
| 72 | + CurrentResults.addInitsToTopLevel = flag; |
| 73 | + } |
| 74 | + bool getAddInitsToTopLevel() const { |
| 75 | + return CurrentResults.addInitsToTopLevel; |
| 76 | + } |
| 77 | + |
| 78 | + void setCallPatternHeuristics(bool flag) { |
| 79 | + CurrentResults.enableCallPatternHeuristics = flag; |
| 80 | + } |
| 81 | + bool getCallPatternHeuristics() const { |
| 82 | + return CurrentResults.enableCallPatternHeuristics; |
| 83 | + } |
| 84 | + |
| 85 | + void setAddCallWithNoDefaultArgs(bool flag) { |
| 86 | + CurrentResults.addCallWithNoDefaultArgs = flag; |
| 87 | + } |
| 88 | + bool addCallWithNoDefaultArgs() const { |
| 89 | + return CurrentResults.addCallWithNoDefaultArgs; |
| 90 | + } |
| 91 | + |
| 92 | + /// Allocate a string owned by the code completion context. |
| 93 | + StringRef copyString(StringRef Str); |
| 94 | + |
| 95 | + /// Sort code completion results in an implementation-defined order |
| 96 | + /// in place. |
| 97 | + static std::vector<CodeCompletionResult *> |
| 98 | + sortCompletionResults(ArrayRef<CodeCompletionResult *> Results); |
| 99 | + |
| 100 | + CodeCompletionResultSink &getResultSink() { return CurrentResults; } |
| 101 | +}; |
| 102 | + |
| 103 | +} // end namespace ide |
| 104 | +} // end namespace swift |
| 105 | + |
| 106 | +#endif // SWIFT_IDE_CODECOMPLETIONCONTEXT |
0 commit comments