File: EclipseThemeImporterBase.cpp

package info (click to toggle)
codelite 10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 71,364 kB
  • sloc: cpp: 415,397; ansic: 18,277; php: 9,547; lex: 4,181; yacc: 2,820; python: 2,294; sh: 383; makefile: 51; xml: 13
file content (199 lines) | stat: -rw-r--r-- 7,312 bytes parent folder | download | duplicates (3)
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
#include "EclipseThemeImporterBase.h"
#include "drawingutils.h"
#include "xmlutils.h"
#include <wx/colour.h>
#include "cl_standard_paths.h"
#include "globals.h"
#include "ColoursAndFontsManager.h"
#include "lexer_configuration.h"
#include <wx/arrstr.h>
#include <wx/tokenzr.h>

EclipseThemeImporterBase::EclipseThemeImporterBase() {}

EclipseThemeImporterBase::~EclipseThemeImporterBase() {}

bool EclipseThemeImporterBase::GetProperty(const wxString& name, EclipseThemeImporterBase::Property& prop) const
{
    prop.colour = "";
    prop.isBold = false;
    prop.isItalic = false;
    if(!m_doc.IsOk()) return false;

    wxXmlNode* child = m_doc.GetRoot()->GetChildren();
    while(child) {
        if(child->GetName() == name) {
            prop.colour = child->GetAttribute("color");
            prop.isBold = child->GetAttribute("bold", "false") == "true";
            prop.isItalic = child->GetAttribute("italic", "false") == "true";
            return true;
        }
        child = child->GetNext();
    }
    return false;
}

LexerConf::Ptr_t
EclipseThemeImporterBase::InitializeImport(const wxFileName& eclipseXml, const wxString& langName, int langId)
{
    m_langName = langName;
    if(!m_doc.Load(eclipseXml.GetFullPath())) return NULL;

    LexerConf::Ptr_t lexer(new LexerConf());

    // Add the lexer basic properties (laguage, file extensions, keywords, name)
    AddBaseProperties(lexer, m_langName, wxString::Format("%d", langId));

    // Read the basic properties
    if(!GetProperty("background", m_background)) return NULL;
    if(!GetProperty("foreground", m_foreground)) return NULL;
    if(!GetProperty("selectionForeground", m_selectionForeground)) return NULL;
    if(!GetProperty("selectionBackground", m_selectionBackground)) return NULL;
    if(!GetProperty("lineNumber", m_lineNumber)) return NULL;
    if(!GetProperty("singleLineComment", m_singleLineComment)) return NULL;
    if(!GetProperty("multiLineComment", m_multiLineComment)) return NULL;
    if(!GetProperty("number", m_number)) return NULL;
    if(!GetProperty("string", m_string)) return NULL;
    if(!GetProperty("operator", m_oper)) return NULL;
    if(!GetProperty("keyword", m_keyword)) return NULL;
    if(!GetProperty("class", m_klass)) {
        m_klass = m_foreground;
    }
    if(!GetProperty("parameterVariable", m_variable)) {
        m_variable = m_foreground;
    }
    if(!GetProperty("javadoc", m_javadoc)) {
        m_javadoc = m_multiLineComment;
    }

    if(!GetProperty("javadocKeyword", m_javadocKeyword)) {
        m_javadocKeyword = m_multiLineComment;
    }
    
    m_oper = m_foreground;
    return lexer;
}

void EclipseThemeImporterBase::FinalizeImport(LexerConf::Ptr_t lexer)
{
    AddCommonProperties(lexer);
    ColoursAndFontsManager::Get().UpdateLexerColours(lexer, true);
}

bool EclipseThemeImporterBase::IsDarkTheme() const
{
    // load the theme background colour
    EclipseThemeImporterBase::Property p;
    if(!GetProperty("background", p)) return false;

    // test the colour
    return DrawingUtils::IsDark(p.colour);
}

wxString EclipseThemeImporterBase::GetName() const
{
    if(!IsValid()) return "";
    return m_doc.GetRoot()->GetAttribute("name");
}

