summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/ide/completion/analysis/local/cursor_token_context.cpp
blob: ce12e435f6996f623618917c5bbcf390b50bfefd (plain) (blame)
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
#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;
    return NSQLTranslation::Tokenize(
        *lexer, TString(input.Text), /* queryName = */ "",
        tokens, issues, /* maxErrors = */ 1);
}

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;
}

bool IsWhitespace(const TParsedToken& token) {
    return token.Name == "WS";
}

} // namespace

bool TRichParsedToken::IsLiteral() const {
    return Base->Name == "STRING_VALUE" ||
           Base->Name == "DIGIGTS" ||
           Base->Name == "INTEGER_VALUE" ||
           Base->Name == "REAL";
}

size_t TRichParsedToken::End() const {
    return Position + Base->Content.size();
}

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 {
    if (pattern.empty()) {
        return Nothing();
    }

    size_t index = Cursor.NextTokenIndex;
    if (auto enclosing = Enclosing()) {
        index = enclosing->Index;
    }

    const auto prefix = std::span{Tokens.begin(), index};

    ssize_t i = static_cast<ssize_t>(prefix.size()) - 1;
    ssize_t j = static_cast<ssize_t>(pattern.size()) - 1;
    for (; 0 <= j && 0 <= i; --i) {
        Y_ENSURE(pattern[j] != "WS");

        if (IsWhitespace(prefix[i])) {
            continue;
        }

        if (!pattern[j].empty() && prefix[i].Name != pattern[j]) {
            return Nothing();
        }

        --j;
    }

    if (0 <= j) {
        return Nothing();
    }

    Y_ENSURE(-1 <= i);
    Y_ENSURE(-1 == j);
    return TokenAt(*this, i + 1);
}

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