From abaa1fe61ad1171ca697d07b85a3d98aa99a0070 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 23 Feb 2024 18:19:47 +0100 Subject: [PATCH] fix: shellscript escape, fix https://github.com/shikijs/shiki/issues/591 --- packages/tm-grammars/README.md | 2 +- packages/tm-grammars/grammars/shellscript.json | 6 +++--- packages/tm-grammars/index.js | 2 +- scripts/grammars/cleanup.ts | 11 +++++++++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/tm-grammars/README.md b/packages/tm-grammars/README.md index ee78359..66a73a8 100644 --- a/packages/tm-grammars/README.md +++ b/packages/tm-grammars/README.md @@ -161,7 +161,7 @@ import { grammars } from 'tm-grammars' | `scheme` | | [sjhuangx/vscode-scheme](https://github.com/sjhuangx/vscode-scheme/blob/4e21683666e1e2edfa21d040ecd427f628e7b5ff/syntaxes/scheme.tmLanguage) | [MIT](https://raw.githubusercontent.com/sjhuangx/vscode-scheme/master/LICENSE.md) | | 8.35 kB | | `scss` | | [microsoft/vscode](https://github.com/microsoft/vscode/blob/cf8d61ebd2f022f4ce8280171f0360d1fe0a206d/extensions/scss/syntaxes/scss.tmLanguage.json) | [MIT](https://raw.githubusercontent.com/microsoft/vscode/main/LICENSE.txt) | `css` | 26.21 kB | | `shaderlab` | `shader` | [microsoft/vscode](https://github.com/microsoft/vscode/blob/8fdf170a0850c1cc027382f31650aaf300d3ae2a/extensions/shaderlab/syntaxes/shaderlab.tmLanguage.json) | [MIT](https://raw.githubusercontent.com/microsoft/vscode/main/LICENSE.txt) | `hlsl` | 5.83 kB | -| `shellscript` | `bash` `sh` `shell` `zsh` | [microsoft/vscode](https://github.com/microsoft/vscode/blob/a16d8d0a3e2062678c599134e78f8aa7b124363b/extensions/shellscript/syntaxes/shell-unix-bash.tmLanguage.json) | [MIT](https://raw.githubusercontent.com/microsoft/vscode/main/LICENSE.txt) | | 32.83 kB | +| `shellscript` | `bash` `sh` `shell` `zsh` | [microsoft/vscode](https://github.com/microsoft/vscode/blob/a16d8d0a3e2062678c599134e78f8aa7b124363b/extensions/shellscript/syntaxes/shell-unix-bash.tmLanguage.json) | [MIT](https://raw.githubusercontent.com/microsoft/vscode/main/LICENSE.txt) | | 32.86 kB | | `shellsession` | `console` | [hronro/sublime-linguist-syntax](https://github.com/hronro/sublime-linguist-syntax/blob/9c84f1d69dbe89bc23292f129cf2d408a9c7afe1/syntaxes/ShellSession.tmLanguage) | [MIT](https://raw.githubusercontent.com/hronro/sublime-linguist-syntax/master/LICENSE) | `shellscript` | 567.00 B | | `smalltalk` | | [leocamello/vscode-smalltalk](https://github.com/leocamello/vscode-smalltalk/blob/bc888519a21740b4dbd4edcf2f884c61855dc58b/syntaxes/smalltalk.tmLanguage.json) | [MIT](https://raw.githubusercontent.com/leocamello/vscode-smalltalk/master/LICENSE) | | 3.91 kB | | `solidity` | | [juanfranblanco/vscode-solidity](https://github.com/juanfranblanco/vscode-solidity/blob/75531be0beda99b7293d778eb0b1c63f70d8ac59/syntaxes/solidity.json) | [MIT](https://raw.githubusercontent.com/juanfranblanco/vscode-solidity/master/License.txt) | | 15.15 kB | diff --git a/packages/tm-grammars/grammars/shellscript.json b/packages/tm-grammars/grammars/shellscript.json index ac5c623..456189e 100644 --- a/packages/tm-grammars/grammars/shellscript.json +++ b/packages/tm-grammars/grammars/shellscript.json @@ -105,7 +105,7 @@ ] } }, - "match": "[ \\t]*+([^\n'&;<>\\(\\)\\$`\\\\\"\\|]+(?!>))" + "match": "[ \\t]*+([^ \\t\\n'&;<>\\(\\)\\$`\\\\\"\\|]+(?!>))" }, { "include": "#normal_statement_context" @@ -1755,7 +1755,7 @@ ] } }, - "match": "(?!(?:!|&|\\||\\(|\\)|\\{|\\[|<|>|#|\\n|$|;|[ \\t]))(?:[ \\t]*+([^\n'&;<>\\(\\)\\$`\\\\\"\\|]+(?!>)))?(?:(?:\\$\")|\")", + "match": "(?!(?:!|&|\\||\\(|\\)|\\{|\\[|<|>|#|\\n|$|;|[ \\t]))(?:[ \\t]*+([^ \\t\\n'&;<>\\(\\)\\$`\\\\\"\\|]+(?!>)))?(?:(?:\\$\")|\")", "name": "meta.statement.command.name.quoted.shell string.quoted.double.shell punctuation.definition.string.begin.shell entity.name.function.call.shell entity.name.command.shell" }, "start_of_single_quoted_command_name": { @@ -1784,7 +1784,7 @@ ] } }, - "match": "(?!(?:!|&|\\||\\(|\\)|\\{|\\[|<|>|#|\\n|$|;|[ \\t]))(?:[ \\t]*+([^\n'&;<>\\(\\)\\$`\\\\\"\\|]+(?!>)))?(?:(?:\\$')|')", + "match": "(?!(?:!|&|\\||\\(|\\)|\\{|\\[|<|>|#|\\n|$|;|[ \\t]))(?:[ \\t]*+([^ \\t\\n'&;<>\\(\\)\\$`\\\\\"\\|]+(?!>)))?(?:(?:\\$')|')", "name": "meta.statement.command.name.quoted.shell string.quoted.single.shell punctuation.definition.string.begin.shell entity.name.function.call.shell entity.name.command.shell" }, "string": { diff --git a/packages/tm-grammars/index.js b/packages/tm-grammars/index.js index 606af17..fbc8dc9 100644 --- a/packages/tm-grammars/index.js +++ b/packages/tm-grammars/index.js @@ -2270,7 +2270,7 @@ export const grammars = [ 'shell', 'zsh', ], - byteSize: 33623, + byteSize: 33650, categories: [ 'scripting', ], diff --git a/scripts/grammars/cleanup.ts b/scripts/grammars/cleanup.ts index b3074dd..21a42b5 100644 --- a/scripts/grammars/cleanup.ts +++ b/scripts/grammars/cleanup.ts @@ -24,12 +24,19 @@ export function cleanupGrammar(lang: any) { // (_, key, value) => console.log('lang key removal', key, '|', value), ) + const reGrammarComment = /\s#.*$/ function cleanupMatch(match: string) { - const lines = match.split(/\n/g) + // https://github.com/shikijs/shiki/issues/591#issuecomment-1961637557 + // It seems the `shellscript` grammars has a selector that missing the backslash escape, we patched them here + if (lang.name === 'shellscript') + match = match.replace(/\[\^ \t\n/g, '[^ \\t\\n') + + const lines = match + .split(/\n/g) if (lines.length === 1) return match return lines - .map(i => i.replace(/\s#.*$/, '').trim()) + .map(i => i.replace(reGrammarComment, '').trim()) .join('\n') } function cleanupPattern(a: any) {