mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-19 23:23:52 +00:00
Enable highlighting when cursor is placed over a name (script editor)
This commit is contained in:
parent
80d1bb571c
commit
a8e7628e83
5 changed files with 48 additions and 4 deletions
|
@ -156,6 +156,7 @@ void CSMPrefs::State::declare()
|
||||||
declareColour ("colour-int", "Highlight Colour: Integer Literals", QColor ("darkmagenta"));
|
declareColour ("colour-int", "Highlight Colour: Integer Literals", QColor ("darkmagenta"));
|
||||||
declareColour ("colour-float", "Highlight Colour: Float Literals", QColor ("magenta"));
|
declareColour ("colour-float", "Highlight Colour: Float Literals", QColor ("magenta"));
|
||||||
declareColour ("colour-name", "Highlight Colour: Names", QColor ("grey"));
|
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-keyword", "Highlight Colour: Keywords", QColor ("red"));
|
||||||
declareColour ("colour-special", "Highlight Colour: Special Characters", QColor ("darkorange"));
|
declareColour ("colour-special", "Highlight Colour: Special Characters", QColor ("darkorange"));
|
||||||
declareColour ("colour-comment", "Highlight Colour: Comments", QColor ("green"));
|
declareColour ("colour-comment", "Highlight Colour: Comments", QColor ("green"));
|
||||||
|
|
|
@ -49,6 +49,7 @@ CSVWorld::ScriptEdit::ScriptEdit(
|
||||||
mDefaultFont(font()),
|
mDefaultFont(font()),
|
||||||
mMonoFont(QFont("Monospace")),
|
mMonoFont(QFont("Monospace")),
|
||||||
mTabCharCount(4),
|
mTabCharCount(4),
|
||||||
|
mMarkOccurrencesRunning(false),
|
||||||
mDocument(document),
|
mDocument(document),
|
||||||
mWhiteListQoutes("^[a-z|_]{1}[a-z|0-9|_]{0,}$", Qt::CaseInsensitive)
|
mWhiteListQoutes("^[a-z|_]{1}[a-z|0-9|_]{0,}$", Qt::CaseInsensitive)
|
||||||
{
|
{
|
||||||
|
@ -86,6 +87,8 @@ CSVWorld::ScriptEdit::ScriptEdit(
|
||||||
<<CSMWorld::UniversalId::Type_Script
|
<<CSMWorld::UniversalId::Type_Script
|
||||||
<<CSMWorld::UniversalId::Type_Region;
|
<<CSMWorld::UniversalId::Type_Region;
|
||||||
|
|
||||||
|
connect(this, SIGNAL(selectionChanged()), this, SLOT(markOccurrences()));
|
||||||
|
|
||||||
mHighlighter = new ScriptHighlighter (document.getData(), mode, ScriptEdit::document());
|
mHighlighter = new ScriptHighlighter (document.getData(), mode, ScriptEdit::document());
|
||||||
|
|
||||||
connect (&document.getData(), SIGNAL (idListChanged()), this, SLOT (idListChanged()));
|
connect (&document.getData(), SIGNAL (idListChanged()), this, SLOT (idListChanged()));
|
||||||
|
@ -284,6 +287,26 @@ void CSVWorld::ScriptEdit::updateLineNumberArea(const QRect &rect, int dy)
|
||||||
updateLineNumberAreaWidth(0);
|
updateLineNumberAreaWidth(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSVWorld::ScriptEdit::markOccurrences()
|
||||||
|
{
|
||||||
|
// prevent infinite recursion with cursor.select(),
|
||||||
|
// which ends up calling this function again
|
||||||
|
// could be fixed with blockSignals, but mDocument is const
|
||||||
|
if (mMarkOccurrencesRunning)
|
||||||
|
return;
|
||||||
|
|
||||||
|
mMarkOccurrencesRunning = true;
|
||||||
|
|
||||||
|
QTextCursor cursor = textCursor();
|
||||||
|
cursor.select(QTextCursor::WordUnderCursor);
|
||||||
|
QString word = cursor.selectedText();
|
||||||
|
|
||||||
|
mHighlighter->setMarkedWord(word.toStdString());
|
||||||
|
mHighlighter->rehighlight();
|
||||||
|
|
||||||
|
mMarkOccurrencesRunning = false;
|
||||||
|
}
|
||||||
|
|
||||||
void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e)
|
void CSVWorld::ScriptEdit::resizeEvent(QResizeEvent *e)
|
||||||
{
|
{
|
||||||
QPlainTextEdit::resizeEvent(e);
|
QPlainTextEdit::resizeEvent(e);
|
||||||
|
|
|
@ -54,6 +54,7 @@ namespace CSVWorld
|
||||||
QFont mDefaultFont;
|
QFont mDefaultFont;
|
||||||
QFont mMonoFont;
|
QFont mMonoFont;
|
||||||
int mTabCharCount;
|
int mTabCharCount;
|
||||||
|
bool mMarkOccurrencesRunning;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
@ -111,6 +112,8 @@ namespace CSVWorld
|
||||||
void updateLineNumberAreaWidth(int newBlockCount);
|
void updateLineNumberAreaWidth(int newBlockCount);
|
||||||
|
|
||||||
void updateLineNumberArea(const QRect &, int);
|
void updateLineNumberArea(const QRect &, int);
|
||||||
|
|
||||||
|
void markOccurrences();
|
||||||
};
|
};
|
||||||
|
|
||||||
class LineNumberArea : public QWidget
|
class LineNumberArea : public QWidget
|
||||||
|
|
|
@ -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)
|
// compensate for bug in Compiler::Scanner (position of token is the character after the token)
|
||||||
index -= length;
|
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,
|
CSVWorld::ScriptHighlighter::ScriptHighlighter (const CSMWorld::Data& data, Mode mode,
|
||||||
|
@ -105,6 +109,11 @@ void CSVWorld::ScriptHighlighter::highlightBlock (const QString& text)
|
||||||
catch (...) {} // ignore syntax errors
|
catch (...) {} // ignore syntax errors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSVWorld::ScriptHighlighter::setMarkedWord(const std::string& name)
|
||||||
|
{
|
||||||
|
mMarkedWord = name;
|
||||||
|
}
|
||||||
|
|
||||||
void CSVWorld::ScriptHighlighter::invalidateIds()
|
void CSVWorld::ScriptHighlighter::invalidateIds()
|
||||||
{
|
{
|
||||||
mContext.invalidateIds();
|
mContext.invalidateIds();
|
||||||
|
@ -117,7 +126,7 @@ bool CSVWorld::ScriptHighlighter::settingChanged (const CSMPrefs::Setting *setti
|
||||||
static const char *const colours[Type_Id+2] =
|
static const char *const colours[Type_Id+2] =
|
||||||
{
|
{
|
||||||
"colour-int", "colour-float", "colour-name", "colour-keyword",
|
"colour-int", "colour-float", "colour-name", "colour-keyword",
|
||||||
"colour-special", "colour-comment", "colour-id",
|
"colour-special", "colour-comment", "colour-highlight", "colour-id",
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -125,7 +134,10 @@ bool CSVWorld::ScriptHighlighter::settingChanged (const CSMPrefs::Setting *setti
|
||||||
if (setting->getKey()==colours[i])
|
if (setting->getKey()==colours[i])
|
||||||
{
|
{
|
||||||
QTextCharFormat format;
|
QTextCharFormat format;
|
||||||
format.setForeground (setting->toColor());
|
if (i == Type_Highlight)
|
||||||
|
format.setBackground (setting->toColor());
|
||||||
|
else
|
||||||
|
format.setForeground (setting->toColor());
|
||||||
mScheme[static_cast<Type> (i)] = format;
|
mScheme[static_cast<Type> (i)] = format;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define CSV_WORLD_SCRIPTHIGHLIGHTER_H
|
#define CSV_WORLD_SCRIPTHIGHLIGHTER_H
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <QSyntaxHighlighter>
|
#include <QSyntaxHighlighter>
|
||||||
|
|
||||||
|
@ -30,7 +31,8 @@ namespace CSVWorld
|
||||||
Type_Keyword = 3,
|
Type_Keyword = 3,
|
||||||
Type_Special = 4,
|
Type_Special = 4,
|
||||||
Type_Comment = 5,
|
Type_Comment = 5,
|
||||||
Type_Id = 6
|
Type_Highlight = 6,
|
||||||
|
Type_Id = 7
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Mode
|
enum Mode
|
||||||
|
@ -47,6 +49,7 @@ namespace CSVWorld
|
||||||
CSMWorld::ScriptContext mContext;
|
CSMWorld::ScriptContext mContext;
|
||||||
std::map<Type, QTextCharFormat> mScheme;
|
std::map<Type, QTextCharFormat> mScheme;
|
||||||
Mode mMode;
|
Mode mMode;
|
||||||
|
std::string mMarkedWord;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -91,6 +94,8 @@ namespace CSVWorld
|
||||||
|
|
||||||
virtual void highlightBlock (const QString& text);
|
virtual void highlightBlock (const QString& text);
|
||||||
|
|
||||||
|
void setMarkedWord(const std::string& name);
|
||||||
|
|
||||||
void invalidateIds();
|
void invalidateIds();
|
||||||
|
|
||||||
bool settingChanged (const CSMPrefs::Setting *setting);
|
bool settingChanged (const CSMPrefs::Setting *setting);
|
||||||
|
|
Loading…
Reference in a new issue