wxString EclipseThemeImporterBase::GetOutputFile(const wxString& language) const
{
    wxString name = GetName();
    name.MakeLower();

    // Normalize the file name
    name.Replace(" ", "_");
    name.Replace("::", "_");
    name.Replace("(", "_");
    name.Replace(")", "_");
    name.Replace(":", "_");
    name.Replace(",", "_");
    name.Replace(".", "_");
    name.Replace(";", "_");

    wxString xmlFileName;
    xmlFileName << "lexer_" << language.Lower() << "_" << name << ".xml";
    return xmlFileName;
}

void EclipseThemeImporterBase::AddProperty(LexerConf::Ptr_t lexer,
                                           const wxString& id,
                                           const wxString& name,
                                           const wxString& colour,
                                           const wxString& bgColour,
                                           bool bold,
                                           bool italic,
                                           bool isEOLFilled)
{
    wxASSERT(!colour.IsEmpty());
    wxASSERT(!bgColour.IsEmpty());

    long ID;
    id.ToCLong(&ID);

    StyleProperty sp(ID, colour, bgColour, 11, name, "", bold, italic, false, isEOLFilled, 50);
    lexer->GetLexerProperties().insert(std::make_pair(sp.GetId(), sp));
}

void EclipseThemeImporterBase::AddBaseProperties(LexerConf::Ptr_t lexer, const wxString& lang, const wxString& id)
{
    lexer->SetName(lang);
    lexer->SetThemeName(GetName());
    lexer->SetIsActive(false);
    lexer->SetUseCustomTextSelectionFgColour(true);
    lexer->SetStyleWithinPreProcessor(true);
    long ID;
    id.ToCLong(&ID);
    lexer->SetLexerId(ID);
    lexer->SetKeyWords(GetKeywords0(), 0);
    lexer->SetKeyWords(GetKeywords1(), 1);
    lexer->SetKeyWords(GetKeywords2(), 2);
    lexer->SetKeyWords(GetKeywords3(), 3);
    lexer->SetKeyWords(GetKeywords4(), 4);
    lexer->SetFileSpec(GetFileExtensions());
}

void EclipseThemeImporterBase::AddCommonProperties(LexerConf::Ptr_t lexer)
{
    // Set the brace match based on the background colour
    Property background, foreground, selectionBackground, selectionForeground, lineNumber;

    GetProperty("background", background);
    GetProperty("foreground", foreground);
    GetProperty("selectionForeground", selectionForeground);
    GetProperty("selectionBackground", selectionBackground);
    GetProperty("lineNumber", lineNumber);

    wxString whitespaceColour;
    if(IsDarkTheme()) {
        // dark theme
        // Whitespace should be a bit lighether
        whitespaceColour = wxColour(background.colour).ChangeLightness(150).GetAsString(wxC2S_HTML_SYNTAX);
        AddProperty(lexer, "34", "Brace match", "yellow", background.colour, true);
        AddProperty(lexer, "35", "Brace bad match", "red", background.colour, true);
        AddProperty(lexer, "37", "Indent Guide", background.colour, background.colour);

    } else {
        // light theme
        whitespaceColour = wxColour(background.colour).ChangeLightness(50).GetAsString(wxC2S_HTML_SYNTAX);
        AddProperty(lexer, "34", "Brace match", "black", "cyan", true);
        AddProperty(lexer, "35", "Brace bad match", "black", "red", true);
        AddProperty(lexer, "37", "Indent Guide", whitespaceColour, background.colour);
    }
    AddProperty(lexer, "-1", "Fold Margin", background.colour, background.colour);
    AddProperty(lexer, "-2", "Text Selection", selectionForeground.colour, selectionBackground.colour);
    AddProperty(lexer, "-3", "Caret Colour", IsDarkTheme() ? "white" : "black", background.colour);
    AddProperty(lexer, "-4", "Whitespace", whitespaceColour, background.colour);
    AddProperty(lexer, "38", "Calltip", foreground.colour, background.colour);
    AddProperty(
        lexer, "33", "Line Numbers", lineNumber.colour, background.colour, lineNumber.isBold, lineNumber.isItalic);
}

void EclipseThemeImporterBase::DoSetKeywords(wxString& wordset, const wxString& words)
{
    wordset.clear();
    wxArrayString arr = ::wxStringTokenize(words, " \t\n", wxTOKEN_STRTOK);
    arr.Sort();
    
    wordset = ::wxJoin(arr, ' ');
}