Skip to content

Instantly share code, notes, and snippets.

@chemzqm
Created May 26, 2019 18:55
Show Gist options
  • Save chemzqm/dc95ae734abd63563a6a51529cf6cd98 to your computer and use it in GitHub Desktop.
Save chemzqm/dc95ae734abd63563a6a51529cf6cd98 to your computer and use it in GitHub Desktop.
coc.nvim address extension.
const {sources} = require('coc.nvim')
const {spawn} = require('child_process')
const readline = require('readline')
exports.activate = async context => {
context.subscriptions.push(
sources.createSource({
// unique id
name: 'address',
// unsed in menu
shortcut: 'address',
filetypes: ['mail'],
// not trigger when trigger patterns doesn't match
triggerOnly: true,
priority: 99,
triggerPatterns: [
/^(Bcc|Cc|From|Reply-To|To):\s*/,
/^(Bcc|Cc|From|Reply-To|To):.*,\s*/
],
doComplete: async function(opt) {
let matches = await getAddresses(opt.input)
return {
items: matches.map(m => {
return {
word: `${m[1]} <${m[0]}>`,
abbr: `${m[0]} ${m[1]}`,
filterText: `${m[0]} ${m[1]}`,
menu: this.menu
}
})
}
}
})
)
}
async function getAddresses(input) {
let result = []
return new Promise((resolve, reject) => {
const p = spawn('lbdbq', [input])
const rl = readline.createInterface(p.stdout)
p.on('error', reject)
rl.on('line', line => {
if (line.startsWith('lbdbq:')) return
let [email, name] = line.split('\t')
result.push([email, name])
})
rl.on('close', () => {
resolve(result)
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment