1
1
import type { ShikiInternal , ThemeRegistrationResolved } from '@shikijs/types'
2
- import type { StateStack } from '@shikijs/vscode-textmate'
3
2
import type monacoNs from 'monaco-editor-core'
3
+ import type { MonacoLineToken } from './types'
4
4
import { EncodedTokenMetadata , INITIAL } from '@shikijs/vscode-textmate'
5
+ import { TokenizerState } from './tokenizer'
6
+ import { normalizeColor } from './utils'
5
7
6
8
export interface MonacoTheme extends monacoNs . editor . IStandaloneThemeData { }
7
9
@@ -28,8 +30,10 @@ export function textmateThemeToMonacoTheme(theme: ThemeRegistrationResolved): Mo
28
30
if ( ! rules ) {
29
31
rules = [ ]
30
32
const themeSettings = theme . settings || theme . tokenColors
33
+
31
34
for ( const { scope, settings : { foreground, background, fontStyle } } of themeSettings ) {
32
35
const scopes = Array . isArray ( scope ) ? scope : [ scope ]
36
+
33
37
for ( const s of scopes ) {
34
38
if ( s && ( foreground || background || fontStyle ) ) {
35
39
rules . push ( {
@@ -108,13 +112,15 @@ export function shikiToMonaco(
108
112
} = options
109
113
110
114
const monacoLanguageIds = new Set ( monaco . languages . getLanguages ( ) . map ( l => l . id ) )
115
+
111
116
for ( const lang of highlighter . getLoadedLanguages ( ) ) {
112
117
if ( monacoLanguageIds . has ( lang ) ) {
113
118
monaco . languages . setTokensProvider ( lang , {
114
119
getInitialState ( ) {
115
120
return new TokenizerState ( INITIAL )
116
121
} ,
117
- tokenize ( line , state : TokenizerState ) {
122
+
123
+ tokenize ( line : string , state : TokenizerState ) {
118
124
if ( line . length >= tokenizeMaxLineLength ) {
119
125
return {
120
126
endState : state ,
@@ -129,11 +135,13 @@ export function shikiToMonaco(
129
135
console . warn ( `Time limit reached when tokenizing line: ${ line . substring ( 0 , 100 ) } ` )
130
136
131
137
const tokensLength = result . tokens . length / 2
132
- const tokens : any [ ] = [ ]
138
+ const tokens : MonacoLineToken [ ] = [ ]
139
+
133
140
for ( let j = 0 ; j < tokensLength ; j ++ ) {
134
141
const startIndex = result . tokens [ 2 * j ]
135
142
const metadata = result . tokens [ 2 * j + 1 ]
136
143
const color = normalizeColor ( colorMap [ EncodedTokenMetadata . getForeground ( metadata ) ] || '' )
144
+
137
145
// Because Monaco only support one scope per token,
138
146
// we workaround this to use color to trace back the scope
139
147
const scope = findScopeByColor ( color ) || ''
@@ -146,52 +154,3 @@ export function shikiToMonaco(
146
154
}
147
155
}
148
156
}
149
-
150
- class TokenizerState implements monacoNs . languages . IState {
151
- constructor (
152
- private _ruleStack : StateStack ,
153
- ) { }
154
-
155
- public get ruleStack ( ) : StateStack {
156
- return this . _ruleStack
157
- }
158
-
159
- public clone ( ) : TokenizerState {
160
- return new TokenizerState ( this . _ruleStack )
161
- }
162
-
163
- public equals ( other : monacoNs . languages . IState ) : boolean {
164
- if (
165
- ! other
166
- || ! ( other instanceof TokenizerState )
167
- || other !== this
168
- || other . _ruleStack !== this . _ruleStack
169
- ) {
170
- return false
171
- }
172
-
173
- return true
174
- }
175
- }
176
-
177
- function normalizeColor ( color : undefined ) : undefined
178
- function normalizeColor ( color : string | string [ ] ) : string
179
- function normalizeColor ( color : string | string [ ] | undefined ) : string | undefined
180
- function normalizeColor ( color : string | string [ ] | undefined ) : string | undefined {
181
- // Some themes have an array of colors (not yet sure why), here we pick the first one
182
- // https://github.com/shikijs/shiki/issues/894
183
- // https://github.com/shikijs/textmate-grammars-themes/pull/117
184
- if ( Array . isArray ( color ) )
185
- color = color [ 0 ]
186
-
187
- if ( ! color )
188
- return undefined
189
-
190
- color = ( color . charCodeAt ( 0 ) === 35 ? color . slice ( 1 ) : color ) . toLowerCase ( )
191
-
192
- // #RGB => #RRGGBB - Monaco does not support hex color with 3 or 4 digits
193
- if ( color . length === 3 || color . length === 4 )
194
- color = color . split ( '' ) . map ( c => c + c ) . join ( '' )
195
-
196
- return color
197
- }
0 commit comments