Skip to content

Commit 6a7196a

Browse files
committed
📝 Add tips to use a real path in bufname module
1 parent 2f2c9b2 commit 6a7196a

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

denops_std/bufname/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@ assertEquals(
9999
);
100100
```
101101

102+
This function does not handle path separator differences among platforms (Unix
103+
uses `/` but Windows uses `\`). That's why it's recommended to normalize the
104+
`expr` with [`toFileUrl`](https://deno.land/std/path#tofileurl) before when
105+
constructing a buffer name from a real path. For example
106+
107+
```typescript
108+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
109+
import * as path from "https://deno.land/std/path/mod.ts";
110+
import { format } from "https://deno.land/x/denops_std/bufname/mod.ts";
111+
112+
// NOTE:
113+
// Works only on Windows (Use path.win32.toFileUrl instead on other platforms)
114+
assertEquals(
115+
format({
116+
scheme: "denops",
117+
expr: path.toFileUrl("C:\\Users\John Titor\test.git").pathname,
118+
}),
119+
"denops:///C:/Users/John%20Titor/test.git",
120+
);
121+
```
122+
102123
### parse
103124

104125
Use `parse()` to parse Vim's buffer name and get a `Bufname` instance like
@@ -151,3 +172,26 @@ assertEquals(
151172
},
152173
);
153174
```
175+
176+
This function does not handle path separator differences among platforms. That's
177+
why it's recommended to restore the `expr` with
178+
[`fromFileUrl`](https://deno.land/std/path#fromfileurl) after if a buffer name
179+
was constructed from a real path. For example
180+
181+
```typescript
182+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
183+
import * as path from "https://deno.land/std/path/mod.ts";
184+
import { parse } from "https://deno.land/x/denops_std/bufname/mod.ts";
185+
186+
const bufname = parse("denops:///C:/Users/John%20Titor/test.git");
187+
assertEquals(bufname, {
188+
scheme: "denops",
189+
expr: "/C:/Users/John Titor/test.git",
190+
});
191+
// NOTE:
192+
// Works only on Windows (Use path.win32.fromFileUrl instead on other platforms)
193+
assertEquals(
194+
path.fromFileUrl(`file://${bufname.expr}`),
195+
"C:\\Users\\John Titor\\test.git",
196+
);
197+
```

denops_std/bufname/bufname_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assertEquals, assertThrows } from "../deps_test.ts";
2+
import { path } from "../deps.ts";
23
import { format, parse } from "./bufname.ts";
34

45
Deno.test("format throws exception when 'scheme' contains unusable characters", () => {
@@ -191,6 +192,14 @@ Deno.test("format pass example in README.md", () => {
191192
}),
192193
"denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2#README.md",
193194
);
195+
196+
assertEquals(
197+
format({
198+
scheme: "denops",
199+
expr: path.win32.toFileUrl("C:\\Users\\John Titor\\test.git").pathname,
200+
}),
201+
"denops:///C:/Users/John%20Titor/test.git",
202+
);
194203
});
195204

196205
Deno.test("parse throws exception when 'expr' contains unusable characters", () => {
@@ -373,4 +382,17 @@ Deno.test("parse pass example in README.md", () => {
373382
fragment: "README.md",
374383
},
375384
);
385+
386+
const bufname = parse("denops:///C:/Users/John%20Titor/test.git");
387+
assertEquals(
388+
bufname,
389+
{
390+
scheme: "denops",
391+
expr: "/C:/Users/John Titor/test.git",
392+
},
393+
);
394+
assertEquals(
395+
path.win32.fromFileUrl(`file://${bufname.expr}`),
396+
"C:\\Users\\John Titor\\test.git",
397+
);
376398
});

0 commit comments

Comments
 (0)