2016-01-15 14:57:40 +01:00
|
|
|
/****************************************************************************
|
2013-12-11 21:55:45 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 Lorenz Haas
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-12-11 21:55:45 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2013-12-11 21:55:45 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2013-12-11 21:55:45 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "configurationeditor.h"
|
|
|
|
|
|
|
|
|
|
#include "abstractsettings.h"
|
|
|
|
|
|
|
|
|
|
#include <texteditor/fontsettings.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QAbstractItemView>
|
|
|
|
|
#include <QCompleter>
|
|
|
|
|
#include <QScrollBar>
|
|
|
|
|
#include <QStringListModel>
|
|
|
|
|
|
|
|
|
|
namespace Beautifier {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
ConfigurationSyntaxHighlighter::ConfigurationSyntaxHighlighter(QTextDocument *parent) :
|
|
|
|
|
QSyntaxHighlighter(parent)
|
|
|
|
|
{
|
|
|
|
|
const TextEditor::FontSettings fs = TextEditor::TextEditorSettings::instance()->fontSettings();
|
|
|
|
|
m_formatKeyword = fs.toTextCharFormat(TextEditor::C_FIELD);
|
|
|
|
|
m_formatComment = fs.toTextCharFormat(TextEditor::C_COMMENT);
|
|
|
|
|
|
2016-04-21 21:08:57 +02:00
|
|
|
m_expressionComment.setPattern("#[^\\n]*");
|
2013-12-11 21:55:45 +01:00
|
|
|
m_expressionComment.setMinimal(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationSyntaxHighlighter::setKeywords(const QStringList &keywords)
|
|
|
|
|
{
|
|
|
|
|
if (keywords.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2014-02-19 06:48:37 +01:00
|
|
|
// Check for empty keywords since they can cause an endless loop in highlightBlock().
|
2013-12-11 21:55:45 +01:00
|
|
|
QStringList pattern;
|
2016-04-21 21:08:57 +02:00
|
|
|
for (const QString &word : keywords) {
|
2014-02-19 06:48:37 +01:00
|
|
|
if (!word.isEmpty())
|
|
|
|
|
pattern << QRegExp::escape(word);
|
|
|
|
|
}
|
2013-12-11 21:55:45 +01:00
|
|
|
|
2016-04-21 21:08:57 +02:00
|
|
|
m_expressionKeyword.setPattern("(?:\\s|^)(" + pattern.join('|') + ")(?=\\s|\\:|\\=|\\,|$)");
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationSyntaxHighlighter::setCommentExpression(const QRegExp &rx)
|
|
|
|
|
{
|
|
|
|
|
m_expressionComment = rx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationSyntaxHighlighter::highlightBlock(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
int pos = 0;
|
|
|
|
|
if (!m_expressionKeyword.isEmpty()) {
|
|
|
|
|
while ((pos = m_expressionKeyword.indexIn(text, pos)) != -1) {
|
|
|
|
|
const int length = m_expressionKeyword.matchedLength();
|
|
|
|
|
setFormat(pos, length, m_formatKeyword);
|
|
|
|
|
pos += length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_expressionComment.isEmpty()) {
|
|
|
|
|
pos = 0;
|
|
|
|
|
while ((pos = m_expressionComment.indexIn(text, pos)) != -1) {
|
|
|
|
|
const int length = m_expressionComment.matchedLength();
|
|
|
|
|
setFormat(pos, length, m_formatComment);
|
|
|
|
|
pos += length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 21:08:57 +02:00
|
|
|
ConfigurationEditor::ConfigurationEditor(QWidget *parent) :
|
|
|
|
|
QPlainTextEdit(parent),
|
|
|
|
|
m_completer(new QCompleter(this)),
|
|
|
|
|
m_model(new QStringListModel(QStringList(), m_completer)),
|
|
|
|
|
m_highlighter(new ConfigurationSyntaxHighlighter(document()))
|
2013-12-11 21:55:45 +01:00
|
|
|
{
|
|
|
|
|
m_completer->setModel(m_model);
|
|
|
|
|
m_completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
|
|
|
|
|
m_completer->setWrapAround(false);
|
|
|
|
|
m_completer->setWidget(this);
|
|
|
|
|
m_completer->setCompletionMode(QCompleter::PopupCompletion);
|
|
|
|
|
m_completer->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
|
m_completer->popup()->installEventFilter(this);
|
|
|
|
|
|
2015-01-29 14:32:36 +01:00
|
|
|
connect(m_completer, static_cast<void (QCompleter::*)(const QString &)>(&QCompleter::activated),
|
|
|
|
|
this, &ConfigurationEditor::insertCompleterText);
|
|
|
|
|
connect(this, &ConfigurationEditor::cursorPositionChanged,
|
|
|
|
|
this, &ConfigurationEditor::updateDocumentation);
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationEditor::setSettings(AbstractSettings *settings)
|
|
|
|
|
{
|
2014-09-11 10:43:54 +02:00
|
|
|
QTC_ASSERT(settings, return);
|
2013-12-11 21:55:45 +01:00
|
|
|
m_settings = settings;
|
|
|
|
|
|
|
|
|
|
QStringList keywords = m_settings->options();
|
|
|
|
|
m_highlighter->setKeywords(keywords);
|
|
|
|
|
keywords << m_settings->completerWords();
|
|
|
|
|
keywords.sort(Qt::CaseInsensitive);
|
|
|
|
|
m_model->setStringList(keywords);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationEditor::setCommentExpression(const QRegExp &rx)
|
|
|
|
|
{
|
|
|
|
|
m_highlighter->setCommentExpression(rx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Workaround for handling "ESC" right when popup is shown.
|
|
|
|
|
bool ConfigurationEditor::eventFilter(QObject *object, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::ShortcutOverride) {
|
2015-06-21 19:12:02 +02:00
|
|
|
const QKeyEvent *key = static_cast<const QKeyEvent *>(event);
|
2013-12-11 21:55:45 +01:00
|
|
|
if (key->key() == Qt::Key_Escape) {
|
|
|
|
|
event->accept();
|
|
|
|
|
m_completer->popup()->hide();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QObject::eventFilter(object, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationEditor::keyPressEvent(QKeyEvent *event)
|
|
|
|
|
{
|
|
|
|
|
const int key = event->key();
|
|
|
|
|
|
|
|
|
|
if (key == Qt::Key_Escape) {
|
|
|
|
|
event->ignore();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_completer->popup()->isVisible()) {
|
|
|
|
|
switch (key) {
|
|
|
|
|
case Qt::Key_Backtab:
|
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
|
case Qt::Key_Return:
|
|
|
|
|
case Qt::Key_Tab:
|
|
|
|
|
event->ignore();
|
|
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool isShortcut = ((event->modifiers() & Qt::ControlModifier) && key == Qt::Key_Space);
|
|
|
|
|
if (!isShortcut)
|
|
|
|
|
QPlainTextEdit::keyPressEvent(event);
|
|
|
|
|
|
|
|
|
|
const int cursorPosition = textCursor().position();
|
2015-06-21 19:12:02 +02:00
|
|
|
const QTextCursor cursor = cursorForTextUnderCursor();
|
2013-12-11 21:55:45 +01:00
|
|
|
const QString prefix = cursor.selectedText();
|
|
|
|
|
|
|
|
|
|
if (!isShortcut && (prefix.length() < 2 || cursorPosition != cursor.position())) {
|
|
|
|
|
m_completer->popup()->hide();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prefix != m_completer->completionPrefix()) {
|
|
|
|
|
m_completer->setCompletionPrefix(prefix);
|
|
|
|
|
m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_completer->completionCount() == 1 && prefix == m_completer->currentCompletion()) {
|
|
|
|
|
m_completer->popup()->hide();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QRect popupRect = cursorRect();
|
|
|
|
|
popupRect.setWidth(m_completer->popup()->sizeHintForColumn(0)
|
|
|
|
|
+ m_completer->popup()->verticalScrollBar()->sizeHint().width());
|
|
|
|
|
m_completer->complete(popupRect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hack because '-' and '=' are treated as End/StartOfWord
|
|
|
|
|
QTextCursor ConfigurationEditor::cursorForTextUnderCursor(QTextCursor tc) const
|
|
|
|
|
{
|
|
|
|
|
if (tc.isNull())
|
|
|
|
|
tc = textCursor();
|
|
|
|
|
|
|
|
|
|
tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
|
|
|
|
|
QChar ch = document()->characterAt(tc.position() - 1);
|
2016-04-21 21:08:57 +02:00
|
|
|
while (!(ch.isNull() || ch.isSpace() || ch == ':' || ch == ',')) {
|
2013-12-11 21:55:45 +01:00
|
|
|
tc.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor);
|
|
|
|
|
ch = document()->characterAt(tc.position() - 1);
|
|
|
|
|
}
|
|
|
|
|
tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
|
|
|
|
ch = document()->characterAt(tc.position());
|
2016-04-21 21:08:57 +02:00
|
|
|
while (!(ch.isNull() || ch.isSpace() || ch == ':' || ch == ',')) {
|
2013-12-11 21:55:45 +01:00
|
|
|
tc.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
|
|
|
|
ch = document()->characterAt(tc.position());
|
|
|
|
|
}
|
|
|
|
|
return tc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationEditor::insertCompleterText(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
QTextCursor tc = textCursor();
|
|
|
|
|
// Replace entire word to get case sensitivity right.
|
|
|
|
|
tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor,
|
|
|
|
|
m_completer->completionPrefix().length());
|
|
|
|
|
tc.insertText(text);
|
|
|
|
|
setTextCursor(tc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationEditor::updateDocumentation()
|
|
|
|
|
{
|
|
|
|
|
QTC_CHECK(m_settings);
|
|
|
|
|
QTextCursor cursor = textCursor();
|
|
|
|
|
|
|
|
|
|
QString word = cursorForTextUnderCursor(cursor).selectedText();
|
|
|
|
|
if (word == m_lastDocumentation)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QString doc = m_settings->documentation(word);
|
|
|
|
|
if (!doc.isEmpty()) {
|
|
|
|
|
m_lastDocumentation = word;
|
|
|
|
|
emit documentationChanged(word, doc);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the documentation was empty, then try to use the line's first word or the first word
|
|
|
|
|
// in front of a colon for providing a documentation.
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousWord);
|
|
|
|
|
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
|
2016-04-21 21:08:57 +02:00
|
|
|
const int pos = cursor.selectedText().lastIndexOf(',');
|
2013-12-11 21:55:45 +01:00
|
|
|
if (-1 != pos) {
|
|
|
|
|
cursor.setPosition(cursor.position() + pos);
|
|
|
|
|
cursor.movePosition(QTextCursor::NextWord);
|
|
|
|
|
}
|
|
|
|
|
word = cursorForTextUnderCursor(cursor).selectedText();
|
|
|
|
|
|
|
|
|
|
if (word == m_lastDocumentation)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
doc = m_settings->documentation(word);
|
|
|
|
|
if (doc.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_lastDocumentation = word;
|
|
|
|
|
emit documentationChanged(word, doc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Beautifier
|