Skip to content

Commit

Permalink
Go back to using setImmediate for background tokenization (#138887)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Dec 14, 2021
1 parent 93376c6 commit 5a1b499
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/vs/base/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface INodeProcess {
platform: string;
arch: string;
env: IProcessEnvironment;
nextTick?: (callback: (...args: any[]) => void) => void;
versions?: {
electron?: string;
};
Expand Down Expand Up @@ -189,6 +190,10 @@ export const locale = _locale;
*/
export const translationsConfigFile = _translationsConfigFile;

interface ISetImmediate {
(callback: (...args: unknown[]) => void): void;
}

/**
* See https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#:~:text=than%204%2C%20then-,set%20timeout%20to%204,-.
*
Expand Down Expand Up @@ -227,6 +232,20 @@ export const setTimeout0 = (() => {
return (callback: () => void) => setTimeout(callback);
})();

export const setImmediate: ISetImmediate = (function defineSetImmediate() {
if (globals.setImmediate) {
return globals.setImmediate.bind(globals);
}
if (typeof globals.postMessage === 'function' && !globals.importScripts) {
return setTimeout0;
}
if (typeof nodeProcess?.nextTick === 'function') {
return nodeProcess.nextTick.bind(nodeProcess);
}
const _promise = Promise.resolve();
return (callback: (...args: unknown[]) => void) => _promise.then(callback);
})();

export const enum OperatingSystem {
Windows = 1,
Macintosh = 2,
Expand Down
10 changes: 5 additions & 5 deletions src/vs/editor/common/model/textModelTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { TextModel } from 'vs/editor/common/model/textModel';
import { Disposable } from 'vs/base/common/lifecycle';
import { StopWatch } from 'vs/base/common/stopwatch';
import { MultilineTokensBuilder, countEOL } from 'vs/editor/common/model/tokensStore';
import { runWhenIdle, IdleDeadline } from 'vs/base/common/async';
import { setImmediate } from 'vs/base/common/platform';

const enum Constants {
CHEAP_TOKENIZATION_LENGTH_LIMIT = 2048
Expand Down Expand Up @@ -262,19 +262,19 @@ export class TextModelTokenization extends Disposable {
}

this._isScheduled = true;
runWhenIdle((deadline) => {
setImmediate(() => {
this._isScheduled = false;

if (this._isDisposed) {
// disposed in the meantime
return;
}

this._revalidateTokensNow(deadline);
this._revalidateTokensNow();
});
}

private _revalidateTokensNow(deadline: IdleDeadline): void {
private _revalidateTokensNow(): void {
const textModelLastLineNumber = this._textModel.getLineCount();

const MAX_ALLOWED_TIME = 1;
Expand All @@ -293,7 +293,7 @@ export class TextModelTokenization extends Disposable {
if (tokenizedLineNumber >= textModelLastLineNumber) {
break;
}
} while (this._hasLinesToTokenize() && deadline.timeRemaining() > 0);
} while (this._hasLinesToTokenize());

this._beginBackgroundTokenization();
this._textModel.setTokens(builder.tokens, !this._hasLinesToTokenize());
Expand Down

0 comments on commit 5a1b499

Please sign in to comment.