Repository navigation
feat(highlighting): colour CI job scripts as shell - #395
Open
X-Guardian wants to merge 1 commit into
Open
X-Guardian wants to merge 1 commit into
X-Guardian wants to merge 1 commit into
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
before_script,scriptandafter_scriptvalues are now highlighted as shell in YAML. This is done with injection grammars that work on top of either VS Code's YAML grammar or Red Hat's, plus VS Code's shell grammar.Change Type
Context
User-facing impact
if/fistructure. YAML structure keeps YAML colours: keys,-markers,|/>headers and!reference [...].script,include: inputs: script:, and keys nested inside a job (such as underrules:) stay YAML.default:, hidden jobs, interpolated job names and names containing colons, legacy globalbefore_script/after_script, and 2- or 4-space indentation.extensionPackinstalls Red Hat YAML, whose grammar replaces VS Code's.How the job-level rule works in a grammar
TextMate regexes can't see a key's parent, and the active YAML grammar can't be relied on to say either: VS Code's nests a scope per mapping level, but Red Hat's has none. So the injection tracks structure itself, by indentation, and hands everything that isn't a script back to
source.yaml:spec,include,variables,workflowand so on, matchingNON_JOB_TOP_LEVEL_KEYWORDS) opens ameta.gitlab-ci.non-jobregion. A legacybefore_script/after_scriptopens a script region. Any other key, includingdefault:, opens ameta.gitlab-ci.jobregion. Each runs until the next top-level key or---.script,before_scriptorafter_scriptentry is a script region; any other entry is YAML. So ascript:nested underrules:sits inside a YAML entry and never matches.-item is its own region, closed by the next item at the same indentation, so an unterminated quote or a trailing\in one item can't spill into the next.!reference,*alias,[],|/>headers and-markers keep YAML colours.The injection selector excludes the three region scopes, so it only ever matches at the top level.
The bash grammar only starts a statement after
^,;,|and similar, so a command after YAML indentation or-would get no colour. Astatementrule starts one at each line's first word, usingsource.shell#typical_statements.Inside a block scalar every line keeps its YAML indentation, but the bash grammar only ends a heredoc on a terminator at column 0. So
done <<EOF…EOFwould turn the rest of the block into heredoc text. A second injection,gitlab-ci-script-heredoc.injection.tmLanguage.json, mirrors the bash heredoc rules but allows the terminator to be indented. It is a separate grammar because it must run inside the script region at any depth (insidecase, loops and subshells), which the main grammar's selector excludes.Line continuations.
source.shellstarts a command name with a\Grule. In a.shfile,\Gnever matches at the start of a new line. Inside a script region, thewhilerules (ours, and the built-in YAML grammar's) leave\Gat the start of every line. Without a fix, every\-continued line was read as a new command name, so a longtool \ --a x \ --b ycommand alternated colours line by line. A third injection,gitlab-ci-script-continuation.injection.tmLanguage.json, handles this:source.shell#optionand#argument, which win over the\Grule.||.Checked against real pipelines
Across 919 real CI files, under both YAML grammars, the highlighted lines match the script lines the linter finds. The script blocks are coloured the same as the same script in a
.shfile, apart from minor scope differences.Known limits (in the README)
script:keys in other YAML files are coloured too. The original extension behaves the same way.GitLab scope
Affected areas
What changed by bucket
syntaxes/gitlab-ci-script.injection.tmLanguage.json!reference, aliases,[], block headers, sequence markers and statement starts. Script text goes tosource.shell, everything else tosource.yaml.syntaxes/gitlab-ci-script-heredoc.injection.tmLanguage.jsonsyntaxes/gitlab-ci-script-continuation.injection.tmLanguage.json\-continued lines aren't read as new commands.package.json,.vscode/extensions.json,.gitignorecontributes.grammarsentries withinjectTo: ["source.yaml"]. Recommends the TmLanguage Syntax Highlighter extension, which validates grammar files. Dev dependenciesvscode-textmateandvscode-onigurumafor the test.README.mdtests/extension-host/suite/scriptGrammar.test.ts,tests/fixtures/grammars/Validation
Local checks
npm ci(clean install)npm run check-typesnpm run lint(clean,--max-warnings 0)npm run test:extension-host(42 passing)npx @vscode/vsce lslists all three grammar files insyntaxes/Tests
scriptGrammar.test.tstokenises withvscode-textmate, and runs every case twice:vscode.env.appRoot, so it matches the VS Code version under test;tests/fixtures/grammars/(MIT, source noted in its README).scriptnext to a real jobscript:;scriptbetween two jobs;default:, hidden jobs at 4-space indentation, legacy globals;\in one item not spilling into the next;\-continued options and arguments, including inside$( ), after an assignment, and a continuation starting with||;!reference,*aliasitems andbefore_script: [];case, and quoted<<-), with the shell after them coloured normally;entity.name.command.shell.Breaking Changes