@@ -44,7 +44,7 @@ export type BufnameParams = Record<string, string | string[] | undefined>;
44
44
* scheme expr params fragment
45
45
* ```
46
46
*/
47
- export type Bufname = {
47
+ export interface Bufname {
48
48
// Scheme part of a buffer name. Note that Vim supports only alphabets in scheme part.
49
49
scheme : string ;
50
50
// Expression part of a buffer name. Note that '<>|?*' are not supported in Vim on Windows.
@@ -53,7 +53,7 @@ export type Bufname = {
53
53
params ?: BufnameParams ;
54
54
// Fragment part of a buffer name. This is mainly used to regulate the suffix of the buffer name.
55
55
fragment ?: string ;
56
- } ;
56
+ }
57
57
58
58
// Vim only supports alphabet characters in the scheme part of a buffer name.
59
59
// That behavior has slightly improved from Vim 8.2.3153 but we suppor Vim 8.2.0662
@@ -76,6 +76,74 @@ const exprPattern = /^(.*?)(?:;(.*?))?(?:#(.*))?$/;
76
76
* All unusable characters ("<>|?*) are replaced with percent-encoded characters.
77
77
* In addition to the above, all semicolons (;) and sharps (#) in `path` are replaced with
78
78
* percent-encoded characters. It's required to distinguish `params` and or `fragment`.
79
+ *
80
+ * ```typescript
81
+ * import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
82
+ * import { format } from "../bufname/mod.ts";
83
+ *
84
+ * assertEquals(
85
+ * format({
86
+ * scheme: "denops",
87
+ * expr: "/Users/John Titor/test.git",
88
+ * }),
89
+ * "denops:///Users/John Titor/test.git",
90
+ * );
91
+ *
92
+ * assertEquals(
93
+ * format({
94
+ * scheme: "denops",
95
+ * expr: "/Users/John Titor/test.git",
96
+ * params: {
97
+ * foo: "foo",
98
+ * bar: ["bar1", "bar2"],
99
+ * },
100
+ * }),
101
+ * "denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2",
102
+ * );
103
+ *
104
+ * assertEquals(
105
+ * format({
106
+ * scheme: "denops",
107
+ * expr: "/Users/John Titor/test.git",
108
+ * fragment: "README.md",
109
+ * }),
110
+ * "denops:///Users/John Titor/test.git#README.md",
111
+ * );
112
+ *
113
+ * assertEquals(
114
+ * format({
115
+ * scheme: "denops",
116
+ * expr: "/Users/John Titor/test.git",
117
+ * params: {
118
+ * foo: "foo",
119
+ * bar: ["bar1", "bar2"],
120
+ * },
121
+ * fragment: "README.md",
122
+ * }),
123
+ * "denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2#README.md",
124
+ * );
125
+ * ```
126
+ *
127
+ * This function does not handle path separator differences among platforms (Unix
128
+ * uses `/` but Windows uses `\`). That's why it's recommended to normalize the
129
+ * `expr` with [`toFileUrl`](https://deno.land/std/path#tofileurl) before when
130
+ * constructing a buffer name from a real path. For example
131
+ *
132
+ * ```typescript
133
+ * import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
134
+ * import * as path from "https://deno.land/std/path/mod.ts";
135
+ * import { format } from "../bufname/mod.ts";
136
+ *
137
+ * // NOTE:
138
+ * // Works only on Windows (Use path.win32.toFileUrl instead on other platforms)
139
+ * assertEquals(
140
+ * format({
141
+ * scheme: "denops",
142
+ * expr: path.toFileUrl("C:\\Users\John Titor\test.git").pathname,
143
+ * }),
144
+ * "denops:///C:/Users/John%2520Titor/test.git",
145
+ * );
146
+ * ```
79
147
*/
80
148
export function format (
81
149
{ scheme, expr, params, fragment } : Bufname ,
@@ -100,6 +168,78 @@ export function format(
100
168
*
101
169
* It throws errors when a given `bufname` is not valid Vim's buffer name.
102
170
* For example, if it contains unusable characters ("<>|?*).
171
+ *
172
+ * ```typescript
173
+ * import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
174
+ * import { parse } from "../bufname/mod.ts";
175
+ *
176
+ * assertEquals(
177
+ * parse("denops:///Users/John Titor/test.git"),
178
+ * {
179
+ * scheme: "denops",
180
+ * expr: "/Users/John Titor/test.git",
181
+ * },
182
+ * );
183
+ *
184
+ * assertEquals(
185
+ * parse("denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2"),
186
+ * {
187
+ * scheme: "denops",
188
+ * expr: "/Users/John Titor/test.git",
189
+ * params: {
190
+ * foo: "foo",
191
+ * bar: ["bar1", "bar2"],
192
+ * },
193
+ * },
194
+ * );
195
+ *
196
+ * assertEquals(
197
+ * parse("denops:///Users/John Titor/test.git#README.md"),
198
+ * {
199
+ * scheme: "denops",
200
+ * expr: "/Users/John Titor/test.git",
201
+ * fragment: "README.md",
202
+ * },
203
+ * );
204
+ *
205
+ * assertEquals(
206
+ * parse(
207
+ * "denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2#README.md",
208
+ * ),
209
+ * {
210
+ * scheme: "denops",
211
+ * expr: "/Users/John Titor/test.git",
212
+ * params: {
213
+ * foo: "foo",
214
+ * bar: ["bar1", "bar2"],
215
+ * },
216
+ * fragment: "README.md",
217
+ * },
218
+ * );
219
+ * ```
220
+ *
221
+ * This function does not handle path separator differences among platforms. That's
222
+ * why it's recommended to restore the `expr` with
223
+ * [`fromFileUrl`](https://deno.land/std/path#fromfileurl) after if a buffer name
224
+ * was constructed from a real path. For example
225
+ *
226
+ * ```typescript
227
+ * import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
228
+ * import * as path from "https://deno.land/std/path/mod.ts";
229
+ * import { parse } from "../bufname/mod.ts";
230
+ *
231
+ * const bufname = parse("denops:///C:/Users/John%2520Titor/test.git");
232
+ * assertEquals(bufname, {
233
+ * scheme: "denops",
234
+ * expr: "/C:/Users/John%20Titor/test.git",
235
+ * });
236
+ * // NOTE:
237
+ * // Works only on Windows (Use path.win32.fromFileUrl instead on other platforms)
238
+ * assertEquals(
239
+ * path.fromFileUrl(`file://${bufname.expr}`),
240
+ * "C:\\Users\\John Titor\\test.git",
241
+ * );
242
+ * ```
103
243
*/
104
244
export function parse ( bufname : string ) : Bufname {
105
245
if ( bufnameUnusablePattern . test ( bufname ) ) {
0 commit comments