aboutsummaryrefslogtreecommitdiff
path: root/src/background/shared/completions/index.js
blob: 157604e271bf56d35288d69fa28b3f1feb203a28 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import commandDocs from 'background/shared/commands/docs';
import * as tabs from './tabs';
import * as histories from './histories';
import * as bookmarks from './bookmarks';
import * as properties from 'shared/settings/properties';

const completeCommands = (typing) => {
  let keys = Object.keys(commandDocs);
  return keys
    .filter(name => name.startsWith(typing))
    .map(name => ({
      caption: name,
      content: name,
      url: commandDocs[name],
    }));
};

const getSearchCompletions = (command, keywords, searchConfig) => {
  let engineNames = Object.keys(searchConfig.engines);
  let engineItems = engineNames.filter(name => name.startsWith(keywords))
    .map(name => ({
      caption: name,
      content: command + ' ' + name
    }));
  return Promise.resolve(engineItems);
};

const getHistoryCompletions = async(command, keywords) => {
  let items = await histories.getCompletions(keywords);
  return items.map((page) => {
    return {
      caption: page.title,
      content: command + ' ' + page.url,
      url: page.url
    };
  });
};

const getBookmarksCompletions = async(command, keywords) => {
  let items = await bookmarks.getCompletions(keywords);
  return items.map(item => ({
    caption: item.title,
    content: command + ' ' + item.url,
    url: item.url,
  }));
};

const getOpenCompletions = async(command, keywords, searchConfig) => {
  let engineItems = await getSearchCompletions(command, keywords, searchConfig);
  let historyItems = await getHistoryCompletions(command, keywords);
  let bookmarkItems = await getBookmarksCompletions(command, keywords);
  let completions = [];
  if (engineItems.length > 0) {
    completions.push({
      name: 'Search Engines',
      items: engineItems
    });
  }
  if (historyItems.length > 0) {
    completions.push({
      name: 'History',
      items: historyItems
    });
  }
  if (bookmarkItems.length > 0) {
    completions.push({
      name: 'Bookmarks',
      items: bookmarkItems
    });
  }
  return completions;
};

const getBufferCompletions = async(command, keywords, excludePinned) => {
  let items = await tabs.getCompletions(keywords, excludePinned);
  items = items.map(tab => ({
    caption: tab.title,
    content: command + ' ' + tab.title,
    url: tab.url,
    icon: tab.favIconUrl
  }));
  return [
    {
      name: 'Buffers',
      items: items
    }
  ];
};

const getSetCompletions = (command, keywords) => {
  let keys = Object.keys(properties.docs).filter(
    name => name.startsWith(keywords)
  );
  let items = keys.map((key) => {
    if (properties.types[key] === 'boolean') {
      return [
        {
          caption: key,
          content: command + ' ' + key,
          url: 'Enable ' + properties.docs[key],
        }, {
          caption: 'no' + key,
          content: command + ' no' + key,
          url: 'Disable ' + properties.docs[key],
        }
      ];
    }
    return [
      {
        caption: key,
        content: command + ' ' + key,
        url: 'Set ' + properties.docs[key],
      }
    ];
  });
  items = items.reduce((acc, val) => acc.concat(val), []);
  if (items.length === 0) {
    return Promise.resolve([]);
  }
  return Promise.resolve([
    {
      name: 'Properties',
      items,
    }
  ]);
};

const complete = (line, settings) => {
  let trimmed = line.trimStart();
  let words = trimmed.split(/ +/);
  let name = words[0];
  if (words.length === 1) {
    let items = completeCommands(name);
    if (items.length === 0) {
      return Promise.resolve([]);
    }
    return Promise.resolve([
      {
        name: 'Console Command',
        items: completeCommands(name),
      }
    ]);
  }
  let keywords = trimmed.slice(name.length).trimStart();

  switch (words[0]) {
  case 'o':
  case 'open':
  case 't':
  case 'tabopen':
  case 'w':
  case 'winopen':
    return getOpenCompletions(name, keywords, settings.search);
  case 'b':
  case 'buffer':
    return getBufferCompletions(name, keywords, false);
  case 'bd!':
  case 'bdel!':
  case 'bdelete!':
  case 'bdeletes!':
    return getBufferCompletions(name, keywords, false);
  case 'bd':
  case 'bdel':
  case 'bdelete':
  case 'bdeletes':
    return getBufferCompletions(name, keywords, true);
  case 'set':
    return getSetCompletions(name, keywords);
  }
  return Promise.resolve([]);
};

export { complete };