Skip to content

Commit faa8fd3

Browse files
committed
[CodeCompletion] Move CodeCompletionContext.h to its own file
1 parent 85bf521 commit faa8fd3

File tree

5 files changed

+148
-105
lines changed

5 files changed

+148
-105
lines changed

Diff for: include/swift/IDE/CodeCompletion.h

+1-81
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/Basic/OptionSet.h"
2121
#include "swift/Basic/StringExtras.h"
2222
#include "swift/Frontend/Frontend.h"
23+
#include "swift/IDE/CodeCompletionContext.h"
2324
#include "swift/IDE/CodeCompletionResult.h"
2425
#include "swift/IDE/CodeCompletionResultSink.h"
2526
#include "swift/IDE/CodeCompletionString.h"
@@ -96,87 +97,6 @@ class ImportDepth {
9697
}
9798
};
9899

99-
class CodeCompletionContext {
100-
friend class CodeCompletionResultBuilder;
101-
102-
/// A set of current completion results.
103-
CodeCompletionResultSink CurrentResults;
104-
105-
public:
106-
CodeCompletionCache &Cache;
107-
CompletionKind CodeCompletionKind = CompletionKind::None;
108-
109-
enum class TypeContextKind {
110-
/// There is no known contextual type. All types are equally good.
111-
None,
112-
113-
/// There is a contextual type from a single-expression closure/function
114-
/// body. The context is a hint, and enables unresolved member completion,
115-
/// but should not hide any results.
116-
SingleExpressionBody,
117-
118-
/// There are known contextual types, or there aren't but a nonvoid type is expected.
119-
Required,
120-
};
121-
122-
TypeContextKind typeContextKind = TypeContextKind::None;
123-
124-
/// Whether there may be members that can use implicit member syntax,
125-
/// e.g. `x = .foo`.
126-
bool MayUseImplicitMemberExpr = false;
127-
128-
/// Flag to indicate that the completion is happening reusing ASTContext
129-
/// from the previous completion.
130-
/// NOTE: Do not use this to change the behavior. This is only for debugging.
131-
bool ReusingASTContext = false;
132-
133-
CodeCompletionContext(CodeCompletionCache &Cache)
134-
: Cache(Cache) {}
135-
136-
void setAnnotateResult(bool flag) { CurrentResults.annotateResult = flag; }
137-
bool getAnnotateResult() const { return CurrentResults.annotateResult; }
138-
139-
void setIncludeObjectLiterals(bool flag) {
140-
CurrentResults.includeObjectLiterals = flag;
141-
}
142-
bool includeObjectLiterals() const {
143-
return CurrentResults.includeObjectLiterals;
144-
}
145-
146-
void setAddInitsToTopLevel(bool flag) {
147-
CurrentResults.addInitsToTopLevel = flag;
148-
}
149-
bool getAddInitsToTopLevel() const {
150-
return CurrentResults.addInitsToTopLevel;
151-
}
152-
153-
void setCallPatternHeuristics(bool flag) {
154-
CurrentResults.enableCallPatternHeuristics = flag;
155-
}
156-
bool getCallPatternHeuristics() const {
157-
return CurrentResults.enableCallPatternHeuristics;
158-
}
159-
160-
void setAddCallWithNoDefaultArgs(bool flag) {
161-
CurrentResults.addCallWithNoDefaultArgs = flag;
162-
}
163-
bool addCallWithNoDefaultArgs() const {
164-
return CurrentResults.addCallWithNoDefaultArgs;
165-
}
166-
167-
/// Allocate a string owned by the code completion context.
168-
StringRef copyString(StringRef Str);
169-
170-
/// Sort code completion results in an implementation-defined order
171-
/// in place.
172-
static std::vector<CodeCompletionResult *>
173-
sortCompletionResults(ArrayRef<CodeCompletionResult *> Results);
174-
175-
CodeCompletionResultSink &getResultSink() {
176-
return CurrentResults;
177-
}
178-
};
179-
180100
struct SwiftCompletionInfo {
181101
swift::ASTContext *swiftASTContext = nullptr;
182102
const swift::CompilerInvocation *invocation = nullptr;

Diff for: include/swift/IDE/CodeCompletionContext.h

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Diff for: lib/IDE/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
add_swift_host_library(swiftIDE STATIC
33
CodeCompletion.cpp
44
CodeCompletionCache.cpp
5+
CodeCompletionContext.cpp
56
CodeCompletionDiagnostics.cpp
67
CodeCompletionResultBuilder.cpp
78
CodeCompletionResultPrinter.cpp

Diff for: lib/IDE/CodeCompletion.cpp

-24
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,6 @@ std::string swift::ide::removeCodeCompletionTokens(
9595
return CleanFile;
9696
}
9797

98-
StringRef CodeCompletionContext::copyString(StringRef Str) {
99-
return Str.copy(*CurrentResults.Allocator);
100-
}
101-
102-
std::vector<CodeCompletionResult *>
103-
CodeCompletionContext::sortCompletionResults(
104-
ArrayRef<CodeCompletionResult *> Results) {
105-
std::vector<CodeCompletionResult *> SortedResults(Results.begin(),
106-
Results.end());
107-
108-
std::sort(SortedResults.begin(), SortedResults.end(),
109-
[](const auto &LHS, const auto &RHS) {
110-
int Result = StringRef(LHS->getFilterName())
111-
.compare_insensitive(RHS->getFilterName());
112-
// If the case insensitive comparison is equal, then secondary
113-
// sort order should be case sensitive.
114-
if (Result == 0)
115-
Result = LHS->getFilterName().compare(RHS->getFilterName());
116-
return Result < 0;
117-
});
118-
119-
return SortedResults;
120-
}
121-
12298
namespace {
12399

124100
class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {

Diff for: lib/IDE/CodeCompletionContext.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- CodeCompletionContext.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/IDE/CodeCompletionContext.h"
14+
15+
using namespace swift;
16+
using namespace swift::ide;
17+
18+
StringRef CodeCompletionContext::copyString(StringRef Str) {
19+
return Str.copy(*CurrentResults.Allocator);
20+
}
21+
22+
std::vector<CodeCompletionResult *>
23+
CodeCompletionContext::sortCompletionResults(
24+
ArrayRef<CodeCompletionResult *> Results) {
25+
std::vector<CodeCompletionResult *> SortedResults(Results.begin(),
26+
Results.end());
27+
28+
std::sort(SortedResults.begin(), SortedResults.end(),
29+
[](const auto &LHS, const auto &RHS) {
30+
int Result = StringRef(LHS->getFilterName())
31+
.compare_insensitive(RHS->getFilterName());
32+
// If the case insensitive comparison is equal, then secondary
33+
// sort order should be case sensitive.
34+
if (Result == 0)
35+
Result = LHS->getFilterName().compare(RHS->getFilterName());
36+
return Result < 0;
37+
});
38+
39+
return SortedResults;
40+
}

0 commit comments

Comments
 (0)