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
|
#include "name_mapping.h"
#include <yql/essentials/sql/v1/complete/syntax/format.h>
namespace NSQLComplete {
namespace {
TString ToIdentifier(TString content, const TLocalSyntaxContext& local) {
if (IsPlain(content) && !local.IsQuoted.AtLhs && !local.IsQuoted.AtRhs) {
return content;
}
SubstGlobal(content, "`", "``");
if (!local.IsQuoted.AtLhs) {
content.prepend('`');
}
if (!local.IsQuoted.AtRhs) {
content.append('`');
}
return content;
}
} // namespace
TCandidate ToCandidate(TKeyword name, TLocalSyntaxContext& local) {
TVector<TString>& seq = local.Keywords[name.Content];
seq.insert(std::begin(seq), name.Content);
TCandidate candidate = {
.Kind = ECandidateKind::Keyword,
.Content = FormatKeywords(seq),
};
if (candidate.Content.EndsWith('(')) {
candidate.Content += ')';
candidate.CursorShift = 1;
}
return candidate;
}
TCandidate ToCandidate(TPragmaName name) {
return {ECandidateKind::PragmaName, std::move(name.Identifier)};
}
TCandidate ToCandidate(TTypeName name) {
TCandidate candidate = {
.Kind = ECandidateKind::TypeName,
.Content = std::move(name.Identifier),
};
switch (name.Kind) {
case TTypeName::EKind::Simple: {
} break;
case TTypeName::EKind::Container: {
candidate.Content += "<>";
candidate.CursorShift = 1;
} break;
case TTypeName::EKind::Parameterized: {
candidate.Content += "()";
candidate.CursorShift = 1;
} break;
}
return candidate;
}
TCandidate ToCandidate(TFunctionName name) {
TCandidate candidate = {
.Kind = ECandidateKind::FunctionName,
.Content = std::move(name.Identifier),
};
candidate.Content += "()";
candidate.CursorShift = 1;
return candidate;
}
TCandidate ToCandidate(THintName name) {
return {ECandidateKind::HintName, std::move(name.Identifier)};
}
TCandidate ToCandidate(TFolderName name, TLocalSyntaxContext& local) {
TCandidate candidate = {
.Kind = ECandidateKind::FolderName,
.Content = std::move(name.Identifier),
};
if (!local.IsQuoted.AtLhs) {
candidate.Content.prepend('`');
}
candidate.Content.append('/');
if (!local.IsQuoted.AtRhs) {
candidate.Content.append('`');
candidate.CursorShift = 1;
}
return candidate;
}
TCandidate ToCandidate(TTableName name, TLocalSyntaxContext& local) {
if (!local.IsQuoted.AtLhs) {
name.Identifier.prepend('`');
}
if (!local.IsQuoted.AtRhs) {
name.Identifier.append('`');
}
return {ECandidateKind::TableName, std::move(name.Identifier)};
}
TCandidate ToCandidate(TClusterName name) {
return {ECandidateKind::ClusterName, std::move(name.Identifier)};
}
TCandidate ToCandidate(TColumnName name, TLocalSyntaxContext& local) {
name.Identifier = ToIdentifier(std::move(name.Identifier), local);
if (local.Column->Table.empty() && !name.TableAlias.empty()) {
name.Identifier.prepend('.');
name.Identifier.prepend(name.TableAlias);
}
return {ECandidateKind::ColumnName, std::move(name.Identifier)};
}
TCandidate ToCandidate(TBindingName name) {
name.Identifier.prepend('$');
return {ECandidateKind::BindingName, std::move(name.Identifier)};
}
TCandidate ToCandidate(TUnknownName name, TLocalSyntaxContext& local) {
name.Content = ToIdentifier(std::move(name.Content), local);
return {ECandidateKind::UnknownName, std::move(name.Content)};
}
TCandidate ToCandidate(TGenericName generic, TLocalSyntaxContext& local) {
return std::visit([&](auto&& name) -> TCandidate {
using T = std::decay_t<decltype(name)>;
constexpr bool IsContextSensitive =
std::is_same_v<T, TKeyword> ||
std::is_same_v<T, TFolderName> ||
std::is_same_v<T, TTableName> ||
std::is_same_v<T, TColumnName> ||
std::is_same_v<T, TUnknownName>;
constexpr bool IsDocumented =
std::is_base_of_v<TDescribed, T>;
TMaybe<TString> documentation;
if constexpr (IsDocumented) {
documentation = std::move(name.Description);
}
TCandidate candidate;
if constexpr (IsContextSensitive) {
candidate = ToCandidate(std::move(name), local);
} else {
candidate = ToCandidate(std::move(name));
}
candidate.Documentation = std::move(documentation);
return candidate;
}, std::move(generic));
}
TVector<TCandidate> ToCandidate(TVector<TGenericName> names, TLocalSyntaxContext local) {
TVector<TCandidate> candidates;
candidates.reserve(names.size());
for (auto& name : names) {
candidates.emplace_back(ToCandidate(std::move(name), local));
}
return candidates;
}
} // namespace NSQLComplete
|