Buffer completion on Clean Architecture

jh-changes
Shin'ya Ueoka 7 years ago
parent bf7c125fb2
commit 890d93fe77
  1. 19
      src/background/controllers/completions.js
  2. 11
      src/background/repositories/completions.js
  3. 32
      src/background/usecases/completions.js

@ -13,6 +13,7 @@ export default class ContentMessageController {
if (words.length === 1) {
return this.completionsInteractor.queryConsoleCommand(name);
}
let keywords = trimmed.slice(name.length).trimStart();
switch (words[0]) {
case 'o':
case 'open':
@ -20,22 +21,22 @@ export default class ContentMessageController {
case 'tabopen':
case 'w':
case 'winopen':
break;
return this.completionsInteractor.queryOpen(name);
case 'b':
case 'buffer':
break;
case 'bd!':
case 'bdel!':
case 'bdelete!':
case 'bdeletes!':
break;
return this.completionsInteractor.queryBuffer(name, keywords);
case 'bd':
case 'bdel':
case 'bdelete':
case 'bdeletes':
break;
return this.completionsInteractor.queryBdelete(name, keywords);
case 'bd!':
case 'bdel!':
case 'bdelete!':
case 'bdeletes!':
return this.completionsInteractor.queryBdeleteForce(name, keywords);
case 'set':
break;
return this.completionsInteractor.querySet(name, keywords);
}
return Promise.resolve(Completions.empty());
}

@ -0,0 +1,11 @@
export default class CompletionsRepository {
async queryTabs(keywords, excludePinned) {
let tabs = await browser.tabs.query({ currentWindow: true });
return tabs.filter((t) => {
return t.url.toLowerCase().includes(keywords.toLowerCase()) ||
t.title && t.title.toLowerCase().includes(keywords.toLowerCase());
}).filter((t) => {
return !(excludePinned && t.pinned);
});
}
}

@ -22,19 +22,39 @@ export default class CompletionsInteractor {
if (items.length === 0) {
return Promise.resolve(Completions.empty());
}
return Promise.resolve(new Completions(
[new CompletionGroup('Console Command', items)]
));
return Promise.resolve(
new Completions([new CompletionGroup('Console Command', items)])
);
}
async queryBdeleteCommand(name, force, args) {
let tabs = await this.completionRepository.queryTabs(args);
queryOpen() {
return Promise.resolve(Completions.empty());
}
queryBuffer(name, keywords) {
return this.queryTabs(name, true, keywords);
}
queryBdelete(name, keywords) {
return this.queryTabs(name, true, keywords);
}
queryBdeleteForce(name, keywords) {
return this.queryTabs(name, false, keywords);
}
querySet() {
return Promise.resolve(Completions.empty());
}
async queryTabs(name, excludePinned, args) {
let tabs = await this.completionRepository.queryTabs(args, excludePinned);
let items = tabs.map(tab => new CompletionItem({
caption: tab.title,
content: name + ' ' + tab.title,
url: tab.url,
icon: tab.favIconUrl
}));
return [new CompletionGroup('Buffers', items)];
return new Completions([new CompletionGroup('Buffers', items)]);
}
}