2017-09-16 16:04:29 +03:00
|
|
|
#include "lrcodeeditor.h"
|
|
|
|
|
|
|
|
#include <QAbstractItemView>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QCompleter>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QTextBlock>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include "lrscripthighlighter.h"
|
2018-02-15 02:21:00 +03:00
|
|
|
#include "lrglobal.h"
|
2017-09-16 16:04:29 +03:00
|
|
|
|
|
|
|
namespace LimeReport{
|
|
|
|
|
|
|
|
CodeEditor::CodeEditor(QWidget *parent)
|
|
|
|
: QPlainTextEdit(parent), m_compleater(0)
|
|
|
|
{
|
|
|
|
lineNumberArea = new LineNumberArea(this);
|
|
|
|
|
|
|
|
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
|
|
|
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
|
|
|
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
|
|
|
|
|
|
|
|
updateLineNumberAreaWidth(0);
|
|
|
|
highlightCurrentLine();
|
2017-09-19 19:05:38 +03:00
|
|
|
new ScriptHighlighter(document());
|
|
|
|
connect(this, SIGNAL(cursorPositionChanged()),
|
|
|
|
this, SLOT(matchParentheses()));
|
2017-09-16 16:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeEditor::setCompleter(QCompleter *value)
|
|
|
|
{
|
|
|
|
if (value) disconnect(value,0,this,0);
|
|
|
|
m_compleater = value;
|
|
|
|
if (!m_compleater) return;
|
|
|
|
m_compleater->setWidget(this);
|
|
|
|
m_compleater->setCompletionMode(QCompleter::PopupCompletion);
|
|
|
|
m_compleater->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
connect(m_compleater,SIGNAL(activated(QString)),this,SLOT(insertCompletion(QString)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent* event)
|
|
|
|
{
|
|
|
|
QPainter painter(lineNumberArea);
|
2017-10-05 01:28:24 +03:00
|
|
|
QStyleOption option;
|
|
|
|
option.initFrom(this);
|
|
|
|
//painter.fillRect(event->rect(), QPalette().background().color());
|
|
|
|
QColor bg = option.palette.background().color().darker(150);
|
|
|
|
painter.fillRect(event->rect(), bg);
|
2017-09-16 16:04:29 +03:00
|
|
|
|
|
|
|
QTextBlock block = firstVisibleBlock();
|
|
|
|
int blockNumber = block.blockNumber();
|
|
|
|
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
|
|
|
|
int bottom = top + (int) blockBoundingRect(block).height();
|
|
|
|
|
|
|
|
while (block.isValid() && top <= event->rect().bottom()) {
|
|
|
|
if (block.isVisible() && bottom >= event->rect().top()) {
|
|
|
|
QString number = QString::number(blockNumber + 1);
|
2017-10-05 01:28:24 +03:00
|
|
|
painter.setPen(option.palette.text().color());
|
2017-09-16 16:04:29 +03:00
|
|
|
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
|
|
|
|
Qt::AlignCenter, number);
|
|
|
|
}
|
|
|
|
|
|
|
|
block = block.next();
|
|
|
|
top = bottom;
|
|
|
|
bottom = top + (int) blockBoundingRect(block).height();
|
|
|
|
++blockNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CodeEditor::lineNumberAreaWidth()
|
|
|
|
{
|
|
|
|
int digits = 1;
|
|
|
|
int max = qMax(1, blockCount());
|
|
|
|
while (max >= 10) {
|
|
|
|
max /= 10;
|
|
|
|
++digits;
|
|
|
|
}
|
|
|
|
|
|
|
|
int space = fontMetrics().width(QLatin1Char('9'))*2 + fontMetrics().width(QLatin1Char('9')) * digits;
|
|
|
|
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeEditor::keyPressEvent(QKeyEvent *e)
|
|
|
|
{
|
|
|
|
if (m_compleater && m_compleater->popup()->isVisible()) {
|
|
|
|
switch (e->key()) {
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Escape:
|
|
|
|
case Qt::Key_Tab:
|
|
|
|
case Qt::Key_Backtab:
|
2018-02-15 02:21:00 +03:00
|
|
|
case Qt::Key_Right:
|
|
|
|
case Qt::Key_Left:
|
|
|
|
case Qt::Key_Up:
|
|
|
|
case Qt::Key_Down:
|
2017-09-16 16:04:29 +03:00
|
|
|
e->ignore();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space);
|
|
|
|
if (!m_compleater || !isShortcut) QPlainTextEdit::keyPressEvent(e);
|
|
|
|
|
|
|
|
const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
|
|
|
|
if (!m_compleater || (ctrlOrShift && e->text().isEmpty()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
|
|
|
|
|
|
|
|
QString completionPrefix = textUnderCursor();
|
|
|
|
|
|
|
|
if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
|
2018-02-15 02:21:00 +03:00
|
|
|
|| Const::EOW.contains(e->text().right(1)))
|
|
|
|
)
|
|
|
|
{
|
2017-09-16 16:04:29 +03:00
|
|
|
m_compleater->popup()->hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (completionPrefix != m_compleater->completionPrefix()) {
|
|
|
|
m_compleater->setCompletionPrefix(completionPrefix);
|
|
|
|
m_compleater->popup()->setCurrentIndex(m_compleater->completionModel()->index(0, 0));
|
|
|
|
}
|
|
|
|
|
2018-02-15 02:21:00 +03:00
|
|
|
QModelIndex ci = m_compleater->completionModel()->index(0,0);
|
|
|
|
if (ci.isValid() && m_compleater->completionModel()->data(ci).toString().compare(completionPrefix) == 0){
|
|
|
|
m_compleater->popup()->hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-16 16:04:29 +03:00
|
|
|
QRect cr = cursorRect();
|
|
|
|
cr.setWidth(m_compleater->popup()->sizeHintForColumn(0)
|
|
|
|
+ m_compleater->popup()->verticalScrollBar()->sizeHint().width());
|
|
|
|
m_compleater->complete(cr);
|
|
|
|
|
2018-02-15 02:21:00 +03:00
|
|
|
if (!completionPrefix.isEmpty() &&
|
|
|
|
completionPrefix.at(completionPrefix.length()-1) == '.')
|
|
|
|
{
|
|
|
|
m_compleater->popup();
|
|
|
|
}
|
2017-09-16 16:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeEditor::focusInEvent(QFocusEvent *e)
|
|
|
|
{
|
|
|
|
if (m_compleater) m_compleater->setWidget(this);
|
|
|
|
QPlainTextEdit::focusInEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeEditor::resizeEvent(QResizeEvent* event)
|
|
|
|
{
|
|
|
|
QPlainTextEdit::resizeEvent(event);
|
|
|
|
QRect cr = contentsRect();
|
|
|
|
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CodeEditor::textUnderCursor() const
|
|
|
|
{
|
|
|
|
QTextCursor tc = textCursor();
|
2018-02-15 02:21:00 +03:00
|
|
|
QString currentText;
|
|
|
|
tc.movePosition(QTextCursor::StartOfBlock,QTextCursor::KeepAnchor);
|
|
|
|
QString blockText = tc.selectedText();
|
|
|
|
for(int i = blockText.length(); i>0; --i){
|
|
|
|
if (!Const::EOW.contains(blockText.at(i-1)))
|
|
|
|
currentText = blockText.at(i-1) + currentText;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
return currentText;
|
2017-09-16 16:04:29 +03:00
|
|
|
}
|
|
|
|
|
2017-09-19 19:05:38 +03:00
|
|
|
bool CodeEditor::matchLeftParenthesis(QTextBlock currentBlock, QChar parenthesisType, int i, int numLeftParentheses)
|
|
|
|
{
|
|
|
|
TextBlockData *data = static_cast<TextBlockData *>(currentBlock.userData());
|
2017-10-04 17:14:07 +03:00
|
|
|
if (data){
|
|
|
|
QVector<ParenthesisInfo *> infos = data->parentheses();
|
|
|
|
|
|
|
|
int docPos = currentBlock.position();
|
|
|
|
for (; i < infos.size(); ++i) {
|
|
|
|
ParenthesisInfo *info = infos.at(i);
|
|
|
|
|
|
|
|
if (info->character == parenthesisType) {
|
|
|
|
++numLeftParentheses;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->character == getParenthesisReverceChar(parenthesisType)){
|
|
|
|
if (numLeftParentheses == 0) {
|
|
|
|
createParenthesisSelection(docPos + info->position);
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
--numLeftParentheses;
|
|
|
|
}
|
2017-09-19 19:05:38 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:14:07 +03:00
|
|
|
currentBlock = currentBlock.next();
|
|
|
|
if (currentBlock.isValid())
|
|
|
|
return matchLeftParenthesis(currentBlock, parenthesisType, 0, numLeftParentheses);
|
2017-09-19 19:05:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeEditor::matchRightParenthesis(QTextBlock currentBlock, QChar parenthesisType, int i, int numRightParentheses)
|
|
|
|
{
|
|
|
|
TextBlockData *data = static_cast<TextBlockData *>(currentBlock.userData());
|
2017-10-04 17:14:07 +03:00
|
|
|
if (data){
|
2017-11-10 00:47:47 +03:00
|
|
|
QVector<ParenthesisInfo *> parentheses = data->parentheses();
|
2017-10-04 17:14:07 +03:00
|
|
|
int docPos = currentBlock.position();
|
2017-12-16 18:02:13 +03:00
|
|
|
if (i == -2) i = parentheses.size()-1;
|
2017-10-04 17:14:07 +03:00
|
|
|
for (; i > -1 && parentheses.size() > 0; --i) {
|
|
|
|
ParenthesisInfo *info = parentheses.at(i);
|
|
|
|
if (info->character == parenthesisType) {
|
|
|
|
++numRightParentheses;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (info->character == getParenthesisReverceChar(parenthesisType)){
|
|
|
|
if (numRightParentheses == 0) {
|
|
|
|
createParenthesisSelection(docPos + info->position);
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
--numRightParentheses;
|
|
|
|
}
|
2017-09-19 19:05:38 +03:00
|
|
|
}
|
|
|
|
|
2017-10-04 17:14:07 +03:00
|
|
|
currentBlock = currentBlock.previous();
|
|
|
|
if (currentBlock.isValid())
|
2017-12-16 18:02:13 +03:00
|
|
|
return matchRightParenthesis(currentBlock, parenthesisType, -2, numRightParentheses);
|
2017-09-19 19:05:38 +03:00
|
|
|
|
2017-10-04 17:14:07 +03:00
|
|
|
}
|
2017-09-19 19:05:38 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeEditor::createParenthesisSelection(int pos)
|
|
|
|
{
|
|
|
|
QList<QTextEdit::ExtraSelection> selections = extraSelections();
|
|
|
|
|
|
|
|
QTextEdit::ExtraSelection selection;
|
|
|
|
QTextCharFormat format = selection.format;
|
|
|
|
format.setBackground(QColor("#619934"));
|
|
|
|
format.setForeground(QColor("#ffffff"));
|
|
|
|
selection.format = format;
|
|
|
|
|
|
|
|
QTextCursor cursor = textCursor();
|
|
|
|
cursor.setPosition(pos);
|
|
|
|
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
|
|
|
selection.cursor = cursor;
|
|
|
|
|
|
|
|
selections.append(selection);
|
|
|
|
|
|
|
|
setExtraSelections(selections);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeEditor::charIsParenthesis(QChar character, ParenthesisType type)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < PARENHEIS_COUNT; ++i){
|
|
|
|
if (character == parenthesisCharacters[type][i])
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QChar CodeEditor::getParenthesisReverceChar(QChar parenthesisChar)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < PARENHEIS_COUNT; ++i){
|
|
|
|
if ( parenthesisCharacters[RightParenthesis][i] == parenthesisChar)
|
|
|
|
return parenthesisCharacters[LeftParenthesis][i];
|
|
|
|
if ( parenthesisCharacters[LeftParenthesis][i] == parenthesisChar)
|
|
|
|
return parenthesisCharacters[RightParenthesis][i];
|
|
|
|
}
|
|
|
|
return ' ';
|
|
|
|
}
|
|
|
|
|
2017-09-16 16:04:29 +03:00
|
|
|
void CodeEditor::insertCompletion(const QString &completion)
|
|
|
|
{
|
|
|
|
if (m_compleater->widget() != this)
|
|
|
|
return;
|
|
|
|
QTextCursor tc = textCursor();
|
|
|
|
int extra = completion.length() - m_compleater->completionPrefix().length();
|
|
|
|
tc.movePosition(QTextCursor::Left);
|
|
|
|
tc.movePosition(QTextCursor::EndOfWord);
|
|
|
|
tc.insertText(completion.right(extra));
|
|
|
|
setTextCursor(tc);
|
|
|
|
}
|
|
|
|
|
2017-09-19 19:05:38 +03:00
|
|
|
void CodeEditor::updateLineNumberAreaWidth(int /*newBlockCount*/)
|
2017-09-16 16:04:29 +03:00
|
|
|
{
|
|
|
|
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeEditor::highlightCurrentLine()
|
|
|
|
{
|
|
|
|
QList<QTextEdit::ExtraSelection> extraSelections;
|
|
|
|
|
|
|
|
if (!isReadOnly()) {
|
|
|
|
QTextEdit::ExtraSelection selection;
|
|
|
|
|
2017-09-19 19:05:38 +03:00
|
|
|
QColor lineColor = QColor(QPalette().background().color()).darker(100);
|
2017-09-16 16:04:29 +03:00
|
|
|
|
|
|
|
selection.format.setBackground(lineColor);
|
|
|
|
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
|
|
|
|
selection.cursor = textCursor();
|
|
|
|
selection.cursor.clearSelection();
|
|
|
|
extraSelections.append(selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
setExtraSelections(extraSelections);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeEditor::updateLineNumberArea(const QRect& rect, int dy)
|
|
|
|
{
|
|
|
|
if (dy)
|
|
|
|
lineNumberArea->scroll(0, dy);
|
|
|
|
else
|
|
|
|
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
|
|
|
|
|
|
|
|
if (rect.contains(viewport()->rect()))
|
|
|
|
updateLineNumberAreaWidth(0);
|
|
|
|
}
|
|
|
|
|
2017-09-19 19:05:38 +03:00
|
|
|
void CodeEditor::matchParentheses()
|
|
|
|
{
|
|
|
|
QList<QTextEdit::ExtraSelection> selections;
|
|
|
|
setExtraSelections(selections);
|
|
|
|
|
|
|
|
TextBlockData *data = static_cast<TextBlockData *>(textCursor().block().userData());
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
QVector<ParenthesisInfo *> infos = data->parentheses();
|
|
|
|
|
|
|
|
int pos = textCursor().block().position();
|
|
|
|
for (int i = 0; i < infos.size(); ++i) {
|
|
|
|
ParenthesisInfo *info = infos.at(i);
|
|
|
|
|
|
|
|
int curPos = textCursor().position() - textCursor().block().position();
|
|
|
|
|
|
|
|
if ( (info->position == (curPos - 1)) && charIsParenthesis(info->character, LeftParenthesis))
|
|
|
|
{
|
|
|
|
if (matchLeftParenthesis(textCursor().block(), info->character, i + 1, 0))
|
|
|
|
createParenthesisSelection(pos + info->position);
|
|
|
|
} else if ( (info->position == (curPos - 1)) && charIsParenthesis(info->character, RightParenthesis)) {
|
|
|
|
if (matchRightParenthesis(textCursor().block(), info->character, i - 1, 0))
|
|
|
|
createParenthesisSelection(pos + info->position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 16:04:29 +03:00
|
|
|
} //namespace LimeReport
|