diff --git a/CMakeLists.txt b/CMakeLists.txt index 40d5617..3eb93d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -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 @@ -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 diff --git a/example/resources/code_samples/yaml.yaml b/example/resources/code_samples/yaml.yaml new file mode 100644 index 0000000..a5fade3 --- /dev/null +++ b/example/resources/code_samples/yaml.yaml @@ -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: 'from_email@test.com' + # Or multiple receivers: + # to_email: ['to_email1@ourcodeworld.com', 'to_email2@ourcodeworld.com'] + to_email: "to_email@ourcodeworld.com" + subject: 'An Error Occurred! %%message%%' + level: debug + formatter: monolog.formatter.html + content_type: text/html \ No newline at end of file diff --git a/example/resources/demo_resources.qrc b/example/resources/demo_resources.qrc index 146657b..007565f 100644 --- a/example/resources/demo_resources.qrc +++ b/example/resources/demo_resources.qrc @@ -7,5 +7,6 @@ code_samples/json.json code_samples/lua.lua code_samples/python.py + code_samples/yaml.yaml diff --git a/example/src/MainWindow.cpp b/example/src/MainWindow.cpp index 5552835..c70232b 100644 --- a/example/src/MainWindow.cpp +++ b/example/src/MainWindow.cpp @@ -13,6 +13,7 @@ #include #include #include +#include // Qt #include @@ -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 = { @@ -73,6 +75,7 @@ void MainWindow::initData() {"JSON", new QJSONHighlighter}, {"LUA", new QLuaHighlighter}, {"Python", new QPythonHighlighter}, + {"YAML", new QYAMLHighlighter} }; m_styles = { diff --git a/example/src/main.cpp b/example/src/main.cpp index be4ca0b..17cf38d 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -5,92 +5,6 @@ // Demo #include -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 -#include - -#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 diff --git a/include/QYAMLHighlighter b/include/QYAMLHighlighter new file mode 100644 index 0000000..b5e7bcd --- /dev/null +++ b/include/QYAMLHighlighter @@ -0,0 +1,3 @@ +#pragma once + +#include \ No newline at end of file diff --git a/include/internal/QYAMLHighlighter.hpp b/include/internal/QYAMLHighlighter.hpp new file mode 100644 index 0000000..9999b12 --- /dev/null +++ b/include/internal/QYAMLHighlighter.hpp @@ -0,0 +1,39 @@ +#pragma once + +// QCodeEditor +#include + +// Qt +#include + +/** + * @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 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; +}; + diff --git a/src/internal/QYAMLHighlighter.cpp b/src/internal/QYAMLHighlighter.cpp new file mode 100644 index 0000000..2fab3ea --- /dev/null +++ b/src/internal/QYAMLHighlighter.cpp @@ -0,0 +1,97 @@ +#include +#include + + +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 + ); + } +}