do simple completion

jh-changes
Shin'ya Ueoka 7 years ago
parent 1383b8c12d
commit 73a4fe0bb1
  1. 16
      src/background/index.js
  2. 11
      src/background/tabs.js
  3. 5
      src/console/console-frame.js
  4. 26
      src/content/index.js

@ -84,8 +84,20 @@ browser.runtime.onMessage.addListener((request, sender) => {
return keyPressHandle(request, sender); return keyPressHandle(request, sender);
case 'event.cmd.enter': case 'event.cmd.enter':
return cmdEnterHandle(request, sender); return cmdEnterHandle(request, sender);
case 'event.cmd.suggest': case 'event.cmd.tabs.completion':
// TODO make suggestion and return 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; break;
} }
return Promise.resolve(); return Promise.resolve();

@ -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) => { const selectPrevTab = (current, count) => {
return browser.tabs.query({ currentWindow: true }, (tabs) => { return browser.tabs.query({ currentWindow: true }, (tabs) => {
if (tabs.length < 2) { 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 };

@ -53,6 +53,9 @@ export default class ConsoleFrame {
} }
setCompletions(completions) { setCompletions(completions) {
messages.send(this.element.contentWindow, completions); messages.send(this.element.contentWindow, {
type: 'vimvixen.console.set.completions',
completions: completions
});
} }
} }

@ -83,11 +83,27 @@ window.addEventListener("keypress", (e) => {
browser.runtime.sendMessage(request) browser.runtime.sendMessage(request)
.then(handleResponse) .then(handleResponse)
.catch((err) => { .catch((err) => {
console.error("Vim Vixen:", err);
vvConsole.showError(err.message); 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) => { messages.receive(window, (message) => {
switch (message.type) { switch (message.type) {
case 'vimvixen.command.blur': case 'vimvixen.command.blur':
@ -100,16 +116,12 @@ messages.receive(window, (message) => {
type: 'event.cmd.enter', type: 'event.cmd.enter',
text: message.value text: message.value
}).catch((e) => { }).catch((e) => {
console.error("Vim Vixen:", err);
vvConsole.showError(e.message); vvConsole.showError(e.message);
}); });
break; break;
case 'vimvixen.command.change': case 'vimvixen.command.change':
browser.runtime.sendMessage({ doCompletion(message.value);
type: 'event.cmd.suggest',
text: message.value
}).catch((e) => {
vvConsole.showError(e.message);
});
break; break;
default: default:
return; return;