File: helpers.cpp

package info (click to toggle)
kdevelop-python 24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,640 kB
  • sloc: python: 183,048; cpp: 18,798; xml: 140; sh: 14; makefile: 9
file content (475 lines) | stat: -rw-r--r-- 17,582 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
    SPDX-FileCopyrightText: 2011-2012 Sven Brauch <svenbrauch@googlemail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

// Note to confused people reading this code: This is not the parser.
// It's just a minimalist helper class for code completion. The parser is in the parser/ directory.

#include "helpers.h"

#include <language/duchain/abstractfunctiondeclaration.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/ducontext.h>
#include <language/duchain/declaration.h>
#include <language/duchain/types/functiontype.h>
#include <language/duchain/types/integraltype.h>
#include <language/codecompletion/normaldeclarationcompletionitem.h>

#include <QStringList>
#include <QTextFormat>

#include <QDebug>
#include "codecompletiondebug.h"

#include "duchain/declarations/functiondeclaration.h"
#include "parser/codehelpers.h"

using namespace KDevelop;

namespace Python {

QString camelCaseToUnderscore(const QString& camelCase)
{
    QString underscore;
    for ( int i = 0; i < camelCase.size(); i++ ) {
        const QChar& c = camelCase.at(i);
        if ( c.isUpper() && i != 0 ) {
            underscore.append(QLatin1Char('_'));
        }
        underscore.append(c.toLower());
    }
    return underscore;
}

int identifierMatchQuality(const QString& identifier1_, const QString& identifier2_)
{
    QString identifier1 = camelCaseToUnderscore(identifier1_).toLower().replace(QLatin1Char('.'), QLatin1Char('_'));
    QString identifier2 = camelCaseToUnderscore(identifier2_).toLower().replace(QLatin1Char('.'), QLatin1Char('_'));

    if ( identifier1 == identifier2 ) {
        return 3;
    }
    if ( identifier1.contains(identifier2) || identifier2.contains(identifier1) ) {
        return 2;
    }
    QStringList parts1 = identifier1.split(QLatin1Char('_'));
    QStringList parts2 = identifier2.split(QLatin1Char('_'));
    parts1.removeAll(QString());
    parts2.removeAll(QString());
    parts1.removeDuplicates();
    parts2.removeDuplicates();
    if ( parts1.length() > 5 || parts2.length() > 5 ) {
        // don't waste time comparing huge identifiers,
        // the matching is probably pointless anyways for people using
        // more than 5 words for their variable names
        return 0;
    }
    for (const QString& part1 : std::as_const(parts1)) {
        for (const QString& part2 : std::as_const(parts2)) {
            // Don't take very short name parts into account,
            // those are not very descriptive eventually
            if ( part1.size() < 3 || part2.size() < 3 ) {
                continue;
            }
            if ( part1 == part2 ) {
                // partial match
                return 1;
            }
        }
    }
    return 0;
}

typedef QPair<QString, ExpressionParser::Status> keyword;

static QList<keyword> supportedKeywords;
static QList<keyword> controlChars;
static QList<QString> miscKeywords;
static QList<QString> noCompletionKeywords;
static QMutex keywordPopulationLock;

// Keywords known to me:
// and       del       for       is        raise    
// assert    elif      from      lambda    return   
// break     else      global    not       try      
// class     except    if        or        while    
// continue  exec      import    pass      yield    
// def       finally   in        print     with
// async     await

ExpressionParser::ExpressionParser(QString code)
    : m_code(code)
    , m_cursorPositionInString(m_code.length())
{
    keywordPopulationLock.lock();
    if ( supportedKeywords.isEmpty() ) {
        noCompletionKeywords << QStringLiteral("break") << QStringLiteral("class") << QStringLiteral("continue") << QStringLiteral("pass")
                             << QStringLiteral("try") << QStringLiteral("else") << QStringLiteral("as") << QStringLiteral("finally")
                             << QStringLiteral("global") << QStringLiteral("lambda");
        miscKeywords << QStringLiteral("and") << QStringLiteral("assert") << QStringLiteral("del") << QStringLiteral("elif") << QStringLiteral("exec")
                     << QStringLiteral("if") << QStringLiteral("is") << QStringLiteral("not")
                     << QStringLiteral("or") << QStringLiteral("print") << QStringLiteral("return") << QStringLiteral("while")
                     << QStringLiteral("yield") << QStringLiteral("with") << QStringLiteral("await");
        supportedKeywords << keyword(QStringLiteral("import"), ExpressionParser::ImportFound);
        supportedKeywords << keyword(QStringLiteral("from"), ExpressionParser::FromFound);
        supportedKeywords << keyword(QStringLiteral("raise"), ExpressionParser::RaiseFound);
        supportedKeywords << keyword(QStringLiteral("in"), ExpressionParser::InFound);
        supportedKeywords << keyword(QStringLiteral("for"), ExpressionParser::ForFound);
        supportedKeywords << keyword(QStringLiteral("class"), ExpressionParser::ClassFound);
        supportedKeywords << keyword(QStringLiteral("def"), ExpressionParser::DefFound);
        supportedKeywords << keyword(QStringLiteral("except"), ExpressionParser::ExceptFound);
        controlChars << keyword(QStringLiteral(":"), ExpressionParser::ColonFound);
        controlChars << keyword(QStringLiteral(","), ExpressionParser::CommaFound);
        controlChars << keyword(QStringLiteral("("), ExpressionParser::InitializerFound);
        controlChars << keyword(QStringLiteral("{"), ExpressionParser::InitializerFound);
        controlChars << keyword(QStringLiteral("["), ExpressionParser::InitializerFound);
        controlChars << keyword(QStringLiteral("."), ExpressionParser::MemberAccessFound);
        controlChars << keyword(QStringLiteral("="), ExpressionParser::EqualsFound);
    }
    keywordPopulationLock.unlock();
}

QString ExpressionParser::getRemainingCode()
{
    return m_code.mid(0, m_cursorPositionInString);
}

QString ExpressionParser::getScannedCode()
{
    return m_code.mid(m_cursorPositionInString, m_code.length() - m_cursorPositionInString);
}

int ExpressionParser::trailingWhitespace()
{
    int ws = 0;
    int index = m_cursorPositionInString - 1;
    while ( index >= 0 ) {
        if ( m_code.at(index).isSpace() ) {
            ws++;
            index --;
        }
        else {
            break;
        }
    }
    return ws;
}

void ExpressionParser::reset()
{
    m_cursorPositionInString = m_code.length();
}

QString ExpressionParser::skipUntilStatus(ExpressionParser::Status requestedStatus, bool* ok, int* expressionsSkipped)
{
    if ( expressionsSkipped ) {
        *expressionsSkipped = 0;
    }
    QString lastExpression;
    Status currentStatus = InvalidStatus;
    while ( currentStatus != requestedStatus ) {
        lastExpression = popExpression(&currentStatus);
        qCDebug(KDEV_PYTHON_CODECOMPLETION) << lastExpression << currentStatus;
        if ( currentStatus == NothingFound ) {
            *ok = ( requestedStatus == NothingFound ); // ok exactly if the caller requested NothingFound as end status
            return QString();
        }
        if ( expressionsSkipped && currentStatus == ExpressionFound ) {
            *expressionsSkipped += 1;
        }
    }
    *ok = true;
    return lastExpression;
}

TokenList ExpressionParser::popAll()
{
    Status currentStatus = InvalidStatus;
    TokenList items;
    while ( currentStatus != NothingFound ) {
        QString result = popExpression(&currentStatus);
        items << TokenListEntry(currentStatus, result, m_cursorPositionInString);
    }
    std::reverse(items.begin(), items.end());
    return items;
}

bool endsWithSeperatedKeyword(const QString& str, const QString& shouldEndWith) {
    bool endsWith = str.endsWith(shouldEndWith);
    if ( ! endsWith ) {
        return false;
    }
    int l = shouldEndWith.length();
    if ( str.length() == l ) {
        return true;
    }
    if ( str.right(l + 1).at(0).isSpace() ) {
        return true;
    }
    return false;
}

QString ExpressionParser::popExpression(ExpressionParser::Status* status)
{
    const auto remaining = getRemainingCode();
    auto trimmed = remaining.trimmed();
    auto operatingOn = trimmed.replace(QLatin1Char('\t'), QLatin1Char(' '));
    bool lineIsEmpty = false;
    for ( auto it = remaining.constEnd()-1; it != remaining.constEnd(); it-- ) {
        if ( ! it->isSpace() ) {
            break;
        }
        if ( *it == QLatin1Char('\n') ) {
            lineIsEmpty = true;
            break;
        }
    }
    if ( operatingOn.isEmpty() || lineIsEmpty ) {
        m_cursorPositionInString = 0;
        *status = NothingFound;
        return QString();
    }
    bool lastCharIsSpace = getRemainingCode().right(1).at(0).isSpace();
    m_cursorPositionInString -= trailingWhitespace();
    if ( operatingOn.endsWith(QLatin1Char('(')) ) {
        qCDebug(KDEV_PYTHON_CODECOMPLETION) << "eventual call found";
        m_cursorPositionInString -= 1;
        *status = EventualCallFound;
        return QString();
    }
    for (const keyword& kw : std::as_const(controlChars)) {
        if ( operatingOn.endsWith(kw.first) ) {
            m_cursorPositionInString -= kw.first.length();
            *status = kw.second;
            return QString();
        }
    }
    if ( lastCharIsSpace ) {
        for (const keyword& kw : std::as_const(supportedKeywords)) {
            if ( endsWithSeperatedKeyword(operatingOn, kw.first) ) {
                m_cursorPositionInString -= kw.first.length();
                *status = kw.second;
                return QString();
            }
        }
        for (const QString& kw : std::as_const(miscKeywords)) {
            if ( endsWithSeperatedKeyword(operatingOn, kw) ) {
                m_cursorPositionInString -= kw.length();
                *status = MeaninglessKeywordFound;
                return QString();
            }
        }
        for (const QString& kw : std::as_const(noCompletionKeywords)) {
            if ( endsWithSeperatedKeyword(operatingOn, kw) ) {
                m_cursorPositionInString -= kw.length();
                *status = NoCompletionKeywordFound;
                return QString();
            }
        }
    }
    // Otherwise, there's a real expression at the cursor, so scan it.
    QStringList lines = operatingOn.split(QLatin1Char('\n'));
    Python::TrivialLazyLineFetcher f(lines);
    int lastLine = lines.length()-1;
    KTextEditor::Cursor startCursor;
    QString expr = CodeHelpers::expressionUnderCursor(f, KTextEditor::Cursor(lastLine, f.fetchLine(lastLine).length() - 1),
                                                      startCursor, true);
    if ( expr.isEmpty() ) {
        *status = NothingFound;
    }
    else {
        *status = ExpressionFound;
    }
    m_cursorPositionInString -= expr.length();
    return expr;
}


// This is stolen from PHP. For credits, see helpers.cpp in PHP.
void createArgumentList(Declaration* dec_, QString& ret, QList< QVariant >* highlighting, int atArg, bool includeTypes)
{
    auto dec = dynamic_cast<Python::FunctionDeclaration*>(dec_);
    if ( ! dec ) {
        return;
    }
    int textFormatStart = 0;
    QTextFormat normalFormat(QTextFormat::CharFormat);
    QTextFormat highlightFormat(QTextFormat::CharFormat);
    highlightFormat.setBackground(QColor::fromRgb(142, 186, 255));
    highlightFormat.setProperty(QTextFormat::FontWeight, 99);
    
    AbstractFunctionDeclaration* decl = dynamic_cast<AbstractFunctionDeclaration*>(dec);
    FunctionType::Ptr functionType = dec->type<FunctionType>();
    
    if (functionType && decl) {

        QVector<Declaration*> parameters;
        if (DUChainUtils::argumentContext(dec))
            parameters = DUChainUtils::argumentContext(dec)->localDeclarations();

        ret = QLatin1Char('(');
        bool first = true;
        int num = 0;
        
        bool skipFirst = false;
        if ( dec->context() && dec->context()->type() == DUContext::Class && ! dec->isStatic() ) {
            // the function is a class method, and its first argument is "self". Don't display that.
            skipFirst = true;
        }

        uint defaultParamNum = 0;
        int firstDefaultParam = parameters.count() - decl->defaultParametersSize() - skipFirst;

        // disable highlighting when in default arguments, it doesn't make much sense then
        bool disableHighlighting = false;

        for (Declaration* dec : std::as_const(parameters)) {
            if ( skipFirst ) {
                skipFirst = false;
                continue;
            }
            // that has nothing to do with the skip, it's just for the comma
            if (first)
                first = false;
            else
                ret += QStringLiteral(", ");

            bool doHighlight = false;
            QTextFormat doFormat;
            
            if ( num == atArg - 1 )
                doFormat = highlightFormat;
            else
                doFormat = normalFormat;
            
            if ( num == firstDefaultParam ) {
                ret += QStringLiteral("[");
                ++defaultParamNum;
                disableHighlighting = true;
            }
            
            if ( ! disableHighlighting ) {
                doHighlight = true;
            }
            
            if ( includeTypes ) {
                if (num < functionType->arguments().count()) {
                    if (AbstractType::Ptr type = functionType->arguments().at(num)) {
                        if ( type->toString() != QStringLiteral("<unknown>") ) {
                            ret += type->toString() + QLatin1Char(' ');
                        }
                    }
                }
                
                if (doHighlight) {
                    if (highlighting && ret.length() != textFormatStart) {
                        //Add a default-highlighting for the passed text
                        *highlighting << QVariant(textFormatStart);
                        *highlighting << QVariant(ret.length() - textFormatStart);
                        *highlighting << QVariant(normalFormat);
                        textFormatStart = ret.length();
                    }
                }
            }
            
            
            ret += dec->identifier().toString();

            if (doHighlight) {
                if (highlighting && ret.length() != textFormatStart) {
                    *highlighting << QVariant(textFormatStart + 1);
                    *highlighting << QVariant(ret.length() - textFormatStart - 1);
                    *highlighting << doFormat;
                    textFormatStart = ret.length();
                }
            }

            ++num;
        }
        if ( defaultParamNum != 0 ) {
            ret += QLatin1Char(']');
        }
        ret += QLatin1Char(')');

        if (highlighting && ret.length() != textFormatStart) {
            *highlighting <<  QVariant(textFormatStart);
            *highlighting << QVariant(ret.length());
            *highlighting << normalFormat;
            textFormatStart = ret.length();
        }
        return;
    }
}

StringFormatter::StringFormatter(const QString &string)
    : m_string(string)
{
    qCDebug(KDEV_PYTHON_CODECOMPLETION) << "String being parsed: " << string;
    static QRegularExpression regex(QStringLiteral("\\{(\\w+)(?:!([rs]))?(?:\\:(.*))?\\}"),
                                    QRegularExpression::InvertedGreedinessOption);

    QRegularExpressionMatchIterator i = regex.globalMatch(string);
    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        QString identifier = match.captured(1);
        QString conversionStr = match.captured(2);
        QChar conversion = (conversionStr.isNull() || conversionStr.isEmpty()) ? QChar() : conversionStr.at(0);
        QString formatSpec = match.captured(3);

        qCDebug(KDEV_PYTHON_CODECOMPLETION) << "variable: " << match.captured(0);

        // The regex guarantees that conversion is only a single character
        ReplacementVariable variable(identifier, conversion, formatSpec);
        m_replacementVariables.append(variable);

        RangeInString variablePosition(match.capturedStart(), match.capturedEnd());
        m_variablePositions.append(variablePosition);
    }
}

