abbreviationActions.ts 6.9 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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';
import { expand } from '@emmetio/expand-abbreviation';
8 9 10
import parseStylesheet from '@emmetio/css-parser';
import parse from '@emmetio/html-matcher';
import Node from '@emmetio/node';
11
import { getSyntax, getNode, getInnerRange } from './util';
12
import { getExpandOptions, extractAbbreviation, isStyleSheet } from 'vscode-emmet-helper';
13
import { DocumentStreamReader } from './bufferStream';
14

R
Ramya Achutha Rao 已提交
15
interface ExpandAbbreviationInput {
16
	syntax: string;
R
Ramya Achutha Rao 已提交
17 18 19 20 21
	abbreviation: string;
	rangeToReplace: vscode.Range;
	textToWrap?: string;
}

22 23 24 25 26 27
export function wrapWithAbbreviation() {
	let editor = vscode.window.activeTextEditor;
	if (!editor) {
		vscode.window.showInformationMessage('No editor is active');
		return;
	}
28
	const newLine = editor.document.eol === vscode.EndOfLine.LF ? '\n' : '\r\n';
29
	let syntax = getSyntax(editor.document);
30

R
Ramya Achutha Rao 已提交
31 32
	vscode.window.showInputBox({ prompt: 'Enter Abbreviation' }).then(abbreviation => {
		if (!abbreviation || !abbreviation.trim()) { return; }
33

R
Ramya Achutha Rao 已提交
34
		let expandAbbrList: ExpandAbbreviationInput[] = [];
35 36
		let firstTextToReplace: string;
		let allTextToReplaceSame: boolean = true;
37
		let preceedingWhiteSpace = '';
38

39
		editor.selections.forEach(selection => {
40
			let rangeToReplace: vscode.Range = selection.isReversed ? new vscode.Range(selection.active, selection.anchor) : selection;
41 42 43
			if (rangeToReplace.isEmpty) {
				rangeToReplace = new vscode.Range(rangeToReplace.start.line, 0, rangeToReplace.start.line, editor.document.lineAt(rangeToReplace.start.line).text.length);
			}
44 45 46 47 48 49 50 51 52 53 54 55 56
			const firstLine = editor.document.lineAt(rangeToReplace.start).text;
			const matches = firstLine.match(/^(\s*)/);
			if (matches) {
				preceedingWhiteSpace = matches[1];
			}
			if (rangeToReplace.start.character <= preceedingWhiteSpace.length) {
				rangeToReplace = new vscode.Range(rangeToReplace.start.line, 0, rangeToReplace.end.line, rangeToReplace.end.character);
			}

			let textToWrap = '';
			for (let i = rangeToReplace.start.line; i <= rangeToReplace.end.line; i++) {
				textToWrap += (i === rangeToReplace.start.line ? '' : newLine) + '\t' + editor.document.lineAt(i).text.substr(preceedingWhiteSpace.length);
			}
57 58

			if (!firstTextToReplace) {
R
Ramya Achutha Rao 已提交
59 60
				firstTextToReplace = textToWrap;
			} else if (allTextToReplaceSame && firstTextToReplace !== textToWrap) {
61 62 63
				allTextToReplaceSame = false;
			}

64
			expandAbbrList.push({ syntax, abbreviation, rangeToReplace, textToWrap });
65
		});
66

67
		expandAbbreviationInRange(editor, expandAbbrList, syntax, allTextToReplaceSame, preceedingWhiteSpace);
68 69 70
	});
}

