Skip to content

Commit d05f9d9

Browse files
authored
Merge pull request #67 from vim-denops/mapping
👍 Add `mapping` module to manage mappings
2 parents 754cf29 + f2731d8 commit d05f9d9

File tree

6 files changed

+770
-0
lines changed

6 files changed

+770
-0
lines changed

denops_std/mapping/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# mapping
2+
3+
`mapping` is a module to provide helper functions to manage mappings.
4+
5+
- [API documentation](https://doc.deno.land/https/deno.land/x/denops_std/mapping/mod.ts)
6+
7+
## Usage
8+
9+
### map
10+
11+
Use `map()` to register a mapping like:
12+
13+
```typescript
14+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
15+
import * as mapping from "https://deno.land/x/denops_std/mapping/mod.ts";
16+
17+
export async function main(denops: Denops): Promise<void> {
18+
await mapping.map(denops, "<Plug>(test-denops-std)", "Hello");
19+
await mapping.map(denops, "<Plug>(test-denops-std)", "Hello", {
20+
mode: "i",
21+
});
22+
}
23+
```
24+
25+
### unmap
26+
27+
Use `unmap()` to unregister a mapping like:
28+
29+
```typescript
30+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
31+
import * as mapping from "https://deno.land/x/denops_std/mapping/mod.ts";
32+
33+
export async function main(denops: Denops): Promise<void> {
34+
await mapping.map(denops, "<Plug>(test-denops-std)", "Hello");
35+
await mapping.map(denops, "<Plug>(test-denops-std)", "Hello", {
36+
mode: "i",
37+
});
38+
39+
await mapping.unmap(denops, "<Plug>(test-denops-std)");
40+
await mapping.unmap(denops, "<Plug>(test-denops-std)", {
41+
mode: "i",
42+
});
43+
}
44+
```
45+
46+
### read
47+
48+
Use `read()` to read a mapping like:
49+
50+
```typescript
51+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
52+
import * as mapping from "https://deno.land/x/denops_std/mapping/mod.ts";
53+
54+
export async function main(denops: Denops): Promise<void> {
55+
await denops.cmd(`map <Plug>(test-denops-std) Hello`);
56+
57+
console.log(await mapping.read(denops, "<Plug>(test-denops-std)"));
58+
// {
59+
// mode: "",
60+
// lhs: "<Plug>(test-denops-std)",
61+
// rhs: "Hello",
62+
// // ...
63+
// }
64+
}
65+
```
66+
67+
### list
68+
69+
Use `list()` to list mappings like:
70+
71+
```typescript
72+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
73+
import * as mapping from "https://deno.land/x/denops_std/mapping/mod.ts";
74+
75+
export async function main(denops: Denops): Promise<void> {
76+
const result = await mapping.list(denops, "<Plug>");
77+
console.log(result);
78+
// [
79+
// {
80+
// mode: "",
81+
// lhs: "<Plug>(...)",
82+
// // ...
83+
// },
84+
// // ...
85+
// ]
86+
}
87+
```

denops_std/mapping/mod.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { Denops } from "../deps.ts";
2+
import * as fn from "../function/mod.ts";
3+
import { Mapping, Mode } from "./types.ts";
4+
import { parse } from "./parser.ts";
5+
6+
export type MapOptions = {
7+
mode?: Mode;
8+
noremap?: boolean;
9+
script?: boolean;
10+
buffer?: boolean;
11+
expr?: boolean;
12+
nowait?: boolean;
13+
silent?: boolean;
14+
unique?: boolean;
15+
};
16+
17+
/**
18+
* Register a mapping for `lhs` to `rhs` with given options.
19+
*/
20+
export async function map(
21+
denops: Denops,
22+
lhs: string,
23+
rhs: string,
24+
options: MapOptions = {},
25+
): Promise<void> {
26+
const mode = options.mode ?? "";
27+
const prefix = options.noremap ? "nore" : "";
28+
const arg = [
29+
options.buffer ? "<buffer>" : "",
30+
options.nowait ? "<nowait>" : "",
31+
options.silent ? "<silent>" : "",
32+
options.script ? "<script>" : "",
33+
options.expr ? "<expr>" : "",
34+
options.unique ? "<unique>" : "",
35+
].filter((v) => v).join("");
36+
await denops.cmd(`${mode}${prefix}map ${arg} ${lhs} ${rhs}`);
37+
}
38+
39+
export type UnmapOptions = {
40+
mode?: Mode;
41+
};
42+
43+
/**
44+
* Remove a mapping for `lhs`.
45+
*/
46+
export async function unmap(
47+
denops: Denops,
48+
lhs: string,
49+
options: UnmapOptions = {},
50+
): Promise<void> {
51+
const mode = options.mode ?? "";
52+
await denops.cmd(`${mode}unmap ${lhs}`);
53+
}
54+
55+
export type ReadOptions = {
56+
mode?: Mode;
57+
};
58+
59+
/**
60+
* Read a mapping and return a `Mapping` instance.
61+
*
62+
* Note that prior to Vim 8.2.0491 or Neovim 0.5.0, `script` is
63+
* not detectable by `maparg` function internally used in this
64+
* function.
65+
*/
66+
export async function read(
67+
denops: Denops,
68+
lhs: string,
69+
options: ReadOptions = {},
70+
): Promise<Mapping> {
71+
const info = await fn.maparg(
72+
denops,
73+
lhs,
74+
options.mode ?? "",
75+
false,
76+
true,
77+
) as Record<string, unknown>;
78+
if (Object.keys(info).length === 0) {
79+
throw new Error(`No mapping found for '${lhs}'`);
80+
}
81+
return {
82+
mode: (info.mode as string).trim() as Mode,
83+
lhs: info.lhs as string,
84+
rhs: info.rhs as string,
85+
noremap: !!info.noremap,
86+
script: !!info.script,
87+
buffer: !!info.buffer,
88+
sid: info.sid as number,
89+
lnum: info.lnum as number,
90+
expr: !!info.expr,
91+
nowait: !!info.nowait,
92+
silent: !!info.silent,
93+
};
94+
}
95+
96+
export type ListOptions = {
97+
mode?: Mode;
98+
};
99+
100+
/**
101+
* List mappings which starts from `lhs`.
102+
*/
103+
export async function list(
104+
denops: Denops,
105+
lhs: string,
106+
options: ListOptions = {},
107+
): Promise<Mapping[]> {
108+
const mode = options.mode ?? "";
109+
const result = await fn.execute(denops, `${mode}map ${lhs}`) as string;
110+
return result.split(/\r?\n/).filter((v) => v).map(parse);
111+
}

0 commit comments

Comments
 (0)