bool StringFormatter::isInsideReplacementVariable(int cursorPosition) const
{
    return getReplacementVariable(cursorPosition) != nullptr;
}

const ReplacementVariable *StringFormatter::getReplacementVariable(int cursorPosition) const
{
    int index = 0;
    for ( const RangeInString &variablePosition : m_variablePositions ) {
        if ( cursorPosition >= variablePosition.beginIndex && cursorPosition <= variablePosition.endIndex ) {
            return &m_replacementVariables.at(index);
        }
        index++;
    }

    return nullptr;
}

RangeInString StringFormatter::getVariablePosition(int cursorPosition) const
{
    int index = 0;
    for ( const RangeInString &variablePosition : m_variablePositions ) {
        if ( cursorPosition >= variablePosition.beginIndex && cursorPosition <= variablePosition.endIndex ) {
            return m_variablePositions.at(index);
        }
        index++;
    }
    return RangeInString();
}

int StringFormatter::nextIdentifierId() const
{
    int highestIdFound = -1;
    for ( const ReplacementVariable &variable : m_replacementVariables ) {
        bool isNumeric;
        int identifier = variable.identifier().toInt(&isNumeric);
        if ( isNumeric && identifier > highestIdFound ) {
            highestIdFound = identifier;
        }
    }
    return highestIdFound + 1;
}

}