tab completion

jh-changes
Shin'ya Ueoka 7 years ago
parent 5e124e7561
commit 1383b8c12d
  1. 49
      src/console/console.js

@ -1,10 +1,13 @@
import './console.scss'; import './console.scss';
import Completion from './completion';
import * as messages from '../shared/messages'; import * as messages from '../shared/messages';
const parent = window.parent; const parent = window.parent;
// TODO consider object-oriented // TODO consider object-oriented
var prevValue = ""; var prevValue = "";
window.completion = null;
var completionOrigin = "";
const blurMessage = () => { const blurMessage = () => {
return { return {
@ -30,6 +33,32 @@ const handleBlur = () => {
messages.send(parent, blurMessage()); messages.send(parent, blurMessage());
}; };
const completeNext = () => {
if (!window.completion) {
return;
}
let item = window.completion.next();
if (!item) {
return;
}
let input = window.document.querySelector('#vimvixen-console-command-input');
input.value = completionOrigin + item.content;
}
const completePrev = () => {
if (!window.completion) {
return;
}
let item = window.completion.prev();
if (!item) {
return;
}
let input = window.document.querySelector('#vimvixen-console-command-input');
input.value = completionOrigin + item.content;
}
const handleKeydown = (e) => { const handleKeydown = (e) => {
switch(e.keyCode) { switch(e.keyCode) {
case KeyboardEvent.DOM_VK_ESCAPE: case KeyboardEvent.DOM_VK_ESCAPE:
@ -38,6 +67,15 @@ const handleKeydown = (e) => {
case KeyboardEvent.DOM_VK_RETURN: case KeyboardEvent.DOM_VK_RETURN:
messages.send(parent, keydownMessage(e.target)); messages.send(parent, keydownMessage(e.target));
break; break;
case KeyboardEvent.DOM_VK_TAB:
if (e.shiftKey) {
completePrev();
} else {
completeNext();
}
e.stopPropagation();
e.preventDefault();
break;
} }
}; };
@ -77,7 +115,7 @@ const showError = (text) => {
command.style.display = 'none'; command.style.display = 'none';
let completion = window.document.querySelector('#vimvixen-console-completion'); let completion = window.document.querySelector('#vimvixen-console-completion');
command.style.display = 'none'; completion.style.display = 'none';
} }
const setCompletions = (completions) => { const setCompletions = (completions) => {
@ -111,6 +149,15 @@ const setCompletions = (completions) => {
completion.append(li); completion.append(li);
} }
} }
let flatten = [];
for (let group of completions) {
flatten = flatten.concat(group.items);
}
window.completion = new Completion(flatten);
let input = window.document.querySelector('#vimvixen-console-command-input');
completionOrigin = input.value;
} }
messages.receive(window, (message) => { messages.receive(window, (message) => {