diff --git a/extensions/emmet/package.json b/extensions/emmet/package.json index 48aa76ce747b0..69fc0e8873358 100644 --- a/extensions/emmet/package.json +++ b/extensions/emmet/package.json @@ -57,13 +57,7 @@ "default": "\n" } }, - "default":{ - "lang": "en", - "locale": "en-US", - "charset": "UTF-8", - "indentation": "\t", - "newline": "\n" - }, + "default":{}, "description": "Variables to be used in emmet snippets" } } diff --git a/extensions/emmet/src/abbreviationActions.ts b/extensions/emmet/src/abbreviationActions.ts index 9281a7c4281a1..f3ffe745f4652 100644 --- a/extensions/emmet/src/abbreviationActions.ts +++ b/extensions/emmet/src/abbreviationActions.ts @@ -10,7 +10,7 @@ import parseStylesheet from '@emmetio/css-parser'; import parse from '@emmetio/html-matcher'; import Node from '@emmetio/node'; -import { getSyntax, getProfile, isStyleSheet, getNode, getInnerRange } from './util'; +import { getSyntax, getProfile, getVariables, isStyleSheet, getNode, getInnerRange } from './util'; import { DocumentStreamReader } from './bufferStream'; const field = (index, placeholder) => `\${${index}${placeholder ? ':' + placeholder : ''}}`; @@ -147,7 +147,7 @@ function getExpandOptions(syntax: string, textToReplace?: string) { syntax: syntax, profile: getProfile(syntax), addons: syntax === 'jsx' ? { 'jsx': true } : null, - variables: vscode.workspace.getConfiguration('emmet')['variables'], + variables: getVariables(), text: textToReplace ? textToReplace : '' }; } \ No newline at end of file diff --git a/extensions/emmet/src/extension.ts b/extensions/emmet/src/extension.ts index 8222b0eef69a1..eaaf869a602d9 100644 --- a/extensions/emmet/src/extension.ts +++ b/extensions/emmet/src/extension.ts @@ -15,6 +15,7 @@ import { mergeLines } from './mergeLines'; import { toggleComment } from './toggleComment'; import { fetchEditPoint } from './editPoint'; import { fetchSelectItem } from './selectItem'; +import { updateExtensionsPath } from './util'; const LANGUAGE_MODES: Object = { 'html': ['!', '.', '}'], @@ -109,6 +110,10 @@ export function activate(context: vscode.ExtensionContext) { fetchSelectItem('prev'); })); + updateExtensionsPath(); + context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => { + updateExtensionsPath(); + })); } export function deactivate() { diff --git a/extensions/emmet/src/util.ts b/extensions/emmet/src/util.ts index a99e71e49f5df..9dfc5e89d9141 100644 --- a/extensions/emmet/src/util.ts +++ b/extensions/emmet/src/util.ts @@ -7,7 +7,12 @@ 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 = ''; export function validate(allowStylesheet: boolean = true): boolean { let editor = vscode.window.activeTextEditor; if (!editor) { @@ -36,8 +41,10 @@ export function isStyleSheet(syntax): boolean { } export function getProfile(syntax: string): any { - let config = vscode.workspace.getConfiguration('emmet')['syntaxProfiles'] || {}; - let options = config[syntax]; + 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 { @@ -84,6 +91,46 @@ export function getProfile(syntax: string): any { return newOptions; } +export function getVariables(): any { + let variablesFromSettings = vscode.workspace.getConfiguration('emmet')['variables']; + return Object.assign({}, variablesFromFile, variablesFromSettings); +} + +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); @@ -259,4 +306,13 @@ export function sameNodes(node1: Node, node2: Node): boolean { return false; } return (node1.start).isEqual(node2.start) && (node1.end).isEqual(node2.end); -} \ No newline at end of file +} + +function dirExists(dirPath: string): boolean { + try { + + return fs.statSync(dirPath).isDirectory(); + } catch (e) { + return false; + } +}