onEnter.test.ts 7.1 KB
Newer Older
E
Erich Gamma 已提交
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 * as assert from 'assert';
8 9
import {IndentAction, CharacterPair} from 'vs/editor/common/modes';
import {OnEnterSupport} from 'vs/editor/common/modes/supports/onEnter';
E
Erich Gamma 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

suite('OnEnter', () => {

	test('uses indentationRules', () => {
		var support = new OnEnterSupport(null, {
			indentationRules: {
				decreaseIndentPattern: /^\s*((?!\S.*\/[*]).*[*]\/\s*)?[})\]]|^\s*(case\b.*|default):\s*(\/\/.*|\/[*].*[*]\/\s*)?$/,
				increaseIndentPattern: /(\{[^}"']*|\([^)"']*|\[[^\]"']*|^\s*(\{\}|\(\)|\[\]|(case\b.*|default):))\s*(\/\/.*|\/[*].*[*]\/\s*)?$/,
				indentNextLinePattern: /^\s*(for|while|if|else)\b(?!.*[;{}]\s*(\/\/.*|\/[*].*[*]\/\s*)?$)/,
				unIndentedLinePattern: /^(?!.*([;{}]|\S:)\s*(\/\/.*|\/[*].*[*]\/\s*)?$)(?!.*(\{[^}"']*|\([^)"']*|\[[^\]"']*|^\s*(\{\}|\(\)|\[\]|(case\b.*|default):))\s*(\/\/.*|\/[*].*[*]\/\s*)?$)(?!^\s*((?!\S.*\/[*]).*[*]\/\s*)?[})\]]|^\s*(case\b.*|default):\s*(\/\/.*|\/[*].*[*]\/\s*)?$)(?!^\s*(for|while|if|else)\b(?!.*[;{}]\s*(\/\/.*|\/[*].*[*]\/\s*)?$))/
			}
		});

		var testIndentAction = (oneLineAboveText:string, beforeText:string, afterText:string, expected:IndentAction) => {
			var actual = support._actualOnEnter(oneLineAboveText, beforeText, afterText);
			if (expected === IndentAction.None) {
				assert.equal(actual, null);
			} else {
				assert.equal(actual.indentAction, expected);
			}
		};

		testIndentAction('', 'case', '', IndentAction.None);
		testIndentAction('', 'case:', '', IndentAction.Indent);
		testIndentAction('', 'if (true) {', '', IndentAction.Indent);
		testIndentAction('', 'if (true)', '', IndentAction.Indent);
		testIndentAction('', ' ', '}', IndentAction.Outdent);
		testIndentAction('if(true)', '\treturn false', '', IndentAction.Outdent);
	});

	test('uses brackets', () => {
41 42 43
		var brackets: CharacterPair[] = [
			['(', ')'],
			['begin', 'end']
E
Erich Gamma 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
		];
		var support = new OnEnterSupport(null, {
			brackets: brackets
		});
		var testIndentAction = (beforeText:string, afterText:string, expected:IndentAction) => {
			var actual = support._actualOnEnter('', beforeText, afterText);
			if (expected === IndentAction.None) {
				assert.equal(actual, null);
			} else {
				assert.equal(actual.indentAction, expected);
			}
		};

		testIndentAction('a', '', IndentAction.None);
		testIndentAction('', 'b', IndentAction.None);
		testIndentAction('(', 'b', IndentAction.Indent);
		testIndentAction('a', ')', IndentAction.None);
		testIndentAction('begin', 'ending', IndentAction.Indent);
		testIndentAction('abegin', 'end', IndentAction.None);
		testIndentAction('begin', ')', IndentAction.Indent);
		testIndentAction('begin', 'end', IndentAction.IndentOutdent);
		testIndentAction('begin ', ' end', IndentAction.IndentOutdent);
		testIndentAction(' begin', 'end//as', IndentAction.IndentOutdent);
		testIndentAction('(', ')', IndentAction.IndentOutdent);
		testIndentAction('( ', ')', IndentAction.IndentOutdent);
		testIndentAction('a(', ')b', IndentAction.IndentOutdent);

		testIndentAction('(', '', IndentAction.Indent);
		testIndentAction('(', 'foo', IndentAction.Indent);
		testIndentAction('begin', 'foo', IndentAction.Indent);
		testIndentAction('begin', '', IndentAction.Indent);
	});

	test('uses regExpRules', () => {
		var support = new OnEnterSupport(null, {
			regExpRules: [
				{
					beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
					afterText: /^\s*\*\/$/,
					action: { indentAction: IndentAction.IndentOutdent, appendText: ' * ' }
				},
				{
					beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
					action: { indentAction: IndentAction.None, appendText: ' * ' }
				},
				{
90
					beforeText: /^(\t|(\ \ ))*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
E
Erich Gamma 已提交
91 92 93 94 95
					action: { indentAction: IndentAction.None, appendText: '* ' }
				},
				{
					beforeText: /^(\t|(\ \ ))*\ \*\/\s*$/,
					action: { indentAction: IndentAction.None, removeText: 1 }
96 97 98 99
				},
				{
					beforeText: /^(\t|(\ \ ))*\ \*[^/]*\*\/\s*$/,
					action: { indentAction: IndentAction.None, removeText: 1 }
E
Erich Gamma 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
				}
			]
		});
		var testIndentAction = (beforeText:string, afterText:string, expectedIndentAction:IndentAction, expectedAppendText:string, removeText:number = 0) => {
			var actual = support._actualOnEnter('', beforeText, afterText);
			if (expectedIndentAction === null) {
				assert.equal(actual, null, 'isNull:' + beforeText);
			} else {
				assert.equal(actual !== null, true, 'isNotNull:' + beforeText);
				assert.equal(actual.indentAction, expectedIndentAction, 'indentAction:' + beforeText);
				if (expectedAppendText !== null) {
					assert.equal(actual.appendText, expectedAppendText, 'appendText:' + beforeText);
				}
				if (removeText !== 0) {
					assert.equal(actual.removeText, removeText, 'removeText:' + beforeText);
				}
			}
		};

		testIndentAction('\t/**', ' */', IndentAction.IndentOutdent, ' * ');
		testIndentAction('\t/**', '', IndentAction.None, ' * ');
		testIndentAction('\t/** * / * / * /', '', IndentAction.None, ' * ');
		testIndentAction('\t/** /*', '', IndentAction.None, ' * ');
		testIndentAction('/**', '', IndentAction.None, ' * ');
		testIndentAction('\t/**/', '', null, null);
		testIndentAction('\t/***/', '', null, null);
		testIndentAction('\t/*******/', '', null, null);
		testIndentAction('\t/** * * * * */', '', null, null);
		testIndentAction('\t/** */', '', null, null);
		testIndentAction('\t/** asdfg */', '', null, null);
		testIndentAction('\t/* asdfg */', '', null, null);
		testIndentAction('\t/* asdfg */', '', null, null);
		testIndentAction('\t/** asdfg */', '', null, null);
		testIndentAction('*/', '', null, null);
		testIndentAction('\t/*', '', null, null);
		testIndentAction('\t*', '', null, null);
136
		testIndentAction('\t *', '', IndentAction.None, '* ');
E
Erich Gamma 已提交
137
		testIndentAction('\t */', '', IndentAction.None, null, 1);
138
		testIndentAction('\t * */', '', IndentAction.None, null, 1);
E
Erich Gamma 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152
		testIndentAction('\t * * / * / * / */', '', null, null);
		testIndentAction('\t * ', '', IndentAction.None, '* ');
		testIndentAction(' * ', '', IndentAction.None, '* ');
		testIndentAction(' * asdfsfagadfg', '', IndentAction.None, '* ');
		testIndentAction(' * asdfsfagadfg * * * ', '', IndentAction.None, '* ');
		testIndentAction(' * /*', '', IndentAction.None, '* ');
		testIndentAction(' * asdfsfagadfg * / * / * /', '', IndentAction.None, '* ');
		testIndentAction(' * asdfsfagadfg * / * / * /*', '', IndentAction.None, '* ');
		testIndentAction(' */', '', IndentAction.None, null, 1);
		testIndentAction('\t */', '', IndentAction.None, null, 1);
		testIndentAction('\t\t */', '', IndentAction.None, null, 1);
		testIndentAction('   */', '', IndentAction.None, null, 1);
		testIndentAction('     */', '', IndentAction.None, null, 1);
		testIndentAction('\t     */', '', IndentAction.None, null, 1);
153
		testIndentAction(' *--------------------------------------------------------------------------------------------*/', '', IndentAction.None, null, 1);
E
Erich Gamma 已提交
154 155
	});
});