Simplify onEnter calls

上级 2c208a99
......@@ -3,7 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { onUnexpectedError } from 'vs/base/common/errors';
import { Emitter, Event } from 'vs/base/common/event';
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import * as strings from 'vs/base/common/strings';
......@@ -17,7 +16,7 @@ import { createScopedLineTokens, ScopedLineTokens } from 'vs/editor/common/modes
import { CharacterPairSupport } from 'vs/editor/common/modes/supports/characterPair';
import { BracketElectricCharacterSupport, IElectricAction } from 'vs/editor/common/modes/supports/electricCharacter';
import { IndentConsts, IndentRulesSupport } from 'vs/editor/common/modes/supports/indentRules';
import { IOnEnterSupportOptions, OnEnterSupport } from 'vs/editor/common/modes/supports/onEnter';
import { OnEnterSupport } from 'vs/editor/common/modes/supports/onEnter';
import { RichEditBrackets } from 'vs/editor/common/modes/supports/richEditBrackets';
import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions';
......@@ -49,11 +48,11 @@ export class RichEditSupport {
private readonly _languageIdentifier: LanguageIdentifier;
private _brackets: RichEditBrackets | null;
private _electricCharacter: BracketElectricCharacterSupport | null;
private readonly _onEnterSupport: OnEnterSupport | null;
public readonly comments: ICommentsConfiguration | null;
public readonly characterPair: CharacterPairSupport;
public readonly wordDefinition: RegExp;
public readonly onEnter: OnEnterSupport | null;
public readonly indentRulesSupport: IndentRulesSupport | null;
public readonly indentationRules: IndentationRule | undefined;
public readonly foldingRules: FoldingRules;
......@@ -71,8 +70,7 @@ export class RichEditSupport {
this._conf = RichEditSupport._mergeConf(prev, rawConf);
this.onEnter = RichEditSupport._handleOnEnter(this._conf);
this._onEnterSupport = (this._conf.brackets || this._conf.indentationRules || this._conf.onEnterRules ? new OnEnterSupport(this._conf) : null);
this.comments = RichEditSupport._handleComments(this._conf);
this.characterPair = new CharacterPairSupport(this._conf);
......@@ -103,6 +101,13 @@ export class RichEditSupport {
return this._electricCharacter;
}
public onEnter(autoIndent: EditorAutoIndentStrategy, oneLineAboveText: string, beforeEnterText: string, afterEnterText: string): EnterAction | null {
if (!this._onEnterSupport) {
return null;
}
return this._onEnterSupport.onEnter(autoIndent, oneLineAboveText, beforeEnterText, afterEnterText);
}
private static _mergeConf(prev: LanguageConfiguration | null, current: LanguageConfiguration): LanguageConfiguration {
return {
comments: (prev ? current.comments || prev.comments : current.comments),
......@@ -118,29 +123,6 @@ export class RichEditSupport {
};
}
private static _handleOnEnter(conf: LanguageConfiguration): OnEnterSupport | null {
// on enter
let onEnter: IOnEnterSupportOptions = {};
let empty = true;
if (conf.brackets) {
empty = false;
onEnter.brackets = conf.brackets;
}
if (conf.indentationRules) {
empty = false;
}
if (conf.onEnterRules) {
empty = false;
onEnter.regExpRules = conf.onEnterRules;
}
if (!empty) {
return new OnEnterSupport(onEnter);
}
return null;
}
private static _handleComments(conf: LanguageConfiguration): ICommentsConfiguration | null {
let commentRule = conf.comments;
if (!commentRule) {
......@@ -481,6 +463,11 @@ export class LanguageConfigurationRegistryImpl {
return null;
}
const richEditSupport = this._getRichEditSupport(languageId);
if (!richEditSupport) {
return null;
}
const indentRulesSupport = this.getIndentRulesSupport(languageId);
if (!indentRulesSupport) {
return null;
......@@ -492,15 +479,7 @@ export class LanguageConfigurationRegistryImpl {
if (indent) {
const inheritLine = indent.line;
if (inheritLine !== undefined) {
const onEnterSupport = this._getOnEnterSupport(languageId);
let enterResult: EnterAction | null = null;
try {
if (onEnterSupport) {
enterResult = onEnterSupport.onEnter(autoIndent, '', virtualModel.getLineContent(inheritLine), '');
}
} catch (e) {
onUnexpectedError(e);
}
const enterResult = richEditSupport.onEnter(autoIndent, '', virtualModel.getLineContent(inheritLine), '');
if (enterResult) {
let indentation = strings.getLeadingWhitespace(virtualModel.getLineContent(inheritLine));
......@@ -689,18 +668,10 @@ export class LanguageConfigurationRegistryImpl {
// begin onEnter
private _getOnEnterSupport(languageId: LanguageId): OnEnterSupport | null {
const value = this._getRichEditSupport(languageId);
if (!value) {
return null;
}
return value.onEnter || null;
}
public getEnterAction(autoIndent: EditorAutoIndentStrategy, model: ITextModel, range: Range): CompleteEnterAction | null {
const scopedLineTokens = this.getScopedLineTokens(model, range.startLineNumber, range.startColumn);
const onEnterSupport = this._getOnEnterSupport(scopedLineTokens.languageId);
if (!onEnterSupport) {
const richEditSupport = this._getRichEditSupport(scopedLineTokens.languageId);
if (!richEditSupport) {
return null;
}
......@@ -726,13 +697,7 @@ export class LanguageConfigurationRegistryImpl {
}
}
let enterResult: EnterAction | null = null;
try {
enterResult = onEnterSupport.onEnter(autoIndent, oneLineAboveText, beforeEnterText, afterEnterText);
} catch (e) {
onUnexpectedError(e);
}
const enterResult = richEditSupport.onEnter(autoIndent, oneLineAboveText, beforeEnterText, afterEnterText);
if (!enterResult) {
return null;
}
......
......@@ -10,7 +10,7 @@ import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions'
export interface IOnEnterSupportOptions {
brackets?: CharacterPair[];
regExpRules?: OnEnterRule[];
onEnterRules?: OnEnterRule[];
}
interface IProcessedBracketPair {
......@@ -25,7 +25,7 @@ export class OnEnterSupport {
private readonly _brackets: IProcessedBracketPair[];
private readonly _regExpRules: OnEnterRule[];
constructor(opts?: IOnEnterSupportOptions) {
constructor(opts: IOnEnterSupportOptions) {
opts = opts || {};
opts.brackets = opts.brackets || [
['(', ')'],
......@@ -46,7 +46,7 @@ export class OnEnterSupport {
});
}
});
this._regExpRules = opts.regExpRules || [];
this._regExpRules = opts.onEnterRules || [];
}
public onEnter(autoIndent: EditorAutoIndentStrategy, oneLineAboveText: string, beforeEnterText: string, afterEnterText: string): EnterAction | null {
......
......@@ -49,7 +49,7 @@ suite('OnEnter', () => {
test('uses regExpRules', () => {
let support = new OnEnterSupport({
regExpRules: javascriptOnEnterRules
onEnterRules: javascriptOnEnterRules
});
let testIndentAction = (oneLineAboveText: string, beforeText: string, afterText: string, expectedIndentAction: IndentAction | null, expectedAppendText: string | null, removeText: number = 0) => {
let actual = support.onEnter(EditorAutoIndentStrategy.Advanced, oneLineAboveText, beforeText, afterText);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册