From 128498a6786fb9ca193006a79453347b8f876c8b Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Sat, 20 Dec 2014 17:27:18 +0100 Subject: [PATCH] Allow reading the keyword lists from the color scheme files. --- src/scadlexer.cpp | 86 ++++++++++++++++++++++++----------------- src/scadlexer.h | 4 +- src/scintillaeditor.cpp | 66 +++++++++++++++++++++---------- src/scintillaeditor.h | 1 + 4 files changed, 100 insertions(+), 57 deletions(-) diff --git a/src/scadlexer.cpp b/src/scadlexer.cpp index cb04d2c7..9fc2d4d3 100644 --- a/src/scadlexer.cpp +++ b/src/scadlexer.cpp @@ -1,47 +1,63 @@ +#include + #include "scadlexer.h" -ScadLexer::ScadLexer(QObject *parent) - : QsciLexerCPP(parent) -{ } +ScadLexer::ScadLexer(QObject *parent) : QsciLexerCPP(parent) +{ + // -> Style: Keyword (lexer.l) + keywordSet[0] = + "if else let for module function true false undef " + "include use"; + + // -> Style: KeywordSet2 (func.cc) + keywordSet[1] = + "abs sign rands min max sin cos asin acos tan atan atan2 " + "round ceil floor pow sqrt exp len log ln str chr concat " + "lookup search version version_num norm cross parent_module " + "dxf_dim dxf_cross"; + + // -> used in comments only like /*! \cube */ + keywordSet[2] = + "struct union enum fn var def typedef file namespace package " + "interface param see return class brief"; + + // -> Style: GlobalClass + keywordSet[3] = + "cube sphere cylinder polyhedron square circle polygon text " + "minkowski hull resize child echo union difference " + "intersection linear_extrude rotate_extrude import group " + "projection render surface scale rotate mirror translate " + "multmatrix color offset "; +} ScadLexer::~ScadLexer() -{ } +{ +} const char *ScadLexer::language() const { - return "SCAD"; + return "SCAD"; +} + +void ScadLexer::setKeywords(int set, const std::string& keywords) +{ + if ((set < 1) || (set > 4)) { + return; + } + + std::string trimmedKeywords(keywords); + boost::algorithm::trim(trimmedKeywords); + if (trimmedKeywords.empty()) { + return; + } + + keywordSet[set - 1] = trimmedKeywords; } const char *ScadLexer::keywords(int set) const { - if (set == 1) { - // -> Style: Keyword (lexer.l / primitives.cc) - return "if else let for module function true false undef " - "cube sphere cylinder polyhedron square circle polygon text " - "include use"; - } - - if (set == 2) { - // -> Style: KeywordSet2 (func.cc) - return "abs sign rands min max sin cos asin acos tan atan atan2 " - "round ceil floor pow sqrt exp len log ln str chr concat " - "lookup search version version_num norm cross parent_module " - "dxf_dim dxf_cross"; - } - - if (set == 3) { - // -> used in comments only like /*! \cube */ - return "struct union enum fn var def typedef file namespace package " - "interface param see return class brief"; - } - - if (set == 4) { - // -> Style: GlobalClass - return "minkowski hull resize child echo union difference " - "intersection linear_extrude rotate_extrude import group " - "projection render surface scale rotate mirror translate " - "multmatrix color offset "; - } - - return 0; + if ((set < 1) || (set > 4)) { + return 0; + } + return keywordSet[set - 1].c_str(); } diff --git a/src/scadlexer.h b/src/scadlexer.h index 50e6347a..b6a349d7 100644 --- a/src/scadlexer.h +++ b/src/scadlexer.h @@ -12,8 +12,10 @@ public: virtual ~ScadLexer(); const char *language() const; const char *keywords(int set) const; - + + void setKeywords(int set, const std::string& keywords); private: + std::string keywordSet[4]; ScadLexer(const ScadLexer &); ScadLexer &operator=(const ScadLexer &); }; diff --git a/src/scintillaeditor.cpp b/src/scintillaeditor.cpp index 0612ef9d..2d0f07d0 100644 --- a/src/scintillaeditor.cpp +++ b/src/scintillaeditor.cpp @@ -89,7 +89,7 @@ ScintillaEditor::ScintillaEditor(QWidget *parent) : EditorInterface(parent) qsci->setIndentationsUseTabs(false); lexer = new ScadLexer(this); - initLexer(); + qsci->setLexer(lexer); initMargin(); qsci->setFolding(QsciScintilla::BoxedTreeFoldStyle, 4); qsci->setCaretLineVisible(true); @@ -150,6 +150,16 @@ QColor ScintillaEditor::readColor(const boost::property_tree::ptree &pt, const s } } +std::string ScintillaEditor::readString(const boost::property_tree::ptree &pt, const std::string name, const std::string defaultValue) +{ + try { + const std::string val = pt.get(name); + return val; + } catch (std::exception e) { + return defaultValue; + } +} + int ScintillaEditor::readInt(const boost::property_tree::ptree &pt, const std::string name, const int defaultValue) { try { @@ -165,28 +175,47 @@ void ScintillaEditor::setColormap(const EditorColorScheme *colorScheme) const boost::property_tree::ptree & pt = colorScheme->propertyTree(); try { + QFont font = lexer->font(lexer->defaultStyle()); const QColor textColor(pt.get("text").c_str()); const QColor paperColor(pt.get("paper").c_str()); - lexer->setColor(textColor); - lexer->setPaper(paperColor); + ScadLexer *l = new ScadLexer(this); + + // Keywords must be set before the lexer is attached to QScintilla + // as they seem to be read and cached at attach time. + boost::optional keywords = pt.get_child_optional("keywords"); + if (keywords.is_initialized()) { + l->setKeywords(1, readString(keywords.get(), "keyword-set1", "")); + l->setKeywords(2, readString(keywords.get(), "keyword-set2", "")); + l->setKeywords(3, readString(keywords.get(), "keyword-set-doc", "")); + l->setKeywords(4, readString(keywords.get(), "keyword-set3", "")); + } + + qsci->setLexer(l); + delete lexer; + lexer = l; + + // All other properties must be set after attaching to QSCintilla so + // the editor gets the change events and updates itself to match + l->setFont(font); + l->setColor(textColor); + l->setPaper(paperColor); const boost::property_tree::ptree& colors = pt.get_child("colors"); - lexer->setColor(readColor(colors, "keyword1", textColor), QsciLexerCPP::Keyword); - lexer->setColor(readColor(colors, "keyword2", textColor), QsciLexerCPP::KeywordSet2); - lexer->setColor(readColor(colors, "keyword3", textColor), QsciLexerCPP::GlobalClass); - lexer->setColor(readColor(colors, "number", textColor), QsciLexerCPP::Number); - lexer->setColor(readColor(colors, "string", textColor), QsciLexerCPP::SingleQuotedString); - lexer->setColor(readColor(colors, "string", textColor), QsciLexerCPP::DoubleQuotedString); - lexer->setColor(readColor(colors, "operator", textColor), QsciLexerCPP::Operator); - lexer->setColor(readColor(colors, "comment", textColor), QsciLexerCPP::Comment); - lexer->setColor(readColor(colors, "commentline", textColor), QsciLexerCPP::CommentLine); - lexer->setColor(readColor(colors, "commentdoc", textColor), QsciLexerCPP::CommentDoc); - lexer->setColor(readColor(colors, "commentdoc", textColor), QsciLexerCPP::CommentLineDoc); - lexer->setColor(readColor(colors, "commentdockeyword", textColor), QsciLexerCPP::CommentDocKeyword); + l->setColor(readColor(colors, "keyword1", textColor), QsciLexerCPP::Keyword); + l->setColor(readColor(colors, "keyword2", textColor), QsciLexerCPP::KeywordSet2); + l->setColor(readColor(colors, "keyword3", textColor), QsciLexerCPP::GlobalClass); + l->setColor(readColor(colors, "number", textColor), QsciLexerCPP::Number); + l->setColor(readColor(colors, "string", textColor), QsciLexerCPP::SingleQuotedString); + l->setColor(readColor(colors, "string", textColor), QsciLexerCPP::DoubleQuotedString); + l->setColor(readColor(colors, "operator", textColor), QsciLexerCPP::Operator); + l->setColor(readColor(colors, "comment", textColor), QsciLexerCPP::Comment); + l->setColor(readColor(colors, "commentline", textColor), QsciLexerCPP::CommentLine); + l->setColor(readColor(colors, "commentdoc", textColor), QsciLexerCPP::CommentDoc); + l->setColor(readColor(colors, "commentdoc", textColor), QsciLexerCPP::CommentLineDoc); + l->setColor(readColor(colors, "commentdockeyword", textColor), QsciLexerCPP::CommentDocKeyword); const boost::property_tree::ptree& caret = pt.get_child("caret"); - qsci->setCaretWidth(readInt(caret, "width", 1)); qsci->setCaretForegroundColor(readColor(caret, "foreground", textColor)); qsci->setCaretLineBackgroundColor(readColor(caret, "line-background", paperColor)); @@ -356,11 +385,6 @@ void ScintillaEditor::initFont(const QString& fontName, uint size) lexer->setFont(font); } -void ScintillaEditor::initLexer() -{ - qsci->setLexer(lexer); -} - void ScintillaEditor::initMargin() { QFontMetrics fontmetrics = QFontMetrics(qsci->font()); diff --git a/src/scintillaeditor.h b/src/scintillaeditor.h index 3b582ea6..42fcd274 100644 --- a/src/scintillaeditor.h +++ b/src/scintillaeditor.h @@ -58,6 +58,7 @@ private: void getRange(int *lineFrom, int *lineTo); void setColormap(const EditorColorScheme *colorScheme); int readInt(const boost::property_tree::ptree &pt, const std::string name, const int defaultValue); + std::string readString(const boost::property_tree::ptree &pt, const std::string name, const std::string defaultValue); QColor readColor(const boost::property_tree::ptree &pt, const std::string name, const QColor defaultColor); void enumerateColorSchemesInPath(colorscheme_set_t &result_set, const fs::path path); colorscheme_set_t enumerateColorSchemes();