Skip to content

Commit 276f8df

Browse files
authored
Defer RichEditBrackets creation (microsoft#38659)
* Defer RichEditBrackets creation **Bug** `RichEditBrackets` are currently created eagerly pretty early in startup. This can be a fairly call as well. **fix** Lazily create `RichEditBrackets` instead * Also defer electricChars in CursorConfiguration * Init all new members in ctor
1 parent 73f3e0d commit 276f8df

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

src/vs/editor/common/controller/cursorCommon.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ export class CursorConfiguration {
8282
public readonly autoClosingPairsOpen: CharacterMap;
8383
public readonly autoClosingPairsClose: CharacterMap;
8484
public readonly surroundingPairs: CharacterMap;
85-
public readonly electricChars: { [key: string]: boolean; };
85+
86+
private readonly _languageIdentifier: LanguageIdentifier;
87+
private _electricChars: { [key: string]: boolean; };
8688

8789
public static shouldRecreate(e: IConfigurationChangedEvent): boolean {
8890
return (
@@ -102,6 +104,8 @@ export class CursorConfiguration {
102104
modelOptions: TextModelResolvedOptions,
103105
configuration: IConfiguration
104106
) {
107+
this._languageIdentifier = languageIdentifier;
108+
105109
let c = configuration.editor;
106110

107111
this.readOnly = c.readOnly;
@@ -119,14 +123,7 @@ export class CursorConfiguration {
119123
this.autoClosingPairsOpen = {};
120124
this.autoClosingPairsClose = {};
121125
this.surroundingPairs = {};
122-
this.electricChars = {};
123-
124-
let electricChars = CursorConfiguration._getElectricCharacters(languageIdentifier);
125-
if (electricChars) {
126-
for (let i = 0; i < electricChars.length; i++) {
127-
this.electricChars[electricChars[i]] = true;
128-
}
129-
}
126+
this._electricChars = null;
130127

131128
let autoClosingPairs = CursorConfiguration._getAutoClosingPairs(languageIdentifier);
132129
if (autoClosingPairs) {
@@ -144,6 +141,19 @@ export class CursorConfiguration {
144141
}
145142
}
146143

144+
public get electricChars() {
145+
if (!this._electricChars) {
146+
this._electricChars = {};
147+
let electricChars = CursorConfiguration._getElectricCharacters(this._languageIdentifier);
148+
if (electricChars) {
149+
for (let i = 0; i < electricChars.length; i++) {
150+
this._electricChars[electricChars[i]] = true;
151+
}
152+
}
153+
}
154+
return this._electricChars;
155+
}
156+
147157
public normalizeIndentation(str: string): string {
148158
return TextModel.normalizeIndentation(str, this.tabSize, this.insertSpaces);
149159
}

src/vs/editor/common/modes/languageConfigurationRegistry.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,23 @@ export interface IIndentConverter {
4646
export class RichEditSupport {
4747

4848
private readonly _conf: LanguageConfiguration;
49+
private readonly _languageIdentifier: LanguageIdentifier;
50+
private _brackets: RichEditBrackets;
51+
private _electricCharacter: BracketElectricCharacterSupport;
4952

50-
public readonly electricCharacter: BracketElectricCharacterSupport;
5153
public readonly comments: ICommentsConfiguration;
5254
public readonly characterPair: CharacterPairSupport;
5355
public readonly wordDefinition: RegExp;
5456
public readonly onEnter: OnEnterSupport;
5557
public readonly indentRulesSupport: IndentRulesSupport;
56-
public readonly brackets: RichEditBrackets;
5758
public readonly indentationRules: IndentationRule;
5859
public readonly foldingRules: FoldingRules;
5960

6061
constructor(languageIdentifier: LanguageIdentifier, previous: RichEditSupport, rawConf: LanguageConfiguration) {
62+
this._languageIdentifier = languageIdentifier;
63+
64+
this._brackets = null;
65+
this._electricCharacter = null;
6166

6267
let prev: LanguageConfiguration = null;
6368
if (previous) {
@@ -66,16 +71,11 @@ export class RichEditSupport {
6671

6772
this._conf = RichEditSupport._mergeConf(prev, rawConf);
6873

69-
if (this._conf.brackets) {
70-
this.brackets = new RichEditBrackets(languageIdentifier, this._conf.brackets);
71-
}
72-
7374
this.onEnter = RichEditSupport._handleOnEnter(this._conf);
7475

7576
this.comments = RichEditSupport._handleComments(this._conf);
7677

7778
this.characterPair = new CharacterPairSupport(this._conf);
78-
this.electricCharacter = new BracketElectricCharacterSupport(this.brackets, this.characterPair.getAutoClosingPairs(), this._conf.__electricCharacterSupport);
7979

8080
this.wordDefinition = this._conf.wordPattern || DEFAULT_WORD_REGEXP;
8181

@@ -87,6 +87,20 @@ export class RichEditSupport {
8787
this.foldingRules = this._conf.folding || {};
8888
}
8989

90+
public get brackets(): RichEditBrackets {
91+
if (!this._brackets && this._conf.brackets) {
92+
this._brackets = new RichEditBrackets(this._languageIdentifier, this._conf.brackets);
93+
}
94+
return this._brackets;
95+
}
96+
97+
public get electricCharacter(): BracketElectricCharacterSupport {
98+
if (!this._electricCharacter) {
99+
this._electricCharacter = new BracketElectricCharacterSupport(this.brackets, this.characterPair.getAutoClosingPairs(), this._conf.__electricCharacterSupport);
100+
}
101+
return this._electricCharacter;
102+
}
103+
90104
private static _mergeConf(prev: LanguageConfiguration, current: LanguageConfiguration): LanguageConfiguration {
91105
return {
92106
comments: (prev ? current.comments || prev.comments : current.comments),

0 commit comments

Comments
 (0)