aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/beautifier/configurationeditor.cpp
diff options
context:
space:
mode:
authorLorenz Haas <lykurg@gmail.com>2013-12-11 21:55:45 +0100
committerDavid Schulz <david.schulz@digia.com>2014-02-18 07:36:12 +0100
commit4988b41b3f419dff3c6bf4fcf22d484fbc5384f4 (patch)
treeb25004f51d29003e85a75275fe95953aeeffd62a /src/plugins/beautifier/configurationeditor.cpp
parentf2ae69b1bf7f7ade6c01b42ed8444cd7e1b3ab94 (diff)
Introduce new Beautifier plug-in
Beautifier is a plug-in for source code beautifiers. Currently Artistic Style, Clang-format and Uncrustify are supported. Task-number: QTCREATORBUG-7489 Change-Id: I8dde9a8ba6a6a55537bfd450ec82ef966ed283c0 Reviewed-by: David Schulz <david.schulz@digia.com>
Diffstat (limited to 'src/plugins/beautifier/configurationeditor.cpp')
-rw-r--r--src/plugins/beautifier/configurationeditor.cpp267
1 files changed, 267 insertions, 0 deletions
diff --git a/src/plugins/beautifier/configurationeditor.cpp b/src/plugins/beautifier/configurationeditor.cpp
new file mode 100644
index 00000000000..0fdbeaa3884
--- /dev/null
+++ b/src/plugins/beautifier/configurationeditor.cpp
@@ -0,0 +1,267 @@
+/**************************************************************************
+**
+** Copyright (c) 2014 Lorenz Haas
+** Contact: http://www.qt-project.org/legal
+**
+** 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
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#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);
+
+ m_expressionComment.setPattern(QLatin1String("#[^\\n]*"));
+ m_expressionComment.setMinimal(false);
+}
+
+void ConfigurationSyntaxHighlighter::setKeywords(const QStringList &keywords)
+{
+ if (keywords.isEmpty())
+ return;
+
+ QStringList pattern;
+ for (int i = 0, total = keywords.count(); i < total; ++i)
+ pattern << QRegExp::escape(keywords.at(i));
+
+ m_expressionKeyword.setPattern(QLatin1String("(?:\\s|^)(") + pattern.join(QLatin1Char('|'))
+ + QLatin1String(")(?=\\s|\\:|\\=|\\,|$)"));
+}
+
+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;
+ }
+ }
+}
+
+ConfigurationEditor::ConfigurationEditor(QWidget *parent)
+ : QPlainTextEdit(parent)
+ , m_settings(0)
+ , m_completer(new QCompleter(this))
+ , m_model(new QStringListModel(QStringList(), m_completer))
+ , m_highlighter(new ConfigurationSyntaxHighlighter(document()))
+{
+ 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);
+
+ connect(m_completer, SIGNAL(activated(QString)), this, SLOT(insertCompleterText(QString)));
+ connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateDocumentation()));
+}
+
+void ConfigurationEditor::setSettings(AbstractSettings *settings)
+{
+ QTC_CHECK(settings);
+ 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) {
+ QKeyEvent *key = static_cast<QKeyEvent *>(event);
+ 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();
+ QTextCursor cursor = cursorForTextUnderCursor();
+ 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);
+ while (!(ch.isNull() || ch.isSpace() || ch == QLatin1Char(':') || ch == QLatin1Char(','))) {
+ tc.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor);
+ ch = document()->characterAt(tc.position() - 1);
+ }
+ tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+ ch = document()->characterAt(tc.position());
+ while (!(ch.isNull() || ch.isSpace() || ch == QLatin1Char(':') || ch == QLatin1Char(','))) {
+ 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);
+ const int pos = cursor.selectedText().lastIndexOf(QLatin1Char(','));
+ 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