File: lexer_configuration.h

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (265 lines) | stat: -rw-r--r-- 7,846 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : lexer_configuration.h
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#ifndef LEXER_CONFIGURATION_H
#define LEXER_CONFIGURATION_H

#include "JSON.h"
#include "attribute_style.h"
#include "codelite_exports.h"
#include "wx/filename.h"
#include "wx/string.h"
#include "wx/xml/xml.h"

#include <smart_ptr.h>
#include <wx/font.h>
#include <wx/sharedptr.h>
#include <wx/stc/stc.h>

#define ANNOTATION_STYLE_WARNING 210
#define ANNOTATION_STYLE_ERROR 211
#define ANNOTATION_STYLE_CC_ERROR 212

struct WXDLLIMPEXP_SDK WordSetIndex {
    int index = wxNOT_FOUND;
    // when set to true, the `index` is the style to which we append substyles
    bool is_substyle = false;
    WordSetIndex(int idx, bool b)
        : index(idx)
        , is_substyle(b)
    {
    }
    WordSetIndex() {}
    JSONItem to_json() const
    {
        auto item = JSONItem::createObject();
        item.addProperty("index", index);
        item.addProperty("is_substyle", is_substyle);
        return item;
    }

    void from_json(const JSONItem& json)
    {
        if(json.isNumber()) {
            // old style, for migration purposes
            index = json.toInt(wxNOT_FOUND);
        } else {
            index = json["index"].toInt(wxNOT_FOUND);
            is_substyle = json["is_substyle"].toBool(false);
        }
    }

    bool is_ok() const { return index != wxNOT_FOUND; }
};

class WXDLLIMPEXP_SDK LexerConf
{
public:
    enum eWordSetIndex {
        WS_FIRST = 0,
        WS_CLASS = WS_FIRST,
        WS_FUNCTIONS,
        WS_VARIABLES,
        WS_OTHERS,
        WS_LAST = WS_OTHERS,
    };

private:
    StyleProperty::Vec_t m_properties;
    int m_lexerId;
    wxString m_name;
    wxString m_extension;
    wxString m_keyWords[10];
    wxString m_themeName;
    size_t m_flags = 0;

    WordSetIndex m_wordSets[4];
    int m_substyleBase = wxNOT_FOUND;

public:
    typedef SmartPtr<LexerConf> Ptr_t;

protected:
    enum eLexerConfFlags {
        kNone = 0,
        kStyleInPP = (1 << 0),
        kIsActive = (1 << 1),
        kUseCustomTextSelectionFgColour = (1 << 2),
    };

    inline void EnableFlag(eLexerConfFlags flag, bool b)
    {
        if(b) {
            m_flags |= flag;
        } else {
            m_flags &= ~flag;
        }
    }

    inline bool HasFlag(eLexerConfFlags flag) const { return m_flags & flag; }

public:
    struct FindByNameAndTheme {
        wxString m_name;
        wxString m_theme;
        FindByNameAndTheme(const wxString& name, const wxString& theme)
            : m_name(name)
            , m_theme(theme)
        {
        }

        bool operator()(LexerConf::Ptr_t lexer) const
        {
            return lexer->GetName() == m_name && lexer->GetThemeName() == m_theme;
        }
    };

public:
    void SetSubstyleBase(int style) { m_substyleBase = style; }
    int GetSubStyleBase() const { return m_substyleBase; }
    bool IsSubstyleSupported() const { return m_substyleBase != wxNOT_FOUND; }

    /**
     * @brief convert the lexer settings into a JSON object
     */
    JSONItem ToJSON(bool forExport = false) const;

    /**
     * @brief construt this object from a JSON object
     * @param json
     */
    void FromJSON(const JSONItem& json);

    void SetWordSet(eWordSetIndex index, const WordSetIndex& word_set) { this->m_wordSets[index] = word_set; }
    const WordSetIndex& GetWordSet(eWordSetIndex index) const { return m_wordSets[index]; }
    void ApplyWordSet(wxStyledTextCtrl* ctrl, eWordSetIndex index, const wxString& keywords);

public:
    LexerConf();
    virtual ~LexerConf();

    void SetUseCustomTextSelectionFgColour(bool b) { EnableFlag(kUseCustomTextSelectionFgColour, b); }
    bool IsUseCustomTextSelectionFgColour() const { return HasFlag(kUseCustomTextSelectionFgColour); }
    void SetStyleWithinPreProcessor(bool b) { EnableFlag(kStyleInPP, b); }
    bool GetStyleWithinPreProcessor() const { return HasFlag(kStyleInPP); }
    void SetIsActive(bool b) { EnableFlag(kIsActive, b); }
    bool IsActive() const { return HasFlag(kIsActive); }

    void SetThemeName(const wxString& themeName) { this->m_themeName = themeName; }
    const wxString& GetThemeName() const { return m_themeName; }

    /**
     * @brief return true if the colours represented by this lexer are a "dark" theme
     */
    bool IsDark() const;
    /**
     * @brief apply the current lexer configuration on an input
     * wxStyledTextCtrl
     */
    void Apply(wxStyledTextCtrl* ctrl, bool applyKeywords = false);

    /**
     * @brief similar to `Apply`, but use the system colours instead of the theme colours
     */
    void ApplySystemColours(wxStyledTextCtrl* ctrl);

    /**
     * Get the lexer ID, which should be in sync with values of Scintilla
     * @return
     */
    int GetLexerId() const { return m_lexerId; }

    /**
     * Set the lexer ID
     * @param id
     */
    void SetLexerId(int id) { m_lexerId = id; }

    /**
     * Return the lexer description as described in the XML file
     */
    const wxString& GetName() const { return m_name; }

    void SetName(const wxString& name) { m_name = name; }
    /**
     * Return the lexer keywords
     * @return
     */
    const wxString& GetKeyWords(int set) const { return m_keyWords[set]; }

    void SetKeyWords(const wxString& keywords, int set);

    /**
     * File patterns that this lexer should apply to
     */
    const wxString& GetFileSpec() const { return m_extension; }
    /**
     * Return a list of the lexer properties
     * @return
     */
    const StyleProperty::Vec_t& GetLexerProperties() const { return m_properties; }

    /**
     * Return a list of the lexer properties
     * @return
     */
    StyleProperty::Vec_t& GetLexerProperties() { return m_properties; }

    /**
     * @brief return property. Check for IsNull() to make sure we got a valid property
     * @param propertyId
     * @return
     */
    StyleProperty& GetProperty(int propertyId);
    const StyleProperty& GetProperty(int propertyId) const;

    /**
     * @brief set the line numbers colour
     */
    void SetLineNumbersFgColour(const wxColour& colour);

    /**
     * @brief set the default fg colour
     */
    void SetDefaultFgColour(const wxColour& colour);

    /**
     * Set the lexer properties
     * @param &properties
     */
    void SetProperties(StyleProperty::Vec_t& properties) { m_properties.swap(properties); }
    /**
     * Set file spec for the lexer
     * @param &spec
     */
    void SetFileSpec(const wxString& spec) { m_extension = spec; }

    /**
     * @brief return the font for a given style id
     * @return return wxNullFont if error occurred or could locate the style
     */
    wxFont GetFontForStyle(int styleId, const wxWindow* win) const;
};

#endif // LEXER_CONFIGURATION_H