Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 0bafcaa

Browse files
authored
Merge pull request #279 from rubyide/make_grammar_optimistic_again
Modify upstream grammar to support optimistic syntax. Resolves #251.
2 parents 2e2ef99 + 3339109 commit 0bafcaa

File tree

4 files changed

+92
-23
lines changed

4 files changed

+92
-23
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"begin": "(?=[a-zA-Z0-9_!?]+\\()",
3+
"end": "(?<=\\))",
4+
"name": "meta.function-call.ruby",
5+
"patterns": [
6+
{ "include": "#nest_function_parens" },
7+
{ "include": "#known_function_names" },
8+
{
9+
"match": "([a-zA-Z0-9_!?]+)(?=\\()",
10+
"name": "entity.name.function.ruby"
11+
},
12+
{ "include": "$self" }
13+
]
14+
}

scripts/update-grammar.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ function getCommitSha(repoId, repoPath) {
9292
});
9393
}
9494

95+
function modifyRubyGrammar(grammar) {
96+
const metaFunctionCallRuby = require('./meta.function-call.ruby.json');
97+
const variableOtherRuby = require('./variable.other.ruby.json');
98+
99+
// Replace the constant.other.symbol scope with constant.language.symbol
100+
// this is technically incorrect per the docs but a lot of themes do not
101+
// have colors defined for constant.other.symbol resulting in users
102+
// believing there is no syntax highlighting
103+
let stringPatterns = JSON.stringify(grammar.patterns);
104+
stringPatterns = stringPatterns.replace(/constant\.other\.symbol/g, 'constant.language.symbol');
105+
grammar.patterns = JSON.parse(stringPatterns);
106+
107+
// Insert meta.function-call.ruby and variable.other.ruby at end
108+
grammar.patterns.push(metaFunctionCallRuby, variableOtherRuby);
109+
}
110+
111+
function getGrammarModifier(grammarName) {
112+
switch(grammarName) {
113+
case 'ruby':
114+
return modifyRubyGrammar;
115+
}
116+
}
117+
95118
exports.update = function (repoId, repoPath, dest, modifyGrammar, version = 'master') {
96119
var contentPath = 'https://raw.githubusercontent.com/' + repoId + `/${version}/` + repoPath;
97120
console.log('Reading from ' + contentPath);
@@ -145,5 +168,5 @@ if (path.basename(process.argv[1]).indexOf('update-grammar') !== -1) {
145168
let grammar = process.argv[3];
146169
let outputFile = path.join('syntaxes', grammar + '.cson.json');
147170
let repoFile = grammarToTmLanguage(grammar);
148-
exports.update(repo, repoFile, outputFile);
171+
exports.update(repo, repoFile, outputFile, getGrammarModifier(grammar));
149172
}

scripts/variable.other.ruby.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"comment":
3+
"This is kindof experimental. There really is no way to perfectly match all regular variables, but you can pretty well assume that any normal word in certain curcumstances that havnt already been scoped as something else are probably variables, and the advantages beat the potential errors",
4+
"match":
5+
"((?<=\\W)\\b|^)\\w+\\b(?=\\s*([\\]\\)\\}\\=\\+\\-\\*\\/\\^\\$\\,\\.]|<\\s|<<[\\s|\\.]))",
6+
"name": "variable.other.ruby"
7+
}

