This commit is contained in:
Steven Degutis 2025-02-05 04:26:58 +07:00 committed by GitHub
commit 89f98119c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -75,138 +75,47 @@ export const conf: languages.LanguageConfiguration = {
}; };
export const language = { export const language = {
// Set defaultToken to invalid to see what you do not tokenize yet
defaultToken: 'invalid', defaultToken: 'invalid',
tokenPostfix: '.ts', tokenPostfix: '.ts',
typeKeywords: [
'any', 'bigint', 'boolean', 'number', 'object', 'string', 'unknown', 'void',
],
ctrlKeywords: [
'export', 'default', 'return', 'as', 'if', 'break', 'case', 'catch', 'continue',
'do', 'else', 'finally', 'for', 'throw', 'try', 'with', 'yield', 'await',
'import', 'from', 'type',
],
alwaysKeyword: [
'constructor', 'super',
],
keywords: [ keywords: [
// Should match the keys of textToKeywordObj in // Should match the keys of textToKeywordObj in
// https://github.com/microsoft/TypeScript/blob/master/src/compiler/scanner.ts // https://github.com/microsoft/TypeScript/blob/master/src/compiler/scanner.ts
'abstract', 'abstract', 'asserts',
'any', 'class', 'const', 'debugger',
'as', 'declare', 'delete', 'enum',
'asserts', 'extends', 'false', 'function', 'get',
'bigint', 'implements', 'in', 'infer', 'instanceof', 'interface',
'boolean', 'is', 'keyof', 'let', 'module', 'namespace', 'never', 'new',
'break', 'null', 'out', 'package', 'private', 'protected',
'case', 'public', 'override', 'readonly', 'require', 'global', 'satisfies',
'catch', 'set', 'static', 'switch', 'symbol', 'this',
'class', 'true', 'typeof', 'undefined', 'unique',
'continue', 'var', 'while', 'async', 'of'
'const',
'constructor',
'debugger',
'declare',
'default',
'delete',
'do',
'else',
'enum',
'export',
'extends',
'false',
'finally',
'for',
'from',
'function',
'get',
'if',
'implements',
'import',
'in',
'infer',
'instanceof',
'interface',
'is',
'keyof',
'let',
'module',
'namespace',
'never',
'new',
'null',
'number',
'object',
'out',
'package',
'private',
'protected',
'public',
'override',
'readonly',
'require',
'global',
'return',
'satisfies',
'set',
'static',
'string',
'super',
'switch',
'symbol',
'this',
'throw',
'true',
'try',
'type',
'typeof',
'undefined',
'unique',
'unknown',
'var',
'void',
'while',
'with',
'yield',
'async',
'await',
'of'
], ],
operators: [ operators: [
'<=', '<=', '>=', '==', '!=', '===', '!==', '=>',
'>=', '+', '-', '**', '*', '/', '%', '++',
'==', '--', '<<', '</', '>>', '>>>', '&', '|',
'!=', '^', '!', '~', '&&', '||', '??', '?',
'===', ':', '=', '+=', '-=', '*=', '**=', '/=',
'!==', '%=', '<<=', '>>=', '>>>=', '&=', '|=', '^=', '@'
'=>',
'+',
'-',
'**',
'*',
'/',
'%',
'++',
'--',
'<<',
'</',
'>>',
'>>>',
'&',
'|',
'^',
'!',
'~',
'&&',
'||',
'??',
'?',
':',
'=',
'+=',
'-=',
'*=',
'**=',
'/=',
'%=',
'<<=',
'>>=',
'>>>=',
'&=',
'|=',
'^=',
'@'
], ],
// we include these common regular expressions // we include these common regular expressions
@ -222,21 +131,70 @@ export const language = {
// The main tokenizer for our languages // The main tokenizer for our languages
tokenizer: { tokenizer: {
root: [[/[{}]/, 'delimiter.bracket'], { include: 'common' }], root: [
[/}/, {
cases: {
'$S2==INJSX': { token: '@brackets', next: '@pop' },
'@default': '@brackets',
}
}],
common: [ [/{/, 'delimiter.bracket'],
// identifiers and keywords
[ // highlight class field-properties
/#?[a-z_$][\w$]*/, [/^\s+#?[\w$]+(?=\s*[;=:])/, 'variable.property'],
// highlight function/class defs
[/(function|class|new)(\s+)(#?[\w$]+)(\s*)([<(]?)/, ['keyword', '',
{ {
cases: { cases: {
'@keywords': 'keyword', '$1==function': 'method',
'@default': 'identifier' '$1==class': 'type.identifier',
'$1==new': 'type.identifier',
} }
}, '', {
cases: {
'<': { token: '@brackets', next: '@typeparams' },
'@default': '@rematch',
} }
},
]],
// highlight var/const/let defs
[/(const|let|var)(\s+)(#?[\w$]+)/, ['keyword', '', {
cases: {
'$1==const': 'constant',
'@default': 'variable',
}
}]],
{ include: 'jsxReady' },
{ include: 'common' },
], ],
[/[A-Z][\w\$]*/, 'type.identifier'], // to show class names nicely
// [/[A-Z][\w\$]*/, 'identifier'], common: [
// identifiers and keywords
[/(#?[a-zA-Z_$][\w$]*)([<(]?)/, [
{
cases: {
'@typeKeywords': 'type.identifier',
'@alwaysKeyword': 'keyword',
'$1~#?[A-Z].*': 'type.identifier',
'$2': 'method',
'@ctrlKeywords': 'keyword.flow',
'@keywords': 'keyword',
'@default': 'identifier',
}
},
{
cases: {
'$2==<': { token: '@brackets', next: '@typeparams' },
'@default': '@rematch',
}
},
]],
// whitespace // whitespace
{ include: '@whitespace' }, { include: '@whitespace' },
@ -261,6 +219,8 @@ export const language = {
} }
], ],
[/\.\.\./, 'keyword'],
// numbers // numbers
[/(@digits)[eE]([\-+]?(@digits))?/, 'number.float'], [/(@digits)[eE]([\-+]?(@digits))?/, 'number.float'],
[/(@digits)\.(@digits)([eE][\-+]?(@digits))?/, 'number.float'], [/(@digits)\.(@digits)([eE][\-+]?(@digits))?/, 'number.float'],
@ -280,6 +240,60 @@ export const language = {
[/`/, 'string', '@string_backtick'] [/`/, 'string', '@string_backtick']
], ],
typeparams: [
[/>/, '@brackets', '@pop'],
{ include: 'common' }
],
jsxReady: [
[/<>/, 'delimiter.html', '@jsxText.FRAGMENT'],
[/(<)([A-Z][\w$]*\s*(?:,|extends|implements))/, ['@brackets', { token: '@rematch', next: '@typeparams' }]],
[/(<)(\s*)([\w$])/, ['delimiter.html', '',
{ token: '@rematch', next: '@jsxIdent.jsxOpen.' },
]],
],
jsxIdent: [
[/\./, { token: 'delimiter', switchTo: '$S0^' }],
[/[A-Z][\w$]*/, { token: 'type.identifier', switchTo: '$S0$0' }],
[/[\w$-]+/, { token: 'tag', switchTo: '$S0$0' }],
[/.+/, { token: '@rematch', switchTo: '@$S2.$S3.$S4' }],
],
jsxOpen: [
[/{/, { token: 'keyword', next: '@root.INJSX', bracket: '@open' }],
[/>/, { token: 'delimiter.html', switchTo: '@jsxText.$S2' }],
[/\/>/, { token: 'delimiter.html', next: '@pop' }],
[/ +([\w-$]+)/, 'attribute.name'],
[/(=)(')/, ['delimiter', { token: 'string', next: '@string_single' }]],
[/(=)(")/, ['delimiter', { token: 'string', next: '@string_double' }]],
[/(=)({)/, ['delimiter', { token: '@brackets', next: '@root.INJSX' }]],
],
jsxText: [
[/{/, { token: 'keyword', next: '@root.INJSX', bracket: '@open' }],
[/<\/>/, {
cases: {
'$S2==FRAGMENT': { token: 'delimiter.html', next: '@pop' },
'@default': { token: 'invalid', next: '@pop' },
}
}],
[/(<\/)(\s*)([\w$])/, ['delimiter.html', '',
{ token: '@rematch', switchTo: '@jsxIdent.jsxClose.$S2.' },
]],
{ include: 'jsxReady' },
[/./, 'string'],
],
jsxClose: [
[/>/, {
cases: {
'$S2==$S3': { token: 'delimiter.html', next: '@pop' },
'@default': { token: 'invalid', next: '@pop' },
}
}],
],
whitespace: [ whitespace: [
[/[ \t\r\n]+/, ''], [/[ \t\r\n]+/, ''],
[/\/\*\*(?!\/)/, 'comment.doc', '@jsdoc'], [/\/\*\*(?!\/)/, 'comment.doc', '@jsdoc'],
@ -361,4 +375,5 @@ export const language = {
{ include: 'common' } { include: 'common' }
] ]
} }
}; };