aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/completions.js
blob: 0bc34d1d4bf93f87a2f66b7dd3bfa2031b831fa8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import CompletionItem from '../domains/completion-item';
import CompletionGroup from '../domains/completion-group';
import Completions from '../domains/completions';
import CompletionRepository from '../repositories/completions';
import CommandDocs from 'background/shared/commands/docs';

export default class CompletionsInteractor {
  constructor() {
    this.completionRepository = new CompletionRepository();
  }

  queryConsoleCommand(prefix) {
    let keys = Object.keys(CommandDocs);
    let items = keys
      .filter(name => name.startsWith(prefix))
      .map(name => ({
        caption: name,
        content: name,
        url: CommandDocs[name],
      }));

    if (items.length === 0) {
      return Promise.resolve(Completions.empty());
    }
    return Promise.resolve(
      new Completions([new CompletionGroup('Console Command', items)])
    );
  }

  async queryOpen(name, keywords) {
    let groups = [];
    let histories = await this.completionRepository.queryHistories(keywords);
    if (histories.length > 0) {
      let items = histories.map(page => new CompletionItem({
        caption: page.title,
        content: name + ' ' + page.url,
        url: page.url
      }));
      groups.push(new CompletionGroup('History', items));
    }

    let bookmarks = await this.completionRepository.queryBookmarks(keywords);
    if (bookmarks.length > 0) {
      let items = bookmarks.map(page => new CompletionItem({
        caption: page.title,
        content: name + ' ' + page.url,
        url: page.url
      }));
      groups.push(new CompletionGroup('Bookmarks', items));
    }
    return new Completions(groups);
  }

  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 Completions([new CompletionGroup('Buffers', items)]);
  }
}