Combine custom snippets from basesyntax

This commit is contained in:
Ramya Achutha Rao 2017-07-19 12:53:52 -07:00
Родитель 5352e18602
Коммит d9514ac1b9
1 изменённых файлов: 64 добавлений и 48 удалений

Просмотреть файл

@ -32,7 +32,7 @@ export function doComplete(document: TextDocument, position: Position, syntax: s
} }
if (!snippetKeyCache.has('html')) { if (!snippetKeyCache.has('html')) {
let registry = customSnippetRegistry[syntax] ? customSnippetRegistry[syntax] : createSnippetsRegistry('html'); let registry = customSnippetRegistry['html'] ? customSnippetRegistry['html'] : createSnippetsRegistry('html');
htmlSnippetKeys = registry.all({ type: 'string' }).map(snippet => { htmlSnippetKeys = registry.all({ type: 'string' }).map(snippet => {
return snippet.key; return snippet.key;
}); });
@ -96,18 +96,11 @@ export function doComplete(document: TextDocument, position: Position, syntax: s
} }
function getAbbreviationSuggestions(syntax: string, prefix: string, abbreviation: string, abbreviationRange: Range, expandOptions: object): CompletionItem[] { function getAbbreviationSuggestions(syntax: string, prefix: string, abbreviation: string, abbreviationRange: Range, expandOptions: object): CompletionItem[] {
if (!prefix) { if (!prefix || isStyleSheet(syntax)) {
return []; return [];
} }
if (!snippetKeyCache.has(syntax)) {
let registry = customSnippetRegistry[syntax] ? customSnippetRegistry[syntax] : createSnippetsRegistry(syntax);
let snippetKeys: string[] = registry.all({ type: 'string' }).map(snippet => {
return snippet.key;
});
snippetKeyCache.set(syntax, snippetKeys);
}
let snippetKeys = snippetKeyCache.get(syntax); let snippetKeys = snippetKeyCache.has(syntax) ? snippetKeyCache.get(syntax) : snippetKeyCache.get('html');
let snippetCompletions = []; let snippetCompletions = [];
snippetKeys.forEach(snippetKey => { snippetKeys.forEach(snippetKey => {
if (!snippetKey.startsWith(prefix) || snippetKey === prefix) { if (!snippetKey.startsWith(prefix) || snippetKey === prefix) {
@ -177,7 +170,6 @@ function getCurrentLine(document: TextDocument, position: Position): string {
let customSnippetRegistry = {}; let customSnippetRegistry = {};
let variablesFromFile = {}; let variablesFromFile = {};
let profilesFromFile = {}; let profilesFromFile = {};
let emmetExtensionsPath = '';
const field = (index, placeholder) => `\${${index}${placeholder ? ':' + placeholder : ''}}`; const field = (index, placeholder) => `\${${index}${placeholder ? ':' + placeholder : ''}}`;
@ -221,6 +213,11 @@ export function isAbbreviationValid(syntax: string, abbreviation: string): boole
* @param textToReplace * @param textToReplace
*/ */
export function getExpandOptions(syntaxProfiles: object, variables: object, syntax: string, textToReplace?: string) { export function getExpandOptions(syntaxProfiles: object, variables: object, syntax: string, textToReplace?: string) {
let baseSyntax = isStyleSheet(syntax) ? 'css' : 'html';
if (!customSnippetRegistry[syntax] && customSnippetRegistry[baseSyntax]) {
customSnippetRegistry[syntax] = customSnippetRegistry[baseSyntax];
}
return { return {
field: field, field: field,
syntax: syntax, syntax: syntax,
@ -290,21 +287,26 @@ function getProfile(syntax: string, profilesFromSettings: object): any {
* Returns variables to be used while expanding snippets * Returns variables to be used while expanding snippets
*/ */
function getVariables(variablesFromSettings: object): any { function getVariables(variablesFromSettings: object): any {
if (!variablesFromSettings) {
return variablesFromFile;
}
return Object.assign({}, variablesFromFile, variablesFromSettings); return Object.assign({}, variablesFromFile, variablesFromSettings);
} }
/** /**
* Updates customizations from snippets.json and syntaxProfiles.json files in the directory configured in emmet.extensionsPath setting * Updates customizations from snippets.json and syntaxProfiles.json files in the directory configured in emmet.extensionsPath setting
*/ */
export function updateExtensionsPath(currentEmmetExtensionsPath: string) { export function updateExtensionsPath(emmetExtensionsPath: string) {
if (emmetExtensionsPath !== currentEmmetExtensionsPath) { if (!emmetExtensionsPath || !emmetExtensionsPath.trim() || !path.isAbsolute(emmetExtensionsPath.trim()) || !dirExists(emmetExtensionsPath.trim())) {
emmetExtensionsPath = currentEmmetExtensionsPath; customSnippetRegistry = {};
snippetKeyCache.clear();
return;
}
if (emmetExtensionsPath && emmetExtensionsPath.trim() && path.isAbsolute(emmetExtensionsPath.trim())) {
let dirPath = emmetExtensionsPath.trim(); let dirPath = emmetExtensionsPath.trim();
let snippetsPath = path.join(dirPath, 'snippets.json'); let snippetsPath = path.join(dirPath, 'snippets.json');
let profilesPath = path.join(dirPath, 'syntaxProfiles.json'); let profilesPath = path.join(dirPath, 'syntaxProfiles.json');
if (dirExists(dirPath)) {
fs.readFile(snippetsPath, (err, snippetsData) => { fs.readFile(snippetsPath, (err, snippetsData) => {
if (err) { if (err) {
return; return;
@ -312,10 +314,24 @@ export function updateExtensionsPath(currentEmmetExtensionsPath: string) {
try { try {
let snippetsJson = JSON.parse(snippetsData.toString()); let snippetsJson = JSON.parse(snippetsData.toString());
variablesFromFile = snippetsJson['variables']; variablesFromFile = snippetsJson['variables'];
customSnippetRegistry = {};
snippetKeyCache.clear();
Object.keys(snippetsJson).forEach(syntax => { Object.keys(snippetsJson).forEach(syntax => {
if (snippetsJson[syntax]['snippets']) { if (!snippetsJson[syntax]['snippets']) {
customSnippetRegistry[syntax] = createSnippetsRegistry(syntax, snippetsJson[syntax]['snippets']); return;
} }
let baseSyntax = isStyleSheet(syntax) ? 'css' : 'html';
let customSnippets = snippetsJson[syntax]['snippets'];
if (snippetsJson[baseSyntax]['snippets'] && baseSyntax !== syntax) {
customSnippets = Object.assign({}, snippetsJson[baseSyntax]['snippets'], snippetsJson[syntax]['snippets'])
}
customSnippetRegistry[syntax] = createSnippetsRegistry(syntax, customSnippets);
let snippetKeys: string[] = customSnippetRegistry[syntax].all({ type: 'string' }).map(snippet => {
return snippet.key;
});
snippetKeyCache.set(syntax, snippetKeys);
}); });
} catch (e) { } catch (e) {
@ -331,9 +347,9 @@ export function updateExtensionsPath(currentEmmetExtensionsPath: string) {
} }
}); });
}
}
}
} }
function dirExists(dirPath: string): boolean { function dirExists(dirPath: string): boolean {