Skip to content

Follow latest #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ Use your favorite Vim plugin manager to install it.

Use one of the following commands to test the features:

| Command | Description |
| ----------------------- | ---------------------------------------------------- |
| `DenopsEcho` | Echo a text which is constructed in Deno plugin |
| `DenopsHello` | Interactively construct text in Deno plugin and echo |
| `DenopsGetVariables` | |
| `DenopsSetVariables` | |
| `DenopsRemoveVariables` | |
| `DenopsRegisterAutocmd` | |

Or use `denops#request({plugin-name}, {fn}, {params})` to call a function and
get the result like
| Command | Description |
| ------------- | ----------------------- |
| `HelloWorld` | Say hello to the world |
| `HelloDenops` | Say hello to the denops |

Or use `denops#request({name}, {fn}, {args})` to call a function and get the
result like

```
echo denops#request("helloworld", "echo", ["Hello"])
call denops#request("helloworld", "echo", []) " This will raise exception
echo denops#request("helloworld", "say", ["World"])
```

Or use `denops#notify({plugin-name}, {fn}, {params})` to call a function and
leave like
Or use `denops#notify({name}, {fn}, {params})` to call a function and leave like

```
call denops#notify("helloworld", "say", ["Hello"])
call denops#notify("helloworld", "say", []) " This will echo exception
call denops#notify("helloworld", "say", ["World"])
```

Some APIs of this sample plugin are not defined as commands so user need to use
`denops#request()` or `denops#notify()` to invoke it. See
[`app.ts`](./denops/helloworld/app.ts) to find out what's more.

See
[deno doc for denops_std](https://doc.deno.land/https/deno.land/x/denops_std/mod.ts)
for API details.
64 changes: 21 additions & 43 deletions denops/helloworld/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Import 'start' function from denops_std
import { main } from "https://deno.land/x/[email protected]/mod.ts";
import {
ensureString,
main,
} from "https://deno.land/x/[email protected]/mod.ts";

// Call 'main' with async callback. The callback get RunnerContext.
main(async ({ vim }) => {
Expand All @@ -8,44 +11,23 @@ main(async ({ vim }) => {
// Developers can define multiple endpoints which take arbitrary number of arguments
// and return arbitrary value as a Promise.
// This function can be called by denops#request() or denops#notify() functions.
async echo(text: unknown): Promise<unknown> {
if (typeof text !== "string") {
throw new Error(`'text' in 'echo()' of ${vim.name} must be a string`);
}
// console.log (console.info, console.debug as well) output message to Vim echo
// area with [denops] prefix.
console.log("echo is called");

// console.error (console.warn, console.critical as well) output message to Vim
// echo area with [denops] prefix as error message
console.error("echo is not really implemented yet");

return await Promise.resolve(text);
},

async hello(app: unknown): Promise<void> {
if (typeof app !== "string") {
throw new Error(`'app' in 'say()' of ${vim.name} must be a string`);
}

// Use 'vim.call(func, ...args)' to call Vim's function and get result
async say(where: unknown): Promise<void> {
// Ensure that `prefix` is 'string' here
ensureString(where, "where");
// Use `call` to call Vim's function
const name = await vim.call("input", "Your name: ");
console.log("name", name);

// Use 'vim.eval(expr, context)' to evaluate Vim's expression and get result
const result = await vim.eval("1 + 1 + value", {
value: 2,
// Use `eval` to evaluate Vim's expression
const progname = await vim.eval("v:progname");
// Construct messages
const messages = [
`Hello ${where}`,
`Your name is ${name}`,
`This is ${progname}`,
];
// Use `cmd` to execute Vim's command
await vim.cmd(`redraw | echomsg message`, {
message: messages.join(". "),
});
console.log("result", result);

// Use 'vim.cmd(cmd, context)' to execute Vim's ex command
await vim.cmd(
`echomsg printf('Hello %s. Your are using ${app} in Vim/Neovim: %s', name, result)`,
{
name,
result,
},
);
},

async get_variables(): Promise<void> {
Expand Down Expand Up @@ -110,12 +92,8 @@ main(async ({ vim }) => {

// Use 'vim.execute()' to execute Vim script
await vim.execute(`
command! DenopsEcho echo denops#request("${vim.name}", "echo", ["This is hello world message"])
command! DenopsHello echo denops#notify("${vim.name}", "hello", ["Denops"])
command! DenopsGetVariables echo denops#notify("${vim.name}", "get_variables", [])
command! DenopsSetVariables echo denops#notify("${vim.name}", "set_variables", [])
command! DenopsRemoveVariables echo denops#notify("${vim.name}", "remove_variables", [])
command! DenopsRegisterAutocmd echo denops#notify("${vim.name}", "register_autocmd", [])
command! HelloWorld call denops#notify("${vim.name}", "say", ["World"])
command! HelloDenops call denops#notify("${vim.name}", "say", ["Denops"])
`);

console.log("denops-helloworld.vim (std) has loaded");
Expand Down