rob-revisited/ide/ui/js/yaml-lang.js

137 lines
7.2 KiB
JavaScript

// YAML for Monaco (the IDE's editor and the quick editor), read as YAML is: Monaco's own rules colour keys like plain text and
// know nothing of merge keys, custom tags or Compose's ${VAR:-default}. Here: keys, anchors &x / aliases *x / merge <<,
// tags (!!str, !php/const, !tagged_iterator), block scalars (| >) and their text, comments, booleans / null / numbers,
// quoted strings, flow collections, environment interpolation (compose files, .env-style values).
// Problems, outline and completion come from the PHP side (yaml.diagnostics, yaml.outline, config.complete).
const END = /(?=[ \t]*(?:#.*)?$|[ \t]*[,\]}])/.source;
export const YAML_LANGUAGE = {
tokenPostfix: '.yaml',
defaultToken: 'string',
tokenizer: {
root: [
[/^%.*$/, 'meta.directive'],
[/^(?:---|\.\.\.)(?=\s|$)/, 'delimiter.document'],
{ include: '@line' },
],
line: [
[/[ \t]+/, 'white'],
[/#.*$/, 'comment'],
[/-(?=[ \t]|$)/, 'delimiter.dash'],
[/\?(?=[ \t])/, 'delimiter'],
[/<<(?=[ \t]*:(?:[ \t]|$))/, 'keyword.merge'],
[/("(?:[^"\\]|\\.)*"|'(?:[^']|'')*'|[^\s#'"[\]{},&*!|>?%@`-](?:[^:#]|:(?![ \t]|$)|[ \t]#)*?|-[^\s#](?:[^:#]|:(?![ \t]|$))*?)([ \t]*)(:)(?=[ \t]|$)/,
['key', 'white', 'delimiter.colon']],
{ include: '@value' },
],
value: [
[/&[^\s,[\]{}]+/, 'variable.anchor'],
[/\*[^\s,[\]{}]+/, 'variable.alias'],
[/!(?:<[^>]*>|[\w!/.:%-]*)/, 'tag'],
[/[|>][1-9+-]{0,2}(?=[ \t]*(?:#.*)?$)/, { token: 'operator.block', next: '@blockStart' }],
[/[[{]/, { token: 'delimiter.bracket', next: '@flow' }],
[/"/, { token: 'string.quote', next: '@dq' }],
[/'/, { token: 'string.quote', next: '@sq' }],
[new RegExp('(?:true|false|True|False|TRUE|FALSE|null|Null|NULL|~)' + END), 'keyword'],
[new RegExp('[-+]?(?:\\.inf|\\.Inf|\\.INF|\\.nan|\\.NaN|\\.NAN|0x[0-9a-fA-F]+|0o[0-7]+|\\d[\\d_]*(?:\\.\\d*)?(?:[eE][-+]?\\d+)?)' + END), 'number'],
[/\$\{[^}\s]*\}|\$[A-Za-z_]\w*|\$\$/, 'variable.env'],
[/[^\s#$"'[\]{},][^\s$[\]{},]*/, 'string'],
[/[$,\]}]/, 'string'],
],
flow: [
[/[\]}]/, { token: 'delimiter.bracket', next: '@pop' }],
[/,/, 'delimiter.comma'],
[/[ \t]+/, 'white'],
[/#.*$/, 'comment'],
[/("(?:[^"\\]|\\.)*"|'(?:[^']|'')*'|[^\s#'"[\]{},:]+)([ \t]*)(:)(?=[ \t]|$)/, ['key', 'white', 'delimiter.colon']],
{ include: '@value' },
],
dq: [
[/\$\{[^}"]*\}/, 'variable.env'],
[/[^\\"$]+/, 'string'],
[/\\./, 'string.escape'],
[/\$/, 'string'],
[/"/, { token: 'string.quote', next: '@pop' }],
],
sq: [
[/\$\{[^}']*\}/, 'variable.env'],
[/[^'$]+/, 'string'],
[/''/, 'string.escape'],
[/\$/, 'string'],
[/'/, { token: 'string.quote', next: '@pop' }],
],
// a block scalar: the lines more indented than its first one are its text
blockStart: [
[/^[ \t]*$/, 'white'],
[/^( +)(?=\S)/, { token: 'white', switchTo: '@blockBody.$1' }],
[/^(?=\S)/, { token: '@rematch', next: '@pop' }],
[/[ \t]+/, 'white'],
[/#.*$/, 'comment'],
],
blockBody: [
[/^[ \t]*$/, 'white'],
[/^( *)(\S.*)$/, { cases: { '$1~$S2.*': 'string.block', '@default': { token: '@rematch', next: '@pop' } } }],
[/.+$/, 'string.block'],
],
},
};
// colours (the IDE's themes): keys orange, values green, booleans / null blue, anchors pink, tags yellow, ${VAR} violet
export const YAML_DARK = [
{ token: 'key.yaml', foreground: 'CF8E6D' }, { token: 'string.yaml', foreground: '6AAB73' }, { token: 'string.block.yaml', foreground: '6AAB73' },
{ token: 'string.quote.yaml', foreground: '6AAB73' }, { token: 'string.escape.yaml', foreground: 'CF8E6D' }, { token: 'keyword.yaml', foreground: '56A8F5' },
{ token: 'number.yaml', foreground: '2AACB8' }, { token: 'variable.anchor.yaml', foreground: 'C77DBB' }, { token: 'variable.alias.yaml', foreground: 'C77DBB', fontStyle: 'italic' },
{ token: 'keyword.merge.yaml', foreground: 'C77DBB', fontStyle: 'bold' }, { token: 'tag.yaml', foreground: 'D5B778' }, { token: 'variable.env.yaml', foreground: 'B189F5' },
{ token: 'operator.block.yaml', foreground: 'CF8E6D', fontStyle: 'bold' }, { token: 'delimiter.dash.yaml', foreground: 'CF8E6D' },
{ token: 'delimiter.colon.yaml', foreground: 'BCBEC4' }, { token: 'delimiter.document.yaml', foreground: 'CF8E6D' }, { token: 'meta.directive.yaml', foreground: '7A7E85' },
];
export const YAML_LIGHT = [
{ token: 'key.yaml', foreground: '0033B3' }, { token: 'string.yaml', foreground: '067D17' }, { token: 'string.block.yaml', foreground: '067D17' },
{ token: 'string.quote.yaml', foreground: '067D17' }, { token: 'string.escape.yaml', foreground: '0037A6' }, { token: 'keyword.yaml', foreground: '0033B3', fontStyle: 'bold' },
{ token: 'number.yaml', foreground: '1750EB' }, { token: 'variable.anchor.yaml', foreground: '871094' }, { token: 'variable.alias.yaml', foreground: '871094', fontStyle: 'italic' },
{ token: 'keyword.merge.yaml', foreground: '871094', fontStyle: 'bold' }, { token: 'tag.yaml', foreground: '9E880D' }, { token: 'variable.env.yaml', foreground: '00627A' },
{ token: 'operator.block.yaml', foreground: '0033B3', fontStyle: 'bold' }, { token: 'comment.yaml', foreground: '8C8C8C', fontStyle: 'italic' },
];
/** compose.yaml, docker-compose.yml, compose.override.yaml, compose.prod.yaml... (the same rule as ComposeLint::isCompose) */
export function isComposeFile(path) {
return /^(docker-)?compose([.-][\w.-]+)?\.ya?ml$/i.test(String(path).split('/').pop());
}
/** The files config.complete knows: JumPHP's config, Symfony's services, forms, Compose files. */
export function completesYaml(path) {
return /(^|\/)config\/(game|input|services(_\w+)?|lang)\.ya?ml$|\.form\.ya?ml$/.test(path) || isComposeFile(path);
}
/**
* The image named on a line ("image: nginx:1.27", "- image: 'bitnami/redis'") when the column is on its value: its Docker Hub
* name (no tag) and where it is; null for another registry (ghcr.io/..., localhost:5000/...) or another line.
*/
export function imageAt(line, column) {
const m = /^(\s*(?:-\s+)?image\s*:\s*["']?)([^\s"'#]+)/.exec(line);
if (!m) return null;
const start = m[1].length + 1;
const end = start + m[2].length;
if (column < start || column > end) return null;
const ref = m[2].replace(/@sha256:.*$/, '');
const name = ref.replace(/:[\w.-]+$/, '').toLowerCase();
if (/^[^/]*[.:][^/]*\//.test(name) || !/^[a-z0-9]+(?:[._-][a-z0-9]+)*(?:\/[a-z0-9]+(?:[._-][a-z0-9]+)*)?$/.test(name)) return null;
return { name, tag: ref.includes(':') ? ref.slice(ref.lastIndexOf(':') + 1) : 'latest', start, end };
}
/** The text of a line once "image: name:tag" is put there (its value replaced, or the key written at its indentation). */
export function withImage(line, ref) {
const m = /^(\s*(?:-\s+)?image\s*:\s*)(["']?)[^\s"'#]*\2(.*)$/.exec(line);
if (m) return `${m[1]}${ref}${m[3]}`;
const indent = /^\s*/.exec(line)[0];
return line.trim() === '' ? `${indent}image: ${ref}` : null;
}
let done = false;
/** Sets the rules over Monaco's own (a tokens provider set here wins over its lazy one). */
export function registerYaml(monaco) {
if (done) return;
done = true;
monaco.languages.setMonarchTokensProvider('yaml', YAML_LANGUAGE);
}