characterPair.ts 2.7 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

A
Alex Dima 已提交
7
import {IAutoClosingPair, IAutoClosingPairConditional, ILineContext, IRichEditCharacterPair, CharacterPair} from 'vs/editor/common/modes';
8
import {handleEvent} from 'vs/editor/common/modes/supports';
9
import {LanguageConfigurationRegistryImpl} from 'vs/editor/common/modes/languageConfigurationRegistry';
10

A
Alex Dima 已提交
11
export class CharacterPairSupport implements IRichEditCharacterPair {
12

13
	private _registry: LanguageConfigurationRegistryImpl;
14
	private _modeId: string;
A
Alex Dima 已提交
15 16
	private _autoClosingPairs: IAutoClosingPairConditional[];
	private _surroundingPairs: IAutoClosingPair[];
17

18 19
	constructor(registry: LanguageConfigurationRegistryImpl, modeId: string, config: { brackets?: CharacterPair[]; autoClosingPairs?: IAutoClosingPairConditional[], surroundingPairs?: IAutoClosingPair[]}) {
		this._registry = registry;
20
		this._modeId = modeId;
21 22 23 24 25
		this._autoClosingPairs = config.autoClosingPairs;
		if (!this._autoClosingPairs) {
			this._autoClosingPairs = config.brackets ? config.brackets.map(b => ({ open: b[0], close: b[1] })) : [];
		}
		this._surroundingPairs = config.surroundingPairs || this._autoClosingPairs;
26 27
	}

A
Alex Dima 已提交
28
	public getAutoClosingPairs(): IAutoClosingPair[] {
29 30 31
		return this._autoClosingPairs;
	}

A
Alex Dima 已提交
32
	public shouldAutoClosePair(character:string, context:ILineContext, offset:number): boolean {
A
Alex Dima 已提交
33 34
		return handleEvent(context, offset, (nestedModeId:string, context:ILineContext, offset:number) => {
			if (this._modeId === nestedModeId) {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

				// Always complete on empty line
				if (context.getTokenCount() === 0) {
					return true;
				}

				var tokenIndex = context.findIndexOfOffset(offset - 1);
				var tokenType = context.getTokenType(tokenIndex);

				for (var i = 0; i < this._autoClosingPairs.length; ++i) {
					if (this._autoClosingPairs[i].open === character) {
						if (this._autoClosingPairs[i].notIn) {
							for (var notInIndex = 0; notInIndex < this._autoClosingPairs[i].notIn.length; ++notInIndex) {
								if (tokenType.indexOf(this._autoClosingPairs[i].notIn[notInIndex]) > -1) {
									return false;
								}
							}
						}
						break;
					}
				}

				return true;
			}
59

A
Alex Dima 已提交
60
			let characterPairSupport = this._registry.getCharacterPairSupport(nestedModeId);
61 62 63 64 65
			if (characterPairSupport) {
				return characterPairSupport.shouldAutoClosePair(character, context, offset);
			}

			return null;
66 67 68
		});
	}

A
Alex Dima 已提交
69
	public getSurroundingPairs(): IAutoClosingPair[]{
70 71 72
		return this._surroundingPairs;
	}
}