-
Notifications
You must be signed in to change notification settings - Fork 3.8k
QuickInput : Add an example of prompt with history #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AdrieanKhisbe
wants to merge
9
commits into
microsoft:main
Choose a base branch
from
AdrieanKhisbe:prototype-prompt-with-history
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
368ec49
Prototype a prompt where you can accept current input
AdrieanKhisbe 50e3fc4
Refactor factorizing commands items and relying on built in filtering
AdrieanKhisbe bfcdd4c
Retrieve commands suggestions from history file
AdrieanKhisbe c41231a
Record new item in file history
AdrieanKhisbe 5fd3155
Only save history file when needed (file opened and no leading space)
AdrieanKhisbe fb4b0b9
Refactor and typing to make the compiler happy
AdrieanKhisbe 9bb3238
Applied review recommendations and drop lodash
AdrieanKhisbe aace6b6
Refactor taking into account feedbacks from reviews
AdrieanKhisbe cf3fe7d
Fix the lint, and further refactor
AdrieanKhisbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { window, Disposable, Memento } from 'vscode'; | ||
import { QuickPickItem } from 'vscode'; | ||
|
||
export const historyPath = `${process.env.HOME}/.vscode-cmd-history`; | ||
|
||
const HISTORY_KEY = 'HISTORY_KEY'; | ||
|
||
/** | ||
* A command prompt with history | ||
* | ||
*/ | ||
export const getPromptCommand = (memo: Memento) => { | ||
const pickCommand = getPickCommand(memo); | ||
// §TODO: cache promptCommand! | ||
return async function promptCommand() { | ||
const command = await pickCommand(); | ||
if (command) { | ||
window.showInformationMessage(`You picked the following command: '${command}'`) | ||
AdrieanKhisbe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
abstract class CommandItem implements QuickPickItem { | ||
public label: string; | ||
public description?: string; | ||
public abstract type: string; | ||
AdrieanKhisbe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
constructor(label: string, description?: string) { | ||
this.label = label; | ||
this.description = description; | ||
} | ||
} | ||
class HistoryItem extends CommandItem { | ||
public type: string; | ||
constructor(label: string, description?: string) { | ||
super(label, description); | ||
this.type = 'history' | ||
} | ||
} | ||
class InputItem extends CommandItem { | ||
public type: string; | ||
constructor(public label: string) { | ||
super(label, '(current input)'); | ||
this.type = 'input'; | ||
}; | ||
} | ||
|
||
const getPickCommand = (memo: Memento) => async function pickCommand() { | ||
const disposables: Disposable[] = []; | ||
try { | ||
return await new Promise<string | undefined>((resolve, reject) => { | ||
const input = window.createQuickPick<CommandItem>(); | ||
input.placeholder = 'Type a command'; | ||
input.items = []; | ||
|
||
const updateQuickPick = (value?: string): void => { | ||
if (value === input.value) return; | ||
if (!value) { | ||
AdrieanKhisbe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (input.items[0] && input.items[0].type === 'input') | ||
input.items = input.items.slice(1); | ||
return; | ||
} | ||
if (input.items[0] && input.items[0].type === 'input') { | ||
AdrieanKhisbe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
input.items = [new InputItem(value)].concat(input.items.slice(1)); | ||
} else { | ||
input.items = [new InputItem(value)].concat(input.items); | ||
} | ||
// §todo: add autocomplete suggestions | ||
} | ||
|
||
disposables.push( | ||
input.onDidChangeValue(updateQuickPick), | ||
input.onDidChangeSelection((items: CommandItem[]) => { | ||
const item = items[0]; | ||
if (item instanceof HistoryItem) { | ||
resolve(item.label); | ||
input.hide(); | ||
// do not record new input in history | ||
// §todo : maybe reorder | ||
} else if (item instanceof InputItem) { | ||
resolve(item.label); | ||
input.hide(); | ||
// record new input in history | ||
if (!item.label.startsWith(' ')) { | ||
chrmarti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const currentHistory: string[] = memo.get(HISTORY_KEY, []); | ||
currentHistory.unshift(item.label); | ||
memo.update(HISTORY_KEY, currentHistory); | ||
} | ||
} | ||
}), | ||
input.onDidHide(() => { | ||
resolve(undefined); | ||
input.dispose(); | ||
}) | ||
); | ||
input.show(); | ||
const historyItems: HistoryItem[] = memo.get(HISTORY_KEY, []).map( | ||
(cmd: string, index: number) => new HistoryItem(cmd, `(history item ${index})`) | ||
); | ||
input.items = input.items.concat(historyItems) | ||
}); | ||
} finally { | ||
disposables.forEach(d => d.dispose()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.