Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type VimStatusPrompt = string;
type VimStatusPromptMap = {
[status in vimStatus]: VimStatusPrompt;
};
type Timeoutlen = number;

interface Settings {
vimrcFileName: string,
Expand All @@ -29,6 +30,7 @@ interface Settings {
capturedKeyboardMap: Record<string, string>,
supportJsCommands?: boolean
vimStatusPromptMap: VimStatusPromptMap;
timeoutlen: Timeoutlen;
}

const DEFAULT_SETTINGS: Settings = {
Expand All @@ -38,6 +40,7 @@ const DEFAULT_SETTINGS: Settings = {
fixedNormalModeLayout: false,
capturedKeyboardMap: {},
supportJsCommands: false,
timeoutlen: 200, // 200ms as a Default
vimStatusPromptMap: {
normal: '🟢',
insert: '🟠',
Expand Down Expand Up @@ -79,6 +82,7 @@ export default class VimrcPlugin extends Plugin {
private customVimKeybinds: { [name: string]: boolean } = {};
private currentSelection: [EditorSelection] = null;
private isInsertMode: boolean = false;
private keyChordTimeout: NodeJS.Timeout | number = 0;

updateVimStatusBar() {
this.vimStatusBar.setText(
Expand Down Expand Up @@ -176,7 +180,7 @@ export default class VimrcPlugin extends Plugin {
}

async updateVimEvents() {
if (!(this.app as Any).isVimEnabled())
if (!(this.app as any).isVimEnabled())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this, but I can remove it if needed since it doesn't fit the description of what the PR is doing, and I can open another PR just for this?? Not sure.

return;
let view = this.getActiveView();
if (view) {
Expand Down Expand Up @@ -601,6 +605,10 @@ export default class VimrcPlugin extends Plugin {
}

onVimKeypress = async (vimKey: any) => {
// Clear the existing timeout so that the chord extends
// so long as the keys are passed within the defined `timeoutlen`
clearTimeout(this.keyChordTimeout);

if (vimKey != "<Esc>") { // TODO figure out what to actually look for to exit commands.
this.currentKeyChord.push(vimKey);
if (this.customVimKeybinds[this.currentKeyChord.join("")] != undefined) { // Custom key chord exists.
Expand All @@ -619,6 +627,12 @@ export default class VimrcPlugin extends Plugin {
tempS += "-";
}
this.vimChordStatusBar?.setText(tempS);

// Set a timeout to clear the chord after settings.timeoutlen milliseconds.
this.keyChordTimeout = setTimeout(() => {
this.currentKeyChord = [];
this.vimChordStatusBar?.setText("");
}, this.settings.timeoutlen);
}

onVimCommandDone = async (reason: any) => {
Expand Down Expand Up @@ -742,6 +756,27 @@ class SettingsTab extends PluginSettingTab {
})
});

new Setting(containerEl)
.setName('Vim chord timeout length')
.setDesc('Sets the timeoutlen in milliseconds which controls how long the editor waits for a complete key sequence.\nEnter a number between 0 and 5000 (milliseconds)')
.addText((text) => {
text.setPlaceholder('200'); // default suggestion
text.setValue(this.plugin.settings.timeoutlen.toString());
text.inputEl.addEventListener('change', (e: Event) => {
const input = (e.target as HTMLInputElement).value;
const num = parseInt(input, 10);
// first we check if the parsed value is a valid number within 0 and 5000
if (!isNaN(num) && num >= 0 && num <= 5000) {
this.plugin.settings.timeoutlen = num;
this.plugin.saveSettings();
} else {
new Notice('Please enter a valid number between 0 and 5000.');
// reset the field to the last valid value
text.setValue(this.plugin.settings.timeoutlen.toString());
}
});
});

new Setting(containerEl)
.setName('Vim chord display')
.setDesc('Displays the current chord until completion. Ex: "<Space> f-" (requires restart)')
Expand Down