71 72
export function expandAbbreviation(args) {

73 74 75 76 77
	let editor = vscode.window.activeTextEditor;
	if (!editor) {
		vscode.window.showInformationMessage('No editor is active');
		return;
	}
78
	if (typeof args !== 'object' || !args['syntax']) {
79 80
		return;
	}
81
	let syntax = args['syntax'];
82
	let parseContent = isStyleSheet(syntax) ? parseStylesheet : parse;
83
	let rootNode: Node = parseContent(new DocumentStreamReader(editor.document));
84

R
Ramya Achutha Rao 已提交
85
	let abbreviationList: ExpandAbbreviationInput[] = [];
86 87 88
	let firstAbbreviation: string;
	let allAbbreviationsSame: boolean = true;

89
	editor.selections.forEach(selection => {
R
Ramya Achutha Rao 已提交
90
		let rangeToReplace: vscode.Range = selection;
91
		let position = selection.isReversed ? selection.anchor : selection.active;
R
Ramya Achutha Rao 已提交
92 93 94
		let abbreviation = editor.document.getText(rangeToReplace);
		if (rangeToReplace.isEmpty) {
			[rangeToReplace, abbreviation] = extractAbbreviation(editor.document, position);
95
		}
96

97 98 99 100 101
		let currentNode = getNode(rootNode, position);
		if (!isValidLocationForEmmetAbbreviation(currentNode, syntax, position)) {
			return;
		}

102 103 104 105
		if (!firstAbbreviation) {
			firstAbbreviation = abbreviation;
		} else if (allAbbreviationsSame && firstAbbreviation !== abbreviation) {
			allAbbreviationsSame = false;
106
		}
107

108
		abbreviationList.push({ syntax, abbreviation, rangeToReplace });
109 110
	});

111
	expandAbbreviationInRange(editor, abbreviationList, syntax, allAbbreviationsSame);
112 113 114 115
}


/**
116 117
 * Checks if given position is a valid location to expand emmet abbreviation.
 * Works only on html and css/less/scss syntax
118 119 120 121
 * @param currentNode parsed node at given position
 * @param syntax syntax of the abbreviation
 * @param position position to validate
 */
122
export function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: string, position: vscode.Position): boolean {
123 124 125 126 127 128 129 130 131 132 133 134 135 136
	if (!currentNode) {
		return true;
	}

	if (isStyleSheet(syntax)) {
		return currentNode.type !== 'rule'
			|| (currentNode.selectorToken && position.isAfter(currentNode.selectorToken.end));
	}

	if (currentNode.close) {
		return getInnerRange(currentNode).contains(position);
	}

	return false;
R
Ramya Achutha Rao 已提交
137 138
}

139
function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: ExpandAbbreviationInput[], syntax: string, insertSameSnippet: boolean, preceedingWhiteSpace: string = '') {
R
Ramya Achutha Rao 已提交
140 141 142
	if (!expandAbbrList || expandAbbrList.length === 0) {
		return;
	}
143
	const newLine = editor.document.eol === vscode.EndOfLine.LF ? '\n' : '\r\n';
R
Ramya Achutha Rao 已提交
144 145 146 147 148 149

	// 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
	if (!insertSameSnippet) {
		expandAbbrList.forEach((expandAbbrInput: ExpandAbbreviationInput) => {
R
Ramya Achutha Rao 已提交
150
			let expandedText = expandAbbrInput.textToWrap ? wrapAbbr(expandAbbrInput, preceedingWhiteSpace, newLine) : expand(expandAbbrInput.abbreviation, getExpandOptions(expandAbbrInput.syntax));
R
Ramya Achutha Rao 已提交
151 152 153 154 155 156 157 158 159 160 161
			if (expandedText) {
				editor.insertSnippet(new vscode.SnippetString(expandedText), expandAbbrInput.rangeToReplace);
			}
		});
		return;
	}

	// Snippet to replace at all cursors are the same
	// We can pass all ranges to `editor.insertSnippet` in a single call so that 
	// all cursors are maintained after snippet insertion
	const anyExpandAbbrInput = expandAbbrList[0];
R
Ramya Achutha Rao 已提交
162
	let expandedText = anyExpandAbbrInput.textToWrap ? wrapAbbr(anyExpandAbbrInput, preceedingWhiteSpace, newLine) : expand(anyExpandAbbrInput.abbreviation, getExpandOptions(anyExpandAbbrInput.syntax));
R
Ramya Achutha Rao 已提交
163 164 165 166 167 168
	let allRanges = expandAbbrList.map(value => {
		return value.rangeToReplace;
	});
	if (expandedText) {
		editor.insertSnippet(new vscode.SnippetString(expandedText), allRanges);
	}
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

function wrapAbbr(input: ExpandAbbreviationInput, preceedingWhiteSpace: string, newLine: string): string {

	let expandedText = expand(input.abbreviation, getExpandOptions(input.syntax, newLine + input.textToWrap + newLine));
	if (!expandedText) {
		return;
	}

	let lines = expandedText.split(newLine);
	let finalExpandedText = '';
	for (let i = 0; i < lines.length; i++) {
		finalExpandedText += newLine + preceedingWhiteSpace + lines[i];
	}
	return finalExpandedText.substr(newLine.length);
184
}