本文整理汇总了TypeScript中@chemzqm/neovim.attach函数的典型用法代码示例。如果您正苦于以下问题:TypeScript attach函数的具体用法?TypeScript attach怎么用?TypeScript attach使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了attach函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createNvim
export function createNvim(): Neovim {
let p = which.sync('nvim')
let proc = cp.spawn(p, ['-u', 'NORC', '-i', 'NONE', '--embed', '--headless'], {
shell: false
})
return attach({ proc })
}
示例2: Promise
return new Promise(async resolve => {
if (!env) {
env = await workspace.nvim.call('coc#util#highlight_options') as Env
if (!env) resolve([])
let paths = env.runtimepath.split(',')
let dirs = paths.filter(p => {
if (env.colorscheme) {
let schemeFile = path.join(p, `colors/${env.colorscheme}.vim`)
if (fs.existsSync(schemeFile)) return true
}
if (fs.existsSync(path.join(p, 'syntax'))) return true
if (fs.existsSync(path.join(p, 'after/syntax'))) return true
return false
})
env.runtimepath = dirs.join(',')
}
let proc = cp.spawn('nvim', ['-u', 'NORC', '-i', 'NONE', '--embed', uuid()], {
shell: false,
cwd: os.tmpdir(),
env: omit(process.env, ['NVIM_LISTEN_ADDRESS'])
})
let timer: NodeJS.Timer
let exited = false
const exit = () => {
if (exited) return
exited = true
if (timer) clearTimeout(timer)
if (nvim) {
nvim.command('qa!').catch(() => {
let killed = terminate(proc)
if (!killed) {
setTimeout(() => {
terminate(proc)
}, 50)
}
})
}
}
try {
proc.once('exit', () => {
if (exited) return
logger.info('highlight nvim exited.')
resolve([])
})
timer = setTimeout(() => {
exit()
resolve([])
}, 500)
nvim = attach({ proc }, null, false)
const callback = (method, args) => {
if (method == 'redraw') {
for (let arr of args) {
let [name, ...list] = arr
if (name == 'hl_attr_define') {
for (let item of list) {
let id = item[0]
let { hi_name } = item[item.length - 1][0]
hlMap.set(id, hi_name)
}
}
if (name == 'grid_line') {
// logger.debug('list:', JSON.stringify(list, null, 2))
for (let def of list) {
let [, line, col, cells] = def
if (line >= lines.length) continue
let colStart = 0
let hlGroup = ''
let currId = 0
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < cells.length; i++) {
let cell = cells[i]
let [ch, hlId, repeat] = cell as [string, number?, number?]
repeat = repeat || 1
let len = byteLength(ch.repeat(repeat))
// append result
if (hlId == 0 || (hlId > 0 && hlId != currId)) {
if (hlGroup) {
res.push({
line,
hlGroup,
colStart: colStart + 1,
colEnd: col + 1,
isMarkdown: filetype == 'markdown'
})
}
colStart = col
hlGroup = hlId == 0 ? '' : hlMap.get(hlId)
}
if (hlId != null) currId = hlId
col = col + len
}
if (hlGroup) {
res.push({
hlGroup,
line,
colStart: colStart + 1,
colEnd: col + 1,
isMarkdown: filetype == 'markdown'
})
}
//.........这里部分代码省略.........
示例3: default
export default (opts: Attach): Plugin => {
const nvim: NeovimClient = attach(opts)
const plugin = new Plugin(nvim)
let initialized = false
nvim.on('notification', async (method, args) => {
switch (method) {
case 'VimEnter': {
if (!initialized) {
initialized = true
await plugin.init()
}
break
}
case 'OptionSet':
await events.fire('OptionSet', args)
break
case 'InputChar':
await events.fire('InputChar', args)
break
case 'GlobalChange':
await events.fire('GlobalChange', args)
break
case 'CocAutocmd':
await events.fire(args[0], args.slice(1))
break
default:
const m = method[0].toLowerCase() + method.slice(1)
if (typeof plugin[m] == 'function') {
try {
await Promise.resolve(plugin[m].apply(plugin, args))
} catch (e) {
// tslint:disable-next-line:no-console
console.error(`error on notification '${method}': ${e}`)
}
}
}
})
nvim.on('request', async (method: string, args, resp) => {
try {
if (method == 'CocAutocmd') {
await events.fire(args[0], args.slice(1))
resp.send()
return
}
let m = method[0].toLowerCase() + method.slice(1)
if (typeof plugin[m] !== 'function') {
return resp.send(`Method ${m} not found`, true)
}
let res = await Promise.resolve(plugin[m].apply(plugin, args))
resp.send(res)
} catch (e) {
logger.error(`Error on "${method}": ` + e.stack)
resp.send(e.message, true)
}
})
nvim.channelId.then(async channelId => {
if (isTest) nvim.command(`let g:coc_node_channel_id = ${channelId}`, true)
let json = require('../package.json')
let { major, minor, patch } = semver.parse(json.version)
nvim.setClientInfo('coc', { major, minor, patch }, 'remote', {}, {})
let entered = await nvim.getVvar('vim_did_enter')
if (entered && !initialized) {
initialized = true
await plugin.init()
}
}).catch(e => {
console.error(`Channel create error: ${e.message}`) // tslint:disable-line
})
return plugin
}