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

Modify upstream grammar to support optimistic syntax. Resolves #251. #279

Merged
merged 2 commits into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions scripts/meta.function-call.ruby.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"begin": "(?=[a-zA-Z0-9_!?]+\\()",
"end": "(?<=\\))",
"name": "meta.function-call.ruby",
"patterns": [
{ "include": "#nest_function_parens" },
{ "include": "#known_function_names" },
{
"match": "([a-zA-Z0-9_!?]+)(?=\\()",
"name": "entity.name.function.ruby"
},
{ "include": "$self" }
]
}
25 changes: 24 additions & 1 deletion scripts/update-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ function getCommitSha(repoId, repoPath) {
});
}

function modifyRubyGrammar(grammar) {
const metaFunctionCallRuby = require('./meta.function-call.ruby.json');
const variableOtherRuby = require('./variable.other.ruby.json');

// Replace the constant.other.symbol scope with constant.language.symbol
// this is technically incorrect per the docs but a lot of themes do not
// have colors defined for constant.other.symbol resulting in users
// believing there is no syntax highlighting
let stringPatterns = JSON.stringify(grammar.patterns);
stringPatterns = stringPatterns.replace(/constant\.other\.symbol/g, 'constant.language.symbol');
grammar.patterns = JSON.parse(stringPatterns);

// Insert meta.function-call.ruby and variable.other.ruby at end
grammar.patterns.push(metaFunctionCallRuby, variableOtherRuby);
}

function getGrammarModifier(grammarName) {
switch(grammarName) {
case 'ruby':
return modifyRubyGrammar;
}
}

