mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-30 21:41:46 +01:00
215 lines
6.2 KiB
TypeScript
215 lines
6.2 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* 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 parse from '@emmetio/html-matcher';
|
|
import Node from '@emmetio/node';
|
|
import * as extract from '@emmetio/extract-abbreviation';
|
|
|
|
|
|
export function validate(allowStylesheet: boolean = true): boolean {
|
|
let editor = vscode.window.activeTextEditor;
|
|
if (!editor) {
|
|
vscode.window.showInformationMessage('No editor is active');
|
|
return false;
|
|
}
|
|
if (!allowStylesheet && isStyleSheet(editor.document.languageId)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export function getSyntax(document: vscode.TextDocument): string {
|
|
if (document.languageId === 'jade') {
|
|
return 'pug';
|
|
}
|
|
if (document.languageId === 'javascriptreact' || document.languageId === 'typescriptreact') {
|
|
return 'jsx';
|
|
}
|
|
return document.languageId;
|
|
}
|
|
|
|
export function isStyleSheet(syntax): boolean {
|
|
let stylesheetSyntaxes = ['css', 'scss', 'sass', 'less', 'stylus'];
|
|
return (stylesheetSyntaxes.indexOf(syntax) > -1);
|
|
}
|
|
|
|
export function getProfile(syntax: string): any {
|
|
let config = vscode.workspace.getConfiguration('emmet')['syntaxProfiles'] || {};
|
|
let options = config[syntax];
|
|
if (!options || typeof options === 'string') {
|
|
return {};
|
|
}
|
|
let newOptions = {};
|
|
for (let key in options) {
|
|
switch (key) {
|
|
case 'tag_case':
|
|
newOptions['tagCase'] = (options[key] === 'lower' || options[key] === 'upper') ? options[key] : '';
|
|
break;
|
|
case 'attr_case':
|
|
newOptions['attributeCase'] = (options[key] === 'lower' || options[key] === 'upper') ? options[key] : '';
|
|
break;
|
|
case 'attr_quotes':
|
|
newOptions['attributeQuotes'] = options[key];
|
|
break;
|
|
case 'tag_nl':
|
|
newOptions['format'] = (options[key] === 'true' || options[key] === 'false') ? options[key] : 'true';
|
|
break;
|
|
case 'indent':
|
|
newOptions['attrCase'] = (options[key] === 'true' || options[key] === 'false') ? '\t' : options[key];
|
|
break;
|
|
case 'inline_break':
|
|
newOptions['inlineBreak'] = options[key];
|
|
break;
|
|
case 'self_closing_tag':
|
|
if (options[key] === true) {
|
|
newOptions['selfClosingStyle'] = 'xml'; break;
|
|
}
|
|
if (options[key] === false) {
|
|
newOptions['selfClosingStyle'] = 'html'; break;
|
|
}
|
|
newOptions['selfClosingStyle'] = options[key];
|
|
break;
|
|
default:
|
|
newOptions[key] = options[key];
|
|
break;
|
|
}
|
|
}
|
|
return newOptions;
|
|
}
|
|
|
|
export function getOpenCloseRange(document: vscode.TextDocument, offset: number): [vscode.Range, vscode.Range] {
|
|
let rootNode: Node = parse(document.getText());
|
|
let nodeToUpdate = getNode(rootNode, offset);
|
|
let openRange = new vscode.Range(document.positionAt(nodeToUpdate.open.start), document.positionAt(nodeToUpdate.open.end));
|
|
let closeRange = null;
|
|
if (nodeToUpdate.close) {
|
|
closeRange = new vscode.Range(document.positionAt(nodeToUpdate.close.start), document.positionAt(nodeToUpdate.close.end));
|
|
}
|
|
return [openRange, closeRange];
|
|
}
|
|
|
|
export function getNode(root: Node, offset: number, includeNodeBoundary: boolean = false) {
|
|
let currentNode: Node = root.firstChild;
|
|
let foundNode: Node = null;
|
|
|
|
while (currentNode) {
|
|
if ((currentNode.start < offset && currentNode.end > offset)
|
|
|| (includeNodeBoundary && (currentNode.start <= offset && currentNode.end >= offset))) {
|
|
|
|
foundNode = currentNode;
|
|
// Dig deeper
|
|
currentNode = currentNode.firstChild;
|
|
} else {
|
|
currentNode = currentNode.nextSibling;
|
|
}
|
|
}
|
|
|
|
return foundNode;
|
|
}
|
|
|
|
export function getNodeOuterSelection(document: vscode.TextDocument, node: Node): vscode.Selection {
|
|
return new vscode.Selection(document.positionAt(node.start), document.positionAt(node.end));
|
|
}
|
|
|
|
export function getNodeInnerSelection(document: vscode.TextDocument, node: Node): vscode.Selection {
|
|
return new vscode.Selection(document.positionAt(node.open.end), document.positionAt(node.close.start));
|
|
}
|
|
|
|
export function extractAbbreviation(position: vscode.Position): [vscode.Range, string] {
|
|
let editor = vscode.window.activeTextEditor;
|
|
let currentLine = editor.document.lineAt(position.line).text;
|
|
let result = extract(currentLine, position.character, true);
|
|
if (!result) {
|
|
return [null, ''];
|
|
}
|
|
|
|
let rangeToReplace = new vscode.Range(position.line, result.location, position.line, result.location + result.abbreviation.length);
|
|
return [rangeToReplace, result.abbreviation];
|
|
}
|
|
|
|
export function getDeepestNode(node: Node): Node {
|
|
if (!node || !node.children || node.children.length === 0) {
|
|
return node;
|
|
}
|
|
|
|
return getDeepestNode(node.children[node.children.length - 1]);
|
|
}
|
|
|
|
export function findNextWord(propertyValue: string, pos: number): [number, number] {
|
|
|
|
let foundSpace = pos === -1;
|
|
let foundStart = false;
|
|
let foundEnd = false;
|
|
|
|
let newSelectionStart;
|
|
let newSelectionEnd;
|
|
while (pos < propertyValue.length - 1) {
|
|
pos++;
|
|
if (!foundSpace) {
|
|
if (propertyValue[pos] === ' ') {
|
|
foundSpace = true;
|
|
}
|
|
continue;
|
|
}
|
|
if (foundSpace && !foundStart && propertyValue[pos] === ' ') {
|
|
continue;
|
|
}
|
|
if (!foundStart) {
|
|
newSelectionStart = pos;
|
|
foundStart = true;
|
|
continue;
|
|
}
|
|
if (propertyValue[pos] === ' ') {
|
|
newSelectionEnd = pos;
|
|
foundEnd = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (foundStart && !foundEnd) {
|
|
newSelectionEnd = propertyValue.length;
|
|
}
|
|
|
|
return [newSelectionStart, newSelectionEnd];
|
|
}
|
|
|
|
export function findPrevWord(propertyValue: string, pos: number): [number, number] {
|
|
|
|
let foundSpace = pos === propertyValue.length;
|
|
let foundStart = false;
|
|
let foundEnd = false;
|
|
|
|
let newSelectionStart;
|
|
let newSelectionEnd;
|
|
while (pos > -1) {
|
|
pos--;
|
|
if (!foundSpace) {
|
|
if (propertyValue[pos] === ' ') {
|
|
foundSpace = true;
|
|
}
|
|
continue;
|
|
}
|
|
if (foundSpace && !foundEnd && propertyValue[pos] === ' ') {
|
|
continue;
|
|
}
|
|
if (!foundEnd) {
|
|
newSelectionEnd = pos + 1;
|
|
foundEnd = true;
|
|
continue;
|
|
}
|
|
if (propertyValue[pos] === ' ') {
|
|
newSelectionStart = pos + 1;
|
|
foundStart = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (foundEnd && !foundStart) {
|
|
newSelectionStart = 0;
|
|
}
|
|
|
|
return [newSelectionStart, newSelectionEnd];
|
|
} |