-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainwindow.cpp
100 lines (85 loc) · 3.02 KB
/
mainwindow.cpp
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
/* QCommandEdit - a widget for entering commands, with completion and history
* Copyright (C) 2018 Federico Ferri
*
* 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 3 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/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcommandtokenizer.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->commandEdit, &QCommandEdit::execute, this, &MainWindow::onExecute);
connect(ui->commandEdit, &QCommandEdit::askCompletion, this, &MainWindow::onAskCompletion);
connect(ui->commandEdit, &QCommandEdit::escape, this, &MainWindow::onEscape);
history_ << "break if x == 1"
<< "lambda x: try import foo else return"
<< "local y"
<< "raise Exception(...)"
<< "return 0"
<< "while x > 0: print(x); x += 1"
<< "for i in range(100): print(i)";
ui->commandEdit->setHistory(history_);
ui->commandEdit->setShowMatchingHistory(true);
for(const QString &h : history_)
ui->textCmdLog->append(h);
words_ << "True" << "False" << "None" << "and" << "as" << "assert"
<< "break" << "class" << "continue" << "def" << "del" << "elif"
<< "else" << "except" << "finally" << "for" << "from" << "global"
<< "if" << "import" << "in" << "is" << "lambda" << "local"
<< "not" << "or" << "pass" << "raise" << "return" << "try"
<< "while" << "with" << "yield";
ui->listWords->addItems(words_);
ui->commandEdit->setFocus();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onExecute(const QString &s)
{
history_.append(s);
ui->textCmdLog->append(s);
ui->commandEdit->clear();
ui->commandEdit->setHistory(history_);
}
void MainWindow::onAskCompletion(const QString &cmd, int cursorPos)
{
QSimpleCommandTokenizer t;
t.setCommand(cmd);
try
{
QCommandTokenizer::Token tok = t.getTokenAtCharPos(cursorPos);
if(cursorPos != tok.end_)
throw "Not completing at middle of token";
QStringList comp;
for(const QString &w : words_)
if(w.startsWith(tok.token_))
comp << w.mid(tok.token_.length());
ui->commandEdit->setCompletion(comp);
qDebug() << "Completion:" << comp;
}
catch(const char *ex)
{
qDebug() << ex;
return;
}
}
void MainWindow::onEscape()
{
qDebug() << "Escape!";
}