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);
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();

@ -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 };

@ -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
});
}
}

@ -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;