From 1383b8c12d438eaf271bdf528ff033270a45d394 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 5 Sep 2017 12:24:24 +0900 Subject: tab completion --- src/console/console.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'src/console/console.js') diff --git a/src/console/console.js b/src/console/console.js index e54611d..3c7d61f 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,10 +1,13 @@ import './console.scss'; +import Completion from './completion'; import * as messages from '../shared/messages'; const parent = window.parent; // TODO consider object-oriented var prevValue = ""; +window.completion = null; +var completionOrigin = ""; const blurMessage = () => { return { @@ -30,6 +33,32 @@ const handleBlur = () => { 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) => { switch(e.keyCode) { case KeyboardEvent.DOM_VK_ESCAPE: @@ -38,6 +67,15 @@ const handleKeydown = (e) => { case KeyboardEvent.DOM_VK_RETURN: messages.send(parent, keydownMessage(e.target)); 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'; let completion = window.document.querySelector('#vimvixen-console-completion'); - command.style.display = 'none'; + completion.style.display = 'none'; } const setCompletions = (completions) => { @@ -111,6 +149,15 @@ const setCompletions = (completions) => { 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) => { -- cgit v1.2.3