Skip to content

Commit 69b546a

Browse files
committed
Simplify onEnter calls
1 parent 2c208a9 commit 69b546a

3 files changed

Lines changed: 23 additions & 58 deletions

File tree

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

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { onUnexpectedError } from 'vs/base/common/errors';
76
import { Emitter, Event } from 'vs/base/common/event';
87
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
98
import * as strings from 'vs/base/common/strings';
@@ -17,7 +16,7 @@ import { createScopedLineTokens, ScopedLineTokens } from 'vs/editor/common/modes
1716
import { CharacterPairSupport } from 'vs/editor/common/modes/supports/characterPair';
1817
import { BracketElectricCharacterSupport, IElectricAction } from 'vs/editor/common/modes/supports/electricCharacter';
1918
import { IndentConsts, IndentRulesSupport } from 'vs/editor/common/modes/supports/indentRules';
20-
import { IOnEnterSupportOptions, OnEnterSupport } from 'vs/editor/common/modes/supports/onEnter';
19+
import { OnEnterSupport } from 'vs/editor/common/modes/supports/onEnter';
2120
import { RichEditBrackets } from 'vs/editor/common/modes/supports/richEditBrackets';
2221
import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions';
2322

@@ -49,11 +48,11 @@ export class RichEditSupport {
4948
private readonly _languageIdentifier: LanguageIdentifier;
5049
private _brackets: RichEditBrackets | null;
5150
private _electricCharacter: BracketElectricCharacterSupport | null;
51+
private readonly _onEnterSupport: OnEnterSupport | null;
5252

5353
public readonly comments: ICommentsConfiguration | null;
5454
public readonly characterPair: CharacterPairSupport;
5555
public readonly wordDefinition: RegExp;
56-
public readonly onEnter: OnEnterSupport | null;
5756
public readonly indentRulesSupport: IndentRulesSupport | null;
5857
public readonly indentationRules: IndentationRule | undefined;
5958
public readonly foldingRules: FoldingRules;
@@ -71,8 +70,7 @@ export class RichEditSupport {
7170

7271
this._conf = RichEditSupport._mergeConf(prev, rawConf);
7372

74-
this.onEnter = RichEditSupport._handleOnEnter(this._conf);
75-
73+
this._onEnterSupport = (this._conf.brackets || this._conf.indentationRules || this._conf.onEnterRules ? new OnEnterSupport(this._conf) : null);
7674
this.comments = RichEditSupport._handleComments(this._conf);
7775

7876
this.characterPair = new CharacterPairSupport(this._conf);
@@ -103,6 +101,13 @@ export class RichEditSupport {
103101
return this._electricCharacter;
104102
}
105103

104+
public onEnter(autoIndent: EditorAutoIndentStrategy, oneLineAboveText: string, beforeEnterText: string, afterEnterText: string): EnterAction | null {
105+
if (!this._onEnterSupport) {
106+
return null;
107+
}
108+
return this._onEnterSupport.onEnter(autoIndent, oneLineAboveText, beforeEnterText, afterEnterText);
109+
}
110+
106111
private static _mergeConf(prev: LanguageConfiguration | null, current: LanguageConfiguration): LanguageConfiguration {
107112
return {
108113
comments: (prev ? current.comments || prev.comments : current.comments),
@@ -118,29 +123,6 @@ export class RichEditSupport {
118123
};
119124
}
120125

121-
private static _handleOnEnter(conf: LanguageConfiguration): OnEnterSupport | null {
122-
// on enter
123-
let onEnter: IOnEnterSupportOptions = {};
124-
let empty = true;
125-
126-
if (conf.brackets) {
127-
empty = false;
128-
onEnter.brackets = conf.brackets;
129-
}
130-
if (conf.indentationRules) {
131-
empty = false;
132-
}
133-
if (conf.onEnterRules) {
134-
empty = false;
135-
onEnter.regExpRules = conf.onEnterRules;
136-
}
137-
138-
if (!empty) {
139-
return new OnEnterSupport(onEnter);
140-
}
141-
return null;
142-
}
143-
144126
private static _handleComments(conf: LanguageConfiguration): ICommentsConfiguration | null {
145127
let commentRule = conf.comments;
146128
if (!commentRule) {
@@ -481,6 +463,11 @@ export class LanguageConfigurationRegistryImpl {
481463
return null;
482464
}
483465

466+
const richEditSupport = this._getRichEditSupport(languageId);
467+
if (!richEditSupport) {
468+
return null;
469+
}
470+
484471
const indentRulesSupport = this.getIndentRulesSupport(languageId);
485472
if (!indentRulesSupport) {
486473
return null;
@@ -492,15 +479,7 @@ export class LanguageConfigurationRegistryImpl {
492479
if (indent) {
493480
const inheritLine = indent.line;
494481
if (inheritLine !== undefined) {
495-
const onEnterSupport = this._getOnEnterSupport(languageId);
496-
let enterResult: EnterAction | null = null;
497-
try {
498-
if (onEnterSupport) {
499-
enterResult = onEnterSupport.onEnter(autoIndent, '', virtualModel.getLineContent(inheritLine), '');
500-
}
501-
} catch (e) {
502-
onUnexpectedError(e);
503-
}
482+
const enterResult = richEditSupport.onEnter(autoIndent, '', virtualModel.getLineContent(inheritLine), '');
504483

505484
if (enterResult) {
506485
let indentation = strings.getLeadingWhitespace(virtualModel.getLineContent(inheritLine));
@@ -689,18 +668,10 @@ export class LanguageConfigurationRegistryImpl {
689668

690669
// begin onEnter
691670

692-
private _getOnEnterSupport(languageId: LanguageId): OnEnterSupport | null {
693-
const value = this._getRichEditSupport(languageId);
694-
if (!value) {
695-
return null;
696-
}
697-
return value.onEnter || null;
698-
}
699-
700671
public getEnterAction(autoIndent: EditorAutoIndentStrategy, model: ITextModel, range: Range): CompleteEnterAction | null {
701672
const scopedLineTokens = this.getScopedLineTokens(model, range.startLineNumber, range.startColumn);
702-
const onEnterSupport = this._getOnEnterSupport(scopedLineTokens.languageId);
703-
if (!onEnterSupport) {
673+
const richEditSupport = this._getRichEditSupport(scopedLineTokens.languageId);
674+
if (!richEditSupport) {
704675
return null;
705676
}
706677

@@ -726,13 +697,7 @@ export class LanguageConfigurationRegistryImpl {
726697
}
727698
}
728699

729-
let enterResult: EnterAction | null = null;
730-
try {
731-
enterResult = onEnterSupport.onEnter(autoIndent, oneLineAboveText, beforeEnterText, afterEnterText);
732-
} catch (e) {
733-
onUnexpectedError(e);
734-
}
735-
700+
const enterResult = richEditSupport.onEnter(autoIndent, oneLineAboveText, beforeEnterText, afterEnterText);
736701
if (!enterResult) {
737702
return null;
738703
}

src/vs/editor/common/modes/supports/onEnter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions'
1010

1111
export interface IOnEnterSupportOptions {
1212
brackets?: CharacterPair[];
13-
regExpRules?: OnEnterRule[];
13+
onEnterRules?: OnEnterRule[];
1414
}
1515

1616
interface IProcessedBracketPair {
@@ -25,7 +25,7 @@ export class OnEnterSupport {
2525
private readonly _brackets: IProcessedBracketPair[];
2626
private readonly _regExpRules: OnEnterRule[];
2727

28-
constructor(opts?: IOnEnterSupportOptions) {
28+
constructor(opts: IOnEnterSupportOptions) {
2929
opts = opts || {};
3030
opts.brackets = opts.brackets || [
3131
['(', ')'],
@@ -46,7 +46,7 @@ export class OnEnterSupport {
4646
});
4747
}
4848
});
49-
this._regExpRules = opts.regExpRules || [];
49+
this._regExpRules = opts.onEnterRules || [];
5050
}
5151

5252
public onEnter(autoIndent: EditorAutoIndentStrategy, oneLineAboveText: string, beforeEnterText: string, afterEnterText: string): EnterAction | null {

src/vs/editor/test/common/modes/supports/onEnter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ suite('OnEnter', () => {
4949

5050
test('uses regExpRules', () => {
5151
let support = new OnEnterSupport({
52-
regExpRules: javascriptOnEnterRules
52+
onEnterRules: javascriptOnEnterRules
5353
});
5454
let testIndentAction = (oneLineAboveText: string, beforeText: string, afterText: string, expectedIndentAction: IndentAction | null, expectedAppendText: string | null, removeText: number = 0) => {
5555
let actual = support.onEnter(EditorAutoIndentStrategy.Advanced, oneLineAboveText, beforeText, afterText);

0 commit comments

Comments
 (0)