Buffer completion on Clean Architecture

jh-changes
Shin'ya Ueoka 6 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) { if (words.length === 1) {
return this.completionsInteractor.queryConsoleCommand(name); return this.completionsInteractor.queryConsoleCommand(name);
} }
let keywords = trimmed.slice(name.length).trimStart();
switch (words[0]) { switch (words[0]) {
case 'o': case 'o':
case 'open': case 'open':
@ -20,22 +21,22 @@ export default class ContentMessageController {
case 'tabopen': case 'tabopen':
case 'w': case 'w':
case 'winopen': case 'winopen':
break; return this.completionsInteractor.queryOpen(name);
case 'b': case 'b':
case 'buffer': case 'buffer':
break; return this.completionsInteractor.queryBuffer(name, keywords);
case 'bd!':
case 'bdel!':
case 'bdelete!':
case 'bdeletes!':
break;
case 'bd': case 'bd':
case 'bdel': case 'bdel':
case 'bdelete': case 'bdelete':
case 'bdeletes': 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': case 'set':
break; return this.completionsInteractor.querySet(name, keywords);
} }
return Promise.resolve(Completions.empty()); 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) { if (items.length === 0) {
return Promise.resolve(Completions.empty()); return Promise.resolve(Completions.empty());
} }
return Promise.resolve(new Completions( return Promise.resolve(
[new CompletionGroup('Console Command', items)] new Completions([new CompletionGroup('Console Command', items)])
)); );
} }
async queryBdeleteCommand(name, force, args) { queryOpen() {
let tabs = await this.completionRepository.queryTabs(args); 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({ let items = tabs.map(tab => new CompletionItem({
caption: tab.title, caption: tab.title,
content: name + ' ' + tab.title, content: name + ' ' + tab.title,
url: tab.url, url: tab.url,
icon: tab.favIconUrl icon: tab.favIconUrl
})); }));
return [new CompletionGroup('Buffers', items)]; return new Completions([new CompletionGroup('Buffers', items)]);
} }
} }