Skip to content

Commit 40b5b5d

Browse files
committed
Fixes microsoft#84312: Add a time limit to bracket matching
1 parent bc2fbe2 commit 40b5b5d

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/vs/editor/common/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ export interface ITextModel {
898898
* @param position The position at which to start the search.
899899
* @internal
900900
*/
901-
findEnclosingBrackets(position: IPosition): [Range, Range] | null;
901+
findEnclosingBrackets(position: IPosition, maxDuration?: number): [Range, Range] | null;
902902

903903
/**
904904
* Given a `position`, if the position is on top or near a bracket,

src/vs/editor/common/model/textModel.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { withUndefinedAsNull } from 'vs/base/common/types';
3434
import { VSBufferReadableStream, VSBuffer } from 'vs/base/common/buffer';
3535
import { TokensStore, MultilineTokens, countEOL, MultilineTokens2, TokensStore2 } from 'vs/editor/common/model/tokensStore';
3636
import { Color } from 'vs/base/common/color';
37+
import { Constants } from 'vs/base/common/uint';
3738

3839
function createTextBufferBuilder() {
3940
return new PieceTreeTextBufferBuilder();
@@ -2341,7 +2342,7 @@ export class TextModel extends Disposable implements model.ITextModel {
23412342
return null;
23422343
}
23432344

2344-
public findEnclosingBrackets(_position: IPosition): [Range, Range] | null {
2345+
public findEnclosingBrackets(_position: IPosition, maxDuration = Constants.MAX_SAFE_SMALL_INTEGER): [Range, Range] | null {
23452346
const position = this.validatePosition(_position);
23462347
const lineCount = this.getLineCount();
23472348
const savedCounts = new Map<number, number[]>();
@@ -2385,7 +2386,12 @@ export class TextModel extends Disposable implements model.ITextModel {
23852386

23862387
let languageId: LanguageId = -1;
23872388
let modeBrackets: RichEditBrackets | null = null;
2389+
const startTime = Date.now();
23882390
for (let lineNumber = position.lineNumber; lineNumber <= lineCount; lineNumber++) {
2391+
const elapsedTime = Date.now() - startTime;
2392+
if (elapsedTime > maxDuration) {
2393+
return null;
2394+
}
23892395
const lineTokens = this._getLineTokens(lineNumber);
23902396
const tokenCount = lineTokens.getCount();
23912397
const lineText = this._buffer.getLineContent(lineNumber);

src/vs/editor/contrib/bracketMatching/bracketMatching.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export class BracketMatchingController extends Disposable implements editorCommo
334334
let brackets = model.matchBracket(position);
335335
let options = BracketMatchingController._DECORATION_OPTIONS_WITH_OVERVIEW_RULER;
336336
if (!brackets && this._matchBrackets === 'always') {
337-
brackets = model.findEnclosingBrackets(position);
337+
brackets = model.findEnclosingBrackets(position, 20 /* give at most 20ms to compute */);
338338
options = BracketMatchingController._DECORATION_OPTIONS_WITHOUT_OVERVIEW_RULER;
339339
}
340340
newData[newDataLen++] = new BracketsData(position, brackets, options);

0 commit comments

Comments
 (0)