Skip to content

Commit

Permalink
📝 Use Entrypoint style in example codes
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed May 14, 2024
1 parent 6aeb2f2 commit ed114f1
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 73 deletions.
6 changes: 3 additions & 3 deletions src/getting-started/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ $HOME
Next, write the following TypeScript code in `main.ts`:

```typescript
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async hello() {
await denops.cmd(`echo "Hello, Denops!"`);
},
};
}
};
```

## Activate the Plugin
Expand Down
51 changes: 34 additions & 17 deletions src/getting-started/explanation.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,42 +91,42 @@ easily call.
In the Getting Started, we wrote the following code in the `main.ts` file:

```typescript
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async hello() {
await denops.cmd(`echo "Hello, Denops!"`);
},
};
}
};
```

Let's break down this code step by step.

### About Imports

```typescript
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
```

The first line imports the `Denops` type from the [denops_std] standard library.
You can find detailed information about the library by checking the URL:
`https://deno.land/x/denops_std@v6.0.0` (remove `/mod.ts`). We fixed the version
in the import URL, so it's recommended to check for details and update to the
latest version URL.
The first line imports the `Entrypoint` type from the [denops_std] standard
library. You can find detailed information about the library by checking the
URL: `https://deno.land/x/denops_std@v6.5.0` (remove `/mod.ts`). We fixed the
version in the import URL, so it's recommended to check for details and update
to the latest version URL.

Note that we use `import type` syntax, which is part of TypeScript's
[Type-Only Imports and Export](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html).
This syntax can be written as `import { type Denops }` with the same meaning.
Using `import { Denops }` for a type-only import is also valid.
This syntax can be written as `import { type Entrypoint }` with the same
meaning. Using `import { Entrypoint }` for a type-only import is also valid.