exports.update = function (repoId, repoPath, dest, modifyGrammar, version = 'master') {
var contentPath = 'https://raw.githubusercontent.com/' + repoId + `/${version}/` + repoPath;
console.log('Reading from ' + contentPath);
Expand Down Expand Up @@ -145,5 +168,5 @@ if (path.basename(process.argv[1]).indexOf('update-grammar') !== -1) {
let grammar = process.argv[3];
let outputFile = path.join('syntaxes', grammar + '.cson.json');
let repoFile = grammarToTmLanguage(grammar);
exports.update(repo, repoFile, outputFile);
exports.update(repo, repoFile, outputFile, getGrammarModifier(grammar));
}
7 changes: 7 additions & 0 deletions scripts/variable.other.ruby.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"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",
"match":
"((?<=\\W)\\b|^)\\w+\\b(?=\\s*([\\]\\)\\}\\=\\+\\-\\*\\/\\^\\$\\,\\.]|<\\s|<<[\\s|\\.]))",
"name": "variable.other.ruby"
}
69 changes: 47 additions & 22 deletions syntaxes/ruby.cson.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
},
"comment": "symbols as hash key (1.9 syntax)",
"match": "(?>[a-zA-Z_]\\w*(?>[?!])?)(:)(?!:)",
"name": "constant.other.symbol.hashkey.ruby"
"name": "constant.language.symbol.hashkey.ruby"
},
{
"captures": {
Expand All @@ -125,7 +125,7 @@
},
"comment": "symbols as hash key (1.8 syntax)",
"match": "(?<!:)(:)(?>[a-zA-Z_]\\w*(?>[?!])?)(?=\\s*=>)",
"name": "constant.other.symbol.hashkey.ruby"
"name": "constant.language.symbol.hashkey.ruby"
},
{
"comment": "everything being a reserved word, not a value and needing a 'end' is a..",
Expand Down Expand Up @@ -277,7 +277,7 @@
"name": "storage.type.variable.ruby"
},
"2": {
"name": "constant.other.symbol.hashkey.parameter.function.ruby"
"name": "constant.language.symbol.hashkey.parameter.function.ruby"
},
"3": {
"name": "punctuation.definition.constant.hashkey.ruby"
Expand Down Expand Up @@ -319,7 +319,7 @@
"name": "storage.type.variable.ruby"
},
"2": {
"name": "constant.other.symbol.hashkey.parameter.function.ruby"
"name": "constant.language.symbol.hashkey.parameter.function.ruby"
},
"3": {
"name": "punctuation.definition.constant.hashkey.ruby"
Expand Down Expand Up @@ -368,7 +368,7 @@
"name": "punctuation.definition.symbol.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\['\\\\]",
Expand All @@ -390,7 +390,7 @@
"name": "punctuation.section.symbol.end.ruby"
}
},
"name": "constant.other.symbol.interpolated.ruby",
"name": "constant.language.symbol.interpolated.ruby",
"patterns": [
{
"include": "#interpolated_ruby"
Expand Down Expand Up @@ -619,7 +619,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.interpolated.ruby",
"name": "constant.language.symbol.interpolated.ruby",
"patterns": [
{
"include": "#interpolated_ruby"
Expand All @@ -645,7 +645,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.interpolated.ruby",
"name": "constant.language.symbol.interpolated.ruby",
"patterns": [
{
"include": "#interpolated_ruby"
Expand All @@ -671,7 +671,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.interpolated.ruby",
"name": "constant.language.symbol.interpolated.ruby",
"patterns": [
{
"include": "#interpolated_ruby"
Expand All @@ -697,7 +697,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.interpolated.ruby",
"name": "constant.language.symbol.interpolated.ruby",
"patterns": [
{
"include": "#interpolated_ruby"
Expand All @@ -723,7 +723,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.interpolated.ruby",
"name": "constant.language.symbol.interpolated.ruby",
"patterns": [
{
"include": "#interpolated_ruby"
Expand All @@ -746,7 +746,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\]|\\\\\\\\",
Expand All @@ -770,7 +770,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\\\)|\\\\\\\\",
Expand All @@ -794,7 +794,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\>|\\\\\\\\",
Expand All @@ -818,7 +818,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\}|\\\\\\\\",
Expand All @@ -842,7 +842,7 @@
"name": "punctuation.section.array.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"comment": "Cant be named because its not neccesarily an escape.",
Expand Down Expand Up @@ -1374,7 +1374,7 @@
"name": "punctuation.definition.symbol.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\\\)|\\\\\\\\",
Expand All @@ -1398,7 +1398,7 @@
"name": "punctuation.definition.symbol.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\>|\\\\\\\\",
Expand All @@ -1422,7 +1422,7 @@
"name": "punctuation.definition.symbol.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\]|\\\\\\\\",
Expand All @@ -1446,7 +1446,7 @@
"name": "punctuation.definition.symbol.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"match": "\\\\}|\\\\\\\\",
Expand All @@ -1470,7 +1470,7 @@
"name": "punctuation.definition.symbol.end.ruby"
}
},
"name": "constant.other.symbol.ruby",
"name": "constant.language.symbol.ruby",
"patterns": [
{
"comment": "Cant be named because its not neccesarily an escape.",
Expand All @@ -1486,7 +1486,7 @@
},
"comment": "symbols",
"match": "(?x)\n(?<!:)(:)\n(?>\n [$a-zA-Z_]\\w*(?>[?!]|=(?![>=]))?\n |\n ===?|<=>|>[>=]?|<[<=]?|[%&`/\\|]|\\*\\*?|=?~|[-+]@?|\\[]=?\n |\n @@?[a-zA-Z_]\\w*\n)",
"name": "constant.other.symbol.ruby"
"name": "constant.language.symbol.ruby"
},
{
"begin": "^=begin",
Expand Down Expand Up @@ -2179,6 +2179,31 @@
{
"match": "\\(|\\)",
"name": "punctuation.section.function.ruby"
},
{
"begin": "(?=[a-zA-Z0-9_!?]+\\()",
"end": "(?<=\\))",
"name": "meta.function-call.ruby",
"patterns": [
{
"include": "#nest_function_parens"
},
{
"include": "#known_function_names"
},
{
"match": "([a-zA-Z0-9_!?]+)(?=\\()",
"name": "entity.name.function.ruby"
},
{
"include": "$self"
}
]
},
{
"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",
"match": "((?<=\\W)\\b|^)\\w+\\b(?=\\s*([\\]\\)\\}\\=\\+\\-\\*\\/\\^\\$\\,\\.]|<\\s|<<[\\s|\\.]))",
"name": "variable.other.ruby"
}
],
"repository": {
Expand Down