mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-23 01:58:53 +01:00
Prepping to move completion provider to a re-usable module
This commit is contained in:
@@ -6,13 +6,8 @@
|
||||
import * as vscode from 'vscode';
|
||||
import parse from '@emmetio/html-matcher';
|
||||
import Node from '@emmetio/node';
|
||||
import { DocumentStreamReader } from './bufferStream';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
|
||||
let variablesFromFile = {};
|
||||
let profilesFromFile = {};
|
||||
let emmetExtensionsPath = '';
|
||||
import { DocumentStreamReader } from './emmetForVSCode/bufferStream';
|
||||
import { isStyleSheet, getNode } from './emmetForVSCode/emmetUtils';
|
||||
|
||||
export const LANGUAGE_MODES: Object = {
|
||||
'html': ['!', '.', '}'],
|
||||
@@ -56,67 +51,6 @@ export function getSyntax(document: vscode.TextDocument): string {
|
||||
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 profilesFromSettings = vscode.workspace.getConfiguration('emmet')['syntaxProfiles'] || {};
|
||||
let profilesConfig = Object.assign({}, profilesFromFile, profilesFromSettings);
|
||||
|
||||
let options = profilesConfig[syntax];
|
||||
if (!options || typeof options === 'string') {
|
||||
if (options === 'xhtml') {
|
||||
return {
|
||||
selfClosingStyle: 'xhtml'
|
||||
};
|
||||
}
|
||||
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 getVariables(): any {
|
||||
let variablesFromSettings = vscode.workspace.getConfiguration('emmet')['variables'];
|
||||
return Object.assign({}, variablesFromFile, variablesFromSettings);
|
||||
}
|
||||
|
||||
export function getMappedModes(): any {
|
||||
let finalMappedModes = {};
|
||||
let syntaxProfileConfig = vscode.workspace.getConfiguration('emmet')['syntaxProfiles'];
|
||||
@@ -129,41 +63,6 @@ export function getMappedModes(): any {
|
||||
return finalMappedModes;
|
||||
}
|
||||
|
||||
export function updateExtensionsPath() {
|
||||
let currentEmmetExtensionsPath = vscode.workspace.getConfiguration('emmet')['extensionsPath'];
|
||||
if (emmetExtensionsPath !== currentEmmetExtensionsPath) {
|
||||
emmetExtensionsPath = currentEmmetExtensionsPath;
|
||||
|
||||
if (emmetExtensionsPath && emmetExtensionsPath.trim()) {
|
||||
let dirPath = path.isAbsolute(emmetExtensionsPath) ? emmetExtensionsPath : path.join(vscode.workspace.rootPath, emmetExtensionsPath);
|
||||
let snippetsPath = path.join(dirPath, 'snippets.json');
|
||||
let profilesPath = path.join(dirPath, 'syntaxProfiles.json');
|
||||
if (dirExists(dirPath)) {
|
||||
fs.readFile(snippetsPath, (err, snippetsData) => {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
let snippetsJson = JSON.parse(snippetsData.toString());
|
||||
variablesFromFile = snippetsJson['variables'];
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
});
|
||||
fs.readFile(profilesPath, (err, profilesData) => {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
profilesFromFile = JSON.parse(profilesData.toString());
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
export function getOpenCloseRange(document: vscode.TextDocument, position: vscode.Position): [vscode.Range, vscode.Range] {
|
||||
let rootNode: Node = parse(new DocumentStreamReader(document));
|
||||
let nodeToUpdate = getNode(rootNode, position);
|
||||
@@ -175,34 +74,6 @@ export function getOpenCloseRange(document: vscode.TextDocument, position: vscod
|
||||
return [openRange, closeRange];
|
||||
}
|
||||
|
||||
export function getNode(root: Node, position: vscode.Position, includeNodeBoundary: boolean = false) {
|
||||
let currentNode: Node = root.firstChild;
|
||||
let foundNode: Node = null;
|
||||
|
||||
while (currentNode) {
|
||||
const nodeStart: vscode.Position = currentNode.start;
|
||||
const nodeEnd: vscode.Position = currentNode.end;
|
||||
if ((nodeStart.isBefore(position) && nodeEnd.isAfter(position))
|
||||
|| (includeNodeBoundary && (nodeStart.isBeforeOrEqual(position) && nodeEnd.isAfterOrEqual(position)))) {
|
||||
|
||||
foundNode = currentNode;
|
||||
// Dig deeper
|
||||
currentNode = currentNode.firstChild;
|
||||
} else {
|
||||
currentNode = currentNode.nextSibling;
|
||||
}
|
||||
}
|
||||
|
||||
return foundNode;
|
||||
}
|
||||
|
||||
export function getInnerRange(currentNode: Node): vscode.Range {
|
||||
if (!currentNode.close) {
|
||||
return;
|
||||
}
|
||||
return new vscode.Range(currentNode.open.end, currentNode.close.start);
|
||||
}
|
||||
|
||||
export function getDeepestNode(node: Node): Node {
|
||||
if (!node || !node.children || node.children.length === 0) {
|
||||
return node;
|
||||
@@ -341,11 +212,4 @@ export function sameNodes(node1: Node, node2: Node): boolean {
|
||||
return (<vscode.Position>node1.start).isEqual(node2.start) && (<vscode.Position>node1.end).isEqual(node2.end);
|
||||
}
|
||||
|
||||
function dirExists(dirPath: string): boolean {
|
||||
try {
|
||||
|
||||
return fs.statSync(dirPath).isDirectory();
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user