syntaxes/ruby.cson.json

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
},
116116
"comment": "symbols as hash key (1.9 syntax)",
117117
"match": "(?>[a-zA-Z_]\\w*(?>[?!])?)(:)(?!:)",
118-
"name": "constant.other.symbol.hashkey.ruby"
118+
"name": "constant.language.symbol.hashkey.ruby"
119119
},
120120
{
121121
"captures": {
@@ -125,7 +125,7 @@
125125
},
126126
"comment": "symbols as hash key (1.8 syntax)",
127127
"match": "(?<!:)(:)(?>[a-zA-Z_]\\w*(?>[?!])?)(?=\\s*=>)",
128-
"name": "constant.other.symbol.hashkey.ruby"
128+
"name": "constant.language.symbol.hashkey.ruby"
129129
},
130130
{
131131
"comment": "everything being a reserved word, not a value and needing a 'end' is a..",
@@ -277,7 +277,7 @@
277277
"name": "storage.type.variable.ruby"
278278
},
279279
"2": {
280-
"name": "constant.other.symbol.hashkey.parameter.function.ruby"
280+
"name": "constant.language.symbol.hashkey.parameter.function.ruby"
281281
},
282282
"3": {
283283
"name": "punctuation.definition.constant.hashkey.ruby"
@@ -319,7 +319,7 @@
319319
"name": "storage.type.variable.ruby"
320320
},
321321
"2": {
322-
"name": "constant.other.symbol.hashkey.parameter.function.ruby"
322+
"name": "constant.language.symbol.hashkey.parameter.function.ruby"
323323
},
324324
"3": {
325325
"name": "punctuation.definition.constant.hashkey.ruby"
@@ -368,7 +368,7 @@
368368
"name": "punctuation.definition.symbol.end.ruby"
369369
}
370370
},
371-
"name": "constant.other.symbol.ruby",
371+
"name": "constant.language.symbol.ruby",
372372
"patterns": [
373373
{
374374
"match": "\\\\['\\\\]",
@@ -390,7 +390,7 @@
390390
"name": "punctuation.section.symbol.end.ruby"
391391
}
392392
},
393-
"name": "constant.other.symbol.interpolated.ruby",
393+
"name": "constant.language.symbol.interpolated.ruby",
394394
"patterns": [
395395
{
396396
"include": "#interpolated_ruby"
@@ -619,7 +619,7 @@
619619
"name": "punctuation.section.array.end.ruby"
620620
}
621621
},
622-
"name": "constant.other.symbol.interpolated.ruby",
622+
"name": "constant.language.symbol.interpolated.ruby",
623623
"patterns": [
624624
{
625625
"include": "#interpolated_ruby"
@@ -645,7 +645,7 @@
645645
"name": "punctuation.section.array.end.ruby"
646646
}
647647
},
648-
"name": "constant.other.symbol.interpolated.ruby",
648+
"name": "constant.language.symbol.interpolated.ruby",
649649
"patterns": [
650650
{
651651
"include": "#interpolated_ruby"
@@ -671,7 +671,7 @@
671671
"name": "punctuation.section.array.end.ruby"
672672
}
673673
},
674-
"name": "constant.other.symbol.interpolated.ruby",
674+
"name": "constant.language.symbol.interpolated.ruby",
675675
"patterns": [
676676
{
677677
"include": "#interpolated_ruby"
@@ -697,7 +697,7 @@
697697
"name": "punctuation.section.array.end.ruby"
698698
}
699699
},
700-
"name": "constant.other.symbol.interpolated.ruby",
700+
"name": "constant.language.symbol.interpolated.ruby",
701701
"patterns": [
702702
{
703703
"include": "#interpolated_ruby"
@@ -723,7 +723,7 @@
723723
"name": "punctuation.section.array.end.ruby"
724724
}
725725
},
726-
"name": "constant.other.symbol.interpolated.ruby",
726+
"name": "constant.language.symbol.interpolated.ruby",
727727
"patterns": [
728728
{
729729
"include": "#interpolated_ruby"
@@ -746,7 +746,7 @@
746746
"name": "punctuation.section.array.end.ruby"
747747
}
748748
},
749-
"name": "constant.other.symbol.ruby",
749+
"name": "constant.language.symbol.ruby",
750750
"patterns": [
751751
{
752752
"match": "\\\\]|\\\\\\\\",
@@ -770,7 +770,7 @@
770770
"name": "punctuation.section.array.end.ruby"
771771
}
772772
},
773-
"name": "constant.other.symbol.ruby",
773+
"name": "constant.language.symbol.ruby",
774774
"patterns": [
775775
{
776776
"match": "\\\\\\)|\\\\\\\\",
@@ -794,7 +794,7 @@
794794
"name": "punctuation.section.array.end.ruby"
795795
}
796796
},
797-
"name": "constant.other.symbol.ruby",
797+
"name": "constant.language.symbol.ruby",
798798
"patterns": [
799799
{
800800
"match": "\\\\>|\\\\\\\\",
@@ -818,7 +818,7 @@
818818
"name": "punctuation.section.array.end.ruby"
819819
}
820820
},
821-
"name": "constant.other.symbol.ruby",
821+
"name": "constant.language.symbol.ruby",
822822
"patterns": [
823823
{
824824
"match": "\\\\}|\\\\\\\\",
@@ -842,7 +842,7 @@
842842
"name": "punctuation.section.array.end.ruby"
843843
}
844844
},
845-
"name": "constant.other.symbol.ruby",
845+
"name": "constant.language.symbol.ruby",
846846
"patterns": [
847847
{
848848
"comment": "Cant be named because its not neccesarily an escape.",
@@ -1374,7 +1374,7 @@
13741374
"name": "punctuation.definition.symbol.end.ruby"
13751375
}
13761376
},
1377-
"name": "constant.other.symbol.ruby",
1377+
"name": "constant.language.symbol.ruby",
13781378
"patterns": [
13791379
{
13801380
"match": "\\\\\\)|\\\\\\\\",
@@ -1398,7 +1398,7 @@
13981398
"name": "punctuation.definition.symbol.end.ruby"
13991399
}
14001400
},
1401-
"name": "constant.other.symbol.ruby",
1401+
"name": "constant.language.symbol.ruby",
14021402
"patterns": [
14031403
{
14041404
"match": "\\\\>|\\\\\\\\",
@@ -1422,7 +1422,7 @@
14221422
"name": "punctuation.definition.symbol.end.ruby"
14231423
}
14241424
},
1425-
"name": "constant.other.symbol.ruby",
1425+
"name": "constant.language.symbol.ruby",
14261426
"patterns": [
14271427
{
14281428
"match": "\\\\]|\\\\\\\\",
@@ -1446,7 +1446,7 @@
14461446
"name": "punctuation.definition.symbol.end.ruby"
14471447
}
14481448
},
1449-
"name": "constant.other.symbol.ruby",
1449+
"name": "constant.language.symbol.ruby",
14501450
"patterns": [
14511451
{
14521452
"match": "\\\\}|\\\\\\\\",
@@ -1470,7 +1470,7 @@
14701470
"name": "punctuation.definition.symbol.end.ruby"
14711471
}
14721472
},
1473-
"name": "constant.other.symbol.ruby",
1473+
"name": "constant.language.symbol.ruby",
14741474
"patterns": [
14751475
{
14761476
"comment": "Cant be named because its not neccesarily an escape.",
@@ -1486,7 +1486,7 @@
14861486
},
14871487
"comment": "symbols",
14881488
"match": "(?x)\n(?<!:)(:)\n(?>\n [$a-zA-Z_]\\w*(?>[?!]|=(?![>=]))?\n |\n ===?|<=>|>[>=]?|<[<=]?|[%&`/\\|]|\\*\\*?|=?~|[-+]@?|\\[]=?\n |\n @@?[a-zA-Z_]\\w*\n)",
1489-
"name": "constant.other.symbol.ruby"
1489+
"name": "constant.language.symbol.ruby"
14901490
},
14911491
{
14921492
"begin": "^=begin",
@@ -2179,6 +2179,31 @@
21792179
{
21802180
"match": "\\(|\\)",
21812181
"name": "punctuation.section.function.ruby"
2182+
},
2183+
{
2184+
"begin": "(?=[a-zA-Z0-9_!?]+\\()",
2185+
"end": "(?<=\\))",
2186+
"name": "meta.function-call.ruby",
2187+
"patterns": [
2188+
{
2189+
"include": "#nest_function_parens"
2190+
},
2191+
{
2192+
"include": "#known_function_names"
2193+
},
2194+
{
2195+
"match": "([a-zA-Z0-9_!?]+)(?=\\()",
2196+
"name": "entity.name.function.ruby"
2197+
},
2198+
{
2199+
"include": "$self"
2200+
}
2201+
]
2202+
},
2203+
{
2204+
"comment": "This is kindof experimental. There really is no way to perfectly match all regular variables, but you can pretty well assume that any normal word in certain curcumstances that havnt already been scoped as something else are probably variables, and the advantages beat the potential errors",
2205+
"match": "((?<=\\W)\\b|^)\\w+\\b(?=\\s*([\\]\\)\\}\\=\\+\\-\\*\\/\\^\\$\\,\\.]|<\\s|<<[\\s|\\.]))",
2206+
"name": "variable.other.ruby"
21822207
}
21832208
],
21842209
"repository": {

0 commit comments

Comments
 (0)