Skip to content

Commit

Permalink
Add history keybindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed Aug 21, 2017
1 parent 05dc012 commit bde209f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/monaco/ShellHistoryLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {services} from "../services/index";
import * as _ from "lodash";

monaco.languages.setMonarchTokensProvider("shell", {
tokenizer: {
root: [
{
regex: /.+/,
action: {token: "history-item"},
},
],
},
tokenPostfix: ".shell-history",
});

monaco.languages.register({
id: "shell-history",
});

monaco.languages.registerCompletionItemProvider("shell-history", {
triggerCharacters: [" ", "/"],
provideCompletionItems: () => {
return {
isIncomplete: false,
items: _.uniq(services.history.all.map(record => record.command)).reverse().map(command => ({
label: command,
kind: monaco.languages.CompletionItemKind.Value,
})),
};
},
});
22 changes: 20 additions & 2 deletions src/views/PromptComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,27 @@ export class PromptComponent extends React.Component<Props, State> {

startHistorySearch() {
this.editor.setModel(this.historyModel);
this.setValue(this.model.getValue());
this.triggerSuggest();
}

applyHistorySearch() {
// Give it some time to apply a suggestion.
setTimeout(
() => this.cancelHistorySearch(),
200,
);
}

cancelHistorySearch() {
this.editor.setModel(this.model);
this.setValue(this.historyModel.getValue());
}

isInHistorySearch(): boolean {
return this.editor.getModel() === this.historyModel;
}

focus(): void {
this.editor.focus();
}
Expand All @@ -99,7 +117,6 @@ export class PromptComponent extends React.Component<Props, State> {

if (!this.isEmpty()) {
this.props.session.createJob(this.prompt);
this.editor.setModel(this.model);
this.editor.setValue("");
this.setState({
displayedHistoryRecordID: undefined,
Expand Down Expand Up @@ -155,6 +172,7 @@ export class PromptComponent extends React.Component<Props, State> {
this.editor.setValue(value);
this.editor.setPosition({lineNumber: 1, column: value.length + 1});
this.prompt.setValue(value);
this.focus();
}

private get promptContentNode(): HTMLDivElement {
Expand All @@ -167,6 +185,6 @@ export class PromptComponent extends React.Component<Props, State> {
}

private triggerSuggest() {
this.editor.trigger("", "editor.action.triggerSuggest", {});
this.editor.trigger(this.editor.getValue(), "editor.action.triggerSuggest", {});
}
}
10 changes: 10 additions & 0 deletions src/views/keyevents/Keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ export function handleUserEvent(application: ApplicationComponent, search: Searc
return;
}

if (event.keyCode === KeyCode.Tab && promptComponent.isInHistorySearch()) {
promptComponent.applyHistorySearch();
return;
}

if (event.keyCode === KeyCode.Escape && promptComponent.isInHistorySearch()) {
promptComponent.cancelHistorySearch();
return;
}

if (event.ctrlKey && event.keyCode === KeyCode.R) {
promptComponent.startHistorySearch();

Expand Down

0 comments on commit bde209f

Please sign in to comment.