From 73a4fe0bb1f8a7296f0a174e84111ce021f1c14e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 5 Sep 2017 13:44:21 +0900 Subject: [PATCH] do simple completion --- src/background/index.js | 16 ++++++++++++++-- src/background/tabs.js | 11 ++++++++++- src/console/console-frame.js | 5 ++++- src/content/index.js | 26 +++++++++++++++++++------- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/background/index.js b/src/background/index.js index 93d0e74..4a5e6d6 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -84,8 +84,20 @@ browser.runtime.onMessage.addListener((request, sender) => { return keyPressHandle(request, sender); case 'event.cmd.enter': return cmdEnterHandle(request, sender); - case 'event.cmd.suggest': - // TODO make suggestion and return + case 'event.cmd.tabs.completion': + return tabs.getCompletions(request.text).then((tabs) => { + let items = tabs.map((tab) => { + return { + caption: tab.title, + content: tab.title, + url: tab.url + } + }); + return { + name: "Buffers", + items: items + }; + }); break; } return Promise.resolve(); diff --git a/src/background/tabs.js b/src/background/tabs.js index be8ef02..111bbd9 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -49,6 +49,15 @@ const selectByKeyword = (current, keyword) => { }); } +const getCompletions = (keyword) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let matched = tabs.filter((t) => { + return t.url.includes(keyword) || t.title.includes(keyword) + }) + return matched; + }); +}; + const selectPrevTab = (current, count) => { return browser.tabs.query({ currentWindow: true }, (tabs) => { if (tabs.length < 2) { @@ -78,4 +87,4 @@ const reload = (current, cache) => { ); }; -export { closeTab, reopenTab, selectAt, selectByKeyword, selectNextTab, selectPrevTab, reload }; +export { closeTab, reopenTab, selectAt, selectByKeyword, getCompletions, selectPrevTab, selectNextTab, reload }; diff --git a/src/console/console-frame.js b/src/console/console-frame.js index 11c36f4..e6bf3f5 100644 --- a/src/console/console-frame.js +++ b/src/console/console-frame.js @@ -53,6 +53,9 @@ export default class ConsoleFrame { } setCompletions(completions) { - messages.send(this.element.contentWindow, completions); + messages.send(this.element.contentWindow, { + type: 'vimvixen.console.set.completions', + completions: completions + }); } } diff --git a/src/content/index.js b/src/content/index.js index 8b3eb58..e682a7f 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -83,11 +83,27 @@ window.addEventListener("keypress", (e) => { browser.runtime.sendMessage(request) .then(handleResponse) .catch((err) => { + console.error("Vim Vixen:", err); vvConsole.showError(err.message); - console.log(`Vim Vixen: ${err}`); }); }); +const doCompletion = (line) => { + if (line.startsWith('buffer ')) { + let keyword = line.replace('buffer ', ''); + + browser.runtime.sendMessage({ + type: 'event.cmd.tabs.completion', + text: keyword + }).then((completions) => { + vvConsole.setCompletions([completions]); + }).catch((e) => { + console.error("Vim Vixen:", err); + vvConsole.showError(e.message); + }); + } +}; + messages.receive(window, (message) => { switch (message.type) { case 'vimvixen.command.blur': @@ -100,16 +116,12 @@ messages.receive(window, (message) => { type: 'event.cmd.enter', text: message.value }).catch((e) => { + console.error("Vim Vixen:", err); vvConsole.showError(e.message); }); break; case 'vimvixen.command.change': - browser.runtime.sendMessage({ - type: 'event.cmd.suggest', - text: message.value - }).catch((e) => { - vvConsole.showError(e.message); - }); + doCompletion(message.value); break; default: return;