Skip to content

Commit

Permalink
refactor: update some types
Browse files Browse the repository at this point in the history
  • Loading branch information
usernamehw committed Jul 20, 2022
1 parent beb4c12 commit 8f42a43
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 107 deletions.
4 changes: 2 additions & 2 deletions src/TreeViewProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command, Event, EventEmitter, MarkdownString, ThemeColor, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState } from 'vscode';
import { CommandIds } from './commands';
import { CommandId } from './commands';
import { $config } from './extension';
import { createFolderHoverText } from './folderHoverText';
import { CommandFolder, Runnable, TopLevelCommands } from './types';
Expand Down Expand Up @@ -138,7 +138,7 @@ export class CommandsTreeViewProvider implements TreeDataProvider<FolderTreeItem
result.push(new RunCommandTreeItem(
key,
{
command: CommandIds.Run,
command: CommandId.Run,
title: 'Run Command',
arguments: [runnable],
},
Expand Down
33 changes: 17 additions & 16 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CommandIds } from './commands';
import { CommandId } from './commands';
import { $config } from './extension';

/**
* Map commands with arguments;
*/
Expand Down Expand Up @@ -92,40 +93,40 @@ export const commandArgs: Record<string, unknown> = {
apply: 'first',
preferred: false,
},
[CommandIds.ToggleSetting]: {
[CommandId.ToggleSetting]: {
setting: '',
value: [],
},
[CommandIds.IncrementSetting]: {
[CommandId.IncrementSetting]: {
setting: '',
value: 1,
},
[CommandIds.ToggleTheme]: {
[CommandId.ToggleTheme]: {
dark: 'Default Dark+,Abyss',
light: 'Default Light+,Quiet Light',
},
[CommandIds.OpenFolder]: '',
[CommandIds.RunInTerminal]: {
[CommandId.OpenFolder]: '',
[CommandId.RunInTerminal]: {
text: '',
name: '',
reveal: true,
cwd: '',
},
[CommandIds.StartDebugging]: '',
[CommandIds.OpenExternal]: '',
[CommandIds.SetEditorLanguage]: '',
[CommandIds.ClipboardWrite]: '',
[CommandIds.RevealFileInOS]: '',
[CommandIds.ShowNotification]: {
[CommandId.StartDebugging]: '',
[CommandId.OpenExternal]: '',
[CommandId.SetEditorLanguage]: '',
[CommandId.ClipboardWrite]: '',
[CommandId.RevealFileInOS]: '',
[CommandId.ShowNotification]: {
message: '',
severity: 'error',
},
[CommandIds.ShowStatusBarNotification]: {
[CommandId.ShowStatusBarNotification]: {
message: '',
color: '',
timeout: 4000,
},
[CommandIds.Open]: {
[CommandId.Open]: {
target: '',
app: '',
arguments: [],
Expand All @@ -134,7 +135,7 @@ export const commandArgs: Record<string, unknown> = {
/**
* Add arguments if command can accept them (even if they are optional).
*/
export function addArgs(commandId: string) {
export function addArgs(commandId: string): { command: string; args?: unknown } {
if (commandId in commandArgs) {
return {
command: commandId,
Expand All @@ -154,6 +155,6 @@ export function addArgs(commandId: string) {
/**
* Return `true` if command accepts arguments.
*/
export function hasArgs(commandId: string) {
export function hasArgs(commandId: string): boolean {
return commandId in commandArgs || $config.alias[commandId] in commandArgs;
}
2 changes: 1 addition & 1 deletion src/commandPalette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const coreCommandIds = [
*
* This function updates `package.json` file to add items from `commands.commands` to Command Palette (but requires editor reload after changing configuration)
*/
export async function updateCommandPalette(items: TopLevelCommands, context: ExtensionContext) {
export async function updateCommandPalette(items: TopLevelCommands, context: ExtensionContext): Promise<void> {
unregisterCommandPalette();

if (!$config.populateCommandPalette) {
Expand Down
58 changes: 29 additions & 29 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { isWorkspaceCommandItem } from './workspaceCommands';
/**
* All command ids contributed by this extension.
*/
export const enum CommandIds {
export const enum CommandId {
// ──── Core ──────────────────────────────────────────────────
Run = 'commands.run',
Rerun = 'commands.rerun',
Expand Down Expand Up @@ -72,35 +72,35 @@ export const enum CommandIds {
*/
export function registerExtensionCommands() {
// ──── Core Commands ─────────────────────────────────────────
commands.registerCommand(CommandIds.Run, runCommand);
commands.registerCommand(CommandIds.Rerun, rerunCommand);
commands.registerCommand(CommandIds.SelectAndRun, selectAndRunCommand);
commands.registerCommand(CommandIds.RevealCommand, revealCommandCommand);
commands.registerCommand(CommandIds.RevealCommand2, revealCommand2Command);
commands.registerCommand(CommandIds.AssignKeybinding, assignKeybindingCommand);
commands.registerCommand(CommandIds.ToggleStatusBar, toggleStatusBarCommand);
commands.registerCommand(CommandIds.RevealCommandsInSettignsGUI, revealCommandsInSettingsGUICommand);
commands.registerCommand(CommandIds.OpenAsQuickPick, openAsQuickPickCommand);
commands.registerCommand(CommandIds.NewCommand, newCommandCommand);
commands.registerCommand(CommandIds.NewCommandInFolder, newCommandInFolderCommand);
commands.registerCommand(CommandIds.NewFolder, newFolderCommand);
commands.registerCommand(CommandIds.DeleteCommand, deleteCommandCommand);
commands.registerTextEditorCommand(CommandIds.SuggestCommands, suggestCommandsCommand);
commands.registerTextEditorCommand(CommandIds.EscapeCommandUriArgument, escapeCommandUriArgumentCommand);
commands.registerCommand(CommandId.Run, runCommand);
commands.registerCommand(CommandId.Rerun, rerunCommand);
commands.registerCommand(CommandId.SelectAndRun, selectAndRunCommand);
commands.registerCommand(CommandId.RevealCommand, revealCommandCommand);
commands.registerCommand(CommandId.RevealCommand2, revealCommand2Command);
commands.registerCommand(CommandId.AssignKeybinding, assignKeybindingCommand);
commands.registerCommand(CommandId.ToggleStatusBar, toggleStatusBarCommand);
commands.registerCommand(CommandId.RevealCommandsInSettignsGUI, revealCommandsInSettingsGUICommand);
commands.registerCommand(CommandId.OpenAsQuickPick, openAsQuickPickCommand);
commands.registerCommand(CommandId.NewCommand, newCommandCommand);
commands.registerCommand(CommandId.NewCommandInFolder, newCommandInFolderCommand);
commands.registerCommand(CommandId.NewFolder, newFolderCommand);
commands.registerCommand(CommandId.DeleteCommand, deleteCommandCommand);
commands.registerTextEditorCommand(CommandId.SuggestCommands, suggestCommandsCommand);
commands.registerTextEditorCommand(CommandId.EscapeCommandUriArgument, escapeCommandUriArgumentCommand);
// ──── Additional Commands ───────────────────────────────────
commands.registerCommand(CommandIds.ToggleSetting, toggleSettingCommand);
commands.registerCommand(CommandIds.IncrementSetting, incrementSettingCommand);
commands.registerCommand(CommandIds.ClipboardWrite, clipboardWriteCommand);
commands.registerCommand(CommandIds.SetEditorLanguage, setEditorLanguageCommand);
commands.registerCommand(CommandIds.OpenFolder, openFolderCommand);
commands.registerCommand(CommandIds.ShowNotification, showNotificationCommand);
commands.registerCommand(CommandIds.ShowStatusBarNotification, showStatusBarNotificationCommand);
commands.registerCommand(CommandIds.RunInTerminal, runInTerminalCommand);
commands.registerCommand(CommandIds.StartDebugging, startDebuggingCommand);
commands.registerCommand(CommandIds.ToggleTheme, toggleThemeCommand);
commands.registerCommand(CommandIds.OpenExternal, openExternalCommand);
commands.registerCommand(CommandIds.RevealFileInOS, revealFileInOSCommand);
commands.registerCommand(CommandIds.Open, openCommand);
commands.registerCommand(CommandId.ToggleSetting, toggleSettingCommand);
commands.registerCommand(CommandId.IncrementSetting, incrementSettingCommand);
commands.registerCommand(CommandId.ClipboardWrite, clipboardWriteCommand);
commands.registerCommand(CommandId.SetEditorLanguage, setEditorLanguageCommand);
commands.registerCommand(CommandId.OpenFolder, openFolderCommand);
commands.registerCommand(CommandId.ShowNotification, showNotificationCommand);
commands.registerCommand(CommandId.ShowStatusBarNotification, showStatusBarNotificationCommand);
commands.registerCommand(CommandId.RunInTerminal, runInTerminalCommand);
commands.registerCommand(CommandId.StartDebugging, startDebuggingCommand);
commands.registerCommand(CommandId.ToggleTheme, toggleThemeCommand);
commands.registerCommand(CommandId.OpenExternal, openExternalCommand);
commands.registerCommand(CommandId.RevealFileInOS, revealFileInOSCommand);
commands.registerCommand(CommandId.Open, openCommand);
}

export function applyForTreeItem(
Expand Down
3 changes: 1 addition & 2 deletions src/commands/incrementSettingCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { incrementSetting } from '../settings';
import { ToggleSettingType } from '../types';
import { incrementSetting, ToggleSettingType } from '../settings';
import { isSimpleObject } from '../utils';

export function incrementSettingCommand(arg: ToggleSettingType | string) {
Expand Down
8 changes: 7 additions & 1 deletion src/commands/showStatusBarNotificationCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { window } from 'vscode';
import { StatusBarNotification } from '../types';

export function showStatusBarNotificationCommand(arg: StatusBarNotification | string) {
if (typeof arg === 'string') {
Expand All @@ -23,3 +22,10 @@ function showTempStatusBarMessage(notification: StatusBarNotification) {
tempStatusBarMessage.dispose();
}, notification.timeout || 4000);
}

interface StatusBarNotification {
message: string;
color?: string;
timeout?: number;
priority?: number;// TODO: allow to specify priority, make default aligned to the right item
}
3 changes: 1 addition & 2 deletions src/commands/toggleSettingCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toggleSetting } from '../settings';
import { ToggleSettingType } from '../types';
import { toggleSetting, ToggleSettingType } from '../settings';

export async function toggleSettingCommand(arg: ToggleSettingType | string) {
await toggleSetting(arg);
Expand Down
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export const enum Constants {
CommandPaletteWasPopulatedStorageKey = 'was_populated',
}

/** extension config */
export let $config: ExtensionConfig;
/** extension state */
export class $state {
static lastExecutedCommand: Runnable = { command: 'noop' };
static extensionContext: ExtensionContext;
Expand Down Expand Up @@ -71,7 +69,9 @@ export async function activate(extensionContext: ExtensionContext) {
}));
}

/** Merge global and workspace commands */
/**
* Merge global and workspace commands.
*/
export function allCommands(workspaceId: string | undefined): TopLevelCommands {
const workspaceCommands = workspace.getConfiguration(Constants.ExtensionName).inspect('workspaceCommands')?.workspaceValue as ExtensionConfig['workspaceCommands'] | undefined;
if (workspaceId && workspaceCommands) {
Expand Down
2 changes: 1 addition & 1 deletion src/folderHoverText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TopLevelCommands } from './types';
/**
* Create tooltip content for folder that contains all of the nested items.
*/
export function createFolderHoverText(nestedItems: TopLevelCommands) {
export function createFolderHoverText(nestedItems: TopLevelCommands): MarkdownString {
const markdown = new MarkdownString(undefined, true);
markdown.isTrusted = true;
for (const key in nestedItems) {
Expand Down
6 changes: 3 additions & 3 deletions src/quickPick.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import { commands, extensions, QuickInputButton, QuickPickItem, ThemeIcon, window } from 'vscode';
import { hasArgs } from './args';
import { CommandIds } from './commands';
import { CommandId } from './commands';
import { $config, $state } from './extension';
import { run } from './run';
import { Runnable, TopLevelCommands } from './types';
Expand All @@ -11,7 +11,7 @@ import { isWorkspaceCommandItem } from './workspaceCommands';
/**
* Show quick pick with user commands. After picking one - run it.
*/
export async function showQuickPick(commandsForPicking: TopLevelCommands, isFolder = false) {
export async function showQuickPick(commandsForPicking: TopLevelCommands, isFolder = false): Promise<void> {
const treeAsOneLevelMap: Record<string, {
runnable: Runnable;
parentFolderName?: string;
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function showQuickPick(commandsForPicking: TopLevelCommands, isFold
});
quickPick.onDidTriggerButton(e => {
if (e.tooltip === newCommandButton.tooltip) {
commands.executeCommand(CommandIds.NewCommand);
commands.executeCommand(CommandId.NewCommand);
}
quickPick.hide();
quickPick.dispose();
Expand Down
2 changes: 1 addition & 1 deletion src/registerUserCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const registeredCommandsList: Disposable[] = [];
/**
* Register a command to be able to execute it from a keybinding.
*/
export async function updateUserCommands(items: TopLevelCommands) {
export async function updateUserCommands(items: TopLevelCommands): Promise<void> {
unregisterUserCommands();

const allCommands = await getAllVscodeCommands();
Expand Down
8 changes: 4 additions & 4 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isSimpleObject, sleep } from './utils';
* Execute runnable or folder.
* Executing a folder - is to show Quick Pick to choose one of the commands inside that folder.
*/
export async function run(runnable: CommandFolder & Runnable) {
export async function run(runnable: CommandFolder & Runnable): Promise<void> {
$state.lastExecutedCommand = runnable;
if (typeof runnable === 'string') {
const { command, args } = parseSimplifiedArgs(runnable);
Expand All @@ -36,7 +36,7 @@ export async function run(runnable: CommandFolder & Runnable) {
}
window.showErrorMessage(`Unknown command type ${JSON.stringify(runnable)}`);
}
async function runArray(arr: Sequence) {
async function runArray(arr: Sequence): Promise<void> {
for (const item of arr) {
if (typeof item === 'string') {
await runObject({
Expand All @@ -51,7 +51,7 @@ async function runArray(arr: Sequence) {
* `runObject()` must be used in all other `run...` functions because
* it applies `commands.alias` when needed.
*/
async function runObject(object: CommandObject) {
async function runObject(object: CommandObject): Promise<unknown> {
if (object.delay) {
await sleep(object.delay);
}
Expand Down Expand Up @@ -81,7 +81,7 @@ async function runObject(object: CommandObject) {
/**
* Run folder (show Quick pick with all commands inside that folder).
*/
function runFolder(folder: CommandFolder) {
function runFolder(folder: CommandFolder): void {
showQuickPick(folder.nestedItems!, true);
}
/**
Expand Down
17 changes: 12 additions & 5 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import isEqual from 'lodash/isEqual';
import { ConfigurationTarget, window, workspace } from 'vscode';
import { $config } from './extension';
import { ToggleSetting } from './types';
import { isSimpleObject } from './utils';
import isEqual from 'lodash/isEqual';

/**
* Type for `toggleSetting` command.
*/
export interface ToggleSettingType {
setting: string;
value: unknown[] | string;
}

/**
* Toggle global user setting.
*/
export async function toggleSetting(arg: ToggleSetting | string) {
export async function toggleSetting(arg: ToggleSettingType | string): Promise<void> {
const settings = workspace.getConfiguration(undefined, null);
let newValue;

Expand Down Expand Up @@ -47,7 +54,7 @@ export async function toggleSetting(arg: ToggleSetting | string) {
/**
* Increment global user setting. To decrement - just pass a negative number.
*/
export async function incrementSetting(settingName: unknown, n: unknown) {
export async function incrementSetting(settingName: unknown, n: unknown): Promise<void> {
if (typeof settingName !== 'string') {
window.showWarningMessage('Setting name must be a string');
return;
Expand All @@ -71,7 +78,7 @@ export async function incrementSetting(settingName: unknown, n: unknown) {
/**
* Update user setting with the new value.
*/
export async function updateSetting(settingName: string, newValue: unknown, target: 'global' | 'workspace') {
export async function updateSetting(settingName: string, newValue: unknown, target: 'global' | 'workspace'): Promise<void> {
const settings = workspace.getConfiguration(undefined, null);
const configurationTarget = target === 'workspace' ? ConfigurationTarget.Workspace : ConfigurationTarget.Global;
await settings.update(settingName, newValue, configurationTarget);
Expand Down
12 changes: 7 additions & 5 deletions src/statusBar.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Disposable, MarkdownString, StatusBarAlignment, ThemeColor, Uri, window } from 'vscode';
import { CommandIds } from './commands';
import { CommandId } from './commands';
import { createFolderHoverText } from './folderHoverText';
import { TopLevelCommands } from './types';
import { forEachCommand } from './utils';

const statusBarItems: Disposable[] = [];

/** Dispose and refresh all status bar items. */
export function updateStatusBarItems(items: TopLevelCommands) {
/**
* Dispose and refresh all status bar items.
*/
export function updateStatusBarItems(items: TopLevelCommands): void {
disposeStatusBarItems();

forEachCommand((item, key) => {
Expand Down Expand Up @@ -37,14 +39,14 @@ export function updateStatusBarItems(items: TopLevelCommands) {
label: key,
}];
const revealCommandUri = Uri.parse(
`command:commands.revealCommand2?${encodeURIComponent(JSON.stringify(args))}`,
`command:${CommandId.RevealCommand2}?${encodeURIComponent(JSON.stringify(args))}`,
);
mdTooltip.appendMarkdown(`\n\n---\n\n[Reveal in settings.json](${revealCommandUri})`);
newStatusBarItem.tooltip = mdTooltip;

newStatusBarItem.text = icon + (statusBarUserObject.text || '');
newStatusBarItem.command = {
command: CommandIds.Run,
command: CommandId.Run,
title: 'Run Command',
arguments: [item],
};
Expand Down
Loading

0 comments on commit 8f42a43

Please sign in to comment.