Skip to content

Commit e893ef2

Browse files
committed
Search Text, Syntax Builtins
1 parent bcba18e commit e893ef2

File tree

9 files changed

+395
-163
lines changed

9 files changed

+395
-163
lines changed

CodeEditor/pythonsyntaxhighlighter.cpp

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
5252
<< "not"
5353
<< "or"
5454
<< "pass"
55-
<< "print"
5655
<< "raise"
5756
<< "return"
5857
<< "try"
@@ -99,8 +98,90 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
9998
<< "\\["
10099
<< "\\]";
101100

102-
setStyles();
101+
builtins = QStringList() << "abs"
102+
<< "divmod"
103+
<< "input"
104+
<< "open"
105+
<< "staticmethod"
106+
<< "all"
107+
<< "enumerate"
108+
<< "int"
109+
<< "ord"
110+
<< "str"
111+
<< "any"
112+
<< "eval"
113+
<< "isinstance"
114+
<< "pow"
115+
<< "sum"
116+
<< "basestring"
117+
<< "execfile"
118+
<< "issubclass"
119+
<< "print"
120+
<< "super"
121+
<< "bin"
122+
<< "file"
123+
<< "iter"
124+
<< "property"
125+
<< "tuple"
126+
<< "bool"
127+
<< "filter"
128+
<< "len"
129+
<< "range"
130+
<< "type"
131+
<< "bytearray"
132+
<< "float"
133+
<< "list"
134+
<< "raw_input"
135+
<< "unichr"
136+
<< "callable"
137+
<< "format"
138+
<< "locals"
139+
<< "reduce"
140+
<< "unicode"
141+
<< "chr"
142+
<< "frozenset"
143+
<< "long"
144+
<< "reload"
145+
<< "vars"
146+
<< "classmethod"
147+
<< "getattr"
148+
<< "map"
149+
<< "repr"
150+
<< "xrange"
151+
<< "cmp"
152+
<< "globals"
153+
<< "max"
154+
<< "reversed"
155+
<< "zip"
156+
<< "compile"
157+
<< "hasattr"
158+
<< "memoryview"
159+
<< "round"
160+
<< "__import__"
161+
<< "complex"
162+
<< "hash"
163+
<< "min"
164+
<< "set"
165+
<< "apply"
166+
<< "delattr"
167+
<< "help"
168+
<< "next"
169+
<< "setattr"
170+
<< "buffer"
171+
<< "dict"
172+
<< "hex"
173+
<< "object"
174+
<< "slice"
175+
<< "coerce"
176+
<< "dir"
177+
<< "id"
178+
<< "oct"
179+
<< "sorted"
180+
<< "intern";
103181

182+
setStyles();
183+
mSearchRegex = tr("");
184+
mSearchHighlight = getTextCharFormat("black", "bold", "yellow");
104185
triSingleQuote.setPattern("'''");
105186
triDoubleQuote.setPattern("\"\"\"");
106187

