aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/nim/editor/nimhighlighter.cpp
blob: 577d83dcac220a41aba8544933d3204120093527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) Filippo Cucchetto <filippocucchetto@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "nimhighlighter.h"

#include "../tools/nimlexer.h"

#include <texteditor/textdocument.h>
#include <texteditor/texteditorconstants.h>
#include <utils/qtcassert.h>

namespace Nim {

NimHighlighter::NimHighlighter()
{
    setDefaultTextFormatCategories();
}

void NimHighlighter::highlightBlock(const QString &text)
{
    setCurrentBlockState(highlightLine(text, previousBlockState()));
}

TextEditor::TextStyle NimHighlighter::styleForToken(const NimLexer::Token &token,
                                                    const QString &tokenValue)
{
    switch (token.type) {
    case NimLexer::TokenType::Keyword:
        return TextEditor::C_KEYWORD;
    case NimLexer::TokenType::Identifier:
        return styleForIdentifier(token, tokenValue);
    case NimLexer::TokenType::Comment:
        return TextEditor::C_COMMENT;
    case NimLexer::TokenType::Documentation:
        return TextEditor::C_DOXYGEN_COMMENT;
    case NimLexer::TokenType::StringLiteral:
        return TextEditor::C_STRING;
    case NimLexer::TokenType::MultiLineStringLiteral:
        return TextEditor::C_STRING;
    case NimLexer::TokenType::Operator:
        return TextEditor::C_OPERATOR;
    case NimLexer::TokenType::Number:
        return TextEditor::C_NUMBER;
    default:
        return TextEditor::C_TEXT;
    }
}

TextEditor::TextStyle NimHighlighter::styleForIdentifier(const NimLexer::Token &token,
                                                         const QString &tokenValue)
{
    Q_UNUSED(token)
    QTC_ASSERT(token.type == NimLexer::TokenType::Identifier, return TextEditor::C_TEXT);

    static QSet<QString> nimBuiltInValues {
        "true", "false"
    };

    static QSet<QString> nimBuiltInFunctions {
        "echo", "isMainModule"
    };

    static QSet<QString> nimBuiltInTypes {
        "bool", "cbool", "string",
        "cstring", "int", "cint",
        "uint", "cuint", "long",
        "clong", "double", "cdouble",
        "table", "RootObj"
    };

    if (nimBuiltInFunctions.contains(tokenValue))
        return TextEditor::C_TYPE;
    if (nimBuiltInValues.contains(tokenValue))
        return TextEditor::C_KEYWORD;
    if (nimBuiltInTypes.contains(tokenValue))
        return TextEditor::C_TYPE;
    return TextEditor::C_TEXT;
}

int NimHighlighter::highlightLine(const QString &text, int initialState)
{
    NimLexer lexer(text.constData(),
                   text.size(),
                   static_cast<NimLexer::State>(initialState));

    NimLexer::Token tk;
    while ((tk = lexer.next()).type != NimLexer::TokenType::EndOfText) {
        TextEditor::TextStyle style = styleForToken(tk, text.mid(tk.begin, tk.length));
        setFormat(tk.begin, tk.length, formatForCategory(style));
    }

    return lexer.state();
}

}