Skip to content

Commit dc17ead

Browse files
authored
Merge pull request #10 from vim-denops/follow-latest
Follow latest
2 parents 76499d3 + 444805b commit dc17ead

File tree

2 files changed

+39
-60
lines changed

2 files changed

+39
-60
lines changed

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,28 @@ Use your favorite Vim plugin manager to install it.
88

99
Use one of the following commands to test the features:
1010

11-
| Command | Description |
12-
| ----------------------- | ---------------------------------------------------- |
13-
| `DenopsEcho` | Echo a text which is constructed in Deno plugin |
14-
| `DenopsHello` | Interactively construct text in Deno plugin and echo |
15-
| `DenopsGetVariables` | |
16-
| `DenopsSetVariables` | |
17-
| `DenopsRemoveVariables` | |
18-
| `DenopsRegisterAutocmd` | |
19-
20-
Or use `denops#request({plugin-name}, {fn}, {params})` to call a function and
21-
get the result like
11+
| Command | Description |
12+
| ------------- | ----------------------- |
13+
| `HelloWorld` | Say hello to the world |
14+
| `HelloDenops` | Say hello to the denops |
15+
16+
Or use `denops#request({name}, {fn}, {args})` to call a function and get the
17+
result like
2218

2319
```
24-
echo denops#request("helloworld", "echo", ["Hello"])
25-
call denops#request("helloworld", "echo", []) " This will raise exception
20+
echo denops#request("helloworld", "say", ["World"])
2621
```
2722

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

3125
```
32-
call denops#notify("helloworld", "say", ["Hello"])
33-
call denops#notify("helloworld", "say", []) " This will echo exception
26+
call denops#notify("helloworld", "say", ["World"])
3427
```
28+
29+
Some APIs of this sample plugin are not defined as commands so user need to use
30+
`denops#request()` or `denops#notify()` to invoke it. See
31+
[`app.ts`](./denops/helloworld/app.ts) to find out what's more.
32+
33+
See
34+
[deno doc for denops_std](https://doc.deno.land/https/deno.land/x/denops_std/mod.ts)
35+
for API details.

denops/helloworld/app.ts

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Import 'start' function from denops_std
2-
import { main } from "https://deno.land/x/[email protected]/mod.ts";
2+
import {
3+
ensureString,
4+
main,
5+
} from "https://deno.land/x/[email protected]/mod.ts";
36

47
// Call 'main' with async callback. The callback get RunnerContext.
58
main(async ({ vim }) => {
@@ -8,44 +11,23 @@ main(async ({ vim }) => {
811
// Developers can define multiple endpoints which take arbitrary number of arguments
912
// and return arbitrary value as a Promise.
1013
// This function can be called by denops#request() or denops#notify() functions.
11-
async echo(text: unknown): Promise<unknown> {
12-
if (typeof text !== "string") {
13-
throw new Error(`'text' in 'echo()' of ${vim.name} must be a string`);
14-
}
15-
// console.log (console.info, console.debug as well) output message to Vim echo
16-
// area with [denops] prefix.
17-
console.log("echo is called");
18-
19-
// console.error (console.warn, console.critical as well) output message to Vim
20-
// echo area with [denops] prefix as error message
21-
console.error("echo is not really implemented yet");
22-
23-
return await Promise.resolve(text);
24-
},
25-
26-
async hello(app: unknown): Promise<void> {
27-
if (typeof app !== "string") {
28-
throw new Error(`'app' in 'say()' of ${vim.name} must be a string`);
29-
}
30-
31-
// Use 'vim.call(func, ...args)' to call Vim's function and get result
14+
async say(where: unknown): Promise<void> {
15+
// Ensure that `prefix` is 'string' here
16+
ensureString(where, "where");
17+
// Use `call` to call Vim's function
3218
const name = await vim.call("input", "Your name: ");
33-
console.log("name", name);
34-
35-
// Use 'vim.eval(expr, context)' to evaluate Vim's expression and get result
36-
const result = await vim.eval("1 + 1 + value", {
37-
value: 2,
19+
// Use `eval` to evaluate Vim's expression
20+
const progname = await vim.eval("v:progname");
21+
// Construct messages
22+
const messages = [
23+
`Hello ${where}`,
24+
`Your name is ${name}`,
25+
`This is ${progname}`,
26+
];
27+
// Use `cmd` to execute Vim's command
28+
await vim.cmd(`redraw | echomsg message`, {
29+
message: messages.join(". "),
3830
});
39-
console.log("result", result);
40-
41-
// Use 'vim.cmd(cmd, context)' to execute Vim's ex command
42-
await vim.cmd(
43-
`echomsg printf('Hello %s. Your are using ${app} in Vim/Neovim: %s', name, result)`,
44-
{
45-
name,
46-
result,
47-
},
48-
);
4931
},
5032

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

11193
// Use 'vim.execute()' to execute Vim script
11294
await vim.execute(`
113-
command! DenopsEcho echo denops#request("${vim.name}", "echo", ["This is hello world message"])
114-
command! DenopsHello echo denops#notify("${vim.name}", "hello", ["Denops"])
115-
command! DenopsGetVariables echo denops#notify("${vim.name}", "get_variables", [])
116-
command! DenopsSetVariables echo denops#notify("${vim.name}", "set_variables", [])
117-
command! DenopsRemoveVariables echo denops#notify("${vim.name}", "remove_variables", [])
118-
command! DenopsRegisterAutocmd echo denops#notify("${vim.name}", "register_autocmd", [])
95+
command! HelloWorld call denops#notify("${vim.name}", "say", ["World"])
96+
command! HelloDenops call denops#notify("${vim.name}", "say", ["Denops"])
11997
`);
12098

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

0 commit comments

Comments
 (0)