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
|
/*
SPDX-FileCopyrightText: 2011-2012 Sven Brauch <svenbrauch@googlemail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef PYCOMPLETIONHELPERS_H
#define PYCOMPLETIONHELPERS_H
#include <language/duchain/declaration.h>
#include <QString>
#include <QList>
#include <QVariant>
#include <QRegularExpression>
#include "pythoncompletionexport.h"
using namespace KDevelop;
namespace Python {
void createArgumentList(Declaration* dec, QString& ret, QList<QVariant>* highlighting,
int atArg = 0, bool includeTypes = true);
// export needed for unit tests
KDEVPYTHONCOMPLETION_EXPORT int identifierMatchQuality(const QString& identifier1_, const QString& identifier2_);
KDEVPYTHONCOMPLETION_EXPORT QString camelCaseToUnderscore(const QString& camelCase);
class TokenList;
class KDEVPYTHONCOMPLETION_EXPORT ExpressionParser {
public:
ExpressionParser(QString code);
enum Status {
InvalidStatus,
NothingFound,
ExpressionFound,
CommaFound,
EventualCallFound,
InitializerFound,
FromFound,
MemberAccessFound,
ImportFound,
GeneratorFound,
RaiseFound,
ForFound,
ExceptFound,
ColonFound,
InFound,
ClassFound,
DefFound,
EqualsFound,
MeaninglessKeywordFound, // "and", "if", "return", ...
NoCompletionKeywordFound // "for"
};
QString popExpression(Status* status);
QString getRemainingCode();
QString getScannedCode();
QString skipUntilStatus(Status requestedStatus, bool* ok, int* expressionsSkipped = nullptr);
TokenList popAll();
void reset();
int trailingWhitespace();
private:
QString m_code;
int m_cursorPositionInString;
};
struct TokenListEntry {
public:
TokenListEntry(ExpressionParser::Status status_, QString expression_, int charOffset_)
: status(status_)
, expression(expression_)
, charOffset(charOffset_) {};
ExpressionParser::Status status;
QString expression;
int charOffset;
};
class TokenList : public QList<TokenListEntry> {
public:
/**
* @brief Reset the internal pointer to the last item, or offsetAtEnd items before
**/
void reset(int offsetAtEnd = 0) {
m_internalPtr = length() - offsetAtEnd;
};
/**
* @brief Set the internal pointer to "index".
**/
void seek(int index) {
m_internalPtr = index;
};
/**
* @brief Get the last item and advance the internal pointer.
*
* @return :TokenListEntry the item if the internal pointer is valid, an invalid item otherwise
**/
TokenListEntry weakPop() {
m_internalPtr --;
if ( m_internalPtr < 0 ) {
return TokenListEntry(ExpressionParser::InvalidStatus, QString(), -1);
}
return at(m_internalPtr);
};
// First returned value is the *expression count* index, the second one is the *character count*.
// The expressions count from the right, the characters count from the left.
// (see PythonCodeCompletionContext::summonParentForEventualCall for an example why that makes sense)
QPair<int, int> nextIndexOfStatus(ExpressionParser::Status status, int offsetFromEnd = 0) const {
int currentIndex = length() - 1 - offsetFromEnd;
while ( currentIndex >= 0 ) {
if ( at(currentIndex).status == status ) {
return QPair<int, int>(length() - currentIndex, at(currentIndex).charOffset);
}
currentIndex -= 1;
}
return QPair<int, int>(-1, -1);
};
QString toString() {
QString ret;
int pos = 0;
for (TokenListEntry item : std::as_const(*this)) {
ret.append(
QStringLiteral("offset ") + QString::number(item.charOffset) +
QStringLiteral(" position ") + QString::number(pos) +
QStringLiteral(": status ") + QString::number(item.status) +
QStringLiteral(", expression ") + item.expression + QStringLiteral("\n")
);
pos ++;
}
return ret;
};
private:
int m_internalPtr;
};
class ReplacementVariable;
struct RangeInString {
RangeInString() : beginIndex(-1), endIndex(-1) {}
RangeInString(int beginIndex_, int endIndex_)
: beginIndex(beginIndex_)
, endIndex(endIndex_) {}
bool isValid()
{
return beginIndex != -1 && endIndex != -1 && beginIndex < endIndex;
}
int beginIndex, endIndex;
};
class KDEVPYTHONCOMPLETION_EXPORT StringFormatter {
public:
StringFormatter(const QString &string);
bool isInsideReplacementVariable(int cursorPosition) const;
const ReplacementVariable *getReplacementVariable(int cursorPosition) const;
RangeInString getVariablePosition(int cursorPosition) const;
int nextIdentifierId() const;
private:
QString m_string;
QList<ReplacementVariable> m_replacementVariables;
QList<RangeInString> m_variablePositions;
};
class ReplacementVariable {
public:
ReplacementVariable(QString identifier, QChar conversion = QChar(), QString formatSpec = QString())
: m_identifier(identifier),
m_conversion(conversion),
m_formatSpec(formatSpec)
{
}
const QString &identifier() const
{
return m_identifier;
}
bool isIdentifierNumeric() const
{
bool isNumeric;
m_identifier.toInt(&isNumeric);
return isNumeric;
}
const QChar &conversion() const
{
return m_conversion;
}
bool hasConversion() const
{
return ! m_conversion.isNull();
}
const QString &formatSpec() const
{
return m_formatSpec;
}
bool hasFormatSpec() const
{
return ! ( m_formatSpec.isNull() || m_formatSpec.isEmpty() );
}
// Convenience functions for extracting some of the parts of the format spec
QChar fillCharacter() const
{
return hasFillCharacter() ? m_formatSpec.at(0) : QChar();
}
bool hasFillCharacter() const
{
QStringList alignChars = QStringList() << QStringLiteral("<") << QStringLiteral(">") << QStringLiteral("^") << QStringLiteral("=");
return hasAlign() && alignChars.indexOf(m_formatSpec.at(1)) != -1;
}
QChar align() const
{
if ( hasAlign() ) {
return hasFillCharacter() ? m_formatSpec.at(1) : m_formatSpec.at(0);
}
return QChar();
}
bool hasAlign() const
{
static QRegularExpression regex(QStringLiteral("^.?[<>\\^=]"));
return m_formatSpec.contains(regex);
}
bool hasPrecision() const
{
if (fillCharacter() == QLatin1Char('.')) {
return m_formatSpec.count(QLatin1Char('.')) == 2;
}
return m_formatSpec.contains(QLatin1Char('.'));
}
QChar type() const
{
return hasType() ? m_formatSpec.at(m_formatSpec.size() - 1) : QChar();
}
bool hasType() const
{
QStringList possibleTypes = QStringList() << QStringLiteral("b") << QStringLiteral("c") << QStringLiteral("d") << QStringLiteral("e")
<< QStringLiteral("E") << QStringLiteral("f") << QStringLiteral("F") << QStringLiteral("g")
<< QStringLiteral("G") << QStringLiteral("n") << QStringLiteral("o") << QStringLiteral("s")
<< QStringLiteral("x") << QStringLiteral("X") << QStringLiteral("%");
return (hasFormatSpec() && possibleTypes.indexOf(m_formatSpec.at(m_formatSpec.size() - 1)) != -1);
}
QString toString() const
{
QString variable = QLatin1Char('{') + m_identifier;
if (hasConversion()) {
variable += QLatin1Char('!') + m_conversion;
}
if (hasFormatSpec()) {
variable += QLatin1Char(':') + m_formatSpec;
}
variable += QLatin1Char('}');
return variable;
}
private:
QString m_identifier;
QChar m_conversion;
QString m_formatSpec;
};
}
#endif
|