-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathCodeCompletionStringPrinter.cpp
188 lines (163 loc) · 5.78 KB
/
CodeCompletionStringPrinter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//===--- CodeCompletionStringPrinter.cpp ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/IDE/CodeCompletionStringPrinter.h"
#include "CodeCompletionResultBuilder.h"
#include "swift/AST/Module.h"
using namespace swift;
using namespace swift::ide;
using ChunkKind = CodeCompletionString::Chunk::ChunkKind;
llvm::Optional<ChunkKind>
CodeCompletionStringPrinter::getChunkKindForPrintNameContext(
PrintNameContext context) const {
switch (context) {
case PrintNameContext::Keyword:
if (isCurrentStructureKind(PrintStructureKind::EffectsSpecifiers)) {
return ChunkKind::EffectsSpecifierKeyword;
}
return ChunkKind::Keyword;
case PrintNameContext::IntroducerKeyword:
return ChunkKind::DeclIntroducer;
case PrintNameContext::Attribute:
return ChunkKind::Attribute;
case PrintNameContext::FunctionParameterExternal:
if (isInType()) {
return llvm::None;
}
return ChunkKind::ParameterDeclExternalName;
case PrintNameContext::FunctionParameterLocal:
if (isInType()) {
return llvm::None;
}
return ChunkKind::ParameterDeclLocalName;
default:
return llvm::None;
}
}
llvm::Optional<ChunkKind>
CodeCompletionStringPrinter::getChunkKindForStructureKind(
PrintStructureKind Kind) const {
switch (Kind) {
case PrintStructureKind::FunctionParameter:
if (isInType()) {
return llvm::None;
}
return ChunkKind::ParameterDeclBegin;
case PrintStructureKind::DefaultArgumentClause:
return ChunkKind::DefaultArgumentClauseBegin;
case PrintStructureKind::DeclGenericParameterClause:
return ChunkKind::GenericParameterClauseBegin;
case PrintStructureKind::DeclGenericRequirementClause:
return ChunkKind::GenericRequirementClauseBegin;
case PrintStructureKind::EffectsSpecifiers:
return ChunkKind::EffectsSpecifierClauseBegin;
case PrintStructureKind::DeclResultTypeClause:
return ChunkKind::DeclResultTypeClauseBegin;
case PrintStructureKind::FunctionParameterType:
return ChunkKind::ParameterDeclTypeBegin;
default:
return llvm::None;
}
}
void CodeCompletionStringPrinter::startNestedGroup(ChunkKind Kind) {
flush();
Builder.CurrentNestingLevel++;
Builder.addSimpleChunk(Kind);
}
void CodeCompletionStringPrinter::endNestedGroup() {
flush();
Builder.CurrentNestingLevel--;
}
void CodeCompletionStringPrinter::flush() {
if (Buffer.empty())
return;
Builder.addChunkWithText(CurrChunkKind, Buffer);
Buffer.clear();
}
/// Start \c AttributeAndModifierListBegin group. This must be called before
/// any attributes/modifiers printed to the output when printing an override
/// compleion.
void CodeCompletionStringPrinter::startPreamble() {
assert(!InPreamble);
startNestedGroup(ChunkKind::AttributeAndModifierListBegin);
InPreamble = true;
}
void CodeCompletionStringPrinter::endPremable() {
if (!InPreamble)
return;
InPreamble = false;
endNestedGroup();
}
void CodeCompletionStringPrinter::printText(StringRef Text) {
// Detect ': ' and ', ' in parameter clauses.
// FIXME: Is there a better way?
if (isCurrentStructureKind(PrintStructureKind::FunctionParameter) &&
Text == ": ") {
setNextChunkKind(ChunkKind::ParameterDeclColon);
} else if (isCurrentStructureKind(
PrintStructureKind::FunctionParameterList) &&
Text == ", ") {
setNextChunkKind(ChunkKind::Comma);
}
if (CurrChunkKind != NextChunkKind) {
// If the next desired kind is different from the current buffer, flush
// the current buffer.
flush();
CurrChunkKind = NextChunkKind;
}
Buffer.append(Text);
}
void CodeCompletionStringPrinter::printTypeRef(Type T, const TypeDecl *TD,
Identifier Name,
PrintNameContext NameContext) {
NextChunkKind = TD->getModuleContext()->isNonUserModule()
? ChunkKind::TypeIdSystem
: ChunkKind::TypeIdUser;
ASTPrinter::printTypeRef(T, TD, Name, NameContext);
NextChunkKind = ChunkKind::Text;
}
void CodeCompletionStringPrinter::printDeclLoc(const Decl *D) {
endPremable();
setNextChunkKind(ChunkKind::BaseName);
}
void CodeCompletionStringPrinter::printDeclNameEndLoc(const Decl *D) {
setNextChunkKind(ChunkKind::Text);
}
void CodeCompletionStringPrinter::printNamePre(PrintNameContext context) {
if (context == PrintNameContext::IntroducerKeyword)
endPremable();
if (auto Kind = getChunkKindForPrintNameContext(context))
setNextChunkKind(*Kind);
}
void CodeCompletionStringPrinter::printNamePost(PrintNameContext context) {
if (getChunkKindForPrintNameContext(context))
setNextChunkKind(ChunkKind::Text);
}
void CodeCompletionStringPrinter::printTypePre(const TypeLoc &TL) {
++TypeDepth;
}
void CodeCompletionStringPrinter::printTypePost(const TypeLoc &TL) {
assert(TypeDepth > 0);
--TypeDepth;
}
void CodeCompletionStringPrinter::printStructurePre(PrintStructureKind Kind,
const Decl *D) {
StructureStack.push_back(Kind);
if (auto chunkKind = getChunkKindForStructureKind(Kind))
startNestedGroup(*chunkKind);
}
void CodeCompletionStringPrinter::printStructurePost(PrintStructureKind Kind,
const Decl *D) {
if (getChunkKindForStructureKind(Kind))
endNestedGroup();
assert(Kind == StructureStack.back());
StructureStack.pop_back();
}