@@ -111,6 +192,7 @@ void PythonSyntaxHighlighter::setStyles()
111192
{
112193
basicStyles.insert("keyword", getTextCharFormat("orange", "bold"));
113194
basicStyles.insert("operator", getTextCharFormat("yellow", "bold"));
195+
basicStyles.insert("builtins", getTextCharFormat("lightblue", "underline"));
114196
basicStyles.insert("brace", getTextCharFormat("red", "bold"));
115197
basicStyles.insert("string", getTextCharFormat("magenta"));
116198
basicStyles.insert("stringlong", getTextCharFormat("magenta", "bold"));
@@ -138,15 +220,20 @@ void PythonSyntaxHighlighter::initializeRules()
138220
rules.append(HighlightingRule("\\b__[\\w_]+__\\b", 0, basicStyles.value("hackish")));
139221
rules.append(HighlightingRule("\\b_[\\w_]+\\b", 0, basicStyles.value("private")));
140222

223+
foreach (QString currBuiltin, builtins) {
224+
rules.append(HighlightingRule(QString("\\b%1\\b").arg(currBuiltin), 0, basicStyles.value("builtins")));
225+
}
226+
227+
141228
rules.append(HighlightingRule("\\b_\\b", 0, basicStyles.value("special")));
142229
rules.append(HighlightingRule("\\bself\\b", 0, basicStyles.value("special")));
143230

144-
rules.append(HighlightingRule("[uUrR]?\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"", 0, basicStyles.value("string")));
145-
rules.append(HighlightingRule("[uUrR]?'[^'\\\\]*(\\\\.[^'\\\\]*)*'", 0, basicStyles.value("string")));
146-
147231
rules.append(HighlightingRule("(b|B|br|Br|bR|BR|rb|rB|Rb|RB)\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"", 0, basicStyles.value("bytes")));
148232
rules.append(HighlightingRule("(b|B|br|Br|bR|BR|rb|rB|Rb|RB)'[^'\\\\]*(\\\\.[^'\\\\]*)*'", 0, basicStyles.value("bytes")));
149233

234+
rules.append(HighlightingRule("[uUrR]?\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"", 0, basicStyles.value("string")));
235+
rules.append(HighlightingRule("[uUrR]?'[^'\\\\]*(\\\\.[^'\\\\]*)*'", 0, basicStyles.value("string")));
236+
150237
rules.append(HighlightingRule("\\b[+-]?[0-9]+[lL]?\\b", 0, basicStyles.value("numbers")));
151238
rules.append(HighlightingRule("\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b", 0, basicStyles.value("numbers")));
152239
rules.append(HighlightingRule("\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b", 0, basicStyles.value("numbers")));
@@ -178,8 +265,26 @@ void PythonSyntaxHighlighter::highlightBlock(const QString& text)
178265

179266
// Do multi-line strings
180267
bool isInMultilne = matchMultiline(text, triSingleQuote, 1, basicStyles.value("stringlong"));
181-
if (!isInMultilne)
268+
if (!isInMultilne) {
182269
isInMultilne = matchMultiline(text, triDoubleQuote, 2, basicStyles.value("stringlong"));
270+
}
271+
272+
// Highlight found stuff
273+
if (!mSearchRegex.isNull() && !mSearchRegex.isEmpty())
274+
{
275+
QRegExp reg(mSearchRegex);
276+
int idx = reg.indexIn(text, 0);
277+
while (idx >= 0) {
278+
int length = reg.cap(0).length();
279+
setFormat(idx, length, mSearchHighlight);
280+
idx = reg.indexIn(text, idx + length);
281+
}
282+
}
283+
}
284+
285+
void PythonSyntaxHighlighter::SetSearchRegEx(const QString& text)
286+
{
287+
mSearchRegex = text;
183288
}
184289

185290
bool PythonSyntaxHighlighter::matchMultiline(const QString& text, const QRegExp& delimiter, const int inState, const QTextCharFormat& style)

CodeEditor/pythonsyntaxhighlighter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ class PythonSyntaxHighlighter : public QSyntaxHighlighter {
5454
public:
5555
PythonSyntaxHighlighter(QTextDocument* parent = 0);
5656

57+
void SetSearchRegEx(const QString &text);
5758
protected:
5859
void highlightBlock(const QString& text);
5960

6061
private:
6162
QStringList keywords;
6263
QStringList operators;
6364
QStringList braces;
65+
QStringList builtins;
66+
QString mSearchRegex;
67+
QTextCharFormat mSearchHighlight;
6468
QHash<QString, QTextCharFormat> basicStyles;
6569
void initializeRules();
6670
//! Highlighst multi-line strings, returns true if after processing we are still within the multi-line section.

PythonAccess/emb.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ PyObject* ApiSetCode(PyObject* self, PyObject* args)
207207

208208
return Py_BuildValue("i", 0);
209209
}
210+
211+
PyObject* ApiSetSearchRegex(PyObject* self, PyObject* args)
212+
{
213+
char* data;
214+
if (!PyArg_ParseTuple(args, "s", &data))
215+
return NULL;
216+
217+
emit worker->SetSearchRegex(QString(data));
218+
219+
return Py_BuildValue("i", 0);
220+
}
221+
210222
PyObject* ApiWriteOutput(PyObject* self, PyObject* args)
211223
{
212224
char* data;
@@ -236,6 +248,8 @@ PyMethodDef apiMethods[] = {
236248
"Get code textbox's content" },
237249
{ "set_code", ApiSetCode, METH_VARARGS,
238250
"Get code textbox's content" },
251+
{ "set_search_regex", ApiSetSearchRegex, METH_VARARGS,
252+
"Set highlight RegEx" },
239253

240254
{ "write_output", ApiWriteOutput, METH_VARARGS,
241255
"Append to output, It does not automatically add a newline" },

PythonAccess/emb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PyObject* ApiGetOutput(PyObject* self, PyObject* args);
1818
PyObject* ApiGetAppPath(PyObject* self, PyObject* args);
1919
PyObject* ApiSetInput(PyObject* self, PyObject* args);
2020
PyObject* ApiGetInput(PyObject* self, PyObject* args);
21+
PyObject* ApiSetSearchRegex(PyObject* self, PyObject* args);
2122
void ResetStdOut();
2223
void setMainView(MainView* _mainView);
2324
void setWorker(PythonWorker* _worker);

PythonAccess/pythonworker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class PythonWorker : public QObject
1616
void SetInput(QString txt);
1717
void SetOutput(QString txt);
1818
void SetCode(QString txt);
19+
void SetSearchRegex(QString txt);
1920
void StartPythonRun();
2021
void EndPythonRun();
2122

UI/mainview.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void MainView::SetupPython()
3737
connect(worker, &PythonWorker::SetOutput, this, &MainView::SetOutput);
3838
connect(worker, &PythonWorker::StartPythonRun, this, &MainView::StartPythonRun);
3939
connect(worker, &PythonWorker::EndPythonRun, this, &MainView::EndPythonRun);
40+
connect(worker, &PythonWorker::SetSearchRegex, this, &MainView::SetSearchRegex);
4041
workerThread.start();
4142
}
4243
void MainView::StartPythonRun()
@@ -93,6 +94,7 @@ void MainView::SetupHighlighter()
9394
{
9495
mHighlighter = new PythonSyntaxHighlighter(ui->txtCode->document());
9596
ui->txtCode->setFocus();
97+
mHighlighterOneLiner = new PythonSyntaxHighlighter (ui->txtOneLiner->document());
9698
}
9799

98100
void MainView::LoadResources()
@@ -213,6 +215,13 @@ void MainView::SetCode(QString txt)
213215
{
214216
ui->txtCode->setPlainText(txt);
215217
}
218+
219+
void MainView::SetSearchRegex(QString txt)
220+
{
221+
mHighlighter->SetSearchRegEx(txt);
222+
mHighlighter->rehighlight();
223+
}
224+
216225
void MainView::WriteOutput(QString output)
217226
{
218227
QString txt = ui->txtOutput->toPlainText();
@@ -318,6 +327,10 @@ void MainView::on_btnCodeDatabase_clicked()
318327

319328
void MainView::on_btnRunSnippet_clicked()
320329
{
330+
if (!Confirm("Are you sure you want to run this snippet ?")){
331+
return;
332+
}
333+
321334
bool success;
322335
QString code = mSnippets->GetSnippet(ui->cmbSnippets->currentText(), success);
323336
if (success) {
@@ -327,6 +340,10 @@ void MainView::on_btnRunSnippet_clicked()
327340

328341
void MainView::on_btnLoadSnippet_clicked()
329342
{
343+
if (!Confirm("Are you sure you want to load snippet to code area ?")){
344+
return;
345+
}
346+
330347
bool success;
331348
QString code = mSnippets->GetSnippet(ui->cmbSnippets->currentText(), success);
332349
if (success) {
@@ -407,3 +424,11 @@ void MainView::on_btnUpdateSnippet_clicked()
407424
}
408425
LoadSnippetsToCombo();
409426
}
427+
428+
void MainView::on_btnRunOneLiner_clicked()
429+
{
430+
if (!Confirm("Are you sure you want to run this one liner ?")){
431+
return;
432+
}
433+
RunPythonCode(ui->txtOneLiner->toPlainText());
434+
}

UI/mainview.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ private slots:
5454
void SetInput(QString txt);
5555
void SetOutput(QString txt);
5656
void SetCode(QString txt);
57+
void SetSearchRegex(QString txt);
5758
void WriteOutput(QString output);
5859
void StartPythonRun();
5960
void EndPythonRun();
60-
6161
void on_btnUpdateSnippet_clicked();
62+
void on_btnRunOneLiner_clicked();
6263

6364
private:
6465
const QString FILETYPES_PYTHON = tr("Python Code (*.py);;All files (*.*)");
6566
const QString FILETYPES_OTHER = tr("Text files (*.txt);;All files (*.*)");
6667
QThread workerThread;
6768
Ui::MainView* ui;
6869
PythonSyntaxHighlighter* mHighlighter;
70+
PythonSyntaxHighlighter* mHighlighterOneLiner;
6971
QString mStartMe;
7072
QString mAbout;
7173
Snippets* mSnippets;
@@ -80,6 +82,7 @@ private slots:
8082
void LoadSettings();
8183
void SetupPython();
8284
bool Confirm(const QString& what);
85+
8386
signals:
8487
void operate(const QString&, const QString&);
8588
};

0 commit comments

Comments
 (0)