From a8e7628e8301248738a2c2bd9e6f67017a1f2102 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Fri, 28 Apr 2017 19:28:05 +1200 Subject: [PATCH 1/3] Enable highlighting when cursor is placed over a name (script editor) --- apps/opencs/model/prefs/state.cpp | 1 + apps/opencs/view/world/scriptedit.cpp | 23 ++++++++++++++++++++ apps/opencs/view/world/scriptedit.hpp | 3 +++ apps/opencs/view/world/scripthighlighter.cpp | 18 ++++++++++++--- apps/opencs/view/world/scripthighlighter.hpp | 7 +++++- 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/apps/opencs/model/prefs/state.cpp b/apps/opencs/model/prefs/state.cpp index b8d6102ac..6bef55c44 100644 --- a/apps/opencs/model/prefs/state.cpp +++ b/apps/opencs/model/prefs/state.cpp @@ -156,6 +156,7 @@ void CSMPrefs::State::declare() declareColour ("colour-int", "Highlight Colour: Integer Literals", QColor ("darkmagenta")); declareColour ("colour-float", "Highlight Colour: Float Literals", QColor ("magenta")); declareColour ("colour-name", "Highlight Colour: Names", QColor ("grey")); + declareColour ("colour-highlight", "Highlight Colour: Highlighting", QColor("palegreen")); declareColour ("colour-keyword", "Highlight Colour: Keywords", QColor ("red")); declareColour ("colour-special", "Highlight Colour: Special Characters", QColor ("darkorange")); declareColour ("colour-comment", "Highlight Colour: Comments", QColor ("green")); diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 6f27d5656..1581c3e34 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -49,6 +49,7 @@ CSVWorld::ScriptEdit::ScriptEdit( mDefaultFont(font()), mMonoFont(QFont("Monospace")), mTabCharCount(4), + mMarkOccurrencesRunning(false), mDocument(document), mWhiteListQoutes("^[a-z|_]{1}[a-z|0-9|_]{0,}$", Qt::CaseInsensitive) { @@ -85,6 +86,8 @@ CSVWorld::ScriptEdit::ScriptEdit( <setMarkedWord(word.toStdString()); + mHighlighter->rehighlight(); + + mMarkOccurrencesRunning = false; +} + void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) { QPlainTextEdit::resizeEvent(e); diff --git a/apps/opencs/view/world/scriptedit.hpp b/apps/opencs/view/world/scriptedit.hpp index 4977ed8e0..5c78470b3 100644 --- a/apps/opencs/view/world/scriptedit.hpp +++ b/apps/opencs/view/world/scriptedit.hpp @@ -54,6 +54,7 @@ namespace CSVWorld QFont mDefaultFont; QFont mMonoFont; int mTabCharCount; + bool mMarkOccurrencesRunning; protected: @@ -111,6 +112,8 @@ namespace CSVWorld void updateLineNumberAreaWidth(int newBlockCount); void updateLineNumberArea(const QRect &, int); + + void markOccurrences(); }; class LineNumberArea : public QWidget diff --git a/apps/opencs/view/world/scripthighlighter.cpp b/apps/opencs/view/world/scripthighlighter.cpp index 846a61b47..3041eff2d 100644 --- a/apps/opencs/view/world/scripthighlighter.cpp +++ b/apps/opencs/view/world/scripthighlighter.cpp @@ -72,7 +72,11 @@ void CSVWorld::ScriptHighlighter::highlight (const Compiler::TokenLoc& loc, Type // compensate for bug in Compiler::Scanner (position of token is the character after the token) index -= length; - setFormat (index, length, mScheme[type]); + QTextCharFormat scheme = mScheme[type]; + if (loc.mLiteral == mMarkedWord) + scheme.merge(mScheme[Type_Highlight]); + + setFormat (index, length, scheme); } CSVWorld::ScriptHighlighter::ScriptHighlighter (const CSMWorld::Data& data, Mode mode, @@ -105,6 +109,11 @@ void CSVWorld::ScriptHighlighter::highlightBlock (const QString& text) catch (...) {} // ignore syntax errors } +void CSVWorld::ScriptHighlighter::setMarkedWord(const std::string& name) +{ + mMarkedWord = name; +} + void CSVWorld::ScriptHighlighter::invalidateIds() { mContext.invalidateIds(); @@ -117,7 +126,7 @@ bool CSVWorld::ScriptHighlighter::settingChanged (const CSMPrefs::Setting *setti static const char *const colours[Type_Id+2] = { "colour-int", "colour-float", "colour-name", "colour-keyword", - "colour-special", "colour-comment", "colour-id", + "colour-special", "colour-comment", "colour-highlight", "colour-id", 0 }; @@ -125,7 +134,10 @@ bool CSVWorld::ScriptHighlighter::settingChanged (const CSMPrefs::Setting *setti if (setting->getKey()==colours[i]) { QTextCharFormat format; - format.setForeground (setting->toColor()); + if (i == Type_Highlight) + format.setBackground (setting->toColor()); + else + format.setForeground (setting->toColor()); mScheme[static_cast (i)] = format; return true; } diff --git a/apps/opencs/view/world/scripthighlighter.hpp b/apps/opencs/view/world/scripthighlighter.hpp index 33824da0d..d55cf4839 100644 --- a/apps/opencs/view/world/scripthighlighter.hpp +++ b/apps/opencs/view/world/scripthighlighter.hpp @@ -2,6 +2,7 @@ #define CSV_WORLD_SCRIPTHIGHLIGHTER_H #include +#include #include @@ -30,7 +31,8 @@ namespace CSVWorld Type_Keyword = 3, Type_Special = 4, Type_Comment = 5, - Type_Id = 6 + Type_Highlight = 6, + Type_Id = 7 }; enum Mode @@ -47,6 +49,7 @@ namespace CSVWorld CSMWorld::ScriptContext mContext; std::map mScheme; Mode mMode; + std::string mMarkedWord; private: @@ -91,6 +94,8 @@ namespace CSVWorld virtual void highlightBlock (const QString& text); + void setMarkedWord(const std::string& name); + void invalidateIds(); bool settingChanged (const CSMPrefs::Setting *setting); From cbb2b8b119e214123432760c024b234a5d638604 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Fri, 28 Apr 2017 19:57:49 +1200 Subject: [PATCH 2/3] Add setting so feature can be turned off --- apps/opencs/model/prefs/state.cpp | 1 + apps/opencs/view/world/scriptedit.cpp | 25 ++++++++++++++------ apps/opencs/view/world/scriptedit.hpp | 1 + apps/opencs/view/world/scripthighlighter.cpp | 7 +++++- apps/opencs/view/world/scripthighlighter.hpp | 3 +++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/apps/opencs/model/prefs/state.cpp b/apps/opencs/model/prefs/state.cpp index 6bef55c44..0d66caba6 100644 --- a/apps/opencs/model/prefs/state.cpp +++ b/apps/opencs/model/prefs/state.cpp @@ -152,6 +152,7 @@ void CSMPrefs::State::declare() setRange (0, 10000); declareInt ("error-height", "Initial height of the error panel", 100). setRange (100, 10000); + declareBool ("highlight-occurrences", "Highlight other occurrences of selected names", true); declareSeparator(); declareColour ("colour-int", "Highlight Colour: Integer Literals", QColor ("darkmagenta")); declareColour ("colour-float", "Highlight Colour: Float Literals", QColor ("magenta")); diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 1581c3e34..22beb0889 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -49,6 +49,7 @@ CSVWorld::ScriptEdit::ScriptEdit( mDefaultFont(font()), mMonoFont(QFont("Monospace")), mTabCharCount(4), + mMarkOccurrences(true), mMarkOccurrencesRunning(false), mDocument(document), mWhiteListQoutes("^[a-z|_]{1}[a-z|0-9|_]{0,}$", Qt::CaseInsensitive) @@ -233,6 +234,13 @@ void CSVWorld::ScriptEdit::settingChanged(const CSMPrefs::Setting *setting) mTabCharCount = setting->toInt(); setTabWidth(); } + else if (*setting == "Scripts/highlight-occurrences") + { + mMarkOccurrences = setting->isTrue(); + mHighlighter->setMarkedWord(""); + updateHighlighting(); + mHighlighter->setMarkOccurrences(mMarkOccurrences); + } } void CSVWorld::ScriptEdit::idListChanged() @@ -295,16 +303,19 @@ void CSVWorld::ScriptEdit::markOccurrences() if (mMarkOccurrencesRunning) return; - mMarkOccurrencesRunning = true; + if (mMarkOccurrences) + { + mMarkOccurrencesRunning = true; - QTextCursor cursor = textCursor(); - cursor.select(QTextCursor::WordUnderCursor); - QString word = cursor.selectedText(); + QTextCursor cursor = textCursor(); + cursor.select(QTextCursor::WordUnderCursor); + QString word = cursor.selectedText(); - mHighlighter->setMarkedWord(word.toStdString()); - mHighlighter->rehighlight(); + mHighlighter->setMarkedWord(word.toStdString()); + mHighlighter->rehighlight(); - mMarkOccurrencesRunning = false; + mMarkOccurrencesRunning = false; + } } void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e) diff --git a/apps/opencs/view/world/scriptedit.hpp b/apps/opencs/view/world/scriptedit.hpp index 5c78470b3..eb57aed1d 100644 --- a/apps/opencs/view/world/scriptedit.hpp +++ b/apps/opencs/view/world/scriptedit.hpp @@ -54,6 +54,7 @@ namespace CSVWorld QFont mDefaultFont; QFont mMonoFont; int mTabCharCount; + bool mMarkOccurrences; bool mMarkOccurrencesRunning; protected: diff --git a/apps/opencs/view/world/scripthighlighter.cpp b/apps/opencs/view/world/scripthighlighter.cpp index 3041eff2d..7e6ea5844 100644 --- a/apps/opencs/view/world/scripthighlighter.cpp +++ b/apps/opencs/view/world/scripthighlighter.cpp @@ -73,7 +73,7 @@ void CSVWorld::ScriptHighlighter::highlight (const Compiler::TokenLoc& loc, Type index -= length; QTextCharFormat scheme = mScheme[type]; - if (loc.mLiteral == mMarkedWord) + if (mMarkOccurrences && loc.mLiteral == mMarkedWord) scheme.merge(mScheme[Type_Highlight]); setFormat (index, length, scheme); @@ -109,6 +109,11 @@ void CSVWorld::ScriptHighlighter::highlightBlock (const QString& text) catch (...) {} // ignore syntax errors } +void CSVWorld::ScriptHighlighter::setMarkOccurrences(bool flag) +{ + mMarkOccurrences = flag; +} + void CSVWorld::ScriptHighlighter::setMarkedWord(const std::string& name) { mMarkedWord = name; diff --git a/apps/opencs/view/world/scripthighlighter.hpp b/apps/opencs/view/world/scripthighlighter.hpp index d55cf4839..a7d0fc2a1 100644 --- a/apps/opencs/view/world/scripthighlighter.hpp +++ b/apps/opencs/view/world/scripthighlighter.hpp @@ -49,6 +49,7 @@ namespace CSVWorld CSMWorld::ScriptContext mContext; std::map mScheme; Mode mMode; + bool mMarkOccurrences; std::string mMarkedWord; private: @@ -94,6 +95,8 @@ namespace CSVWorld virtual void highlightBlock (const QString& text); + void setMarkOccurrences(bool); + void setMarkedWord(const std::string& name); void invalidateIds(); From 829560719e49be50b39da324e5e7ad42a728ad21 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Tue, 2 May 2017 17:28:42 +1200 Subject: [PATCH 3/3] Make name highlighting more consistent and only for variable names --- apps/opencs/view/world/scriptedit.cpp | 23 ++++++++------------ apps/opencs/view/world/scriptedit.hpp | 1 - apps/opencs/view/world/scripthighlighter.cpp | 2 +- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/apps/opencs/view/world/scriptedit.cpp b/apps/opencs/view/world/scriptedit.cpp index 8cb4494bb..1105404b3 100644 --- a/apps/opencs/view/world/scriptedit.cpp +++ b/apps/opencs/view/world/scriptedit.cpp @@ -52,7 +52,6 @@ CSVWorld::ScriptEdit::ScriptEdit( mMonoFont(QFont("Monospace")), mTabCharCount(4), mMarkOccurrences(true), - mMarkOccurrencesRunning(false), mDocument(document), mWhiteListQoutes("^[a-z|_]{1}[a-z|0-9|_]{0,}$", Qt::CaseInsensitive) { @@ -90,7 +89,7 @@ CSVWorld::ScriptEdit::ScriptEdit( <setMarkedWord(word.toStdString()); mHighlighter->rehighlight(); - - mMarkOccurrencesRunning = false; } } diff --git a/apps/opencs/view/world/scriptedit.hpp b/apps/opencs/view/world/scriptedit.hpp index b1e70a4c8..b0a4b0577 100644 --- a/apps/opencs/view/world/scriptedit.hpp +++ b/apps/opencs/view/world/scriptedit.hpp @@ -56,7 +56,6 @@ namespace CSVWorld QFont mMonoFont; int mTabCharCount; bool mMarkOccurrences; - bool mMarkOccurrencesRunning; QAction *mCommentAction; QAction *mUncommentAction; diff --git a/apps/opencs/view/world/scripthighlighter.cpp b/apps/opencs/view/world/scripthighlighter.cpp index 7e6ea5844..6aba66053 100644 --- a/apps/opencs/view/world/scripthighlighter.cpp +++ b/apps/opencs/view/world/scripthighlighter.cpp @@ -73,7 +73,7 @@ void CSVWorld::ScriptHighlighter::highlight (const Compiler::TokenLoc& loc, Type index -= length; QTextCharFormat scheme = mScheme[type]; - if (mMarkOccurrences && loc.mLiteral == mMarkedWord) + if (mMarkOccurrences && type == Type_Name && loc.mLiteral == mMarkedWord) scheme.merge(mScheme[Type_Highlight]); setFormat (index, length, scheme);