CmakeEditor: Use the generic SyntaxHighlighter.

Removed the cmakehighlighter class, and use the
TextEditor::createGenericSyntaxHighlighter with the correct mime type
instead, as it provides a more robust code highlighting.

Task-number: QTCREATORBUG-3272
Change-Id: I96f6fa0912441500d7aa6ab3735bcfd49d9efa57
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
Charles Huet
2014-07-30 13:08:05 +02:00
committed by Daniel Teske
parent 6af4b5a178
commit 76efc1ef56
6 changed files with 5 additions and 216 deletions

View File

@@ -30,7 +30,6 @@
#include "cmakeeditor.h" #include "cmakeeditor.h"
#include "cmakefilecompletionassist.h" #include "cmakefilecompletionassist.h"
#include "cmakehighlighter.h"
#include "cmakeprojectconstants.h" #include "cmakeprojectconstants.h"
#include "cmakeproject.h" #include "cmakeproject.h"
@@ -38,12 +37,14 @@
#include <coreplugin/infobar.h> #include <coreplugin/infobar.h>
#include <coreplugin/actionmanager/actioncontainer.h> #include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/mimedatabase.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <texteditor/texteditoractionhandler.h> #include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h> #include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
#include <texteditor/highlighterutils.h>
#include <QFileInfo> #include <QFileInfo>
#include <QSharedPointer> #include <QSharedPointer>
@@ -247,13 +248,14 @@ CMakeEditorWidget::Link CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
// //
// CMakeDocument // CMakeDocument
// //
CMakeDocument::CMakeDocument() CMakeDocument::CMakeDocument()
: TextEditor::BaseTextDocument() : TextEditor::BaseTextDocument()
{ {
setId(CMakeProjectManager::Constants::CMAKE_EDITOR_ID); setId(CMakeProjectManager::Constants::CMAKE_EDITOR_ID);
setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE)); setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE));
setSyntaxHighlighter(new CMakeHighlighter);
Core::MimeType mimeType = Core::MimeDatabase::findByType(QLatin1String(Constants::CMAKEMIMETYPE));
setSyntaxHighlighter(TextEditor::createGenericSyntaxHighlighter(mimeType));
} }
QString CMakeDocument::defaultPath() const QString CMakeDocument::defaultPath() const

View File

@@ -1,140 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** 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 "cmakehighlighter.h"
#include <QRegExp>
#include <QTextDocument>
using namespace CMakeProjectManager::Internal;
static bool isVariable(const QByteArray &word)
{
if (word.length() < 4) // must be at least "${.}"
return false;
return word.startsWith("${") && word.endsWith('}');
}
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);
}
void CMakeHighlighter::highlightBlock(const QString &text)
{
QByteArray buf;
bool inCommentMode = false;
bool inStringMode = (previousBlockState() == 1);
QTextCharFormat emptyFormat;
int i=0;
for (i=0; i < text.length(); i++) {
char c = text.at(i).toLatin1();
if (inCommentMode) {
setFormat(i, 1, formatForCategory(CMakeCommentFormat));
} else {
if (c == '#') {
if (!inStringMode) {
inCommentMode = true;
setFormat(i, 1, formatForCategory(CMakeCommentFormat));
buf.clear();
} else {
buf += c;
}
} else if (c == '(') {
if (!inStringMode) {
if (!buf.isEmpty())
setFormat(i - buf.length(), buf.length(), formatForCategory(CMakeFunctionFormat));
buf.clear();
} else {
buf += c;
}
} else if (text.at(i).isSpace()) {
if (!inStringMode)
buf.clear();
else
buf += c;
} else if (c == '\"') {
buf += c;
if (inStringMode) {
setFormat(i + 1 - buf.length(), buf.length(), formatForCategory(CMakeStringFormat));
buf.clear();
} else {
setFormat(i, 1, formatForCategory(CMakeStringFormat));
}
inStringMode = !inStringMode;
} else if (c == '\\') {
setFormat(i, 1, emptyFormat);
buf += c;
i++;
if (i < text.length()) {
setFormat(i, 1, emptyFormat);
buf += c;
}
} else if (c == '$') {
if (inStringMode)
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(), formatForCategory(CMakeVariableFormat));
buf.clear();
}
} else {
buf += c;
setFormat(i, 1, emptyFormat);
}
}
}
if (inStringMode) {
setFormat(i - buf.length(), buf.length(), formatForCategory(CMakeStringFormat));
setCurrentBlockState(1);
} else {
setCurrentBlockState(0);
}
applyFormatToSpaces(text, formatForCategory(CMakeVisualWhiteSpaceFormat));
}

