-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathLanguages.cpp
More file actions
319 lines (276 loc) · 8.6 KB
/
Languages.cpp
File metadata and controls
319 lines (276 loc) · 8.6 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright 2015-2018 Kacper Kasper <[email protected]>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "Languages.h"
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <Catalog.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <Path.h>
#include <String.h>
#include <ILexer.h>
#include <Lexilla.h>
#include <SciLexer.h>
#include <yaml-cpp/yaml.h>
#include "Editor.h"
#include "EditorWindow.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Languages"
std::vector<std::string> Languages::sLanguages;
std::map<std::string, std::string> Languages::sMenuItems;
std::map<std::string, std::string> Languages::sExtensions;
namespace {
/**
* Executes a specified function for each data directory, going from system to
* user, packaged to non-packaged. The path is available as a parameter to the
* user supplied function.
*/
void
DoInAllDataDirectories(std::function<void(const BPath&)> func) {
BPath dataPath;
find_directory(B_SYSTEM_DATA_DIRECTORY, &dataPath);
func(dataPath);
find_directory(B_USER_DATA_DIRECTORY, &dataPath);
func(dataPath);
find_directory(B_SYSTEM_NONPACKAGED_DATA_DIRECTORY, &dataPath);
func(dataPath);
find_directory(B_USER_NONPACKAGED_DATA_DIRECTORY, &dataPath);
func(dataPath);
}
void
DoInAllLibDirectories(std::function<void(const BPath&)> func) {
BPath libPath;
find_directory(B_SYSTEM_LIB_DIRECTORY, &libPath);
func(libPath);
find_directory(B_USER_LIB_DIRECTORY, &libPath);
func(libPath);
find_directory(B_SYSTEM_NONPACKAGED_LIB_DIRECTORY, &libPath);
func(libPath);
find_directory(B_USER_NONPACKAGED_LIB_DIRECTORY, &libPath);
func(libPath);
}
class LexerLibrary {
public:
LexerLibrary(const char* path) {
fLibrary = load_add_on(path);
if(fLibrary > 0) {
if(get_image_symbol(fLibrary, LEXILLA_CREATELEXER, B_SYMBOL_TYPE_TEXT,
reinterpret_cast<void**>(&fCreateLexer)) != B_OK) {
fCreateLexer = nullptr;
}
}
}
~LexerLibrary() {
if(fLibrary > 0) {
unload_add_on(fLibrary);
}
fLibrary = 0;
}
bool IsValid() {
return fLibrary > 0 && fCreateLexer != nullptr;
}
Scintilla::ILexer5* CreateLexer(const char* name) {
return fCreateLexer(name);
}
private:
image_id fLibrary;
Lexilla::CreateLexerFn fCreateLexer;
};
std::vector<std::unique_ptr<LexerLibrary>> sLexerLibraries;
}
/* static */ bool
Languages::GetLanguageForExtension(const std::string ext, std::string& lang)
{
lang = "text";
if(sExtensions.count(ext) > 0) {
lang = sExtensions[ext];
return true;
}
return false;
}
/* static */ void
Languages::SortAlphabetically()
{
std::sort(sLanguages.begin(), sLanguages.end());
}
/**
* Reads YAML files from all data directories and creates a single style map,
* where repeated keys are overridden (user non-packaged being final).
*/
/* static */ std::map<int, int>
Languages::ApplyLanguage(Editor* editor, const char* lang)
{
editor->SendMessage(SCI_FREESUBSTYLES);
std::map<int, int> styleMapping;
DoInAllDataDirectories([&](const BPath& path) {
try {
auto m = _ApplyLanguage(editor, lang, path);
m.merge(styleMapping);
std::swap(styleMapping, m);
} catch (YAML::BadFile &) {}
});
return styleMapping;
}
/**
* Loads YAML file with language specification:
* lexer: string (required)
* properties: (string|string) map -> SCI_SETPROPERTY
* keywords: (index(int)|string) map -> SCI_SETKEYWORDS
* identifiers: (lexem class id(int)|(string array)) map -> SCI_SETIDENTIFIERS
* comments:
* line: string
* block: pair of strings
* styles: (lexem class id(int)|Koder style id(int)) map
* substyles: (lexem class id(int)|(Koder style id(int)) array) map
*
* For substyles, strings in identifiers array are matched with styles in
* substyles array. Array instead of map is used because substyles are allocated
* contiguously. Theoretically there is no limit on how many substyles there
* can be. Substyling of lexem class id must be supported by the lexer.
*
* Substyle ids are created using starting id returned by SCI_ALLOCATESUBSTYLE.
* For example if it returns 128, then 1st id = 128, 2nd = 129, 3rd = 130.
* These are then passed to SCI_SETIDENTIFIERS and merged into regular styles
* map to be handled by the Styler class.
*/
/* static */ std::map<int, int>
Languages::_ApplyLanguage(Editor* editor, const char* lang, const BPath &path)
{
if(sLexerLibraries.empty() == true)
return {};
// TODO: early exit if lexer not changed
BPath p(path);
p.Append(gAppName);
p.Append("languages");
p.Append(lang);
const YAML::Node language = YAML::LoadFile(std::string(p.Path()) + ".yaml");
std::string lexerName = language["lexer"].as<std::string>();
Scintilla::ILexer5* lexer;
// sLexerLibraries contains libraries in the following order:
// * system
// * user
// * non-packaged system
// * non-packaged user
// Going in reverse results in correct override hierarchy.
for(auto it = sLexerLibraries.rbegin(); it != sLexerLibraries.rend(); ++it) {
lexer = (*it)->CreateLexer(lexerName.c_str());
if(lexer != nullptr)
break;
}
editor->SendMessage(SCI_SETILEXER, 0, reinterpret_cast<sptr_t>(lexer));
for(const auto& property : language["properties"]) {
auto name = property.first.as<std::string>();
auto value = property.second.as<std::string>();
editor->SendMessage(SCI_SETPROPERTY, (uptr_t) name.c_str(), (sptr_t) value.c_str());
}
for(const auto& keyword : language["keywords"]) {
auto num = keyword.first.as<int>();
auto words = keyword.second.as<std::string>();
editor->SendMessage(SCI_SETKEYWORDS, num, (sptr_t) words.c_str());
}
std::unordered_map<int, int> substyleStartMap;
const auto& identifiers = language["identifiers"];
if(identifiers && identifiers.IsMap()) {
for(const auto& id : identifiers) {
if(!id.second.IsSequence())
continue;
const int substyleId = id.first.as<int>();
// TODO: allocate only once
const int start = editor->SendMessage(SCI_ALLOCATESUBSTYLES,
substyleId, id.second.size());
substyleStartMap.emplace(substyleId, start);
int i = 0;
for(const auto& idents : id.second) {
editor->SendMessage(SCI_SETIDENTIFIERS, start + i++,
reinterpret_cast<sptr_t>(idents.as<std::string>().c_str()));
}
}
}
const YAML::Node comments = language["comments"];
if(comments) {
const YAML::Node line = comments["line"];
if(line)
editor->SetCommentLineToken(line.as<std::string>());
const YAML::Node block = comments["block"];
if(block && block.IsSequence())
editor->SetCommentBlockTokens(block[0].as<std::string>(),
block[1].as<std::string>());
}
std::map<int, int> styleMap;
const YAML::Node styles = language["styles"];
if(styles) {
styleMap = styles.as<std::map<int, int>>();
}
const YAML::Node substyles = language["substyles"];
if(substyles && substyles.IsMap()) {
for(const auto& id : substyles) {
if(!id.second.IsSequence())
continue;
int i = 0;
for(const auto& styleId : id.second) {
const int substyleStart = substyleStartMap[id.first.as<int>()];
styleMap.emplace(substyleStart + i++, styleId.as<int>());
}
}
}
return styleMap;
}
/* static */ void
Languages::LoadLanguages()
{
DoInAllDataDirectories([](const BPath& path) {
try {
_LoadLanguages(path);
} catch (YAML::BadFile &) {}
});
DoInAllLibDirectories([](const BPath& path) {
BPath p(path);
p.Append(LEXILLA_LIB LEXILLA_EXTENSION);
auto lexilla = std::make_unique<LexerLibrary>(p.Path());
if(lexilla->IsValid() == true) {
sLexerLibraries.push_back(std::move(lexilla));
}
});
DoInAllLibDirectories([](const BPath& path) {
BPath p(path);
p.Append("lexilla");
BDirectory lexersDir(p.Path());
if (lexersDir.InitCheck() != B_OK)
return;
BEntry lexerEntry;
while(lexersDir.GetNextEntry(&lexerEntry, true) == B_OK) {
if(lexerEntry.IsDirectory())
continue;
BPath lexerPath;
lexerEntry.GetPath(&lexerPath);
auto lexer = std::make_unique<LexerLibrary>(lexerPath.Path());
if(lexer->IsValid() == true) {
sLexerLibraries.push_back(std::move(lexer));
}
}
});
}
/* static */ void
Languages::_LoadLanguages(const BPath& path)
{
BPath p(path);
p.Append(gAppName);
p.Append("languages");
const YAML::Node languages = YAML::LoadFile(std::string(p.Path()) + ".yaml");
for(const auto& language : languages) {
auto name = language.first.as<std::string>();
auto menuitem = language.second["name"].as<std::string>();
auto extensions = language.second["extensions"].as<std::vector<std::string>>();
for(auto extension : extensions) {
sExtensions[extension] = name;
}
if(std::find(sLanguages.begin(), sLanguages.end(), name) == sLanguages.end())
sLanguages.push_back(name);
sMenuItems[name] = menuitem;
}
}