UNPKG

@chemzqm/neovim

Version:

NodeJS client API for vim9 and neovim

402 lines (401 loc) 13.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Neovim = void 0; const constants_1 = require("../utils/constants"); const Base_1 = require("./Base"); function getArgs(args) { if (!args) return []; if (Array.isArray(args)) return args; return [args]; } /** * Neovim API */ class Neovim extends Base_1.BaseApi { constructor() { super(...arguments); this.prefix = 'nvim_'; } get apiInfo() { return this.request(`${this.prefix}get_api_info`); } /** Get list of all buffers */ get buffers() { return this.request(`${this.prefix}list_bufs`); } /** Get current buffer */ get buffer() { return this.request(`${this.prefix}get_current_buf`); } /** Retrieves a scoped option depending on type of `this` */ getOption(name) { if (constants_1.isCocNvim) return this.request(`${this.prefix}get_option_value`, [name, {}]); return super.getOption(name); } setOption(name, value, isNotify) { if (constants_1.isCocNvim) return this[isNotify ? 'notify' : 'request'](`${this.prefix}set_option_value`, [name, value, {}]); return this[isNotify ? 'notify' : 'request'](`${this.prefix}set_option`, [name, value]); } /** Set current buffer */ async setBuffer(buffer) { await this.request(`${this.prefix}set_current_buf`, [buffer]); } get chans() { return this.request(`${this.prefix}list_chans`); } getChanInfo(chan) { return this.request(`${this.prefix}get_chan_info`, [chan]); } createNamespace(name = "") { if (constants_1.isCocNvim) name = name.startsWith('coc-') ? name : `coc-${name}`; return this.request(`${this.prefix}create_namespace`, [name]); } get namespaces() { return this.request(`${this.prefix}get_namespaces`, []); } get commands() { return this.getCommands(); } getCommands(options = {}) { return this.request(`${this.prefix}get_commands`, [options]); } /** Get list of all tabpages */ get tabpages() { return this.request(`${this.prefix}list_tabpages`); } /** Get current tabpage */ get tabpage() { return this.request(`${this.prefix}get_current_tabpage`); } /** Set current tabpage */ async setTabpage(tabpage) { await this.request(`${this.prefix}set_current_tabpage`, [tabpage]); } /** Get list of all windows */ get windows() { return this.getWindows(); } /** Get current window */ get window() { return this.request(`${this.prefix}get_current_win`); } /** Get list of all windows */ getWindows() { return this.request(`${this.prefix}list_wins`); } async setWindow(win) { // Throw error if win is not instance of Window? await this.request(`${this.prefix}set_current_win`, [win]); } /** Get list of all runtime paths */ get runtimePaths() { return this.request(`${this.prefix}list_runtime_paths`); } /** Set current directory */ setDirectory(dir) { return this.request(`${this.prefix}set_current_dir`, [dir]); } /** Get current line. Always returns a Promise. */ get line() { return this.getLine(); } createNewBuffer(listed = false, scratch = false) { return this.request(`${this.prefix}create_buf`, [listed, scratch]); } openFloatWindow(buffer, enter, options) { return this.request(`${this.prefix}open_win`, [buffer, enter, options]); } getLine() { return this.request(`${this.prefix}get_current_line`); } /** Set current line */ setLine(line) { return this.request(`${this.prefix}set_current_line`, [line]); } /** Gets keymap */ getKeymap(mode) { return this.request(`${this.prefix}get_keymap`, [mode]); } /** * Add keymap by notification, replace keycodes for expr keymap enabled by default. */ setKeymap(mode, lhs, rhs, opts = {}) { let option = opts.expr ? Object.assign({ replace_keycodes: true }, opts) : opts; this.notify(`${this.prefix}set_keymap`, [mode, lhs, rhs, option]); } deleteKeymap(mode, lhs) { this.notify(`${this.prefix}del_keymap`, [mode, lhs]); } /** Gets current mode */ get mode() { return this.request(`${this.prefix}get_mode`); } /** Gets map of defined colors */ get colorMap() { return this.request(`${this.prefix}get_color_map`); } /** Get color by name */ getColorByName(name) { return this.request(`${this.prefix}get_color_by_name`, [name]); } /** Get highlight by name or id */ getHighlight(nameOrId, isRgb = true) { const functionName = typeof nameOrId === 'string' ? 'by_name' : 'by_id'; return this.request(`${this.prefix}get_hl_${functionName}`, [ nameOrId, isRgb, ]); } getHighlightByName(name, isRgb = true) { return this.request(`${this.prefix}get_hl_by_name`, [name, isRgb]); } getHighlightById(id, isRgb = true) { return this.request(`${this.prefix}get_hl_by_id`, [id, isRgb]); } /** Delete current line in buffer */ deleteCurrentLine() { return this.request(`${this.prefix}del_current_line`); } /** * Evaluates a VimL expression (:help expression). Dictionaries * and Lists are recursively expanded. On VimL error: Returns a * generic error; v:errmsg is not updated. * */ eval(expr) { return this.request(`${this.prefix}eval`, [expr]); } /** * Executes lua, it's possible neovim client does not support this */ lua(code, args = []) { return this.request(`${this.prefix}exec_lua`, [code, args]); } // Alias for `lua()` to be consistent with neovim API executeLua(code, args = []) { const _args = getArgs(args); return this.lua(code, _args); } callDictFunction(dict, fname, args = []) { const _args = getArgs(args); return this.request(`${this.prefix}call_dict_function`, [ dict, fname, _args, ]); } callVim(fname, args = [], isNotify) { if (!constants_1.isVim) return this.call(fname, args, isNotify); const _args = getArgs(args); if (isNotify) return this.transport.vimCommand('call', fname, _args); return this.transport.vimRequest('call', [fname, _args]); } /** * Use direct expr command on vim9 */ evalVim(expr) { if (!constants_1.isVim) return this.request(`${this.prefix}eval`, [expr]); return this.transport.vimRequest('eval', [expr]); } /** * Use direct ex on vim9 */ exVim(arg) { if (!constants_1.isVim) return this.notify(`${this.prefix}command`, [arg]); this.transport.vimCommand('ex', arg); } call(fname, args = [], isNotify) { const _args = getArgs(args); if (isNotify) { this.notify(`${this.prefix}call_function`, [fname, _args]); return null; } return this.request(`${this.prefix}call_function`, [fname, _args]); } callTimer(fname, args = [], isNotify) { const _args = getArgs(args); if (isNotify) { this.notify(`${this.prefix}call_function`, ['coc#util#timer', [fname, _args]]); return null; } if (constants_1.isVim) { this.notify(`${this.prefix}call_function`, ['coc#util#timer', [fname, _args]]); return new Promise(resolve => { setTimeout(() => { resolve(null); }, 20); }); } return this.request(`${this.prefix}call_function`, ['coc#util#timer', [fname, _args]]); } callAsync(fname, args = []) { const _args = getArgs(args); return this.client.sendAsyncRequest(fname, _args); } /** Alias for `call` */ callFunction(fname, args = []) { return this.call(fname, args); } /** Call Atomic calls */ callAtomic(calls) { return this.request(`${this.prefix}call_atomic`, [calls]); } command(arg, isNotify) { if (isNotify) { this.notify(`${this.prefix}command`, [arg]); return null; } return this.request(`${this.prefix}command`, [arg]); } /** * Runs a command and returns output. * * @deprecated Use exec instead. */ commandOutput(arg) { return this.request(`${this.prefix}command_output`, [arg]); } /** * Executes Vimscript (multiline block of Ex-commands), like * anonymous |:source| */ exec(src, output = false) { return this.request(`${this.prefix}exec`, [src, output]); } /** Gets a v: variable */ getVvar(name) { return this.request(`${this.prefix}get_vvar`, [name]); } /** feedKeys */ feedKeys(keys, mode, escapeCsi) { return this.request(`${this.prefix}feedkeys`, [keys, mode, escapeCsi]); } /** Sends input keys */ input(keys) { return this.request(`${this.prefix}input`, [keys]); } /** * Send mouse event from GUI. Neovim only. * * @param {MouseButton} button Mouse button: one of "left", "right", "middle", "wheel", "move". * @param {ButtonAction} action For ordinary buttons, one of "press", "drag", "release". * @param {string} modifier String of modifiers each represented by a single char. * @param {number} row Mouse row-position (zero-based, like redraw events) * @param {number} col Mouse column-position (zero-based, like redraw events) * @param {number} grid Grid number if the client uses |ui-multigrid|, else 0. * @returns {Promise<null>} */ inputMouse(button, action, modifier, row, col, grid = 0) { return this.request(`${this.prefix}input_mouse`, [button, action, modifier, grid, row, col]); } /** * Parse a VimL Expression * * TODO: return type, see :help */ parseExpression(expr, flags, highlight) { return this.request(`${this.prefix}parse_expression`, [ expr, flags, highlight, ]); } getProc(pid) { return this.request(`${this.prefix}get_proc`, [pid]); } getProcChildren(pid) { return this.request(`${this.prefix}get_proc_children`, [pid]); } /** Replace term codes */ replaceTermcodes(str, fromPart, doIt, special) { return this.request(`${this.prefix}replace_termcodes`, [ str, fromPart, doIt, special, ]); } /** Gets width of string */ strWidth(str) { return this.request(`${this.prefix}strwidth`, [str]); } /** Write to output buffer */ outWrite(str) { this.notify(`${this.prefix}out_write`, [str]); } outWriteLine(str) { this.outWrite(`${str}\n`); } /** Write to error buffer */ errWrite(str) { this.notify(`${this.prefix}err_write`, [str]); } /** Write to error buffer */ errWriteLine(str) { this.notify(`${this.prefix}err_writeln`, [str]); } // TODO: add type get uis() { return this.request(`${this.prefix}list_uis`); } uiAttach(width, height, options) { return this.request(`${this.prefix}ui_attach`, [width, height, options]); } uiDetach() { return this.request(`${this.prefix}ui_detach`, []); } uiTryResize(width, height) { return this.request(`${this.prefix}ui_try_resize`, [width, height]); } /** Set UI Option */ uiSetOption(name, value) { return this.request(`${this.prefix}ui_set_option`, [name, value]); } /** Subscribe to nvim event broadcasts */ subscribe(event) { return this.request(`${this.prefix}subscribe`, [event]); } /** Unsubscribe to nvim event broadcasts */ unsubscribe(event) { return this.request(`${this.prefix}unsubscribe`, [event]); } createAugroup(name, option = {}, isNotify = false) { if (!isNotify) return this.request(`${this.prefix}create_augroup`, [name, option]); this.notify(`${this.prefix}create_augroup`, [name, option]); } createAutocmd(event, option = {}, isNotify = false) { if (!isNotify) return this.request(`${this.prefix}create_autocmd`, [event, option]); this.notify(`${this.prefix}create_autocmd`, [event, option]); } deleteAutocmd(id) { this.notify(`${this.prefix}del_autocmd`, [id]); } setClientInfo(name, version, type, methods, attributes) { this.notify(`${this.prefix}set_client_info`, [ name, version, type, methods, attributes, ]); } /** Quit nvim */ async quit() { this.command('qa!', true); if (this.transport) { this.transport.detach(); } } } exports.Neovim = Neovim;