Skip to content

Commit

Permalink
Fix conditional local var highlight (#2719)
Browse files Browse the repository at this point in the history
* Update ruby.cson.json grammar for local variable assignment and operation assignment in conditions

* add test

* Update ruby.cson.json grammar for local variable assignment and operation assignment in conditions

* Update ruby.cson.json grammar for local variable and/or assignment and operation and/or assignment in conditions

* fix: remove `[` from and/or assignment in a condition
  • Loading branch information
v010maaa authored Oct 17, 2024
1 parent 063b933 commit 01a8ecf
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
40 changes: 35 additions & 5 deletions vscode/grammars/ruby.cson.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,23 @@
"name": "keyword.operator.assignment.augmented.ruby"
}
},
"match": "^\\s*([a-z]([A-Za-z0-9_])*)\\s*(&&=)",
"comment": "A local variable and assignment"
"match": "^\\s*([a-z]([A-Za-z0-9_])*)\\s*((&&|\\|\\|)=)",
"comment": "A local variable and/or assignment"
},
{
"captures": {
"1": {
"name": "variable.ruby"
"name": "keyword.control.ruby"
},
"3": {
"name": "variable.ruby"
},
"5": {
"name": "keyword.operator.assignment.augmented.ruby"
}
},
"match": "^\\s*([a-z]([A-Za-z0-9_])*)\\s*(\\|\\|=)",
"comment": "A local variable or assignment"
"match": "(?<!\\.)\\b(case|if|elsif|unless|until|while)\\b\\s*(\\()*?\\s*([a-z]([A-Za-z0-9_])*)\\s*((&&|\\|\\|)=)",
"comment": "A local variable and/or assignment in a condition"
},
{
"captures": {
Expand All @@ -136,6 +139,21 @@
"match": "^\\s*([a-z]([A-Za-z0-9_])*)\\s*((\\+|\\*|-|\\/|%|\\*\\*|&|\\||\\^|<<|>>)=)",
"comment": "A local variable operation assignment (+=, -=, *=, /=)"
},
{
"captures": {
"1": {
"name": "keyword.control.ruby"
},
"3": {
"name": "variable.ruby"
},
"5": {
"name": "keyword.operator.assignment.augmented.ruby"
}
},
"match": "(?<!\\.)\\b(case|if|elsif|unless|until|while)\\b\\s*(\\()*?\\s*([a-z]([A-Za-z0-9_])*)\\s*((\\+|\\*|-|\\/|%|\\*\\*|&|\\||\\^|<<|>>)=)",
"comment": "A local variable operation assignment in a condition"
},
{
"captures": {
"1": {
Expand All @@ -145,6 +163,18 @@
"match": "^\\s*([a-z]([A-Za-z0-9_])*)\\s*=[^=>]",
"comment": "A local variable assignment"
},
{
"captures": {
"1": {
"name": "keyword.control.ruby"
},
"3": {
"name": "variable.ruby"
}
},
"match": "(?<!\\.)\\b(case|if|elsif|unless|until|while)\\b\\s*(\\()*?\\s*([a-z]([A-Za-z0-9_])*)\\s*=[^=>]",
"comment": "A local variable assignment in a condition"
},
{
"captures": {
"1": {
Expand Down
60 changes: 60 additions & 0 deletions vscode/src/test/suite/grammars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,66 @@ suite("Grammars", () => {
const actualTokens = tokenizeRuby(ruby);
assert.deepStrictEqual(actualTokens, expectedTokens);
});
test("or assignment", () => {
const ruby = "local ||= 1";
const expectedTokens = [
["local", ["source.ruby", "variable.ruby"]],
[" ", ["source.ruby"]],
[
"||=",
["source.ruby", "keyword.operator.assignment.augmented.ruby"],
],
[" ", ["source.ruby"]],
["1", ["source.ruby", "constant.numeric.ruby"]],
];
const actualTokens = tokenizeRuby(ruby);
assert.deepStrictEqual(actualTokens, expectedTokens);
});
test("and assignment in a condition", () => {
const ruby = "if local &&= 1";
const expectedTokens = [
["if", ["source.ruby", "keyword.control.ruby"]],
[" ", ["source.ruby"]],
["local", ["source.ruby", "variable.ruby"]],
[" ", ["source.ruby"]],
[
"&&=",
["source.ruby", "keyword.operator.assignment.augmented.ruby"],
],
[" ", ["source.ruby"]],
["1", ["source.ruby", "constant.numeric.ruby"]],
];
const actualTokens = tokenizeRuby(ruby);
assert.deepStrictEqual(actualTokens, expectedTokens);
});
test("assignment in a condition", () => {
const ruby = "if (local = 1)";
const expectedTokens = [
["if", ["source.ruby", "keyword.control.ruby"]],
[" (", ["source.ruby"]],
["local", ["source.ruby", "variable.ruby"]],
[" = ", ["source.ruby"]],
["1", ["source.ruby", "constant.numeric.ruby"]],
[")", ["source.ruby", "punctuation.section.function.ruby"]],
];
const actualTokens = tokenizeRuby(ruby);
assert.deepStrictEqual(actualTokens, expectedTokens);
});
test("operation assignment in a condition", () => {
const ruby = "if (local += 1)";
const expectedTokens = [
["if", ["source.ruby", "keyword.control.ruby"]],
[" (", ["source.ruby"]],
["local", ["source.ruby", "variable.ruby"]],
[" ", ["source.ruby"]],
["+=", ["source.ruby", "keyword.operator.assignment.augmented.ruby"]],
[" ", ["source.ruby"]],
["1", ["source.ruby", "constant.numeric.ruby"]],
[")", ["source.ruby", "punctuation.section.function.ruby"]],
];
const actualTokens = tokenizeRuby(ruby);
assert.deepStrictEqual(actualTokens, expectedTokens);
});
});

function tokenizeRuby(ruby: string): [string, string[]][] {
Expand Down

0 comments on commit 01a8ecf

Please sign in to comment.