Skip to content

Commit 525509f

Browse files
committed
Fixes tests.
1 parent 083a595 commit 525509f

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

src/vs/editor/common/model/bracketPairsTextModelPart/bracketPairsTree/beforeEditPositionMapper.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export class BeforeEditPositionMapper {
2626
*/
2727
constructor(
2828
edits: readonly TextEditInfo[],
29-
private readonly documentLength: Length,
3029
) {
3130
this.edits = edits.map(edit => TextEditInfoCache.from(edit));
3231
}
@@ -41,12 +40,16 @@ export class BeforeEditPositionMapper {
4140

4241
/**
4342
* @param offset Must be equal to or greater than the last offset this method has been called with.
43+
* Returns null if there is no edit anymore.
4444
*/
45-
getDistanceToNextChange(offset: Length): Length {
45+
getDistanceToNextChange(offset: Length): Length | null {
4646
this.adjustNextEdit(offset);
4747

4848
const nextEdit = this.edits[this.nextEditIdx];
49-
const nextChangeOffset = nextEdit ? this.translateOldToCur(nextEdit.offsetObj) : this.documentLength;
49+
const nextChangeOffset = nextEdit ? this.translateOldToCur(nextEdit.offsetObj) : null;
50+
if (nextChangeOffset === null) {
51+
return null;
52+
}
5053

5154
return lengthDiffNonNegative(offset, nextChangeOffset);
5255
}

src/vs/editor/common/model/bracketPairsTextModelPart/bracketPairsTree/bracketPairsTree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class BracketPairsTree extends Disposable {
131131
this.astWithTokens = this.parseDocumentFromTextBuffer(this.queuedTextEdits, this.astWithTokens, false);
132132
this.queuedTextEdits = [];
133133
}
134-
if (this.queuedTextEditsForInitialAstWithoutTokens) {
134+
if (this.queuedTextEditsForInitialAstWithoutTokens.length > 0) {
135135
if (this.initialAstWithoutTokens) {
136136
this.initialAstWithoutTokens = this.parseDocumentFromTextBuffer(this.queuedTextEditsForInitialAstWithoutTokens, this.initialAstWithoutTokens, false);
137137
}

src/vs/editor/common/model/bracketPairsTextModelPart/bracketPairsTree/parser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Parser {
5353
}
5454

5555
this.oldNodeReader = oldNode ? new NodeReader(oldNode) : undefined;
56-
this.positionMapper = new BeforeEditPositionMapper(edits, tokenizer.length);
56+
this.positionMapper = new BeforeEditPositionMapper(edits);
5757
}
5858

5959
parseDocument(): AstNode {
@@ -104,9 +104,11 @@ class Parser {
104104
private tryReadChildFromCache(openedBracketIds: SmallImmutableSet<number>): AstNode | undefined {
105105
if (this.oldNodeReader) {
106106
const maxCacheableLength = this.positionMapper.getDistanceToNextChange(this.tokenizer.offset);
107-
if (!lengthIsZero(maxCacheableLength)) {
107+
if (maxCacheableLength === null || !lengthIsZero(maxCacheableLength)) {
108108
const cachedNode = this.oldNodeReader.readLongestNodeAt(this.positionMapper.getOffsetBeforeChange(this.tokenizer.offset), curNode => {
109-
if (!lengthLessThan(curNode.length, maxCacheableLength)) {
109+
// The edit could extend the ending token, thus we cannot re-use nodes that touch the edit.
110+
// If there is no edit anymore, we can re-use the node in any case.
111+
if (maxCacheableLength !== null && !lengthLessThan(curNode.length, maxCacheableLength)) {
110112
// Either the node contains edited text or touches edited text.
111113
// In the latter case, brackets might have been extended (`end` -> `ending`), so even touching nodes cannot be reused.
112114
return false;

src/vs/editor/common/model/bracketPairsTextModelPart/bracketPairsTree/tokenizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class TextBufferTokenizer implements Tokenizer {
8181
}
8282

8383
get length() {
84-
return toLength(this.textBufferLineCount, this.textBufferLastLineLength);
84+
return toLength(this.textBufferLineCount - 1, this.textBufferLastLineLength);
8585
}
8686

8787
getText() {

src/vs/editor/test/common/model/bracketPairColorizer/getBracketPairsInRange.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@ import { BracketPairInfo } from 'vs/editor/common/textModelBracketPairs';
1111
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
1212
import { createModelServices, instantiateTextModel } from 'vs/editor/test/common/testTextModel';
1313
import { TextModel } from 'vs/editor/common/model/textModel';
14+
import { TokenInfo, TokenizedDocument } from 'vs/editor/test/common/model/bracketPairColorizer/tokenizer.test';
15+
import { ILanguageService } from 'vs/editor/common/languages/language';
16+
import { StandardTokenType } from 'vs/editor/common/encodedTokenAttributes';
17+
import { TokenizationRegistry } from 'vs/editor/common/languages';
1418

1519
suite('Bracket Pair Colorizer - getBracketPairsInRange', () => {
1620

1721
function createTextModelWithColorizedBracketPairs(store: DisposableStore, text: string): TextModel {
1822
const languageId = 'testLanguage';
1923
const instantiationService = createModelServices(store);
2024
const languageConfigurationService = instantiationService.get(ILanguageConfigurationService);
25+
const languageService = instantiationService.get(ILanguageService);
26+
languageService.registerLanguage({
27+
id: languageId,
28+
});
29+
30+
const encodedMode1 = languageService.languageIdCodec.encodeLanguageId(languageId);
31+
const document = new TokenizedDocument([
32+
new TokenInfo(text, encodedMode1, StandardTokenType.Other, true)
33+
]);
34+
store.add(TokenizationRegistry.register(languageId, document.getTokenizationSupport()));
2135

2236
store.add(languageConfigurationService.register(languageId, {
2337
colorizedBracketPairs: [
@@ -26,13 +40,15 @@ suite('Bracket Pair Colorizer - getBracketPairsInRange', () => {
2640
['(', ')'],
2741
]
2842
}));
29-
return store.add(instantiateTextModel(instantiationService, text, languageId));
43+
const textModel = store.add(instantiateTextModel(instantiationService, text, languageId));
44+
return textModel;
3045
}
3146

3247
test('Basic 1', () => {
3348
disposeOnReturn(store => {
3449
const doc = new AnnotatedDocument(`{ ( [] ¹ ) [ ² { } ] () } []`);
3550
const model = createTextModelWithColorizedBracketPairs(store, doc.text);
51+
model.tokenization.getLineTokens(1).getLanguageId(0);
3652
assert.deepStrictEqual(
3753
model.bracketPairs
3854
.getBracketPairsInRange(doc.range(1, 2))

src/vs/editor/test/common/model/bracketPairColorizer/tokenizer.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function tokenToObj(token: Token, offset: Length, model: TextModel, keyProvider:
124124
};
125125
}
126126

127-
class TokenizedDocument {
127+
export class TokenizedDocument {
128128
private readonly tokensByLine: readonly TokenInfo[][];
129129
constructor(tokens: TokenInfo[]) {
130130
const tokensByLine = new Array<TokenInfo[]>();
@@ -189,7 +189,7 @@ class TokenizedDocument {
189189
}
190190
}
191191

192-
class TokenInfo {
192+
export class TokenInfo {
193193
constructor(
194194
public readonly text: string,
195195
public readonly languageId: LanguageId,

0 commit comments

Comments
 (0)