Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(INCLUDE_FILES
include/QLuaHighlighter
include/QPythonHighlighter
include/QFramedTextAttribute
include/QYAMLHighlighter
include/internal/QHighlightRule.hpp
include/internal/QHighlightBlockRule.hpp
include/internal/QCodeEditor.hpp
Expand All @@ -42,6 +43,7 @@ set(INCLUDE_FILES
include/internal/QGLSLHighlighter.hpp
include/internal/QLanguage.hpp
include/internal/QXMLHighlighter.hpp
include/internal/QYAMLHighlighter.hpp
include/internal/QJSONHighlighter.hpp
include/internal/QLuaCompleter.hpp
include/internal/QLuaHighlighter.hpp
Expand All @@ -60,6 +62,7 @@ set(SOURCE_FILES
src/internal/QGLSLHighlighter.cpp
src/internal/QLanguage.cpp
src/internal/QXMLHighlighter.cpp
src/internal/QYAMLHighlighter.cpp
src/internal/QJSONHighlighter.cpp
src/internal/QLuaCompleter.cpp
src/internal/QLuaHighlighter.cpp
Expand Down
31 changes: 31 additions & 0 deletions example/resources/code_samples/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# app/config/config_prod.yml
imports:
- { resource: config.yml }

monolog:
handlers:
main:
type: fingers_crossed
action_level: critical
handler: grouped
grouped:
type: group
members: [streamed, deduplicated]
streamed:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
deduplicated:
type: deduplication
handler: swift
swift:
noone: null
type: swift_mailer
from_email: '[email protected]'
# Or multiple receivers:
# to_email: ['[email protected]', '[email protected]']
to_email: "[email protected]"
subject: 'An Error Occurred! %%message%%'
level: debug
formatter: monolog.formatter.html
content_type: text/html
1 change: 1 addition & 0 deletions example/resources/demo_resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<file>code_samples/json.json</file>
<file>code_samples/lua.lua</file>
<file>code_samples/python.py</file>
<file>code_samples/yaml.yaml</file>
</qresource>
</RCC>
5 changes: 4 additions & 1 deletion example/src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QJSONHighlighter>
#include <QLuaHighlighter>
#include <QPythonHighlighter>
#include <QYAMLHighlighter>

// Qt
#include <QComboBox>
Expand Down Expand Up @@ -55,7 +56,8 @@ void MainWindow::initData()
{"XML", loadCode(":/code_samples/xml.xml")},
{"JSON", loadCode(":/code_samples/json.json")},
{"LUA", loadCode(":/code_samples/lua.lua")},
{"Python", loadCode(":/code_samples/python.py")}
{"Python", loadCode(":/code_samples/python.py")},
{"YAML", loadCode(":/code_samples/yaml.yaml")}
};

m_completers = {
Expand All @@ -73,6 +75,7 @@ void MainWindow::initData()
{"JSON", new QJSONHighlighter},
{"LUA", new QLuaHighlighter},
{"Python", new QPythonHighlighter},
{"YAML", new QYAMLHighlighter}
};

m_styles = {
Expand Down
86 changes: 0 additions & 86 deletions example/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,6 @@
// Demo
#include <MainWindow.hpp>

const char* codeSample = R"(
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** 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 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.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QCoreApplication>
#include <QTextStream>

#include "game.h"
//! [0]
int main(int argc, char *argv[])
{
)" "\t" R"(QCoreApplication app(argc, argv);
QStringList args = QCoreApplication::arguments();
bool newGame = true;
if (args.length() > 1)
newGame = (args[1].toLower() != QStringLiteral("load"));
bool json = true;
if (args.length() > 2)
json = (args[2].toLower() != QStringLiteral("binary"));

Game game;
if (newGame)
game.newGame();
else if (!game.loadGame(json ? Game::Json : Game::Binary))
return 1;
// Game is played; changes are made...
//! [0]
//! [1]
QTextStream(stdout) << "Game ended in the following state:\n";
game.print();
if (!game.saveGame(json ? Game::Json : Game::Binary))
return 1;

return 0;
}
//! [1]

)";

int main(int argc, char** argv)
{
// Creating application
Expand Down
3 changes: 3 additions & 0 deletions include/QYAMLHighlighter
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include <internal/QYAMLHighlighter.hpp>
39 changes: 39 additions & 0 deletions include/internal/QYAMLHighlighter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

// QCodeEditor
#include <QStyleSyntaxHighlighter>

// Qt
#include <QRegularExpression>

/**
* @brief Class, that describes YAML code
* highlighter.
*/
class QYAMLHighlighter : public QStyleSyntaxHighlighter
{
Q_OBJECT
public:

explicit QYAMLHighlighter(QTextDocument* document=nullptr);

protected:

void highlightBlock(const QString& text) override;

private:
void highlightByRegex(const QTextCharFormat& format,
const QRegularExpression& regex,
const QString& text);

QVector<QRegularExpression> m_yamlKeywordRegexes;
QRegularExpression m_yamlValuesRegex;
QRegularExpression m_yamlBooleansRegex;
QRegularExpression m_yamlNumbersRegex;
QRegularExpression m_yamlArraysRegex;
QRegularExpression m_yamlReservedRegex;
QRegularExpression m_yamlCommentRegex;
QRegularExpression m_yamlStringRegex;
QRegularExpression m_yamlEqualSignRegex;
};

97 changes: 97 additions & 0 deletions src/internal/QYAMLHighlighter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <QYAMLHighlighter>
#include <QSyntaxStyle>


QYAMLHighlighter::QYAMLHighlighter(QTextDocument *document)
: QStyleSyntaxHighlighter(document),
m_yamlKeywordRegexes(),
m_yamlValuesRegex(R"(:\s.+$)"),
m_yamlBooleansRegex(R"( (y|yes|n|no|true|false|on|off)$)"),
m_yamlNumbersRegex(R"( [[:digit:]]+(\.[[:digit:]]+)?)"),
m_yamlArraysRegex(R"(\[" "\]" ":\s+[|>]" "^\s*- )"),
m_yamlReservedRegex(R"((^| )!!(binary|bool|float|int|map|null|omap|seq|set|str) )"),
m_yamlCommentRegex(R"(#.*$)"),
m_yamlStringRegex(R"(["'][^'"]*["'])"),
m_yamlEqualSignRegex(R"(:( |$))")
{
m_yamlKeywordRegexes
<< QRegularExpression(R"(^\s*[\$A-Za-z0-9_-]+\:)")
<< QRegularExpression(R"(^\s*@[\$A-Za-z0-9_-]+\:)");
}

void QYAMLHighlighter::highlightBlock(const QString &text)
{
for (const auto& regex : m_yamlKeywordRegexes)
{
highlightByRegex(
syntaxStyle()->getFormat("Keyword"),
regex,
text
);
}

highlightByRegex(
syntaxStyle()->getFormat("Text"),
m_yamlValuesRegex,
text
);

highlightByRegex(
syntaxStyle()->getFormat("Text"),
m_yamlBooleansRegex,
text
);

highlightByRegex(
syntaxStyle()->getFormat("Number"),
m_yamlNumbersRegex,
text
);

highlightByRegex(
syntaxStyle()->getFormat("Text"),
m_yamlArraysRegex,
text
);

highlightByRegex(
syntaxStyle()->getFormat("Keyword"),
m_yamlReservedRegex,
text
);

highlightByRegex(
syntaxStyle()->getFormat("String"),
m_yamlStringRegex,
text
);

highlightByRegex(
syntaxStyle()->getFormat("Text"),
m_yamlEqualSignRegex,
text
);

highlightByRegex(
syntaxStyle()->getFormat("Comment"),
m_yamlCommentRegex,
text
);
}

void
QYAMLHighlighter::highlightByRegex(const QTextCharFormat& format, const QRegularExpression& regex, const QString& text)
{
auto matchIterator = regex.globalMatch(text);

while (matchIterator.hasNext())
{
auto match = matchIterator.next();

setFormat(
match.capturedStart(),
match.capturedLength(),
format
);
}
}