|
| 1 | +import type * as monaco from 'monaco-editor-core' |
| 2 | +import type { HighlighterGeneric, ShikiInternal } from 'shikiji-core' |
| 3 | +import type { StateStack } from 'shikiji-core/textmate' |
| 4 | +import { INITIAL, StackElementMetadata } from 'shikiji-core/textmate' |
| 5 | + |
| 6 | +type Monaco = typeof monaco |
| 7 | +type AcceptableShiki = HighlighterGeneric<any, any> | ShikiInternal |
| 8 | + |
| 9 | +export interface MonacoInferface { |
| 10 | + editor: Monaco['editor'] |
| 11 | + languages: Monaco['languages'] |
| 12 | +} |
| 13 | + |
| 14 | +export interface HighlighterInterface { |
| 15 | + getLoadedThemes: AcceptableShiki['getLoadedThemes'] |
| 16 | + getTheme: AcceptableShiki['getTheme'] |
| 17 | + getLoadedLanguages: AcceptableShiki['getLoadedLanguages'] |
| 18 | + getLangGrammar: AcceptableShiki['getLangGrammar'] |
| 19 | + setTheme: AcceptableShiki['setTheme'] |
| 20 | +} |
| 21 | + |
| 22 | +export function shikijiToMonaco( |
| 23 | + highlighter: HighlighterInterface, |
| 24 | + monaco: MonacoInferface, |
| 25 | +) { |
| 26 | + const themes = highlighter.getLoadedThemes() |
| 27 | + for (const theme of themes) |
| 28 | + monaco.editor.defineTheme(theme, highlighter.getTheme(theme) as any) |
| 29 | + |
| 30 | + let currentTheme = themes[0] |
| 31 | + |
| 32 | + const _setTheme = monaco.editor.setTheme.bind(monaco.editor) |
| 33 | + monaco.editor.setTheme = (theme: string) => { |
| 34 | + _setTheme(theme) |
| 35 | + currentTheme = theme |
| 36 | + } |
| 37 | + |
| 38 | + for (const lang of highlighter.getLoadedLanguages()) { |
| 39 | + if (monaco.languages.getLanguages().some(l => l.id === lang)) { |
| 40 | + monaco.languages.setTokensProvider(lang, { |
| 41 | + getInitialState() { |
| 42 | + return new TokenizerState(INITIAL, highlighter) |
| 43 | + }, |
| 44 | + tokenize(line, state: TokenizerState) { |
| 45 | + const grammar = state.highlighter.getLangGrammar(lang) |
| 46 | + const { theme, colorMap } = state.highlighter.setTheme(currentTheme) |
| 47 | + const result = grammar.tokenizeLine2(line, state.ruleStack) |
| 48 | + |
| 49 | + const colorToScopeMap = new Map<string, string>() |
| 50 | + |
| 51 | + // @ts-expect-error - 'rules' is presented on resolved theme |
| 52 | + theme?.rules.forEach((rule) => { |
| 53 | + const c = rule.foreground?.replace('#', '').toLowerCase() |
| 54 | + if (c && !colorToScopeMap.has(c)) |
| 55 | + colorToScopeMap.set(c, rule.token) |
| 56 | + }) |
| 57 | + |
| 58 | + function findScopeByColor(color: string) { |
| 59 | + return colorToScopeMap.get(color) |
| 60 | + } |
| 61 | + |
| 62 | + const tokensLength = result.tokens.length / 2 |
| 63 | + const tokens: any[] = [] |
| 64 | + for (let j = 0; j < tokensLength; j++) { |
| 65 | + const startIndex = result.tokens[2 * j] |
| 66 | + const metadata = result.tokens[2 * j + 1] |
| 67 | + const color = (colorMap[StackElementMetadata.getForeground(metadata)] || '').replace('#', '').toLowerCase() |
| 68 | + // Because Monaco only support one scope per token, |
| 69 | + // we workaround this to use color to trace back the scope |
| 70 | + const scope = findScopeByColor(color) || '' |
| 71 | + tokens.push({ |
| 72 | + startIndex, |
| 73 | + scopes: scope, |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + return { |
| 78 | + endState: new TokenizerState(result.ruleStack, state.highlighter), |
| 79 | + tokens, |
| 80 | + } |
| 81 | + }, |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class TokenizerState implements monaco.languages.IState { |
| 88 | + constructor( |
| 89 | + private _ruleStack: StateStack, |
| 90 | + public highlighter: HighlighterInterface, |
| 91 | + ) { } |
| 92 | + |
| 93 | + public get ruleStack(): StateStack { |
| 94 | + return this._ruleStack |
| 95 | + } |
| 96 | + |
| 97 | + public clone(): TokenizerState { |
| 98 | + return new TokenizerState(this._ruleStack, this.highlighter) |
| 99 | + } |
| 100 | + |
| 101 | + public equals(other: monaco.languages.IState): boolean { |
| 102 | + if (!other |
| 103 | + || !(other instanceof TokenizerState) |
| 104 | + || other !== this |
| 105 | + || other._ruleStack !== this._ruleStack |
| 106 | + ) |
| 107 | + return false |
| 108 | + |
| 109 | + return true |
| 110 | + } |
| 111 | +} |
0 commit comments