|
| 1 | +import { bufnameUnusablePattern, decode, encode } from "./utils.ts"; |
| 2 | + |
| 3 | +export type BufnameParams = Record<string, string | string[] | undefined>; |
| 4 | + |
| 5 | +export type Bufname = { |
| 6 | + // Scheme part of a buffer name. Note that Vim supports only alphabets in scheme part. |
| 7 | + scheme: string; |
| 8 | + // Path part of a buffer name. Note that '<>|?*' are not supported in Vim on Windows. |
| 9 | + path: string; |
| 10 | + // Params part of a buffer name. While '?' is not supported, the params part are splitted by ';' instead. |
| 11 | + params?: BufnameParams; |
| 12 | + // Fragment part of a buffer name. This is mainly used to regulate the suffix of the buffer name. |
| 13 | + fragment?: string; |
| 14 | +}; |
| 15 | + |
| 16 | +// Vim only supports alphabet characters in the scheme part of a buffer name. |
| 17 | +// That behavior has slightly improved from Vim 8.2.3153 but we suppor Vim 8.2.0662 |
| 18 | +// thus we need to stack old behavior |
| 19 | +// https://github.com/vim/vim/pull/8299 |
| 20 | +const schemeUnusablePattern = /[^a-zA-Z]/; |
| 21 | + |
| 22 | +// Vim assumes a bufname is URL when it starts with "name://" |
| 23 | +// https://github.com/vim/vim/blob/36698e34aacee4186e6f5f87f431626752fcb337/src/misc1.c#L2567-L2580 |
| 24 | +const schemePattern = /^(\S+):\/\//; |
| 25 | + |
| 26 | +// The path part might contains query and/or fragment |
| 27 | +const pathPattern = /^(.*?)(?:;(.*?))?(?:#(.*))?$/; |
| 28 | + |
| 29 | +/** |
| 30 | + * Format Bufname instance to construct Vim's buffer name. |
| 31 | + */ |
| 32 | +export function format({ scheme, path, params, fragment }: Bufname): string { |
| 33 | + if (schemeUnusablePattern.test(scheme)) { |
| 34 | + throw new Error( |
| 35 | + `Scheme '${scheme}' contains unusable characters. Only alphabets are allowed.`, |
| 36 | + ); |
| 37 | + } |
| 38 | + const encodedPath = encode(path).replaceAll(";", "%3b").replaceAll( |
| 39 | + "#", |
| 40 | + "%23", |
| 41 | + ); |
| 42 | + const suffix1 = params |
| 43 | + ? `;${encode(toURLSearchParams(params).toString())}` |
| 44 | + : ""; |
| 45 | + const suffix2 = fragment ? `#${encode(fragment)}` : ""; |
| 46 | + return `${scheme}://${encodedPath}${suffix1}${suffix2}`; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Parse Vim's buffer name to construct Bufname instance. |
| 51 | + */ |
| 52 | +export function parse(expr: string): Bufname { |
| 53 | + if (bufnameUnusablePattern.test(expr)) { |
| 54 | + throw new Error( |
| 55 | + `Expression '${expr}' contains unusable characters. Vim (on Windows) does not support '<>|?*' in a buffer name.`, |
| 56 | + ); |
| 57 | + } |
| 58 | + // Check if the bufname is remote |
| 59 | + const m1 = expr.match(schemePattern); |
| 60 | + if (!m1) { |
| 61 | + throw new Error(`Expression '${expr}' does not start from '{scheme}://'.`); |
| 62 | + } |
| 63 | + const scheme = m1[1]; |
| 64 | + // Vim (less than 8.2.3153) only supports alphabets in scheme part |
| 65 | + if (schemeUnusablePattern.test(scheme)) { |
| 66 | + throw new Error( |
| 67 | + `Scheme '${scheme}' contains unusable characters. Only alphabets are allowed.`, |
| 68 | + ); |
| 69 | + } |
| 70 | + const remain = decode(expr.substring(`${scheme}://`.length), [ |
| 71 | + "%3b", // ; |
| 72 | + "%23", // # |
| 73 | + ]); |
| 74 | + const m2 = remain.match(pathPattern)!; |
| 75 | + const path = decode(m2[1]); |
| 76 | + const params = m2[2] |
| 77 | + ? fromURLSearchParams(new URLSearchParams(decode(m2[2]))) |
| 78 | + : undefined; |
| 79 | + const fragment = m2[3] ? decode(m2[3]) : undefined; |
| 80 | + return { |
| 81 | + scheme, |
| 82 | + path, |
| 83 | + ...(params ? { params } : {}), |
| 84 | + ...(fragment ? { fragment } : {}), |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +function toURLSearchParams(params: BufnameParams): URLSearchParams { |
| 89 | + const cs = Object.entries(params).flatMap( |
| 90 | + ([key, value]): [string, string][] => { |
| 91 | + if (value == undefined) { |
| 92 | + return []; |
| 93 | + } else if (Array.isArray(value)) { |
| 94 | + return value.map((v) => [key, v]); |
| 95 | + } else { |
| 96 | + return [[key, value]]; |
| 97 | + } |
| 98 | + }, |
| 99 | + ); |
| 100 | + return new URLSearchParams(cs); |
| 101 | +} |
| 102 | + |
| 103 | +function fromURLSearchParams(search: URLSearchParams): BufnameParams { |
| 104 | + const params: BufnameParams = {}; |
| 105 | + search.forEach((value, key) => { |
| 106 | + if (Object.prototype.hasOwnProperty.call(params, key)) { |
| 107 | + const previous = params[key]; |
| 108 | + if (Array.isArray(previous)) { |
| 109 | + previous.push(value); |
| 110 | + return; |
| 111 | + } else if (previous != undefined) { |
| 112 | + params[key] = [previous, value]; |
| 113 | + return; |
| 114 | + } |
| 115 | + } |
| 116 | + params[key] = value; |
| 117 | + }); |
| 118 | + return params; |
| 119 | +} |
0 commit comments