Skip to content

Commit 89edd62

Browse files
committed
Add significantScopeLast argument
1 parent e6aa069 commit 89edd62

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

index.js

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ var chalk = require("chalk"),
1313
path = require("path");
1414

1515

16-
function readGrammarFile(filename) {
16+
function readGrammarFile(filename, significantScopeLast) {
17+
/*
18+
Args:
19+
- `significantScopeLast`: if set to `true`, scopes separated by
20+
whitespace will be outputted in reverse. In ST scopes specified
21+
last have more weight than scopes that specified first; in Atom
22+
it's reversed. In MagicPython, we specify scopes putting
23+
significant scopes first, hence, we need to reverse scopes
24+
when we generate schema for SublimeText.
25+
*/
26+
1727
function read(filename, vars) {
1828
var yamlSource = fs.readFileSync(filename, 'utf8'),
1929
hop = Object.prototype.hasOwnProperty;
@@ -53,7 +63,50 @@ function readGrammarFile(filename) {
5363
return schema;
5464
}
5565

56-
return read(filename);
66+
var schema = read(filename, {});
67+
68+
if (significantScopeLast) {
69+
function patchName(name) {
70+
var names = name.split(/[\s\n]+/);
71+
if (names.length > 1) {
72+
names.reverse();
73+
}
74+
return names.join(' ');
75+
}
76+
77+
function patch(o) {
78+
if (_.has(o, 'name')) {
79+
o.name = patchName(o.name);
80+
}
81+
82+
if (_.has(o, 'patterns')) {
83+
_.each(o.patterns, patch);
84+
}
85+
86+
_.each(
87+
['beginCaptures', 'endCaptures', 'captures'],
88+
function(prop) {
89+
if (!_.has(o, prop)) {
90+
return
91+
}
92+
93+
_.each(o[prop], function(v) {
94+
if (_.has(v, 'name')) {
95+
v.name = patchName(v.name);
96+
}
97+
})
98+
}
99+
);
100+
}
101+
102+
if (schema.repository) {
103+
_.each(schema.repository, function(v, k) {
104+
patch(v);
105+
});
106+
}
107+
}
108+
109+
return schema;
57110
}
58111

59112

@@ -62,7 +115,7 @@ function compileGrammar(grammarFile, additionalGrammars) {
62115
var tmp = temp.openSync();
63116

64117
try {
65-
var yamlSchema = readGrammarFile(filename);
118+
var yamlSchema = readGrammarFile(filename, false);
66119

67120
fs.writeSync(tmp.fd, JSON.stringify(yamlSchema));
68121
fs.closeSync(tmp.fd);
@@ -284,7 +337,7 @@ function test(testFiles, grammarFile, options) {
284337

285338

286339
function buildCson(inName, outName) {
287-
var yamlSchema = readGrammarFile(inName),
340+
var yamlSchema = readGrammarFile(inName, false),
288341
csonSource = cson.createCSONString(yamlSchema, {indent: 2});
289342

290343
csonSource = '# AUTOGENERATED FROM ' + inName + '\n' + csonSource;
@@ -293,7 +346,7 @@ function buildCson(inName, outName) {
293346

294347

295348
function buildPList(inName, outName) {
296-
var yamlSchema = readGrammarFile(inName),
349+
var yamlSchema = readGrammarFile(inName, true),
297350
plistSource = plist.build(yamlSchema);
298351

299352
plistSource = '<!-- AUTOGENERATED FROM ' + inName + ' -->\n' +

0 commit comments

Comments
 (0)