Skip to content

Commit

Permalink
Merge pull request #13 from vim-denops/use-entrypoint
Browse files Browse the repository at this point in the history
📝 Use `Entrypoint` style in example codes
  • Loading branch information
lambdalisue committed May 14, 2024
2 parents 6aeb2f2 + e316b72 commit 4e10ba0
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 79 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/mdbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ jobs:
run: |
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf -y | sh
rustup update
cargo install --version ${MDBOOK_VERSION} mdbook
cargo install mdbook-alerts
cargo install --force --version ${MDBOOK_VERSION} mdbook
cargo install --force mdbook-alerts
- name: Setup Pages
id: pages
uses: actions/configure-pages@v4
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ env:

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
test:
Expand All @@ -30,8 +33,8 @@ jobs:
run: |
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf -y | sh
rustup update
cargo install --version ${MDBOOK_VERSION} mdbook
cargo install mdbook-alerts
cargo install --force --version ${MDBOOK_VERSION} mdbook
cargo install --force mdbook-alerts
- name: Build with mdBook
run: mdbook build
- name: Format
Expand Down
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
8 changes: 4 additions & 4 deletions src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ 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)
(2024-02-03)
- [denops_std v6.0.0](https://github.com/vim-denops/deno-denops-std/releases/tag/v6.0.0)
(2024-02-03)
- [denops.vim v6.0.7](https://github.com/vim-denops/denops.vim/releases/tag/v6.0.7)
(2024-05-15)
- [denops_std v6.5.0](https://github.com/vim-denops/deno-denops-std/releases/tag/v6.5.0)
(2024-05-15)

[vim-jp]: https://vim-jp.org/
[denops.vim]: https://github.com/vim-denops/denops.vim
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
Loading

0 comments on commit 4e10ba0

Please sign in to comment.