Skip to content

Commit a606d44

Browse files
hddhyqantfu
andauthored
fix(monaco): options for tokenize limit (#657)
Co-authored-by: Anthony Fu <[email protected]> Co-authored-by: Anthony Fu <[email protected]>
1 parent 86d5271 commit a606d44

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

packages/monaco/src/index.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ import { INITIAL, StackElementMetadata } from '@shikijs/core/textmate'
55

66
export interface MonacoTheme extends monacoNs.editor.IStandaloneThemeData {}
77

8+
export interface ShikiToMonacoOptions {
9+
/**
10+
* The maximum length of a line to tokenize.
11+
*
12+
* @default 20000
13+
*/
14+
tokenizeMaxLineLength?: number
15+
/**
16+
* The time limit in milliseconds for tokenizing a line.
17+
*
18+
* @default 500
19+
*/
20+
tokenizeTimeLimit?: number
21+
}
22+
823
export function textmateThemeToMonacoTheme(theme: ThemeRegistrationResolved): MonacoTheme {
924
let rules = 'rules' in theme
1025
? theme.rules as MonacoTheme['rules']
@@ -42,6 +57,7 @@ export function textmateThemeToMonacoTheme(theme: ThemeRegistrationResolved): Mo
4257
export function shikiToMonaco(
4358
highlighter: ShikiInternal<any, any>,
4459
monaco: typeof monacoNs,
60+
options: ShikiToMonacoOptions = {},
4561
) {
4662
// Convert themes to Monaco themes and register them
4763
const themeMap = new Map<string, MonacoTheme>()
@@ -82,6 +98,13 @@ export function shikiToMonaco(
8298
return colorToScopeMap.get(color)
8399
}
84100

101+
// Do not attempt to tokenize if a line is too long
102+
// default to 20000 (as in monaco-editor-core defaults)
103+
const {
104+
tokenizeMaxLineLength = 20000,
105+
tokenizeTimeLimit = 500,
106+
} = options
107+
85108
const monacoLanguageIds = new Set(monaco.languages.getLanguages().map(l => l.id))
86109
for (const lang of highlighter.getLoadedLanguages()) {
87110
if (monacoLanguageIds.has(lang)) {
@@ -90,11 +113,6 @@ export function shikiToMonaco(
90113
return new TokenizerState(INITIAL)
91114
},
92115
tokenize(line, state: TokenizerState) {
93-
// Do not attempt to tokenize if a line is too long
94-
// default to 20000 (as in monaco-editor-core defaults)
95-
const tokenizeMaxLineLength = 20000
96-
const tokenizeTimeLimit = 500
97-
98116
if (line.length >= tokenizeMaxLineLength) {
99117
return {
100118
endState: state,

0 commit comments

Comments
 (0)