Skip to content

Commit

Permalink
#182: merge pull request #184 from 0x3508A/add-forth
Browse files Browse the repository at this point in the history
Adding Forth code Highlighting support
  • Loading branch information
pbek authored May 2, 2023
2 parents 5598663 + 2f1a26f commit a23cc53
Show file tree
Hide file tree
Showing 4 changed files with 1,875 additions and 3 deletions.
52 changes: 51 additions & 1 deletion markdownhighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ void MarkdownHighlighter::initCodeLangs() {
{QLatin1String("vex"), MarkdownHighlighter::CodeVex},
{QLatin1String("xml"), MarkdownHighlighter::CodeXML},
{QLatin1String("yml"), MarkdownHighlighter::CodeYAML},
{QLatin1String("yaml"), MarkdownHighlighter::CodeYAML}};
{QLatin1String("yaml"), MarkdownHighlighter::CodeYAML},
{QLatin1String("forth"), MarkdownHighlighter::CodeForth}};
}

/**
Expand Down Expand Up @@ -771,6 +772,7 @@ void MarkdownHighlighter::highlightSyntax(const QString &text) {
bool isCSS = false;
bool isYAML = false;
bool isMake = false;
bool isForth = false;

QMultiHash<char, QLatin1String> keywords{};
QMultiHash<char, QLatin1String> others{};
Expand Down Expand Up @@ -913,6 +915,13 @@ void MarkdownHighlighter::highlightSyntax(const QString &text) {
loadNixData(types, keywords, builtin, literals, others);
comment = QLatin1Char('#');
break;
case HighlighterState::CodeForth:
case HighlighterState::CodeForth + tildeOffset:
case HighlighterState::CodeForthComment:
case HighlighterState::CodeForthComment + tildeOffset:
isForth = true;
loadForthData(types, keywords, builtin, literals, others);
break;
default:
setFormat(0, textLen, _formats[CodeBlock]);
return;
Expand Down Expand Up @@ -1073,6 +1082,7 @@ void MarkdownHighlighter::highlightSyntax(const QString &text) {
if (isCSS) cssHighlighter(text);
if (isYAML) ymlHighlighter(text);
if (isMake) makeHighlighter(text);
if (isForth) forthHighlighter(text);
}

/**
Expand Down Expand Up @@ -1647,6 +1657,46 @@ void MarkdownHighlighter::makeHighlighter(const QString &text) {
setFormat(0, colonPos, _formats[CodeBuiltIn]);
}

/**
* @brief The Forth highlighter
* @param text
* @details This function performs filtering of Forth code and high lights
* the specific details.
* 1. It highlights the "\ " comments
* 2. It highlights the "( " comments
*/
void MarkdownHighlighter::forthHighlighter(const QString &text) {
if (text.isEmpty()) return;

const auto textLen = text.length();

// Default Format
setFormat(0, textLen, _formats[CodeBlock]);

for (int i = 0; i < textLen; ++i) {
// 1, It highlights the "\ " comments
if (i + 1 <= textLen && text[i] == QLatin1Char('\\') &&
text[i + 1] == QLatin1Char(' ')) {
// The full line is commented
setFormat(i + 1, textLen - 1, _formats[CodeComment]);
break;
}
// 2. It highlights the "( " comments
else if (i + 1 <= textLen && text[i] == QLatin1Char('(') &&
text[i + 1] == QLatin1Char(' ')) {
// Find the End bracket
int lastBracket = text.lastIndexOf(QLatin1Char(')'), i);
// Can't Handle wrong Format
if (lastBracket <= 0) return;
// ' )' at the end of the comment
if (lastBracket <= textLen &&
text[lastBracket] == QLatin1Char(' ')) {
setFormat(i, lastBracket, _formats[CodeComment]);
}
}
}
}

/**
* Highlight multi-line frontmatter blocks
*
Expand Down
6 changes: 5 additions & 1 deletion markdownhighlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ class MarkdownHighlighter : public QSyntaxHighlighter {
CodeVexComment = 241,
CodeCMake = 242,
CodeMake = 244,
CodeNix = 246
CodeNix = 246,
CodeForth = 248,
CodeForthComment = 249
};
Q_ENUM(HighlighterState)

Expand Down Expand Up @@ -285,6 +287,8 @@ class MarkdownHighlighter : public QSyntaxHighlighter {

void makeHighlighter(const QString &text);

void forthHighlighter(const QString &text);

void taggerScriptHighlighter(const QString &text);

void addDirtyBlock(const QTextBlock &block);
Expand Down
Loading

0 comments on commit a23cc53

Please sign in to comment.