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 Aug 29, 2018
1 parent 253d2f4 commit c4978ea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
6 changes: 6 additions & 0 deletions quickinput-sample/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions quickinput-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"postinstall": "node ./node_modules/vscode/bin/install"
},
"devDependencies": {
"@types/lodash": "^4.14.116",
"@types/node": "10.3.6",
"tslint": "5.10.0",
"typescript": "2.9.2",
Expand Down
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 c4978ea

Please sign in to comment.