Skip to content

Commit

Permalink
Code mode highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
IgKh committed Feb 8, 2024
1 parent c25dc53 commit e7a9a29
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 20 deletions.
40 changes: 37 additions & 3 deletions src/katvan_highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
*/
#include "katvan_highlighter.h"

#include <QHash>
#include <QTextDocument>

#include <vector>

namespace katvan {

namespace parsing {

constexpr inline size_t qHash(const ParserState& state, size_t seed = 0) noexcept
{
return qHashMulti(seed, state.kind, state.startPos);
}

}

class HighlighterStateBlockData : public QTextBlockUserData
{
public:
Expand All @@ -31,6 +41,8 @@ class HighlighterStateBlockData : public QTextBlockUserData

const parsing::ParserStateStack* stateStack() const { return &d_stateStack; }

int getHash() const { return qHashRange(d_stateStack.begin(), d_stateStack.end()); }

private:
parsing::ParserStateStack d_stateStack;
};
Expand All @@ -51,12 +63,16 @@ void Highlighter::setupFormats()
stringLiteralFormat.setForeground(QColor(0x29, 0x8e, 0x0d));
d_formats.insert(parsing::HiglightingMarker::Kind::STRING_LITERAL, stringLiteralFormat);

QTextCharFormat numberLiteralFormat;
numberLiteralFormat.setForeground(QColor(0xb6, 0x01, 0x57));
d_formats.insert(parsing::HiglightingMarker::Kind::NUMBER_LITERAL, numberLiteralFormat);

QTextCharFormat escapeFormat;
escapeFormat.setForeground(QColor(0x29, 0x8e, 0x0d));
escapeFormat.setForeground(QColor(0x1d, 0x6c, 0x76));
d_formats.insert(parsing::HiglightingMarker::Kind::ESCAPE, escapeFormat);

QTextCharFormat mathDelimiterFormat;
mathDelimiterFormat.setForeground(QColor(0x1d, 0x6c, 0x76));
mathDelimiterFormat.setForeground(QColor(0x29, 0x8e, 0x0d));
d_formats.insert(parsing::HiglightingMarker::Kind::MATH_DELIMITER, mathDelimiterFormat);

QTextCharFormat headingFormat;
Expand Down Expand Up @@ -88,6 +104,18 @@ void Highlighter::setupFormats()
QTextCharFormat termFormat;
termFormat.setFontWeight(QFont::ExtraBold);
d_formats.insert(parsing::HiglightingMarker::Kind::TERM, termFormat);

QTextCharFormat variableNameFormat;
variableNameFormat.setForeground(QColor(0x8b, 0x41, 0xb1));
d_formats.insert(parsing::HiglightingMarker::Kind::VARIABLE_NAME, variableNameFormat);

QTextCharFormat functionNameFormat;
functionNameFormat.setForeground(QColor(0x4b, 0x69, 0xc6));
d_formats.insert(parsing::HiglightingMarker::Kind::FUNCTION_NAME, functionNameFormat);

QTextCharFormat keywordFormat;
keywordFormat.setForeground(QColor(0xd7, 0x3a, 0x49));
d_formats.insert(parsing::HiglightingMarker::Kind::KEYWORD, keywordFormat);
}

void Highlighter::highlightBlock(const QString& text)
Expand Down Expand Up @@ -116,7 +144,13 @@ void Highlighter::highlightBlock(const QString& text)
setFormat(i, 1, charFormats[i]);
}

setCurrentBlockUserData(new HighlighterStateBlockData(parser.stateStack()));
// In addition to storing the parser state stack at the end of the block as
// the block's user data, set a hash of that as the block state. This is to
// force re-highlighting of the next block if something changed - QSyntaxHighlighter
// only tracks changes to the block state number.
auto* blockData = new HighlighterStateBlockData(parser.stateStack());
setCurrentBlockState(blockData->getHash());
setCurrentBlockUserData(blockData);
}

}
Loading

0 comments on commit e7a9a29

Please sign in to comment.