Add common interface for text formats inside syntax highlighter

Change-Id: I87f64446161a57aea0896f68e4eafacef791969b
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
jkobus
2013-08-13 12:57:31 +02:00
committed by Jarek Kobus
parent 760aa0f8bc
commit e8801167aa
69 changed files with 489 additions and 531 deletions

View File

@@ -40,13 +40,13 @@
#include <coreplugin/actionmanager/actionmanager.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <QFileInfo>
#include <QSharedPointer>
#include <QTextBlock>
using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
@@ -141,27 +141,6 @@ void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
showDefaultContextMenu(e, Constants::M_CONTEXT);
}
void CMakeEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
CMakeHighlighter *highlighter = qobject_cast<CMakeHighlighter*>(baseTextDocument()->syntaxHighlighter());
if (!highlighter)
return;
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_LABEL // variables
<< TextEditor::C_KEYWORD // functions
<< TextEditor::C_COMMENT
<< TextEditor::C_STRING
<< TextEditor::C_VISUAL_WHITESPACE;
}
const QVector<QTextCharFormat> formats = fs.toTextCharFormats(categories);
highlighter->setFormats(formats.constBegin(), formats.constEnd());
highlighter->rehighlight();
}
static bool isValidFileNameChar(const QChar &c)
{
if (c.isLetterOrNumber()

View File

@@ -85,7 +85,6 @@ protected:
public slots:
void unCommentSelection();
void setFontSettings(const TextEditor::FontSettings &);
private:
CMakeEditorFactory *m_factory;

View File

@@ -30,9 +30,7 @@
#include "cmakehighlighter.h"
#include <QRegExp>
#include <QColor>
#include <QTextDocument>
#include <QTextEdit>
using namespace CMakeProjectManager::Internal;
@@ -48,6 +46,15 @@ static bool isVariable(const QByteArray &word)
CMakeHighlighter::CMakeHighlighter(QTextDocument *document) :
TextEditor::SyntaxHighlighter(document)
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_LABEL // variables
<< TextEditor::C_KEYWORD // functions
<< TextEditor::C_COMMENT
<< TextEditor::C_STRING
<< TextEditor::C_VISUAL_WHITESPACE;
}
setTextFormatCategories(categories);
}
@@ -62,12 +69,12 @@ void CMakeHighlighter::highlightBlock(const QString &text)
for (i=0; i < text.length(); i++) {
char c = text.at(i).toLatin1();
if (inCommentMode) {
setFormat(i, 1, m_formats[CMakeCommentFormat]);
setFormat(i, 1, formatForCategory(CMakeCommentFormat));
} else {
if (c == '#') {
if (!inStringMode) {
inCommentMode = true;
setFormat(i, 1, m_formats[CMakeCommentFormat]);
setFormat(i, 1, formatForCategory(CMakeCommentFormat));
buf.clear();
} else {
buf += c;
@@ -75,7 +82,7 @@ void CMakeHighlighter::highlightBlock(const QString &text)
} else if (c == '(') {
if (!inStringMode) {
if (!buf.isEmpty())
setFormat(i - buf.length(), buf.length(), m_formats[CMakeFunctionFormat]);
setFormat(i - buf.length(), buf.length(), formatForCategory(CMakeFunctionFormat));
buf.clear();
} else {
buf += c;
@@ -88,10 +95,10 @@ void CMakeHighlighter::highlightBlock(const QString &text)
} else if (c == '\"') {
buf += c;
if (inStringMode) {
setFormat(i + 1 - buf.length(), buf.length(), m_formats[CMakeStringFormat]);
setFormat(i + 1 - buf.length(), buf.length(), formatForCategory(CMakeStringFormat));
buf.clear();
} else {
setFormat(i, 1, m_formats[CMakeStringFormat]);
setFormat(i, 1, formatForCategory(CMakeStringFormat));
}
inStringMode = !inStringMode;
} else if (c == '\\') {
@@ -105,14 +112,14 @@ void CMakeHighlighter::highlightBlock(const QString &text)
}
} else if (c == '$') {
if (inStringMode)
setFormat(i - buf.length(), buf.length(), m_formats[CMakeStringFormat]);
setFormat(i - buf.length(), buf.length(), formatForCategory(CMakeStringFormat));
buf.clear();
buf += c;
setFormat(i, 1, emptyFormat);
} else if (c == '}') {
buf += c;
if (isVariable(buf)) {
setFormat(i + 1 - buf.length(), buf.length(), m_formats[CMakeVariableFormat]);
setFormat(i + 1 - buf.length(), buf.length(), formatForCategory(CMakeVariableFormat));
buf.clear();
}
} else {
@@ -123,12 +130,12 @@ void CMakeHighlighter::highlightBlock(const QString &text)
}
if (inStringMode) {
setFormat(i - buf.length(), buf.length(), m_formats[CMakeStringFormat]);
setFormat(i - buf.length(), buf.length(), formatForCategory(CMakeStringFormat));
setCurrentBlockState(1);
} else {
setCurrentBlockState(0);
}
applyFormatToSpaces(text, m_formats[CMakeVisualWhiteSpaceFormat]);
applyFormatToSpaces(text, formatForCategory(CMakeVisualWhiteSpaceFormat));
}

View File

@@ -31,9 +31,6 @@
#define CMAKEHIGHLIGHTER_H
#include <texteditor/syntaxhighlighter.h>
#include <QtAlgorithms>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
namespace CMakeProjectManager {
namespace Internal {
@@ -50,21 +47,11 @@ public:
CMakeFunctionFormat,
CMakeCommentFormat,
CMakeStringFormat,
CMakeVisualWhiteSpaceFormat,
NumCMakeFormats
CMakeVisualWhiteSpaceFormat
};
CMakeHighlighter(QTextDocument *document = 0);
virtual void highlightBlock(const QString &text);
// Set formats from a sequence of type QTextCharFormat
template <class InputIterator>
void setFormats(InputIterator begin, InputIterator end) {
qCopy(begin, end, m_formats);
}
private:
QTextCharFormat m_formats[NumCMakeFormats];
};
} // namespace Internal