Skip to content

Commit

Permalink
configure snippets offers to create workspace snippets, #8102
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Sep 21, 2018
1 parent 94e1143 commit 1459e9a
Showing 1 changed file with 32 additions and 16 deletions.
Expand Up @@ -6,7 +6,6 @@

import * as nls from 'vs/nls';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { writeFile, exists } from 'vs/base/node/pfs';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IModeService } from 'vs/editor/common/services/modeService';
import { IWindowService } from 'vs/platform/windows/common/windows';
Expand All @@ -18,6 +17,9 @@ import { URI } from 'vs/base/common/uri';
import { ISnippetsService } from 'vs/workbench/parts/snippets/electron-browser/snippets.contribution';
import { values } from 'vs/base/common/map';
import { IQuickPickItem, IQuickInputService, QuickPickInput } from 'vs/platform/quickinput/common/quickInput';
import { SnippetSource } from 'vs/workbench/parts/snippets/electron-browser/snippetsFile';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IFileService } from 'vs/platform/files/common/files';

const id = 'workbench.action.openSnippets';

Expand All @@ -41,7 +43,7 @@ async function computePicks(snippetService: ISnippetsService, envService: IEnvir

for (const file of await snippetService.getSnippetFiles()) {

if (!file.isUserSnippets) {
if (file.source === SnippetSource.Extension) {
// skip extension snippets
continue;
}
Expand Down Expand Up @@ -116,19 +118,20 @@ async function computePicks(snippetService: ISnippetsService, envService: IEnvir
return { existing, future };
}

async function createGlobalSnippetFile(envService: IEnvironmentService, windowService: IWindowService, opener: IOpenerService) {
async function createGlobalSnippetFile(defaultPath: URI, windowService: IWindowService, fileService: IFileService, opener: IOpenerService) {

await fileService.createFolder(defaultPath);
await timeout(100); // ensure quick pick closes...

const defaultPath = join(envService.appSettingsHome, 'snippets');
const path = await windowService.showSaveDialog({
defaultPath,
defaultPath: defaultPath.fsPath,
filters: [{ name: 'Code Snippets', extensions: ['code-snippets'] }]
});
if (!path || dirname(path) !== defaultPath) {
if (!path || dirname(path) !== defaultPath.fsPath) {
return undefined;
}
await writeFile(path, [

await fileService.updateContent(URI.file(path), [
'{',
'\t// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and ',
'\t// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope ',
Expand All @@ -152,8 +155,8 @@ async function createGlobalSnippetFile(envService: IEnvironmentService, windowSe
await opener.open(URI.file(path));
}

async function createLanguageSnippetFile(pick: ISnippetPick) {
if (await exists(pick.filepath)) {
async function createLanguageSnippetFile(pick: ISnippetPick, fileService: IFileService) {
if (await fileService.existsFile(URI.file(pick.filepath))) {
return;
}
const contents = [
Expand All @@ -173,7 +176,7 @@ async function createLanguageSnippetFile(pick: ISnippetPick) {
'\t// }',
'}'
].join('\n');
await writeFile(pick.filepath, contents);
await fileService.updateContent(URI.file(pick.filepath), contents);
}

CommandsRegistry.registerCommand(id, async accessor => {
Expand All @@ -184,28 +187,41 @@ CommandsRegistry.registerCommand(id, async accessor => {
const windowService = accessor.get(IWindowService);
const modeService = accessor.get(IModeService);
const envService = accessor.get(IEnvironmentService);
const workspaceService = accessor.get(IWorkspaceContextService);
const fileService = accessor.get(IFileService);

const picks = await computePicks(snippetService, envService, modeService);
const existing: QuickPickInput[] = picks.existing;
const newGlobalPick = <IQuickPickItem>{ label: nls.localize('new.global', "New Global Snippets file...") };

type GlobalSnippetPick = IQuickPickItem & { uri: URI };
const globalSnippetPicks: GlobalSnippetPick[] = [{
label: nls.localize('new.global', "New Global Snippets file..."),
uri: URI.file(join(envService.appSettingsHome, 'snippets'))
}];
for (const folder of workspaceService.getWorkspace().folders) {
globalSnippetPicks.push({
label: nls.localize('new.folder', "New Snippets file for '{0}'...", folder.name),
uri: folder.toResource('.vscode')
});
}

if (existing.length > 0) {
existing.unshift({ type: 'separator', label: nls.localize('group.global', "Existing Snippets") });
existing.push({ type: 'separator', label: nls.localize('new.global.sep', "New Snippets") });
} else {
existing.push({ type: 'separator', label: nls.localize('new.global.sep', "New Snippets") });
}

const pick = await quickInputService.pick(<(IQuickPickItem | ISnippetPick)[]>[].concat(existing, newGlobalPick, picks.future), {
const pick = await quickInputService.pick(<(IQuickPickItem | ISnippetPick | GlobalSnippetPick)[]>[].concat(existing, globalSnippetPicks, picks.future), {
placeHolder: nls.localize('openSnippet.pickLanguage', "Select Snippets File or Create Snippets"),
matchOnDescription: true
});

if (pick === newGlobalPick) {
return createGlobalSnippetFile(envService, windowService, opener);

if (globalSnippetPicks.indexOf(pick as GlobalSnippetPick) >= 0) {
return createGlobalSnippetFile((pick as GlobalSnippetPick).uri, windowService, fileService, opener);
} else if (ISnippetPick.is(pick)) {
if (pick.hint) {
await createLanguageSnippetFile(pick);
await createLanguageSnippetFile(pick, fileService);
}
return opener.open(URI.file(pick.filepath));
}
Expand Down

0 comments on commit 1459e9a

Please sign in to comment.