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
|
#include "cursor_token_context.h"
#include <yql/essentials/core/issue/yql_issue.h>
#include <yql/essentials/sql/v1/lexer/lexer.h>
namespace NSQLComplete {
namespace {
bool Tokenize(ILexer::TPtr& lexer, TCompletionInput input, TParsedTokenList& tokens) {
NYql::TIssues issues;
if (!NSQLTranslation::Tokenize(
*lexer, TString(input.Text), /* queryName = */ "",
tokens, issues, /* maxErrors = */ 1)) {
return false;
}
return true;
}
TCursor GetCursor(const TParsedTokenList& tokens, size_t cursorPosition) {
size_t current = 0;
for (size_t i = 0; i < tokens.size() && current < cursorPosition; ++i) {
const auto& content = tokens[i].Content;
current += content.size();
if (current < cursorPosition) {
continue;
}
TCursor cursor = {
.PrevTokenIndex = i,
.NextTokenIndex = i,
.Position = cursorPosition,
};
if (current == cursorPosition) {
cursor.NextTokenIndex += 1;
}
return cursor;
}
return {
.PrevTokenIndex = Nothing(),
.NextTokenIndex = 0,
.Position = cursorPosition,
};
}
TVector<size_t> GetTokenPositions(const TParsedTokenList& tokens) {
TVector<size_t> positions;
positions.reserve(tokens.size());
size_t pos = 0;
for (const auto& token : tokens) {
positions.emplace_back(pos);
pos += token.Content.size();
}
return positions;
}
} // namespace
bool TRichParsedToken::IsLiteral() const {
return Base->Name == "STRING_VALUE" ||
Base->Name == "DIGIGTS" ||
Base->Name == "INTEGER_VALUE" ||
Base->Name == "REAL";
}
TRichParsedToken TokenAt(const TCursorTokenContext& context, size_t index) {
return {
.Base = &context.Tokens.at(index),
.Index = index,
.Position = context.TokenPositions.at(index),
};
}
TMaybe<TRichParsedToken> TCursorTokenContext::Enclosing() const {
if (Tokens.size() == 1) {
Y_ENSURE(Tokens[0].Name == "EOF");
return Nothing();
}
if (Cursor.PrevTokenIndex.Empty()) {
return Nothing();
}
auto token = TokenAt(*this, *Cursor.PrevTokenIndex);
if (Cursor.PrevTokenIndex == Cursor.NextTokenIndex ||
!IsWordBoundary(token.Base->Content.back())) {
return token;
}
return Nothing();
}
TMaybe<TRichParsedToken> TCursorTokenContext::MatchCursorPrefix(const TVector<TStringBuf>& pattern) const {
const auto prefix = std::span{Tokens.begin(), Cursor.NextTokenIndex};
if (prefix.size() < pattern.size()) {
return Nothing();
}
ssize_t i = static_cast<ssize_t>(prefix.size()) - 1;
ssize_t j = static_cast<ssize_t>(pattern.size()) - 1;
for (; 0 <= j; --i, --j) {
if (!pattern[j].empty() && prefix[i].Name != pattern[j]) {
return Nothing();
}
}
return TokenAt(*this, prefix.size() - pattern.size());
}
bool GetStatement(
ILexer::TPtr& lexer,
const TMaterializedInput& input,
TCompletionInput& output,
size_t& output_position) {
TVector<TString> statements;
NYql::TIssues issues;
if (!NSQLTranslationV1::SplitQueryToStatements(
input.Text, lexer,
statements, issues, /* file = */ "",
/* areBlankSkipped = */ false)) {
return false;
}
size_t& cursor = output_position;
cursor = 0;
for (const auto& statement : statements) {
if (input.CursorPosition < cursor + statement.size()) {
output = {
.Text = TStringBuf(input.Text).SubStr(cursor, statement.size()),
.CursorPosition = input.CursorPosition - cursor,
};
return true;
}
cursor += statement.size();
}
output.Text = TStringBuf(input.Text);
output.CursorPosition = input.CursorPosition;
return true;
}
bool GetCursorTokenContext(ILexer::TPtr& lexer, TCompletionInput input, TCursorTokenContext& context) {
TParsedTokenList tokens;
if (!Tokenize(lexer, input, tokens)) {
return false;
}
TVector<size_t> positions = GetTokenPositions(tokens);
TCursor cursor = GetCursor(tokens, input.CursorPosition);
context = {
.Tokens = std::move(tokens),
.TokenPositions = std::move(positions),
.Cursor = cursor,
};
return true;
}
} // namespace NSQLComplete
|