Fix tab completions

jh-changes
Shin'ya Ueoka 7 years ago
parent 4116d08642
commit a8ac7415f8
  1. 1
      src/background/actions/command.js
  2. 46
      src/background/shared/completions/index.js
  3. 4
      src/background/shared/completions/tabs.js
  4. 5
      src/background/shared/tabs.js

@ -68,6 +68,7 @@ const setCommand = (args) => {
}; };
}; };
// eslint-disable-next-line complexity
const exec = (tab, line, settings) => { const exec = (tab, line, settings) => {
let [name, args] = parsers.parseCommandLine(line); let [name, args] = parsers.parseCommandLine(line);

@ -65,6 +65,25 @@ const getOpenCompletions = (command, keywords, searchConfig) => {
}); });
}; };
const getBufferCompletions = (command, keywords, excludePinned) => {
return tabs.getCompletions(keywords, excludePinned).then((got) => {
let items = got.map((tab) => {
return {
caption: tab.title,
content: command + ' ' + tab.title,
url: tab.url,
icon: tab.favIconUrl
};
});
return [
{
name: 'Buffers',
items: items
}
];
});
};
const getCompletions = (line, settings) => { const getCompletions = (line, settings) => {
let typedWords = line.trim().split(/ +/); let typedWords = line.trim().split(/ +/);
let typing = ''; let typing = '';
@ -88,30 +107,17 @@ const getCompletions = (line, settings) => {
return getOpenCompletions(name, keywords, settings.search); return getOpenCompletions(name, keywords, settings.search);
case 'b': case 'b':
case 'buffer': case 'buffer':
case 'bd': return getBufferCompletions(name, keywords, false);
case 'bdel':
case 'bdelete':
case 'bd!': case 'bd!':
case 'bdel!': case 'bdel!':
case 'bdelete!': case 'bdelete!':
case 'bdeletes':
case 'bdeletes!': case 'bdeletes!':
return tabs.getCompletions(keywords).then((gotTabs) => { return getBufferCompletions(name, keywords, false);
let items = gotTabs.map((tab) => { case 'bd':
return { case 'bdel':
caption: tab.title, case 'bdelete':
content: name + ' ' + tab.title, case 'bdeletes':
url: tab.url, return getBufferCompletions(name, keywords, true);
icon: tab.favIconUrl
};
});
return [
{
name: 'Buffers',
items: items
}
];
});
} }
return Promise.resolve([]); return Promise.resolve([]);
}; };

@ -1,7 +1,9 @@
const getCompletions = (keyword) => { const getCompletions = (keyword, excludePinned) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => { return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => { let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title && t.title.includes(keyword); return t.url.includes(keyword) || t.title && t.title.includes(keyword);
}).filter((t) => {
return !(excludePinned && t.pinned);
}); });
return matched; return matched;
}); });

@ -16,16 +16,13 @@ const closeTabByKeywords = (keyword) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => { return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => { let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title.includes(keyword); return t.url.includes(keyword) || t.title.includes(keyword);
}); }).filter(t => !t.pinned);
if (matched.length === 0) { if (matched.length === 0) {
throw new Error('No matching buffer for ' + keyword); throw new Error('No matching buffer for ' + keyword);
} else if (matched.length > 1) { } else if (matched.length > 1) {
throw new Error('More than one match for ' + keyword); throw new Error('More than one match for ' + keyword);
} }
if (matched[0].pinned) {
throw new Error('Cannot close a pinned tab (add ! to override)');
}
browser.tabs.remove(matched[0].id); browser.tabs.remove(matched[0].id);
}); });
}; };