Skip to content

Commit

Permalink
Honor emmet.extensionsPath setting for variables and profile
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Jun 20, 2017
1 parent 9100807 commit ac60143
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
8 changes: 1 addition & 7 deletions extensions/emmet/package.json
Expand Up @@ -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"
}
}
Expand Down
4 changes: 2 additions & 2 deletions extensions/emmet/src/abbreviationActions.ts
Expand Up @@ -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 : ''}}`;
Expand Down Expand Up @@ -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 : ''
};
}
5 changes: 5 additions & 0 deletions extensions/emmet/src/extension.ts
Expand Up @@ -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': ['!', '.', '}'],
Expand Down Expand Up @@ -109,6 +110,10 @@ export function activate(context: vscode.ExtensionContext) {
fetchSelectItem('prev');
}));

updateExtensionsPath();
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => {
updateExtensionsPath();
}));
}

export function deactivate() {
Expand Down
62 changes: 59 additions & 3 deletions extensions/emmet/src/util.ts
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -259,4 +306,13 @@ export function sameNodes(node1: Node, node2: Node): boolean {
return false;
}
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;
}
}

0 comments on commit ac60143

Please sign in to comment.