-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
impose timeout when running tokenizeLine2() #39
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,13 @@ export class SimpleLanguageInfoProvider { | |
} | ||
} | ||
|
||
/** | ||
* Specify a timeout when tokenizing a line to prevent a long line from locking | ||
* up the main thread. Note this is used in VS Code: | ||
* https://github.com/microsoft/vscode/blob/504c5a768a001b2099dd2b44e9dc39e10ccdfb56/src/vs/workbench/services/textMate/common/TMTokenization.ts#L39 | ||
*/ | ||
const TOKENIZE_TIMEOUT_MS = 500; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this timeout be configurable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea: will address in a follow-up diff! |
||
|
||
class TokensProviderCache { | ||
private scopeNameToGrammar: Map<string, Promise<IGrammar>> = new Map(); | ||
|
||
|
@@ -172,7 +179,19 @@ class TokensProviderCache { | |
line: string, | ||
state: monaco.languages.IState, | ||
): monaco.languages.IEncodedLineTokens { | ||
const tokenizeLineResult2 = grammar.tokenizeLine2(line, state as StackElement); | ||
const tokenizeLineResult2 = grammar.tokenizeLine2( | ||
line, | ||
state as StackElement, | ||
TOKENIZE_TIMEOUT_MS, | ||
); | ||
|
||
// This is the strategy used by VS Code: | ||
if (tokenizeLineResult2.stoppedEarly) { | ||
console.warn(`Time limit reached when tokenizing line: ${line.substring(0, 100)}`); | ||
// return the state at the beginning of the line | ||
return {tokens: tokenizeLineResult2.tokens, endState: state}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we log when this happens? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added! |
||
} | ||
|
||
const {tokens, ruleStack: endState} = tokenizeLineResult2; | ||
return {tokens, endState}; | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a comment to explain how this is used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea: included a link.