Skip to content

Commit

Permalink
Refactor and typing to make the compiler happy
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrieanKhisbe committed Oct 11, 2019
1 parent 5fd3155 commit fb4b0b9
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions quickinput-sample/src/promptCommandWithHistory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import { Uri, window, Disposable } from 'vscode';
import { window, Disposable } from 'vscode';
import { QuickPickItem } from 'vscode';
import { workspace } from 'vscode';
import * as _ from 'lodash';

export const historyPath = `${process.env.HOME}/.vscode-cmd-history`;
Expand All @@ -18,23 +16,29 @@ export async function promptCommand() {
}
}

class CommandItem implements QuickPickItem { }
class CommandItem implements QuickPickItem {
public label: string;
public description?: string;
constructor(label: string, description?: string) {
this.label = label;
this.description = description;
}
}
class HistoryItem extends CommandItem {
constructor(public label: string, public description: string?) {
super();
constructor(label: string, description?: string) {
super(label, description);
}
}
class InputItem extends CommandItem {
public description = '(current input)';
constructor(public label: string) {
super();
super(label, '(current input)');
};
}

async function pickCommand() {
const disposables: Disposable[] = [];
let commandsItems = [];
let currentValue = undefined;
let commandsItems: CommandItem[] = [];
let currentValue: string | undefined = undefined;
let historyShouldBeUpdated = false;

try {
Expand All @@ -43,7 +47,7 @@ async function pickCommand() {
input.placeholder = 'Type a command';
input.items = commandsItems;

const updateQuickPick = value => {
const updateQuickPick = (value?: string): void => {
if (!value) {
input.items = commandsItems;
return;
Expand All @@ -57,11 +61,11 @@ async function pickCommand() {
}

disposables.push(
input.onDidChangeValue(value => {
input.onDidChangeValue((value?: string) => {
currentValue = value;
updateQuickPick(value);
}),
input.onDidChangeSelection(items => {
input.onDidChangeSelection((items: CommandItem[]) => {
const item = items[0];
if (item instanceof HistoryItem) {
resolve(item.label);
Expand Down Expand Up @@ -92,7 +96,7 @@ async function pickCommand() {
}
historyShouldBeUpdated = true;
const commands = content.toString().trimRight().split('\n').reverse();
commandsItems = _.map(commands, (cmd, index) => new HistoryItem(cmd, `(history item ${index})`));
commandsItems = _.map(commands, (cmd: string, index: number) => new HistoryItem(cmd, `(history item ${index})`));
updateQuickPick(currentValue);
});
} else {
Expand Down

0 comments on commit fb4b0b9

Please sign in to comment.