Skip to content

Commit

Permalink
use new constants for extension views, ids, and commands (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Apr 24, 2022
1 parent 48bee94 commit eba593f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
43 changes: 26 additions & 17 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'vscode';

import * as config from './config';
import * as constants from './constants';

import {
SnippetFile,
Expand All @@ -27,77 +28,85 @@ import {SnippetTreeDataProvider} from './snippets/snippetTreeDataProvider';
*/
export function registerCommands(context: ExtensionContext, snippetProvider: SnippetTreeDataProvider) {
context.subscriptions.push(
commands.registerCommand(`snippets.viewer.refreshSnippets`, () => snippetProvider.refresh())
commands.registerCommand(constants.RefreshSnippetsCommand, () => snippetProvider.refresh())
);

context.subscriptions.push(
commands.registerCommand(`snippets.viewer.combineLanguageSnippets`, () => {
commands.registerCommand(constants.CombineLanguageSnippetsCommand, () => {
snippetProvider.combineLanguageSnippets = true;
config.updateGlobalSetting('combineLanguageSnippets', true);
})
);

context.subscriptions.push(
commands.registerCommand(`snippets.viewer.groupSnippetsByFile`, () => {
commands.registerCommand(constants.GroupSnippetsByFileCommand, () => {
snippetProvider.combineLanguageSnippets = false;
config.updateGlobalSetting('combineLanguageSnippets', false);
})
);

context.subscriptions.push(
commands.registerCommand(`snippets.viewer.sortSnippetsByName`, () => {
commands.registerCommand(constants.SortSnippetsByNameCommand, () => {
snippetProvider.sortSnippetsByName = true;
config.updateGlobalSetting('sortSnippetsByName', true);
})
);

context.subscriptions.push(
commands.registerCommand(`snippets.viewer.sortSnippetsByDefinitionOrder`, () => {
commands.registerCommand(constants.SortSnippetsByDefinitionOrderCommand, () => {
snippetProvider.sortSnippetsByName = false;
config.updateGlobalSetting('sortSnippetsByName', false);
})
);

context.subscriptions.push(
commands.registerCommand(`snippets.viewer.skipLanguageSnippets`, () => {
commands.executeCommand('workbench.action.openSettings', 'snippets.viewer.skipLanguageSnippets');
commands.registerCommand(constants.SkipLanguageSnippetsCommand, () => {
commands.executeCommand(constants.WorkbenchActionOpenSettings, 'snippets.viewer.skipLanguageSnippets');
})
);

context.subscriptions.push(
commands.registerCommand(`snippets.viewer.viewSettings`, () => {
commands.executeCommand('workbench.action.openSettings', 'snippets.viewer');
commands.registerCommand(constants.ViewSettingsCommand, () => {
// show snippets viewer settings
commands.executeCommand(constants.WorkbenchActionOpenSettings, constants.ExtensionId);
})
);

context.subscriptions.push(
commands.registerCommand(`snippets.viewer.insertSnippet`, (snippet: Snippet) => {
commands.registerCommand(constants.InsertSnippetCommand, (snippet: Snippet) => {
// construct snippet code body
let snippetBody: string;
if (Array.isArray(snippet.body)) {
snippetBody = snippet.body.join('\n');
}
else {
snippetBody = snippet.body;
}
commands.executeCommand('editor.action.insertSnippet', {
snippet: snippetBody,
});
commands.executeCommand('workbench.action.focusActiveEditorGroup');

// insert snippet in active text editor
commands.executeCommand(constants.EditorActionInsertSnippet, {snippet: snippetBody});

// release focus from snippets tree view to active text editor
commands.executeCommand(constants.WorkbenchActionFocusActiveEditorGroup);
})
);

context.subscriptions.push(
commands.registerCommand(`snippets.viewer.openSnippetFile`, (snippet: SnippetFile | Snippet) => {
commands.registerCommand(constants.OpenSnippetFileCommand, (snippet: SnippetFile | Snippet) => {
// determine snippets file path
let filePath: string;
if (snippet instanceof Snippet) {
filePath = snippet.snippetFile.filePath;
}
else {
filePath = snippet.filePath;
}

// open snippets file
workspace.openTextDocument(Uri.file(filePath)).then((document) => {
window.showTextDocument(document).then(() => {
if (snippet instanceof Snippet) {
// scroll to requested snippet symbol name
goToSymbol(document, snippet.name);
}
});
Expand Down Expand Up @@ -134,13 +143,13 @@ async function getSymbols(document: TextDocument): Promise<DocumentSymbol[]> {
return new Promise(async (resolve, reject) => {
// get document symbols via built-in vscode document symbol provider
let symbols: DocumentSymbol[] = await commands.executeCommand<DocumentSymbol[]>(
'vscode.executeDocumentSymbolProvider', document.uri);// tslint:disable-line
constants.VSCodeExecuteDocumentSymbolProvider, document.uri);

if (!symbols || symbols.length === 0) {
// retry getting document symbols with a timeout
setTimeout(async () => {
symbols = await commands.executeCommand<DocumentSymbol[]>(
'vscode.executeDocumentSymbolProvider', document.uri);// tslint:disable-line
constants.VSCodeExecuteDocumentSymbolProvider, document.uri);
return resolve(symbols || []);
}, 1200);
}
Expand Down
8 changes: 5 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
workspace
} from 'vscode';

import * as constants from './constants';

/**
* Gets snippets viewer configuration settings.
*
* @returns Worskpace configuration for the snippets viewer.
*/
export function getConfiguration(): WorkspaceConfiguration {
return workspace.getConfiguration('snippets.viewer');
return workspace.getConfiguration(constants.ExtensionId);
}

/**
Expand Down Expand Up @@ -94,7 +96,7 @@ export function skipLanguages(): string[] {
* @param value New snippets viewer setting value.
*/
export function updateWorkspaceSetting(name: string, value: boolean) {
const settings = workspace.getConfiguration('snippets.viewer', null);
const settings = workspace.getConfiguration(constants.ExtensionId, null);
settings.update(name, value, ConfigurationTarget.Workspace);
}

Expand All @@ -105,6 +107,6 @@ export function updateWorkspaceSetting(name: string, value: boolean) {
* @param value New snippets viewer setting value.
*/
export function updateGlobalSetting(name: string, value: boolean) {
const settings = workspace.getConfiguration('snippets.viewer', null);
const settings = workspace.getConfiguration(constants.ExtensionId, null);
settings.update(name, value, ConfigurationTarget.Global);
}
31 changes: 31 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable @typescript-eslint/naming-convention */

/**
* Snippet viewer extensions constants
*/

// Extension constants
export const PublisherId: string = 'RandomFractalsInc';
export const ExtensionName: string = 'vscode-snippets-viewer';
export const ExtensionId: string = 'snippets.viewer';
export const ExtensionDisplayName: string = 'Snippets Viewer';

// View constants
export const SnippetsView: string = 'snippets.view';

// Snippets Viewer command constants
export const RefreshSnippetsCommand: string = `${ExtensionId}.refreshSnippets`;
export const CombineLanguageSnippetsCommand: string = `${ExtensionId}.combineLanguageSnippets`;
export const GroupSnippetsByFileCommand: string = `${ExtensionId}.groupSnippetsByFile`;
export const SortSnippetsByNameCommand: string = `${ExtensionId}.sortSnippetsByName`;
export const SortSnippetsByDefinitionOrderCommand: string = `${ExtensionId}.sortSnippetsByDefinitionOrder`;
export const SkipLanguageSnippetsCommand: string = `${ExtensionId}.skipLanguageSnippets`;
export const ViewSettingsCommand: string = `${ExtensionId}.viewSettings`;
export const InsertSnippetCommand: string = `${ExtensionId}.insertSnippet`;
export const OpenSnippetFileCommand: string = `${ExtensionId}.openSnippetFile`;

// VSCode action/command constants
export const VSCodeExecuteDocumentSymbolProvider: string = 'vscode.executeDocumentSymbolProvider';
export const WorkbenchActionFocusActiveEditorGroup: string = 'workbench.action.focusActiveEditorGroup';
export const WorkbenchActionOpenSettings: string = 'workbench.action.openSettings';
export const EditorActionInsertSnippet: string = 'editor.action.insertSnippet';
4 changes: 3 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
} from 'vscode';

import * as config from './config';
import * as constants from './constants';

import { registerCommands } from './commands';
import { SnippetLoader } from './snippets/snippetLoader';
import { SnippetLanguage } from './snippets/snippets';
Expand All @@ -22,7 +24,7 @@ export function activate(context: ExtensionContext) {
// create snippets tree view
const snippetLoader: SnippetLoader = new SnippetLoader(context);
const snippetProvider: SnippetTreeDataProvider = new SnippetTreeDataProvider(snippetLoader);
const snippetView = window.createTreeView('snippets.view', {
const snippetView = window.createTreeView(constants.SnippetsView, {
treeDataProvider: snippetProvider,
showCollapseAll: true
});
Expand Down

0 comments on commit eba593f

Please sign in to comment.