abbreviationActions.ts 13.0 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.
 *--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
7
import { Node, HtmlNode, Rule } from 'EmmetNode';
8
import { getEmmetHelper, getNode, getInnerRange, getMappingForIncludedLanguages, parseDocument, validate, getEmmetConfiguration, isStyleSheet, getEmmetMode } from './util';
9

10 11
const trimRegex = /[\u00a0]*[\d|#|\-|\*|\u2022]+\.?/;

R
Ramya Achutha Rao 已提交
12
interface ExpandAbbreviationInput {
13
	syntax: string;
R
Ramya Achutha Rao 已提交
14 15
	abbreviation: string;
	rangeToReplace: vscode.Range;
16
	textToWrap?: string[];
17
	filter?: string;
R
Ramya Achutha Rao 已提交
18 19
}

20 21
export function wrapWithAbbreviation(args: any) {
	if (!validate(false) || !vscode.window.activeTextEditor) {
22 23
		return;
	}
24 25

	const editor = vscode.window.activeTextEditor;
26

27
	const syntax = getSyntaxFromArgs({ language: editor.document.languageId });
28 29 30 31 32
	if (!syntax) {
		return;
	}

	const abbreviationPromise = (args && args['abbreviation']) ? Promise.resolve(args['abbreviation']) : vscode.window.showInputBox({ prompt: 'Enter Abbreviation' });
33
	const helper = getEmmetHelper();
34 35

	return abbreviationPromise.then(abbreviation => {
36
		if (!abbreviation || !abbreviation.trim() || !helper.isAbbreviationValid(syntax, abbreviation)) { return false; }
37

R
Ramya Achutha Rao 已提交
38
		let expandAbbrList: ExpandAbbreviationInput[] = [];
39

40
		editor.selections.forEach(selection => {
41
			let rangeToReplace: vscode.Range = selection.isReversed ? new vscode.Range(selection.active, selection.anchor) : selection;
42 43 44
			if (rangeToReplace.isEmpty) {
				rangeToReplace = new vscode.Range(rangeToReplace.start.line, 0, rangeToReplace.start.line, editor.document.lineAt(rangeToReplace.start.line).text.length);
			}
45

46 47 48
			const firstLineOfSelection = editor.document.lineAt(rangeToReplace.start).text.substr(rangeToReplace.start.character);
			const matches = firstLineOfSelection.match(/^(\s*)/);
			const preceedingWhiteSpace = matches ? matches[1].length : 0;
49

50
			rangeToReplace = new vscode.Range(rangeToReplace.start.line, rangeToReplace.start.character + preceedingWhiteSpace, rangeToReplace.end.line, rangeToReplace.end.character);
51
			expandAbbrList.push({ syntax, abbreviation, rangeToReplace, textToWrap: ['\n\t$TM_SELECTED_TEXT\n'] });
52
		});
53

54
		return expandAbbreviationInRange(editor, expandAbbrList, true);
55 56 57
	});
}

58 59
export function wrapIndividualLinesWithAbbreviation(args: any) {
	if (!validate(false) || !vscode.window.activeTextEditor) {
60 61 62 63 64 65 66 67 68
		return;
	}

	const editor = vscode.window.activeTextEditor;
	if (editor.selection.isEmpty) {
		vscode.window.showInformationMessage('Select more than 1 line and try again.');
		return;
	}

69
	const syntax = getSyntaxFromArgs({ language: editor.document.languageId });
70 71 72 73 74
	if (!syntax) {
		return;
	}

	const abbreviationPromise = (args && args['abbreviation']) ? Promise.resolve(args['abbreviation']) : vscode.window.showInputBox({ prompt: 'Enter Abbreviation' });
75
	const lines = editor.document.getText(editor.selection).split('\n').map(x => x.trim());
76
	const helper = getEmmetHelper();
77

78
	return abbreviationPromise.then(inputAbbreviation => {
79
		if (!inputAbbreviation || !inputAbbreviation.trim() || !helper.isAbbreviationValid(syntax, inputAbbreviation)) { return false; }
80

81
		let extractedResults = helper.extractAbbreviationFromText(inputAbbreviation);
82
		if (!extractedResults) {
83
			return false;
84 85
		}

86
		let { abbreviation, filter } = extractedResults;
87 88 89 90
		let input: ExpandAbbreviationInput = {
			syntax,
			abbreviation,
			rangeToReplace: editor.selection,
91
			textToWrap: lines,
92
			filter
93 94 95 96 97 98 99
		};

		return expandAbbreviationInRange(editor, [input], true);
	});

}

100 101 102 103 104 105 106 107 108
export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined> {
	if (!validate() || !vscode.window.activeTextEditor) {
		return fallbackTab();
	}

	args = args || {};
	if (!args['language']) {
		args['language'] = vscode.window.activeTextEditor.document.languageId;
	}
109
	const syntax = getSyntaxFromArgs(args);
110
	if (!syntax) {
111
		return fallbackTab();
112
	}
113 114 115

	const editor = vscode.window.activeTextEditor;

116
	let rootNode = parseDocument(editor.document, false);
117

118 119 120 121 122
	// When tabbed on a non empty selection, do not treat it as an emmet abbreviation, and fallback to tab instead
	if (vscode.workspace.getConfiguration('emmet')['triggerExpansionOnTab'] === true && editor.selections.find(x => !x.isEmpty)) {
		return fallbackTab();
	}

R
Ramya Achutha Rao 已提交
123
	let abbreviationList: ExpandAbbreviationInput[] = [];
124 125
	let firstAbbreviation: string;
	let allAbbreviationsSame: boolean = true;
126
	const helper = getEmmetHelper();
127

128
	let getAbbreviation = (document: vscode.TextDocument, selection: vscode.Selection, position: vscode.Position, syntax: string): [vscode.Range | null, string, string] => {
R
Ramya Achutha Rao 已提交
129
		let rangeToReplace: vscode.Range = selection;
130
		let abbr = document.getText(rangeToReplace);
131
		if (!rangeToReplace.isEmpty) {
132
			let extractedResults = helper.extractAbbreviationFromText(abbr);
133
			if (extractedResults) {
134
				return [rangeToReplace, extractedResults.abbreviation, extractedResults.filter];
135
			}
136
			return [null, '', ''];
137 138
		}

139 140 141
		const currentLine = editor.document.lineAt(position.line).text;
		const textTillPosition = currentLine.substr(0, position.character);

142 143
		// Expand cases like <div to <div></div> explicitly
		// else we will end up with <<div></div>
144
		if (syntax === 'html') {
145 146
			let matches = textTillPosition.match(/<(\w+)$/);
			if (matches) {
147 148
				abbr = matches[1];
				rangeToReplace = new vscode.Range(position.translate(0, -(abbr.length + 1)), position);
149
				return [rangeToReplace, abbr, ''];
150
			}
151
		}
152
		let extractedResults = helper.extractAbbreviation(editor.document, position, false);
153
		if (!extractedResults) {
154
			return [null, '', ''];
155 156
		}

157 158
		let { abbreviationRange, abbreviation, filter } = extractedResults;
		return [new vscode.Range(abbreviationRange.start.line, abbreviationRange.start.character, abbreviationRange.end.line, abbreviationRange.end.character), abbreviation, filter];
159 160
	};

J
jmdowns2 已提交
161 162 163 164 165 166 167 168
	let selectionsInReverseOrder = editor.selections.slice(0);
	selectionsInReverseOrder.sort((a, b) => {
		var posA = a.isReversed ? a.anchor : a.active;
		var posB = b.isReversed ? b.anchor : b.active;
		return posA.compareTo(posB) * -1;
	});

	selectionsInReverseOrder.forEach(selection => {
169
		let position = selection.isReversed ? selection.anchor : selection.active;
170
		let [rangeToReplace, abbreviation, filter] = getAbbreviation(editor.document, selection, position, syntax);
171 172 173
		if (!rangeToReplace) {
			return;
		}
174
		if (!helper.isAbbreviationValid(syntax, abbreviation)) {
175 176
			return;
		}
177

178
		let currentNode = getNode(rootNode, position, true);
179
		if (!isValidLocationForEmmetAbbreviation(currentNode, syntax, position)) {
180 181 182
			return;
		}

183 184 185 186
		if (!firstAbbreviation) {
			firstAbbreviation = abbreviation;
		} else if (allAbbreviationsSame && firstAbbreviation !== abbreviation) {
			allAbbreviationsSame = false;
187
		}
188

189
		abbreviationList.push({ syntax, abbreviation, rangeToReplace, filter });
190 191
	});

192 193 194 195 196
	return expandAbbreviationInRange(editor, abbreviationList, allAbbreviationsSame).then(success => {
		if (!success) {
			return fallbackTab();
		}
	});
197 198
}

199
function fallbackTab(): Thenable<boolean | undefined> {
200 201 202
	if (vscode.workspace.getConfiguration('emmet')['triggerExpansionOnTab'] === true) {
		return vscode.commands.executeCommand('tab');
	}
203
	return Promise.resolve(true);
204
}
205
/**
206 207
 * Checks if given position is a valid location to expand emmet abbreviation.
 * Works only on html and css/less/scss syntax
208 209 210 211
 * @param currentNode parsed node at given position
 * @param syntax syntax of the abbreviation
 * @param position position to validate
 */
212
export function isValidLocationForEmmetAbbreviation(currentNode: Node | null, syntax: string, position: vscode.Position): boolean {
213
	// Continue validation only if the file was parse-able and the currentNode has been found
214
	if (!currentNode) {
215
		return true;
216 217 218
	}

	if (isStyleSheet(syntax)) {
219 220 221
		// If current node is a rule or at-rule, then perform additional checks to ensure
		// emmet suggestions are not provided in the rule selector
		if (currentNode.type !== 'rule' && currentNode.type !== 'at-rule') {
R
Ramya Achutha Rao 已提交
222 223
			return true;
		}
224

R
Ramya Achutha Rao 已提交
225
		const currentCssNode = <Rule>currentNode;
226

227 228 229 230 231
		// Position is valid if it occurs after the `{` that marks beginning of rule contents
		if (position.isAfter(currentCssNode.contentStartToken.end)) {
			return true;
		}

232
		// Workaround for https://github.com/Microsoft/vscode/30188
233 234
		// The line above the rule selector is considered as part of the selector by the css-parser
		// But we should assume it is a valid location for css properties under the parent rule
235
		if (currentCssNode.parent
236
			&& (currentCssNode.parent.type === 'rule' || currentCssNode.parent.type === 'at-rule')
237
			&& currentCssNode.selectorToken
238
			&& position.line !== currentCssNode.selectorToken.end.line) {
239 240 241
			return true;
		}

242
		return false;
243 244
	}

R
Ramya Achutha Rao 已提交
245 246
	const currentHtmlNode = <HtmlNode>currentNode;
	if (currentHtmlNode.close) {
247 248
		const innerRange = getInnerRange(currentHtmlNode);
		return !!innerRange && innerRange.contains(position);
249 250 251
	}

	return false;
R
Ramya Achutha Rao 已提交
252 253
}

254 255
/**
 * Expands abbreviations as detailed in expandAbbrList in the editor
256 257 258
 * @param editor
 * @param expandAbbrList
 * @param insertSameSnippet
259
 * @returns false if no snippet can be inserted.
260
 */
261
function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: ExpandAbbreviationInput[], insertSameSnippet: boolean): Thenable<boolean> {
R
Ramya Achutha Rao 已提交
262
	if (!expandAbbrList || expandAbbrList.length === 0) {
263
		return Promise.resolve(false);
R
Ramya Achutha Rao 已提交
264 265 266 267 268
	}

	// Snippet to replace at multiple cursors are not the same
	// `editor.insertSnippet` will have to be called for each instance separately
	// We will not be able to maintain multiple cursors after snippet insertion
269
	let insertPromises: Thenable<boolean>[] = [];
R
Ramya Achutha Rao 已提交
270 271
	if (!insertSameSnippet) {
		expandAbbrList.forEach((expandAbbrInput: ExpandAbbreviationInput) => {
272
			let expandedText = expandAbbr(expandAbbrInput);
R
Ramya Achutha Rao 已提交
273
			if (expandedText) {
274
				insertPromises.push(editor.insertSnippet(new vscode.SnippetString(expandedText), expandAbbrInput.rangeToReplace));
R
Ramya Achutha Rao 已提交
275 276
			}
		});
277 278 279
		if (insertPromises.length === 0) {
			return Promise.resolve(false);
		}
280
		return Promise.all(insertPromises).then(() => Promise.resolve(true));
R
Ramya Achutha Rao 已提交
281 282 283
	}

	// Snippet to replace at all cursors are the same
284
	// We can pass all ranges to `editor.insertSnippet` in a single call so that
R
Ramya Achutha Rao 已提交
285 286
	// all cursors are maintained after snippet insertion
	const anyExpandAbbrInput = expandAbbrList[0];
287
	let expandedText = expandAbbr(anyExpandAbbrInput);
R
Ramya Achutha Rao 已提交
288
	let allRanges = expandAbbrList.map(value => {
289
		return new vscode.Range(value.rangeToReplace.start.line, value.rangeToReplace.start.character, value.rangeToReplace.end.line, value.rangeToReplace.end.character);
R
Ramya Achutha Rao 已提交
290 291
	});
	if (expandedText) {
292
		return editor.insertSnippet(new vscode.SnippetString(expandedText), allRanges);
R
Ramya Achutha Rao 已提交
293
	}
294
	return Promise.resolve(false);
295 296
}

