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
|
#include "sql_complete.h"
#include <yql/essentials/sql/v1/complete/text/word.h>
#include <yql/essentials/sql/v1/complete/name/service/static/name_service.h>
#include <yql/essentials/sql/v1/complete/syntax/local.h>
#include <yql/essentials/sql/v1/complete/syntax/format.h>
#include <util/generic/algorithm.h>
#include <util/charset/utf8.h>
namespace NSQLComplete {
class TSqlCompletionEngine: public ISqlCompletionEngine {
public:
explicit TSqlCompletionEngine(
TLexerSupplier lexer,
INameService::TPtr names,
ISqlCompletionEngine::TConfiguration configuration)
: Configuration(std::move(configuration))
, SyntaxAnalysis(MakeLocalSyntaxAnalysis(lexer))
, Names(std::move(names))
{
}
TCompletion Complete(TCompletionInput input) override {
return CompleteAsync(std::move(input)).ExtractValueSync();
}
virtual NThreading::TFuture<TCompletion> CompleteAsync(TCompletionInput input) override {
if (
input.CursorPosition < input.Text.length() &&
IsUTF8ContinuationByte(input.Text.at(input.CursorPosition)) ||
input.Text.length() < input.CursorPosition) {
ythrow yexception()
<< "invalid cursor position " << input.CursorPosition
<< " for input size " << input.Text.size();
}
TLocalSyntaxContext context = SyntaxAnalysis->Analyze(input);
TStringBuf prefix = input.Text.Head(input.CursorPosition);
TCompletedToken completedToken = GetCompletedToken(prefix);
return GetCandidates(std::move(context), completedToken)
.Apply([completedToken](NThreading::TFuture<TVector<TCandidate>> f) {
return TCompletion{
.CompletedToken = std::move(completedToken),
.Candidates = f.ExtractValue(),
};
});
}
private:
TCompletedToken GetCompletedToken(TStringBuf prefix) const {
return {
.Content = LastWord(prefix),
.SourcePosition = LastWordIndex(prefix),
};
}
NThreading::TFuture<TVector<TCandidate>> GetCandidates(TLocalSyntaxContext context, const TCompletedToken& prefix) const {
TNameRequest request = {
.Prefix = TString(prefix.Content),
.Limit = Configuration.Limit,
};
for (const auto& [first, _] : context.Keywords) {
request.Keywords.emplace_back(first);
}
if (context.Pragma) {
TPragmaName::TConstraints constraints;
constraints.Namespace = context.Pragma->Namespace;
request.Constraints.Pragma = std::move(constraints);
}
if (context.IsTypeName) {
request.Constraints.Type = TTypeName::TConstraints();
}
if (context.Function) {
TFunctionName::TConstraints constraints;
constraints.Namespace = context.Function->Namespace;
request.Constraints.Function = std::move(constraints);
}
if (context.Hint) {
THintName::TConstraints constraints;
constraints.Statement = context.Hint->StatementKind;
request.Constraints.Hint = std::move(constraints);
}
if (request.IsEmpty()) {
return NThreading::MakeFuture<TVector<TCandidate>>({});
}
return Names->Lookup(std::move(request))
.Apply([keywords = std::move(context.Keywords)](NThreading::TFuture<TNameResponse> f) {
TNameResponse response = f.ExtractValue();
return Convert(std::move(response.RankedNames), std::move(keywords));
});
}
static TVector<TCandidate> Convert(TVector<TGenericName> names, TLocalSyntaxContext::TKeywords keywords) {
TVector<TCandidate> candidates;
for (auto& name : names) {
candidates.emplace_back(std::visit([&](auto&& name) -> TCandidate {
using T = std::decay_t<decltype(name)>;
if constexpr (std::is_base_of_v<TKeyword, T>) {
TVector<TString>& seq = keywords[name.Content];
seq.insert(std::begin(seq), name.Content);
return {ECandidateKind::Keyword, FormatKeywords(seq)};
}
if constexpr (std::is_base_of_v<TPragmaName, T>) {
return {ECandidateKind::PragmaName, std::move(name.Indentifier)};
}
if constexpr (std::is_base_of_v<TTypeName, T>) {
return {ECandidateKind::TypeName, std::move(name.Indentifier)};
}
if constexpr (std::is_base_of_v<TFunctionName, T>) {
name.Indentifier += "(";
return {ECandidateKind::FunctionName, std::move(name.Indentifier)};
}
if constexpr (std::is_base_of_v<THintName, T>) {
return {ECandidateKind::HintName, std::move(name.Indentifier)};
}
}, std::move(name)));
}
return candidates;
}
TConfiguration Configuration;
ILocalSyntaxAnalysis::TPtr SyntaxAnalysis;
INameService::TPtr Names;
};
ISqlCompletionEngine::TPtr MakeSqlCompletionEngine(
TLexerSupplier lexer,
INameService::TPtr names,
ISqlCompletionEngine::TConfiguration configuration) {
return MakeHolder<TSqlCompletionEngine>(
lexer, std::move(names), std::move(configuration));
}
} // namespace NSQLComplete
template <>
void Out<NSQLComplete::ECandidateKind>(IOutputStream& out, NSQLComplete::ECandidateKind kind) {
switch (kind) {
case NSQLComplete::ECandidateKind::Keyword:
out << "Keyword";
break;
case NSQLComplete::ECandidateKind::PragmaName:
out << "PragmaName";
break;
case NSQLComplete::ECandidateKind::TypeName:
out << "TypeName";
break;
case NSQLComplete::ECandidateKind::FunctionName:
out << "FunctionName";
break;
case NSQLComplete::ECandidateKind::HintName:
out << "HintName";
break;
}
}
template <>
void Out<NSQLComplete::TCandidate>(IOutputStream& out, const NSQLComplete::TCandidate& candidate) {
out << "{" << candidate.Kind << ", \"" << candidate.Content << "\"}";
}
|