Skip to content

Commit

Permalink
fix(grammar): Fix syntax highlighting for Kernel.` method in Ruby gra…
Browse files Browse the repository at this point in the history
…mmar (#2493)

fix(grammar): Correct handling of backtick strings

- Updated the Ruby grammar to properly differentiate between backtick string literals and Kernel backtick method calls by modifying the `begin` pattern to use a negative lookbehind `(?<!\\.)`.
- Added tests to ensure correct tokenization of standard backtick strings and Kernel backtick method calls.
  • Loading branch information
Sean0628 authored Aug 27, 2024
1 parent 6b05e67 commit 1891baa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vscode/grammars/ruby.cson.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
]
},
{
"begin": "`",
"begin": "(?<!\\.)`",
"beginCaptures": {
"0": {
"name": "punctuation.definition.string.begin.ruby"
Expand Down
55 changes: 55 additions & 0 deletions vscode/src/test/suite/grammars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,61 @@ suite("Grammars", () => {
});
});

suite("Backtick String Literals", () => {
test("Standard backtick string", () => {
const ruby = "`ruby`";
const expectedTokens = [
[
"`",
[
"source.ruby",
"string.interpolated.ruby",
"punctuation.definition.string.begin.ruby",
],
],
["ruby", ["source.ruby", "string.interpolated.ruby"]],
[
"`",
[
"source.ruby",
"string.interpolated.ruby",
"punctuation.definition.string.end.ruby",
],
],
];
const actualTokens = tokenizeRuby(ruby);
assert.deepStrictEqual(actualTokens, expectedTokens);
});

test("Kernel backtick method", () => {
const ruby = 'Kernel.`"ls"';
const expectedTokens = [
["Kernel", ["source.ruby", "variable.other.constant.ruby"]],
[".", ["source.ruby", "punctuation.separator.method.ruby"]],
["`", ["source.ruby"]],
[
'"',
[
"source.ruby",
"string.quoted.double.interpolated.ruby",
"punctuation.definition.string.begin.ruby",
],
],
["ls", ["source.ruby", "string.quoted.double.interpolated.ruby"]],
[
'"',
[
"source.ruby",
"string.quoted.double.interpolated.ruby",
"punctuation.definition.string.end.ruby",
],
],
];
const actualTokens = tokenizeRuby(ruby);
assert.deepStrictEqual(actualTokens, expectedTokens);
});
});

function tokenizeRuby(ruby: string): [string, string[]][] {
if (!rubyGrammar) {
throw new Error("Ruby grammar not loaded");
Expand Down

0 comments on commit 1891baa

Please sign in to comment.