1
1
# bufname
2
2
3
- ` bufname ` is a module to provide manage Vim's buffer name.
3
+ ` bufname ` is a module to provide functions to handle Vim's buffer name.
4
4
5
5
- [ API documentation] ( https://doc.deno.land/https/deno.land/x/denops_std/bufname/mod.ts )
6
6
7
+ The format of the buffer name assumed in this module is like
8
+
9
+ ``` text
10
+ {scheme}://{expr}[;{params}][#{fragment}]
11
+ ```
12
+
13
+ Where
14
+
15
+ - ` {scheme} ` is used to distinguish a buffer kind. It contains only alphabet
16
+ characters.
17
+ - ` {expr} ` is used to identify a buffer itself. _ Unusable characters_ ,
18
+ semicolons (;), and sharps (#) are replaced with percent-encoded characters.
19
+ - ` {params} ` (Optional) is used to add meta information to the buffer name like
20
+ query parameters of URL. _ Unusable characters_ and sharps (#) are replaced
21
+ with percent-encoded characters.
22
+ - ` {fragment} ` (Optional) is used to add a suffix to the buffer name for file
23
+ type detection or so on. _ Unusable characters_ are replaced with
24
+ percent-encoded characters.
25
+ - _ Unusable characters_ are ` " ` , ` < ` , ` > ` , ` | ` , ` ? ` , or ` * ` . It is caused by the
26
+ limitations of Vim on Windows.
27
+
28
+ For example,
29
+
30
+ ``` text
31
+ denops:///Users/John Titor/test.git
32
+ └─┬──┘ └───────────┬────────────┘
33
+ scheme expr
34
+
35
+ denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2
36
+ └─┬──┘ └───────────┬────────────┘ └───────────┬───────────┘
37
+ scheme expr params
38
+
39
+ denops:///Users/John Titor/test.git#README.md
40
+ └─┬──┘ └───────────┬────────────┘ └───┬───┘
41
+ scheme expr fragment
42
+
43
+ denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2#README.md
44
+ └─┬──┘ └───────────┬────────────┘ └───────────┬───────────┘ └───┬───┘
45
+ scheme expr params fragment
46
+ ```
47
+
7
48
## Usage
8
49
9
50
### format
@@ -13,88 +54,144 @@ instance like:
13
54
14
55
``` typescript
15
56
import { assertEquals } from " https://deno.land/std/testing/asserts.ts" ;
16
- import { Denops } from " https://deno.land/x/denops_std/mod.ts" ;
17
57
import { format } from " https://deno.land/x/denops_std/bufname/mod.ts" ;
18
58
19
- export async function main(denops : Denops ): Promise <void > {
20
- assertEquals (
21
- format ({ scheme: " denops" , path: " /Users/johntitor/.vimrc" }),
22
- " denops:///Users/johntitor/.vimrc" ,
23
- );
24
-
25
- assertEquals (
26
- format ({
27
- scheme: " denops" ,
28
- path: " /Users/johntitor/.vimrc" ,
29
- params: {
30
- foo: " foo" ,
31
- bar: [" bar" , " bar" ],
32
- hoge: undefined ,
33
- },
34
- }),
35
- " denops:///Users/johntitor/.vimrc;foo=foo&bar=bar&bar=bar" ,
36
- );
37
-
38
- assertEquals (
39
- format ({
40
- scheme: " denops" ,
41
- path: " /Users/johntitor/.vimrc" ,
42
- params: {
43
- foo: " foo" ,
44
- bar: [" bar" , " bar" ],
45
- hoge: undefined ,
46
- },
47
- fragment: " README.md" ,
48
- }),
49
- " denops:///Users/johntitor/.vimrc;foo=foo&bar=bar&bar=bar#README.md" ,
50
- );
51
- }
59
+ assertEquals (
60
+ format ({
61
+ scheme: " denops" ,
62
+ expr: " /Users/John Titor/test.git" ,
63
+ }),
64
+ " denops:///Users/John Titor/test.git" ,
65
+ );
66
+
67
+ assertEquals (
68
+ format ({
69
+ scheme: " denops" ,
70
+ expr: " /Users/John Titor/test.git" ,
71
+ params: {
72
+ foo: " foo" ,
73
+ bar: [" bar1" , " bar2" ],
74
+ },
75
+ }),
76
+ " denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2" ,
77
+ );
78
+
79
+ assertEquals (
80
+ format ({
81
+ scheme: " denops" ,
82
+ expr: " /Users/John Titor/test.git" ,
83
+ fragment: " README.md" ,
84
+ }),
85
+ " denops:///Users/John Titor/test.git#README.md" ,
86
+ );
87
+
88
+ assertEquals (
89
+ format ({
90
+ scheme: " denops" ,
91
+ expr: " /Users/John Titor/test.git" ,
92
+ params: {
93
+ foo: " foo" ,
94
+ bar: [" bar1" , " bar2" ],
95
+ },
96
+ fragment: " README.md" ,
97
+ }),
98
+ " denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2#README.md" ,
99
+ );
52
100
```
53
101
54
- Note that unusable characters (` " ` , ` < ` , ` > ` , ` | ` , ` ? ` , ` * ` ) are replaced with
55
- percent-encoded characters.
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\J ohn Titor\t est.git" ).pathname ,
118
+ }),
119
+ " denops:///C:/Users/John%20Titor/test.git" ,
120
+ );
121
+ ```
56
122
57
123
### parse
58
124
59
125
Use ` parse() ` to parse Vim's buffer name and get a ` Bufname ` instance like
60
126
61
127
``` typescript
62
128
import { assertEquals } from " https://deno.land/std/testing/asserts.ts" ;
63
- import { Denops } from " https://deno.land/x/denops_std/mod.ts" ;
64
129
import { parse } from " https://deno.land/x/denops_std/bufname/mod.ts" ;
65
130
66
- export async function main(denops : Denops ): Promise <void > {
67
- assertEquals (parse (" denops:///Users/johntitor/.vimrc" ), {
131
+ assertEquals (
132
+ parse (" denops:///Users/John Titor/test.git" ),
133
+ {
68
134
scheme: " denops" ,
69
- path : " /Users/johntitor/.vimrc " ,
70
- });
71
-
72
- assertEquals (
73
- parse ( " denops:///Users/johntitor/.vimrc;foo=foo&bar=bar&bar=bar " ),
74
- {
75
- scheme: " denops " ,
76
- path : " /Users/johntitor/.vimrc " ,
77
- params: {
78
- foo: " foo " ,
79
- bar: [ " bar " , " bar " ] ,
80
- } ,
135
+ expr : " /Users/John Titor/test.git " ,
136
+ },
137
+ );
138
+
139
+ assertEquals (
140
+ parse ( " denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2 " ),
141
+ {
142
+ scheme : " denops " ,
143
+ expr: " /Users/John Titor/test.git " ,
144
+ params: {
145
+ foo: " foo " ,
146
+ bar: [ " bar1 " , " bar2 " ] ,
81
147
},
82
- );
83
-
84
- assertEquals (
85
- parse (" denops:///Users/johntitor/.vimrc;foo=foo&bar=bar&bar=bar#README.md" ),
86
- {
87
- scheme: " denops" ,
88
- path: " /Users/johntitor/.vimrc" ,
89
- params: {
90
- foo: " foo" ,
91
- bar: [" bar" , " bar" ],
92
- },
93
- fragment: " README.md" ,
148
+ },
149
+ );
150
+
151
+ assertEquals (
152
+ parse (" denops:///Users/John Titor/test.git#README.md" ),
153
+ {
154
+ scheme: " denops" ,
155
+ expr: " /Users/John Titor/test.git" ,
156
+ fragment: " README.md" ,
157
+ },
158
+ );
159
+
160
+ assertEquals (
161
+ parse (
162
+ " denops:///Users/John Titor/test.git;foo=foo&bar=bar1&bar=bar2#README.md" ,
163
+ ),
164
+ {
165
+ scheme: " denops" ,
166
+ expr: " /Users/John Titor/test.git" ,
167
+ params: {
168
+ foo: " foo" ,
169
+ bar: [" bar1" , " bar2" ],
94
170
},
95
- );
96
- }
171
+ fragment: " README.md" ,
172
+ },
173
+ );
97
174
```
98
175
99
- Note that percent-encoded characters of unusable characters (` " ` , ` < ` , ` > ` , ` | ` ,
100
- ` ? ` , ` * ` ) are restored.
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
+ ```
0 commit comments