-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodehelpers.h
More file actions
146 lines (125 loc) · 5.12 KB
/
Copy pathcodehelpers.h
File metadata and controls
146 lines (125 loc) · 5.12 KB
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
/*
This file is part of kdev-python, the python language plugin for KDevelop
Copyright (C) 2012 Sven Brauch svenbrauch@googlemail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CODEHELPERS_H
#define CODEHELPERS_H
#include <QString>
#include <QStringList>
#include <KTextEditor/Document>
#include "parserexport.h"
namespace Python {
// helper class that lazy fetches lines from a document, for performance gain
class KDEVPYTHONPARSER_EXPORT LazyLineFetcher {
public:
virtual QString fetchLine(int lineno) = 0;
virtual ~LazyLineFetcher() { };
};
class KDEVPYTHONPARSER_EXPORT TrivialLazyLineFetcher : public LazyLineFetcher {
public:
TrivialLazyLineFetcher(QStringList lines) : m_lines(lines) { }
virtual QString fetchLine(int lineno) {
return m_lines.at(lineno);
};
private:
QStringList m_lines;
};
class KDEVPYTHONPARSER_EXPORT TextDocumentLazyLineFetcher : public LazyLineFetcher {
public:
TextDocumentLazyLineFetcher(KTextEditor::Document* d) :
m_document(d) { };
QString fetchLine(int lineno) {
return m_document->line(lineno);
};
private:
KTextEditor::Document* m_document;
};
class KDEVPYTHONPARSER_EXPORT FileIndentInformation {
public:
FileIndentInformation(const QStringList& lines);
FileIndentInformation(KTextEditor::Document* document);
FileIndentInformation(const QByteArray& data);
FileIndentInformation(const QString& data);
enum ScanDirection {
Forward,
Backward
};
enum ChangeTypes {
Indent,
Dedent,
AnyChange
};
int indentForLine(int line) const;
int linesCount() const;
int nextChange(int line, ChangeTypes type, ScanDirection direction = Forward) const;
private:
QList<int> m_indents;
void initialize(const QStringList& lines);
};
class KDEVPYTHONPARSER_EXPORT CodeHelpers
{
public:
enum EndLocation {
Code,
String,
Comment
};
CodeHelpers();
/**
* @brief Get the python expression at the specified cursor.
*
* @param lineFetcher An object which is used to get lines from the document
* @param cursor where to search for the expression
* @param forceScanExpression Start scanning even if the first char is non-alphanumeric. Defaults to false.
* @return QString the expression which was found, as string.
**/
static QString expressionUnderCursor(LazyLineFetcher& lineFetcher, KTextEditor::Cursor cursor,
bool forceScanExpression = false);
/**
* @brief Replaces all quoted strings with "S" in the given expression.
* TODO this does not work correctly, I think.
*
* @param stringWithStrings some python code which may contain strings
* @return QString the input code, with all strings replaced by "S", so 'foo("fancy\"\"\"''__/string")' -> 'foo("S")'
**/
static QString killStrings(QString stringWithStrings);
/**
* @brief Check whether the given code ends inside a comment.
**/
static EndLocation endsInside(const QString &code);
/**
* @brief Extracts the string which is under the cursor, if one is present
*
* @param code the code, which to scan
* @param range the range in the document, which contains this code
* @param cursor the location of the cursor in the document
* @param cursorPositionInString optional return value, which is set to
* the relative position of the cursor inside the string
* @return The string under the cursor, or an empty string if none is available.
*/
static QString extractStringUnderCursor(const QString &code, KTextEditor::Range range, KTextEditor::Cursor cursor, int *cursorPositionInString);
/**
* @brief Splits the given code, which is assumed to be contained in
* the range provided range, in two pieces: before and after the
* cursor.
* @param code some code to split
* @param range the range in the document, which contains this code
* @param cursor the location of the cursor in the document
* @return A pair of strings, containing the code and the code after
* the cursor
*/
static QPair<QString, QString> splitCodeByCursor(const QString &code, KTextEditor::Range range, KTextEditor::Cursor cursor);
};
}
#endif // CODEHELPERS_H
class C;