Skip to content

Commit

Permalink
Refactor: move registerCommands to core/commands
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisam committed Oct 25, 2024
1 parent d902e2d commit fd69fd2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 51 deletions.
97 changes: 97 additions & 0 deletions src/core/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import * as vscode from 'vscode';

import type { DirectoryProvider } from '../provider/DirectoryProvider';
import type { IConfiguration } from '../types/Configuration';
import type { FileSystemObject } from '../types/FileSystemObject';
import { EXTENSION_NAME } from './constants';
import { clearScope, scopeToThis } from './scope';

export enum vsCodeCommands {
Open = 'vscode.open',
Expand All @@ -15,3 +21,94 @@ export enum ExtensionCommands {
FocusInExplorer = `${EXTENSION_NAME}.focusInExplorer`,
ClearFocusScope = `${EXTENSION_NAME}.clearFocusScope`,
}

export function registerCommands(
context: vscode.ExtensionContext,
directoryProvider: DirectoryProvider,
config: IConfiguration,
configDirUri: vscode.Uri,
) {
context.subscriptions.push(
registerRefreshEntryCommand(directoryProvider),
registerOpenItemCommand(),
registerSelectItemCommand(directoryProvider),
registerRemoveItemCommand(directoryProvider),
registerRenameItemCommand(directoryProvider),
registerCantRemoveItemCommand(),
registerRemoveAllItemsCommand(directoryProvider),
registerFocusInExplorerCommand(config, configDirUri),
registerClearFocusScopeCommand(config, configDirUri),
);
}

function registerRefreshEntryCommand(directoryProvider: DirectoryProvider) {
return vscode.commands.registerCommand(ExtensionCommands.RefreshEntry, () =>
directoryProvider.refresh(),
);
}

function registerOpenItemCommand() {
return vscode.commands.registerCommand(ExtensionCommands.OpenItem, (file) => {
vscode.commands.executeCommand(
vsCodeCommands.Open,
vscode.Uri.parse(file.resourceUri.path),
);
});
}

function registerSelectItemCommand(directoryProvider: DirectoryProvider) {
return vscode.commands.registerCommand(
ExtensionCommands.SelectItem,
async (args) => await directoryProvider.selectItemAsync(vscode.Uri.parse(args.path)),
);
}

function registerRemoveItemCommand(directoryProvider: DirectoryProvider) {
return vscode.commands.registerCommand(ExtensionCommands.RemoveItem, async (args) => {
await directoryProvider.removeItemAsync(args.resourceUri);
});
}

function registerRenameItemCommand(directoryProvider: DirectoryProvider) {
return vscode.commands.registerCommand(ExtensionCommands.RenameItem, async (args) => {
await directoryProvider.renameItemAsync(args);
});
}

function registerCantRemoveItemCommand() {
return vscode.commands.registerCommand(ExtensionCommands.CantRemoveItem, () => {
vscode.window.showInformationMessage(
'You can only remove items that were directly added to the view',
);
});
}

function registerRemoveAllItemsCommand(directoryProvider: DirectoryProvider) {
return vscode.commands.registerCommand(
ExtensionCommands.RemoveAllItems,
async () => await directoryProvider.removeAllItemsAsync(),
);
}

function registerFocusInExplorerCommand(config: IConfiguration, configDirUri: vscode.Uri) {
return vscode.commands.registerCommand(
ExtensionCommands.FocusInExplorer,
async (element: FileSystemObject) => {
const path = element.resourceUri;
if (!path) {
vscode.window.showInformationMessage(
'Use this command from the Explorer context menu.',
);
return;
}
await clearScope(config, configDirUri);
await scopeToThis(path, config, configDirUri);
},
);
}

function registerClearFocusScopeCommand(config: IConfiguration, configDirUri: vscode.Uri) {
return vscode.commands.registerCommand(ExtensionCommands.ClearFocusScope, async () => {
await clearScope(config, configDirUri);
});
}
54 changes: 3 additions & 51 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as vscode from 'vscode';

import { ExtensionCommands, vsCodeCommands } from './core/commands';
import { registerCommands } from './core/commands';
import { REGISTER_TREE_DATA_PROVIDER } from './core/constants';
import { clearScope, initScope, scopeToThis } from './core/scope';
import { initScope } from './core/scope';
import { DirectoryOperator } from './operator/DirectoryOperator';
import { DirectoryProvider } from './provider/DirectoryProvider';
import type { FileSystemObject } from './types/FileSystemObject';
import { getConfigurationAsync, getConfigurationDirUri } from './utils/configUtils';

export async function activate(context: vscode.ExtensionContext) {
Expand All @@ -15,54 +14,7 @@ export async function activate(context: vscode.ExtensionContext) {
const directoryOperator = new DirectoryOperator(context, config, configDirUri);
const directoryProvider = new DirectoryProvider(directoryOperator);
vscode.window.registerTreeDataProvider(REGISTER_TREE_DATA_PROVIDER, directoryProvider);

context.subscriptions.push(
vscode.commands.registerCommand(ExtensionCommands.RefreshEntry, () =>
directoryProvider.refresh(),
),
vscode.commands.registerCommand(ExtensionCommands.OpenItem, (file) => {
vscode.commands.executeCommand(
vsCodeCommands.Open,
vscode.Uri.parse(file.resourceUri.path),
);
}),
vscode.commands.registerCommand(
ExtensionCommands.SelectItem,
async (args) => await directoryProvider.selectItemAsync(vscode.Uri.parse(args.path)),
),
vscode.commands.registerCommand(ExtensionCommands.RemoveItem, async (args) => {
await directoryProvider.removeItemAsync(args.resourceUri);
}),
vscode.commands.registerCommand(ExtensionCommands.RenameItem, async (args) => {
await directoryProvider.renameItemAsync(args);
}),
vscode.commands.registerCommand(ExtensionCommands.CantRemoveItem, () => {
vscode.window.showInformationMessage(
'You can only remove items that were directly added to the view',
);
}),
vscode.commands.registerCommand(
ExtensionCommands.RemoveAllItems,
async () => await directoryProvider.removeAllItemsAsync(),
),
vscode.commands.registerCommand(
ExtensionCommands.FocusInExplorer,
async (element: FileSystemObject) => {
const path = element.resourceUri;
if (!path) {
vscode.window.showInformationMessage(
'Use this command from the Explorer context menu.',
);
return;
}
await clearScope(config, configDirUri);
await scopeToThis(path, config, configDirUri);
},
),
vscode.commands.registerCommand(ExtensionCommands.ClearFocusScope, async () => {
await clearScope(config, configDirUri);
}),
);
registerCommands(context, directoryProvider, config, configDirUri);
}

export function deactivate() {}

0 comments on commit fd69fd2

Please sign in to comment.