UNPKG

@chemzqm/neovim

Version:

NodeJS client API for vim9 and neovim

59 lines (58 loc) 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../utils/constants"); const func = constants_1.isCocNvim ? 'coc#api#Call' : 'nvim#api#Call'; class Request { constructor(connection, cb, id) { this.connection = connection; this.cb = cb; this.id = id; this._direct = false; } get isDirect() { return this._direct; } request(method, args = []) { this.method = method; this.connection.call(func, [method.slice(5), args], this.id); } call(method, args = []) { this._direct = true; this.method = 'call'; this.connection.call(method, args, this.id); } expr(expr) { this._direct = true; this.method = 'expr'; this.connection.expr(expr, this.id); } callback(client, err, result) { let { method, cb } = this; // error type and error string if (err) return cb([0, err.toString()]); switch (method) { case 'nvim_list_wins': case 'nvim_tabpage_list_wins': return cb(null, result.map(o => client.createWindow(o))); case 'nvim_tabpage_get_win': case 'nvim_get_current_win': case 'nvim_open_win': return cb(null, client.createWindow(result)); case 'nvim_list_bufs': return cb(null, result.map(o => client.createBuffer(o))); case 'nvim_win_get_buf': case 'nvim_create_buf': case 'nvim_get_current_buf': return cb(null, client.createBuffer(result)); case 'nvim_list_tabpages': return cb(null, result.map(o => client.createTabpage(o))); case 'nvim_win_get_tabpage': case 'nvim_get_current_tabpage': return cb(null, client.createTabpage(result)); default: return cb(null, result); } } } exports.default = Request;