Skip to content

Commit 383a0e8

Browse files
committed
refactor(packages/monaco): decomposed into logical parts
- removed `any` types - added indentations for better readability - helper functions moved to `./utils.ts` - types in `./types.ts`
1 parent 26d621c commit 383a0e8

File tree

4 files changed

+65
-52
lines changed

4 files changed

+65
-52
lines changed

packages/monaco/src/index.ts

+11-52
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { ShikiInternal, ThemeRegistrationResolved } from '@shikijs/types'
2-
import type { StateStack } from '@shikijs/vscode-textmate'
32
import type monacoNs from 'monaco-editor-core'
43
import { EncodedTokenMetadata, INITIAL } from '@shikijs/vscode-textmate'
4+
import type { MonacoLineToken } from './types'
5+
import { TokenizerState } from './tokenizer'
6+
import { normalizeColor } from './utils'
57

68
export interface MonacoTheme extends monacoNs.editor.IStandaloneThemeData { }
79

@@ -28,8 +30,10 @@ export function textmateThemeToMonacoTheme(theme: ThemeRegistrationResolved): Mo
2830
if (!rules) {
2931
rules = []
3032
const themeSettings = theme.settings || theme.tokenColors
33+
3134
for (const { scope, settings: { foreground, background, fontStyle } } of themeSettings) {
3235
const scopes = Array.isArray(scope) ? scope : [scope]
36+
3337
for (const s of scopes) {
3438
if (s && (foreground || background || fontStyle)) {
3539
rules.push({
@@ -108,13 +112,15 @@ export function shikiToMonaco(
108112
} = options
109113

110114
const monacoLanguageIds = new Set(monaco.languages.getLanguages().map(l => l.id))
115+
111116
for (const lang of highlighter.getLoadedLanguages()) {
112117
if (monacoLanguageIds.has(lang)) {
113118
monaco.languages.setTokensProvider(lang, {
114119
getInitialState() {
115120
return new TokenizerState(INITIAL)
116121
},
117-
tokenize(line, state: TokenizerState) {
122+
123+
tokenize(line: string, state: TokenizerState) {
118124
if (line.length >= tokenizeMaxLineLength) {
119125
return {
120126
endState: state,
@@ -129,11 +135,13 @@ export function shikiToMonaco(
129135
console.warn(`Time limit reached when tokenizing line: ${line.substring(0, 100)}`)
130136

131137
const tokensLength = result.tokens.length / 2
132-
const tokens: any[] = []
138+
const tokens: MonacoLineToken[] = []
139+
133140
for (let j = 0; j < tokensLength; j++) {
134141
const startIndex = result.tokens[2 * j]
135142
const metadata = result.tokens[2 * j + 1]
136143
const color = normalizeColor(colorMap[EncodedTokenMetadata.getForeground(metadata)] || '')
144+
137145
// Because Monaco only support one scope per token,
138146
// we workaround this to use color to trace back the scope
139147
const scope = findScopeByColor(color) || ''
@@ -146,52 +154,3 @@ export function shikiToMonaco(
146154
}
147155
}
148156
}
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-
}

packages/monaco/src/tokenizer.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { StateStack } from '@shikijs/vscode-textmate'
2+
import type monacoNs from 'monaco-editor-core'
3+
4+
export class TokenizerState implements monacoNs.languages.IState {
5+
constructor(
6+
private _ruleStack: StateStack,
7+
) { }
8+
9+
public get ruleStack(): StateStack {
10+
return this._ruleStack
11+
}
12+
13+
public clone(): TokenizerState {
14+
return new TokenizerState(this._ruleStack)
15+
}
16+
17+
public equals(other: monacoNs.languages.IState): boolean {
18+
if (
19+
!other
20+
|| !(other instanceof TokenizerState)
21+
|| other !== this
22+
|| other._ruleStack !== this._ruleStack
23+
) {
24+
return false
25+
}
26+
27+
return true
28+
}
29+
}

packages/monaco/src/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface MonacoLineToken {
2+
startIndex: number
3+
scopes: string
4+
}

packages/monaco/src/utils.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function normalizeColor(color: undefined): undefined
2+
export function normalizeColor(color: string | string[]): string
3+
export function normalizeColor(color: string | string[] | undefined): string | undefined
4+
export function normalizeColor(color: string | string[] | undefined): string | undefined {
5+
// Some themes have an array of colors (not yet sure why), here we pick the first one
6+
// https://github.com/shikijs/shiki/issues/894
7+
// https://github.com/shikijs/textmate-grammars-themes/pull/117
8+
if (Array.isArray(color))
9+
color = color[0]
10+
11+
if (!color)
12+
return undefined
13+
14+
color = (color.charCodeAt(0) === 35 ? color.slice(1) : color).toLowerCase()
15+
16+
// #RGB => #RRGGBB - Monaco does not support hex color with 3 or 4 digits
17+
if (color.length === 3 || color.length === 4)
18+
color = color.split('').map(c => c + c).join('')
19+
20+
return color
21+
}

0 commit comments

Comments
 (0)