Skip to main content
Module

x/ddc_vim/deps.ts>batch

Dark deno-powered completion framework for neovim/Vim
Latest
function batch
import { batch } from "https://deno.land/x/ddc_vim@v4.3.1/deps.ts";

Call multiple denops functions sequentially without RPC overhead

import { Denops } from "../mod.ts";
import { batch } from "./batch.ts";

export async function main(denops: Denops): Promise<void> {
  await batch(denops, async (denops) => {
    await denops.cmd("setlocal modifiable");
    await denops.cmd("setlocal noswap");
    await denops.cmd("setlocal nobackup");
  });
}

The function can be nested thus users can use functions which may internally use batch() like:

import { Denops } from "../mod.ts";
import { batch } from "./batch.ts";

async function replace(denops: Denops, content: string): Promise<void> {
  await batch(denops, async (denops) => {
    await denops.cmd("setlocal modifiable");
    await denops.cmd("setlocal foldmethod=manual");
    await denops.call("setline", 1, content.split(/\r?\n/));
    await denops.cmd("setlocal nomodifiable");
    await denops.cmd("setlocal nomodified");
  });
}

export async function main(denops: Denops): Promise<void> {
  await batch(denops, async (denops) => {
    await denops.cmd("edit Hello");
    await replace(denops, "Hello Darkness My Old Friend");
  });
}

Note that denops.call(), denops.batch(), or denops.eval() always return falsy value in batch(), indicating that you cannot write code like below:

import { Denops } from "../mod.ts";
import { batch } from "./batch.ts";

export async function main(denops: Denops): Promise<void> {
  await batch(denops, async (denops) => {
    // !!! DON'T DO THIS !!!
    if (await denops.eval("&verbose")) {
      await denops.cmd("echomsg 'VERBOSE'");
    }
    await denops.cmd("echomsg 'Hello world'");
  });
}

The denops instance passed to the batch block is available even outside of the block. It works like a real denops instance, mean that you can write code like:

import { Denops } from "../mod.ts";
import { batch } from "./batch.ts";
import * as lambda from "../lambda/mod.ts";

export async function main(denops: Denops): Promise<void> {
  await batch(denops, async (denops) => {
    const id = lambda.register(denops, async () => {
      // This code is called outside of 'batch' block
      // thus the 'denops' instance works like a real one.
      // That's why you can write code like below
      if (await denops.eval("&verbose")) {
        await denops.cmd("echomsg 'VERBOSE'");
      }
      await denops.cmd("echomsg 'Hello world'");
    });
    await denops.cmd(
      `command! Test call denops#request('${denops.name}', '${id}', [])`,
    );
  });
}

Note that denops.redraw() is executed only once after the batch is actually executed, no matter how many times it is called in the batch(). If the force option is specified even once, the last call will be the one with the force option specified.

Parameters

denops: Denops
executor: (helper: BatchHelper) => Promise<void>

Returns

Promise<void>