> [!NOTE]
>
> Denops plugins are dynamically imported, so there might be differences in
> Denops versions between development and usage. Therefore, to minimize
> differences between Denops versions, only the `Denops` type information is
> exposed. The implementation can be found in
> differences between Denops versions, only type information is exposed. The
> implementation can be found in
> [`denops/@denops-private/denops.ts`](https://github.com/vim-denops/denops.vim/blob/main/denops/%40denops-private/denops.ts),
> but it is not publicly exposed for the reasons mentioned above.
>
Expand All @@ -135,12 +135,29 @@ Using `import { Denops }` for a type-only import is also valid.
> intended to be referenced only by [denops.vim] and [denops_std], so Denops
> plugin developers don't need to use it directly.
> [!NOTE]
>
> Prior to denops-std v6.5.0, the `Entrypoint` type was not defined thus
> developers must define the `main` function as like
>
> ```typescript
> import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
>
> export function main(denops: Denops): void {
> denops.dispatcher = {
> async hello() {
> await denops.cmd(`echo "Hello, Denops!"`);
> },
> };
> }
> ```
### About Entry Point

```typescript
export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
// Omitted...
}
};
```

The above code exports the `main` function. The `main` function is called by
Expand Down Expand Up @@ -211,11 +228,11 @@ recommended to use [denops_std] to call Vim's features in actual plugin
development.

For example, use
[`function` module](https://deno.land/x/denops_std@v6.0.0/function/mod.ts) to
[`function` module](https://deno.land/x/denops_std@v6.5.0/function/mod.ts) to
call Vim's function instead of `denops.call` like:

```typescript
import * as fn from "https://deno.land/x/denops_std@v6.0.0/function/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.5.0/function/mod.ts";

// Bad (result1 is `unknown`)
const result1 = await denops.call("expand", "%");
Expand Down
4 changes: 2 additions & 2 deletions src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ This article is a tutorial on developing Denops plugins.

In this tutorial, we use the following software and version as of writing.

- [denops.vim v6.0.0](https://github.com/vim-denops/denops.vim/releases/tag/v6.0.0)
- [denops.vim v6.1.0](https://github.com/vim-denops/denops.vim/releases/tag/v6.1.0)
(2024-02-03)
- [denops_std v6.0.0](https://github.com/vim-denops/deno-denops-std/releases/tag/v6.0.0)
- [denops_std v6.5.0](https://github.com/vim-denops/deno-denops-std/releases/tag/v6.5.0)
(2024-02-03)

[vim-jp]: https://vim-jp.org/
Expand Down
8 changes: 4 additions & 4 deletions src/tutorial/helloworld/adding-an-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ Open `denops/denops-helloworld/main.ts` and rewrite the content with the
following code:

```typescript:denops/denops-helloworld/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import { assert, is } from "https://deno.land/x/unknownutil@v3.14.1/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import { assert, is } from "https://deno.land/x/unknownutil@v3.18.1/mod.ts";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
hello(name) {
assert(name, is.String);
return `Hello, ${name || "Denops"}!`;
},
};
}
};
```

The above code adds a new API `hello` to the plugin. The `hello` API takes a
Expand Down
8 changes: 4 additions & 4 deletions src/tutorial/helloworld/calling-vim-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ the `denops` instance passed to the plugin's `main` function. You can rewrite
`main.ts` as follows to register the `DenopsHello` as a Vim command:

```ts:denops/denops-helloworld/main.ts
import { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import { assert, is } from "https://deno.land/x/unknownutil@v3.14.1/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import { assert, is } from "https://deno.land/x/unknownutil@v3.18.1/mod.ts";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async init() {
// This is just an example.
Expand All @@ -23,7 +23,7 @@ export function main(denops: Denops): void {
return `Hello, ${name || "Denops"}!`;
},
};
}
};
```

Then, rewrite `plugin/denops-helloworld.vim` to automatically call the `init`
Expand Down
6 changes: 3 additions & 3 deletions src/tutorial/helloworld/creating-a-minimal-denops-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ denops-helloworld
Here is the content of the `denops/denops-helloworld/main.ts` file:

```typescript:denops/denops-helloworld/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
console.log("Hello, Denops from TypeScript!");
}
};
```

> [!WARNING]
Expand Down
8 changes: 4 additions & 4 deletions src/tutorial/maze/adjusting-maze-size-to-fit-the-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Let's modify the plugin to ensure the generated maze fits the current window
size.

```typescript:denops/denops-helloworld/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.0.0/function/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.5.0/function/mod.ts";
import { Maze } from "https://deno.land/x/[email protected]/mod.js";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async maze() {
await denops.cmd("enew");
Expand All @@ -28,7 +28,7 @@ export function main(denops: Denops): void {
await fn.setline(denops, 1, content.split(/\r?\n/g));
},
};
}
};
```

In this code, we utilize the `function` module (aliased to `fn`) of `denops_std`
Expand Down
12 changes: 6 additions & 6 deletions src/tutorial/maze/creating-applicative-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ opener, generate a maze that fits the current window size, configure the buffer
options to make it non-file readonly buffer, etc.

```ts:denops/denops-maze/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import { batch, collect } from "https://deno.land/x/denops_std@v6.0.0/batch/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.0.0/function/mod.ts";
import * as op from "https://deno.land/x/denops_std@v6.0.0/option/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import { batch, collect } from "https://deno.land/x/denops_std@v6.5.0/batch/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.5.0/function/mod.ts";
import * as op from "https://deno.land/x/denops_std@v6.5.0/option/mod.ts";
import { Maze } from "https://deno.land/x/[email protected]/mod.js";
import { assert, is } from "https://deno.land/x/unknownutil@v3.14.1/mod.ts";
import { assert, is } from "https://deno.land/x/unknownutil@v3.18.1/mod.ts";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async maze(opener) {
assert(opener, is.String);
Expand Down
6 changes: 3 additions & 3 deletions src/tutorial/maze/outputting-content-to-buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ the maze to a buffer so that users can yank the maze with daily Vim operations!
Let's modify the code to make the generated maze output to a buffer.

```ts:denops/denops-maze/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import { Maze } from "https://deno.land/x/[email protected]/mod.js";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async maze() {
const maze = new Maze({}).generate();
Expand All @@ -19,7 +19,7 @@ export function main(denops: Denops): void {
await denops.call("setline", 1, content.split(/\r?\n/g));
},
};
}
};
```

In this code, `denops.cmd` executes the Vim command `enew` to open a new buffer
Expand Down
12 changes: 6 additions & 6 deletions src/tutorial/maze/properly-configured-the-buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ buffer after closure. Open the `main.ts` file and modify the `maze` method as
follows:

```typescript:denops/denops-maze/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v6.0.0/buffer/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.0.0/function/mod.ts";
import * as op from "https://deno.land/x/denops_std@v6.0.0/option/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v6.5.0/buffer/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.5.0/function/mod.ts";
import * as op from "https://deno.land/x/denops_std@v6.5.0/option/mod.ts";
import { Maze } from "https://deno.land/x/[email protected]/mod.js";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async maze() {
const { bufnr, winnr } = await buffer.open(denops, "maze://");
Expand All @@ -33,7 +33,7 @@ export function main(denops: Denops): void {
await op.modifiable.setLocal(denops, false);
},
};
}
};
```

In this code, we use `op.bufhidden.setLocal` to set the `bufhidden` option to
Expand Down
10 changes: 5 additions & 5 deletions src/tutorial/maze/properly-create-a-virtual-buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ proper virtual buffer that concretizes the buffer content. Let's modify the
`main.ts` file as follows:

```typescript:denops/denops-maze/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v6.0.0/buffer/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.0.0/function/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v6.5.0/buffer/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.5.0/function/mod.ts";
import { Maze } from "https://deno.land/x/[email protected]/mod.js";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async maze() {
const { bufnr, winnr } = await buffer.open(denops, "maze://");
Expand All @@ -32,7 +32,7 @@ export function main(denops: Denops): void {
await buffer.concrete(denops, bufnr);
},
};
}
};
```

In this code, we use `buffer.open` to open a `maze://` buffer and get the buffer
Expand Down
14 changes: 7 additions & 7 deletions src/tutorial/maze/reduce-the-number-of-rpc-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ enhance performance by reducing the number of RPC calls using the `batch` module
from `denops_std`. Let's revise the `main.ts` file as follows:

```typescript:denops/denops-maze/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import { batch, collect } from "https://deno.land/x/denops_std@v6.0.0/batch/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v6.0.0/buffer/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.0.0/function/mod.ts";
import * as op from "https://deno.land/x/denops_std@v6.0.0/option/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import { batch, collect } from "https://deno.land/x/denops_std@v6.5.0/batch/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v6.5.0/buffer/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.5.0/function/mod.ts";
import * as op from "https://deno.land/x/denops_std@v6.5.0/option/mod.ts";
import { Maze } from "https://deno.land/x/[email protected]/mod.js";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async maze() {
const { bufnr, winnr } = await buffer.open(denops, "maze://");
Expand All @@ -36,7 +36,7 @@ export function main(denops: Denops): void {
});
},
};
}
};
```

In this code, we use the `collect` function to gather window size values and the
Expand Down
12 changes: 6 additions & 6 deletions src/tutorial/maze/utilizing-denops-std-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ command.
First, modify the `denops/denops-helloworld/main.ts` file as follows:

```typescript:denops/denops-helloworld/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v6.0.0/buffer/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.0.0/function/mod.ts";
import * as op from "https://deno.land/x/denops_std@v6.0.0/option/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v6.5.0/buffer/mod.ts";
import * as fn from "https://deno.land/x/denops_std@v6.5.0/function/mod.ts";
import * as op from "https://deno.land/x/denops_std@v6.5.0/option/mod.ts";
import { Maze } from "https://deno.land/x/[email protected]/mod.js";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async maze() {
// Get the current window size
Expand Down Expand Up @@ -54,7 +54,7 @@ export function main(denops: Denops): void {
await op.modifiable.setLocal(denops, false);
},
};
}
};
```

Let's break down this code step by step.
Expand Down
6 changes: 3 additions & 3 deletions src/tutorial/maze/utilizing-third-party-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ directory tree will look like this:
The content of the `denops/denops-maze/main.ts` file will be:

```typescript:denops/denops-maze/main.ts
import type { Denops } from "https://deno.land/x/denops_std@v6.0.0/mod.ts";
import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import { Maze } from "https://deno.land/x/[email protected]/mod.js";

export function main(denops: Denops): void {
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
maze() {
const maze = new Maze({}).generate();
const content = maze.getString();
console.log(content);
},
};
}
};
```

The content of the `plugin/denops-maze.vim` file will be:
Expand Down

0 comments on commit ed114f1

Please sign in to comment.