Skip to content

Commit a1f3fee

Browse files
committed
Allow ts grammar to have variables for the pattern matching for easy update
1 parent deae810 commit a1f3fee

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

build/build.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,28 @@ function changeTsToTsx(str: string) {
1717
return str.replace(/\.ts/g, '.tsx');
1818
}
1919

20-
function fixRuleNames(rule: any, name: string) {
21-
if (typeof rule[name] === 'string') {
22-
rule[name] = changeTsToTsx(rule[name]);
20+
function transformGrammarRule(rule: any, propertyNames: string[], transformProperty: (ruleProperty: string) => string) {
21+
for (const propertyName of propertyNames) {
22+
if (typeof rule[propertyName] === 'string') {
23+
rule[propertyName] = transformProperty(rule[propertyName]);
24+
}
2325
}
24-
}
2526

26-
function fixGrammarScopeNames(rule: any) {
27-
fixRuleNames(rule, "name");
28-
fixRuleNames(rule, "contentName");
29-
for (var property in rule) {
30-
var value = rule[property];
27+
for (var propertyName in rule) {
28+
var value = rule[propertyName];
3129
if (typeof value === 'object') {
32-
fixGrammarScopeNames(value);
30+
transformGrammarRule(value, propertyNames, transformProperty);
3331
}
3432
}
3533
}
3634

35+
function transformGrammarRepository(grammar: any, propertyNames: string[], transformProperty: (ruleProperty: string) => string) {
36+
const repository = grammar.repository;
37+
for (let key in repository) {
38+
transformGrammarRule(repository[key], propertyNames, transformProperty);
39+
}
40+
}
41+
3742
function changeTsToTsxGrammar(grammar: any) {
3843
const tsxUpdates = readYaml("../TypeScriptReact.YAML-tmLanguage");
3944

@@ -45,12 +50,10 @@ function changeTsToTsxGrammar(grammar: any) {
4550
}
4651

4752
// Update scope names to .tsx
48-
const repository = grammar.repository;
49-
for (let key in repository) {
50-
fixGrammarScopeNames(repository[key]);
51-
}
53+
transformGrammarRepository(grammar, ["name", "contentName"], changeTsToTsx);
5254

5355
// Add repository items
56+
const repository = grammar.repository;
5457
const updatesRepository = tsxUpdates.repository;
5558
for (let key in updatesRepository) {
5659
switch(key) {
@@ -67,8 +70,34 @@ function changeTsToTsxGrammar(grammar: any) {
6770
return grammar;
6871
}
6972

73+
function replacePatternVariables(pattern: string, variableReplacers: VariableReplacer[]) {
74+
let result = pattern;
75+
for (const [variableName, value] of variableReplacers) {
76+
result = result.replace(variableName, value);
77+
}
78+
return result;
79+
}
80+
81+
type VariableReplacer = [RegExp, string];
82+
function updateGrammarVariables(grammar: any) {
83+
if (grammar.variables !== undefined) {
84+
const variables = grammar.variables;
85+
delete grammar.variables;
86+
const variableReplacers: VariableReplacer[] = [];
87+
for (const variableName in variables) {
88+
variableReplacers.push([new RegExp(`{{${variableName}}}`, "gim"), variables[variableName]]);
89+
}
90+
transformGrammarRepository(
91+
grammar,
92+
["begin", "end", "match"],
93+
pattern => replacePatternVariables(pattern, variableReplacers)
94+
);
95+
}
96+
return grammar;
97+
}
98+
7099
function buildGrammar() {
71-
const tsGrammar = readYaml("../TypeScript.YAML-tmLanguage");
100+
const tsGrammar = updateGrammarVariables(readYaml("../TypeScript.YAML-tmLanguage"));
72101

73102
// Write TypeScript.tmLanguage
74103
writePlistFile(tsGrammar, "../TypeScript.tmLanguage");

0 commit comments

Comments
 (0)