297
/**
298
 * Expands abbreviation as detailed in given input.
299
 */
300
function expandAbbr(input: ExpandAbbreviationInput): string | undefined {
301 302
	const helper = getEmmetHelper();
	const expandOptions = helper.getExpandOptions(input.syntax, getEmmetConfiguration(input.syntax), input.filter);
303

304
	if (input.textToWrap) {
305
		if (input.filter && input.filter.indexOf('t') > -1) {
306 307 308 309
			input.textToWrap = input.textToWrap.map(line => {
				return line.replace(trimRegex, '').trim();
			});
		}
310 311 312 313 314 315 316 317
		expandOptions['text'] = input.textToWrap;

		// Below fixes https://github.com/Microsoft/vscode/issues/29898
		// With this, Emmet formats inline elements as block elements
		// ensuring the wrapped multi line text does not get merged to a single line
		if (!input.rangeToReplace.isSingleLine) {
			expandOptions.profile['inlineBreak'] = 1;
		}
318 319
	}

320
	try {
321
		// Expand the abbreviation
322
		let expandedText = helper.expandAbbreviation(input.abbreviation, expandOptions);
323

324 325 326 327 328 329 330 331 332
		if (input.textToWrap) {
			// All $anyword would have been escaped by the emmet helper.
			// Remove the escaping backslash from $TM_SELECTED_TEXT so that VS Code Snippet controller can treat it as a variable
			expandedText = expandedText.replace('\\$TM_SELECTED_TEXT', '$TM_SELECTED_TEXT');

			// If the expanded text is single line then we dont need the \t and \n we added to $TM_SELECTED_TEXT earlier
			if (input.textToWrap.length === 1 && expandedText.indexOf('\n') === -1) {
				expandedText = expandedText.replace(/\s*\$TM_SELECTED_TEXT\s*/, '$TM_SELECTED_TEXT');
			}
333
		}
334

335 336
		return expandedText;

337 338
	} catch (e) {
		vscode.window.showErrorMessage('Failed to expand abbreviation');
339 340 341
	}


342 343
}

344
function getSyntaxFromArgs(args: Object): string | undefined {
345
	const mappedModes = getMappingForIncludedLanguages();
346 347
	let language: string = args['language'];
	let parentMode: string = args['parentMode'];
348
	let excludedLanguages = vscode.workspace.getConfiguration('emmet')['excludeLanguages'] ? vscode.workspace.getConfiguration('emmet')['excludeLanguages'] : [];
349
	let syntax = getEmmetMode((mappedModes[language] ? mappedModes[language] : language), excludedLanguages);
350 351
	if (!syntax) {
		syntax = getEmmetMode((mappedModes[parentMode] ? mappedModes[parentMode] : parentMode), excludedLanguages);
352 353
	}

354
	return syntax;
355
}