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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
/*
Actiona
Copyright (C) 2005 Jonathan Mercier-Ganady
Actiona 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.
Actiona 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/>.
Contact : jmgr@jmgr.info
*/
#include "codeedit.h"
#include "codehighlighter.h"
#include "linenumberarea.h"
#include "scriptcompleter.h"
#include "keywords.h"
#include "code/codetools.h"
#include <QAbstractItemView>
#include <QScrollBar>
#include <QApplication>
#include <QPainter>
#include <QScriptEngine>
#include <QDebug>
#include <QStandardItemModel>
namespace ActionTools
{
CodeEdit::CodeEdit(QWidget *parent)
: QPlainTextEdit(parent),
mCompleter(new ScriptCompleter(this)),
mHighlighter(new CodeHighlighter(document())),
mLineNumberArea(new LineNumberArea(this))
{
mCompleter->setWidget(this);
mCompleter->setCompletionMode(QCompleter::PopupCompletion);
mCompleter->setCaseSensitivity(Qt::CaseSensitive);
mCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
mCompleter->setWrapAround(false);
connect(this, &CodeEdit::blockCountChanged, this, &CodeEdit::updateLineNumberAreaWidth);
connect(this, &CodeEdit::updateRequest, this, &CodeEdit::updateLineNumberArea);
connect(this, &CodeEdit::cursorPositionChanged, this, &CodeEdit::highlightCurrentLine);
updateLineNumberAreaWidth(0);
highlightCurrentLine();
setTabStopWidth(30);
QFont font;
font.setFamily(QStringLiteral("Arial"));
font.setFixedPitch(true);
font.setPointSize(10);
setFont(font);
connect(mCompleter, static_cast<void (ScriptCompleter::*)(const QString &)>(&ScriptCompleter::activated), this, &CodeEdit::insertCompletion);
}
void CodeEdit::setCode(bool code)
{
mCode = code;
mHighlighter->setDocument(code ? document() : nullptr);
}
int CodeEdit::lineNumberAreaWidth() const
{
int digits = 1;
int max = qMax(1, blockCount());
while(max >= 10)
{
max /= 10;
++digits;
}
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
return space;
}
void CodeEdit::setCurrentLine(int line)
{
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::Start);
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, line - 1);
setTextCursor(cursor);
}
void CodeEdit::setCurrentColumn(int column)
{
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::StartOfLine);
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, column - 1);
setTextCursor(cursor);
}
void CodeEdit::setCompletionModel(QAbstractItemModel *completionModel)
{
mCompleter->setModel(completionModel);
if(!mCompletionModelSet)
{
mCompletionModelSet = true;
auto standardItemCompletionModel = qobject_cast<QStandardItemModel *>(completionModel);
if(!standardItemCompletionModel)
return;
for(int actionIndex = 0; actionIndex < standardItemCompletionModel->rowCount(); ++actionIndex)
{
QStandardItem *item = standardItemCompletionModel->item(actionIndex, 0);
if(!item)
continue;
mHighlighter->addCodeObject(item->text());
}
}
}
bool CodeEdit::checkSyntax()
{
if(mCode)
{
QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(toPlainText());
if(result.state() != QScriptSyntaxCheckResult::Valid)
{
setCurrentLine(result.errorLineNumber());
setCurrentColumn(result.errorColumnNumber());
mLastSyntaxError = result.errorMessage();
return false;
}
}
return true;
}
void CodeEdit::insertCompletion(const QString &completion)
{
if(mCompleter->widget() != this)
return;
QTextCursor cursor = textCursor();
int extra = completion.length() - mCompleter->completionPrefix().length();
cursor.insertText(completion.right(extra));
setTextCursor(cursor);
}
void CodeEdit::updateLineNumberAreaWidth(int newBlockCount)
{
Q_UNUSED(newBlockCount)
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void CodeEdit::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if(!isReadOnly())
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void CodeEdit::updateLineNumberArea(const QRect &rect, int dy)
{
if(dy)
mLineNumberArea->scroll(0, dy);
else
mLineNumberArea->update(0, rect.y(), mLineNumberArea->width(), rect.height());
if(rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
int spacesLeftFromPosition(const QString &text, int position)
{
int i = position;
while(i > 0)
{
if(!text.at(i-1).isSpace())
break;
--i;
}
return position - i;
}
int columnAt(const QString &text, int position)
{
int column = 0;
for(int i = 0; i < position; ++i)
{
if(text.at(i) == QLatin1Char('\t'))
column = column - (column % 30) + 30;
else
++column;
}
return column;
}
int indentedColumn(int column, bool doIndent)
{
int aligned = (column / 30) * 30;
if(doIndent)
return aligned + 30;
if(aligned < column)
return aligned;
return qMax(0, aligned - 30);
}
QString indentationString(int startColumn, int targetColumn)
{
targetColumn = qMax(startColumn, targetColumn);
QString s;
int alignedStart = startColumn - (startColumn % 30) + 30;
if(alignedStart > startColumn && alignedStart <= targetColumn)
{
s += QLatin1Char('\t');
startColumn = alignedStart;
}
if(int columns = targetColumn - startColumn)
{
int tabs = columns / 30;
s += QString(tabs, QLatin1Char('\t'));
s += QString(columns - tabs * 30, QLatin1Char(' '));
}
return s;
}
int lineIndentPosition(const QString &text)
{
int i = 0;
while(i < text.size())
{
if(!text.at(i).isSpace())
break;
++i;
}
int column = columnAt(text, i);
return i - (column % 30);
}
int firstNonSpace(const QString &text)
{
int i = 0;
while(i < text.size())
{
if(!text.at(i).isSpace())
return i;
++i;
}
return i;
}
void CodeEdit::indentOrUnindent(bool doIndent)
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
int pos = cursor.position();
QTextDocument *doc = document();
if(!cursor.hasSelection() || (doc->findBlock(cursor.selectionStart()) == doc->findBlock(cursor.selectionEnd()) ))
{
cursor.removeSelectedText();
QTextBlock block = cursor.block();
QString text = block.text();
int indentPosition = (cursor.position() - block.position());
int spaces = spacesLeftFromPosition(text, indentPosition);
int startColumn = columnAt(text, indentPosition - spaces);
int targetColumn = indentedColumn(columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(indentationString(startColumn, targetColumn));
}
else
{
int anchor = cursor.anchor();
int start = qMin(anchor, pos);
int end = qMax(anchor, pos);
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock(end-1).next();
for(QTextBlock block = startBlock; block != endBlock; block = block.next())
{
QString text = block.text();
int indentPosition = lineIndentPosition(text);
if(!doIndent && !indentPosition)
indentPosition = firstNonSpace(text);
int targetColumn = indentedColumn(columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.insertText(indentationString(0, targetColumn));
cursor.setPosition(block.position());
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
}
cursor.endEditBlock();
}
void CodeEdit::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(mLineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = static_cast<int>(blockBoundingGeometry(block).translated(contentOffset()).top());
int bottom = top + static_cast<int>(blockBoundingRect(block).height());
while (block.isValid() && top <= event->rect().bottom())
{
if (block.isVisible() && bottom >= event->rect().top())
{
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, mLineNumberArea->width(), fontMetrics().height(), Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + static_cast<int>(blockBoundingRect(block).height());
++blockNumber;
}
}
void CodeEdit::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
mLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
QString CodeEdit::textUnderCursor() const
{
QTextCursor cursor = textCursor();
int curpos = cursor.position();
QString text = cursor.block().text().left(curpos);
QStringList wordList = text.split(QRegExp(QStringLiteral("[^\\w\\.]")));
if (wordList.isEmpty())
return QString();
return wordList.last();
}
void CodeEdit::autoComplete()
{
mCompleter->complete();
}
void CodeEdit::focusInEvent(QFocusEvent *event)
{
if(mCompleter)
mCompleter->setWidget(this);
QPlainTextEdit::focusInEvent(event);
}
void CodeEdit::keyPressEvent(QKeyEvent *event)
{
if(!mCode)
{
QPlainTextEdit::keyPressEvent(event);
return;
}
if(mCompleter->popup()->isVisible())
{
// The following keys are forwarded by the completer to the widget
switch(event->key())
{
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
event->ignore();
return; // let the completer do default behavior
default:
break;
}
}
bool isShortcut = ((event->modifiers() & Qt::ControlModifier) && event->key() == Qt::Key_Space); // CTRL+Space
if(!mCompleter || !isShortcut) // dont process the shortcut when we have a completer
{
switch(event->key())
{
case Qt::Key_Tab:
indentOrUnindent(true);
return;
case Qt::Key_Backtab:
indentOrUnindent(false);
return;
default:
break;
}
QPlainTextEdit::keyPressEvent(event);
}
const bool ctrlOrShift = event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if(!mCompleter || (ctrlOrShift && event->text().isEmpty()))
return;
static QString endOfWord(QStringLiteral("~!@#$%^&*()_+{}|:\"<>?,/;'[]\\-="));
bool hasModifier = (event->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor();
if(!isShortcut && (hasModifier || event->text().isEmpty() || completionPrefix.length() < 1 || endOfWord.contains(event->text().right(1))))
{
mCompleter->popup()->hide();
return;
}
if(completionPrefix != mCompleter->completionPrefix())
{
mCompleter->setCompletionPrefix(completionPrefix);
mCompleter->popup()->setCurrentIndex(mCompleter->completionModel()->index(0, 0));
}
QRect cr = cursorRect();
cr.setWidth(mCompleter->popup()->sizeHintForColumn(0) + mCompleter->popup()->verticalScrollBar()->sizeHint().width());
mCompleter->complete(cr); // popup it up!
}
void CodeEdit::keyReleaseEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Return && event->modifiers() & Qt::ControlModifier)
emit acceptDialog();
else
QPlainTextEdit::keyReleaseEvent(event);
}
bool CodeEdit::event(QEvent *event)
{
//Block the backtab key from changing the focus, since we use it to unindent
if (event->type() == QEvent::KeyPress)
{
auto keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Backtab)
{
keyPressEvent(keyEvent);
return true;
}
}
return QPlainTextEdit::event(event);
}
}
|