Skip to content

Instantly share code, notes, and snippets.

@kkiyama117
Last active June 12, 2021 06:45
Show Gist options
  • Save kkiyama117/27eb3aa41a150de92287632744f5ac20 to your computer and use it in GitHub Desktop.
Save kkiyama117/27eb3aa41a150de92287632744f5ac20 to your computer and use it in GitHub Desktop.
Create plugin via denops.vim

Run plugins based on denops.vim

If you need this section written in Japanese, read article written by @lambdalisue

Requirements

You need to install these softwares on your machine to run denops plugins.

  • Deno (latest stable version is recommended.)
  • Vim/Neovim

TODO: check minimum/maximum versions of each deps.

Install

We need to install both denops itself and plugins you'd like to use.

TODO: write how to install with each plugin managers like vim-plug, dein.vim or etc.

Create plugin via denops.vim (for developers)

Preparation

Denops plugins must be in the runtimepath of Vim/neovim since they're also Vim plugin.

set runtimepath^=~/your-plugin-root

And set option in your vimrc in order to change arguments of deno called by denops, which enables deno runtime type checker.

let g:denops#server#service#deno_args = [
     \ '-q',
     \ '--unstable',
     \ '-A',
     \]

Example of structure

Denops checks if denops/*/app.ts exists in runtimepath of vim recursively, hence denops plugin usually looks like this:

`dps-helloworld` (project root)
|- `denops` (folder)
   |- `helloworld` (folder, we reccomend to name same as your plugin.) - [^1]
     |- `app.ts` (main file of this plugin. must include entrypoint of denops.)
     |- `deps.ts`
     |- `xxx.ts`
     |- ...

example of app.ts

import { main } from "https://deno.land/x/denops_std/mod.ts";

// main entrypoint.
// Call 'main' with async callback. The callback get `RunnerContext`.
main(async ({ vim }) => {
  // Register RPC functions with 'vim.register' like:
  vim.register({
    async draw(text: unknown): Promise<void> {
      // Ensure that `prefix` is 'string' here
      // https://doc.deno.land/https/deno.land/x/denops_core/mod.ts#ensureString
      ensureString(text, "text");
      // Use `cmd` to execute Vim's command
      await vim.cmd(`redraw | echomsg message`, {
        message: text,
      });
    },
  });
  
  // Use 'vim.execute()' to execute Vim script
  await vim.execute(`
    command! HelloWorld call denops#notify("${vim.name}", "drow", ["Hello World"])
  `);
});

If you need detail of each functions, see deno docs below.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment