aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/haskell/haskellhighlighter.cpp
blob: a5061b4757dd1a61f86d828ce8c7d5a8a21b5469 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "haskellhighlighter.h"

#include "haskelltokenizer.h"

#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>

#include <QDebug>
#include <QVector>

Q_GLOBAL_STATIC_WITH_ARGS(QSet<QString>, IMPORT_HIGHLIGHTS, ({
    "qualified",
    "as",
    "hiding"
}));

using namespace TextEditor;

namespace Haskell {
namespace Internal {

HaskellHighlighter::HaskellHighlighter()
{
    setDefaultTextFormatCategories();
}

void HaskellHighlighter::highlightBlock(const QString &text)
{
    const Tokens tokens = HaskellTokenizer::tokenize(text, previousBlockState());
    setCurrentBlockState(tokens.state);
    const Token *firstNonWS = 0;
    const Token *secondNonWS = 0;
    bool inType = false;
    bool inImport = false;
    for (const Token & token : tokens) {
        switch (token.type) {
        case TokenType::Variable:
            if (inType)
                setTokenFormat(token, C_LOCAL);
            else if (inImport && IMPORT_HIGHLIGHTS->contains(token.text.toString()))
                setTokenFormat(token, C_KEYWORD);
//            else
//                setTokenFormat(token, C_TEXT);
            break;
        case TokenType::Constructor:
        case TokenType::OperatorConstructor:
            setTokenFormat(token, C_TYPE);
            break;
        case TokenType::Operator:
            setTokenFormat(token, C_OPERATOR);
            break;
        case TokenType::Whitespace:
            setTokenFormat(token, C_VISUAL_WHITESPACE);
            break;
        case TokenType::Keyword:
            if (token.text == QLatin1String("::") && firstNonWS && !secondNonWS) { // toplevel declaration
                setFormat(firstNonWS->startCol, firstNonWS->length, m_toplevelDeclFormat);
                inType = true;
            } else if (token.text == QLatin1String("import")) {
                inImport = true;
            }
            setTokenFormat(token, C_KEYWORD);
            break;
        case TokenType::Integer:
        case TokenType::Float:
            setTokenFormat(token, C_NUMBER);
            break;
        case TokenType::String:
            setTokenFormatWithSpaces(text, token, C_STRING);
            break;
        case TokenType::Char:
            setTokenFormatWithSpaces(text, token, C_STRING);
            break;
        case TokenType::EscapeSequence:
            setTokenFormat(token, C_PRIMITIVE_TYPE);
            break;
        case TokenType::SingleLineComment:
            setTokenFormatWithSpaces(text, token, C_COMMENT);
            break;
        case TokenType::MultiLineComment:
            setTokenFormatWithSpaces(text, token, C_COMMENT);
            break;
        case TokenType::Special:
//            setTokenFormat(token, C_TEXT);
            break;
        case TokenType::StringError:
        case TokenType::CharError:
        case TokenType::Unknown:
            setTokenFormat(token, C_PARENTHESES_MISMATCH);
            break;
        }
        if (token.type != TokenType::Whitespace) {
            if (!firstNonWS)
                firstNonWS = &token;
            else if (!secondNonWS)
                secondNonWS = &token;
        }
    }
}

void HaskellHighlighter::setFontSettings(const FontSettings &fontSettings)
{
    SyntaxHighlighter::setFontSettings(fontSettings);
    updateFormats(fontSettings);
}

void HaskellHighlighter::updateFormats(const FontSettings &fontSettings)
{
    m_toplevelDeclFormat = fontSettings.toTextCharFormat(
                TextStyles::mixinStyle(C_FUNCTION, C_DECLARATION));
}

void HaskellHighlighter::setTokenFormat(const Token &token, TextStyle style)
{
    setFormat(token.startCol, token.length, formatForCategory(style));
}

void HaskellHighlighter::setTokenFormatWithSpaces(const QString &text, const Token &token,
                                                  TextStyle style)
{
    setFormatWithSpaces(text, token.startCol, token.length, formatForCategory(style));
}

} // Internal
} // Haskell