From eba593f4442948b5288c1a3e17bd4b928b0ca4de Mon Sep 17 00:00:00 2001 From: RandomFractals Date: Sun, 24 Apr 2022 12:28:27 -0500 Subject: [PATCH] use new constants for extension views, ids, and commands (#79) --- src/commands.ts | 43 ++++++++++++++++++++++++++----------------- src/config.ts | 8 +++++--- src/constants.ts | 31 +++++++++++++++++++++++++++++++ src/extension.ts | 4 +++- 4 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 src/constants.ts diff --git a/src/commands.ts b/src/commands.ts index 13077df..dc38232 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -11,6 +11,7 @@ import { } from 'vscode'; import * as config from './config'; +import * as constants from './constants'; import { SnippetFile, @@ -27,51 +28,53 @@ 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'); @@ -79,15 +82,18 @@ export function registerCommands(context: ExtensionContext, snippetProvider: Sni 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; @@ -95,9 +101,12 @@ export function registerCommands(context: ExtensionContext, snippetProvider: Sni 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); } }); @@ -134,13 +143,13 @@ async function getSymbols(document: TextDocument): Promise { return new Promise(async (resolve, reject) => { // get document symbols via built-in vscode document symbol provider let symbols: DocumentSymbol[] = await commands.executeCommand( - '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( - 'vscode.executeDocumentSymbolProvider', document.uri);// tslint:disable-line + constants.VSCodeExecuteDocumentSymbolProvider, document.uri); return resolve(symbols || []); }, 1200); } diff --git a/src/config.ts b/src/config.ts index 166ec80..56797fe 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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); } /** @@ -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); } @@ -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); } diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..d58ace4 --- /dev/null +++ b/src/constants.ts @@ -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'; diff --git a/src/extension.ts b/src/extension.ts index c3753d7..c259167 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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'; @@ -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 });