View File

@@ -1,60 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** 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.
**
****************************************************************************/
#ifndef CMAKEHIGHLIGHTER_H
#define CMAKEHIGHLIGHTER_H
#include <texteditor/syntaxhighlighter.h>
namespace CMakeProjectManager {
namespace Internal {
/* This is a simple syntax highlighter for CMake files.
* It highlights variables, commands, strings and comments.
* Multi-line strings and variables inside strings are also recognized. */
class CMakeHighlighter : public TextEditor::SyntaxHighlighter
{
Q_OBJECT
public:
enum CMakeFormats {
CMakeVariableFormat,
CMakeFunctionFormat,
CMakeCommentFormat,
CMakeStringFormat,
CMakeVisualWhiteSpaceFormat
};
CMakeHighlighter(QTextDocument *document = 0);
virtual void highlightBlock(const QString &text);
};
} // namespace Internal
} // namespace CMakeProjectManager
#endif // PROFILEHIGHLIGHTER_H

View File

@@ -12,7 +12,6 @@ HEADERS = cmakebuildinfo.h \
cmakebuildconfiguration.h \ cmakebuildconfiguration.h \
cmakeeditorfactory.h \ cmakeeditorfactory.h \
cmakeeditor.h \ cmakeeditor.h \
cmakehighlighter.h \
cmakelocatorfilter.h \ cmakelocatorfilter.h \
cmakefilecompletionassist.h \ cmakefilecompletionassist.h \
cmaketool.h \ cmaketool.h \
@@ -30,7 +29,6 @@ SOURCES = cmakeproject.cpp \
cmakebuildconfiguration.cpp \ cmakebuildconfiguration.cpp \
cmakeeditorfactory.cpp \ cmakeeditorfactory.cpp \
cmakeeditor.cpp \ cmakeeditor.cpp \
cmakehighlighter.cpp \
cmakelocatorfilter.cpp \ cmakelocatorfilter.cpp \
cmakefilecompletionassist.cpp \ cmakefilecompletionassist.cpp \
cmaketool.cpp \ cmaketool.cpp \

View File

@@ -29,8 +29,6 @@ QtcPlugin {
"cmakeeditorfactory.h", "cmakeeditorfactory.h",
"cmakefilecompletionassist.cpp", "cmakefilecompletionassist.cpp",
"cmakefilecompletionassist.h", "cmakefilecompletionassist.h",
"cmakehighlighter.cpp",
"cmakehighlighter.h",
"cmakelocatorfilter.cpp", "cmakelocatorfilter.cpp",
"cmakelocatorfilter.h", "cmakelocatorfilter.h",
"cmakeopenprojectwizard.cpp", "cmakeopenprojectwizard.cpp",

View File

@@ -36,12 +36,10 @@
#include "cmakeprojectconstants.h" #include "cmakeprojectconstants.h"
#include "cmakelocatorfilter.h" #include "cmakelocatorfilter.h"
#include "cmakefilecompletionassist.h" #include "cmakefilecompletionassist.h"
#include "cmakehighlighter.h"
#include <coreplugin/featureprovider.h> #include <coreplugin/featureprovider.h>
#include <coreplugin/mimedatabase.h> #include <coreplugin/mimedatabase.h>
#include <texteditor/texteditoractionhandler.h> #include <texteditor/texteditoractionhandler.h>
#include <texteditor/highlighterfactory.h>
#include <QtPlugin> #include <QtPlugin>
#include <QDebug> #include <QDebug>
@@ -73,13 +71,6 @@ bool CMakeProjectPlugin::initialize(const QStringList & /*arguments*/, QString *
addAutoReleasedObject(new CMakeLocatorFilter); addAutoReleasedObject(new CMakeLocatorFilter);
addAutoReleasedObject(new CMakeFileCompletionAssistProvider(cmp)); addAutoReleasedObject(new CMakeFileCompletionAssistProvider(cmp));
auto hf = new TextEditor::HighlighterFactory;
hf->setProductType<CMakeHighlighter>();
hf->setId(CMakeProjectManager::Constants::CMAKE_EDITOR_ID);
hf->addMimeType(CMakeProjectManager::Constants::CMAKEMIMETYPE);
hf->addMimeType(CMakeProjectManager::Constants::CMAKEPROJECTMIMETYPE);
addAutoReleasedObject(hf);
return true; return true;
} }