Skip to content

Commit d2b35a5

Browse files
committed
feat: add absolutePath refiner
1 parent fc8c106 commit d2b35a5

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

builtin/refiner/absolute_path.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as fn from "@denops/std/function";
2+
import { join } from "@std/path/join";
3+
import { isAbsolute } from "@std/path/is-absolute";
4+
5+
import { defineRefiner, type Refiner } from "../../refiner.ts";
6+
7+
type Detail = {
8+
path: string;
9+
};
10+
11+
type DetailAfter = {
12+
abspath: string;
13+
};
14+
15+
/**
16+
* Options for the `absolutePath` Refiner.
17+
*/
18+
export type AbsolutePathOptions = {
19+
/**
20+
* The base directory to calculate the absolute path from.
21+
*/
22+
base?: string;
23+
};
24+
25+
/**
26+
* Creates a Projector that converts file path in value to absolute paths.
27+
*/
28+
export function absolutePath(
29+
options: AbsolutePathOptions = {},
30+
): Refiner<Detail, DetailAfter> {
31+
return defineRefiner(
32+
async function* (denops, { items }, { signal }) {
33+
// Get the current working directory
34+
const base = options.base ?? await fn.getcwd(denops);
35+
signal?.throwIfAborted();
36+
37+
for await (const item of items) {
38+
const abspath = isAbsolute(item.detail.path)
39+
? item.detail.path
40+
: join(base, item.detail.path);
41+
const value = item.value.replace(item.detail.path, abspath);
42+
43+
yield {
44+
...item,
45+
value,
46+
detail: {
47+
...item.detail,
48+
abspath,
49+
},
50+
};
51+
}
52+
},
53+
);
54+
}

builtin/refiner/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// This file is generated by gen-mod.ts
2+
export * from "./absolute_path.ts";
23
export * from "./cwd.ts";
34
export * from "./exists.ts";
45
export * from "./noop.ts";

deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"./builtin/previewer/helptag": "./builtin/previewer/helptag.ts",
3838
"./builtin/previewer/noop": "./builtin/previewer/noop.ts",
3939
"./builtin/refiner": "./builtin/refiner/mod.ts",
40+
"./builtin/refiner/absolute-path": "./builtin/refiner/absolute_path.ts",
4041
"./builtin/refiner/cwd": "./builtin/refiner/cwd.ts",
4142
"./builtin/refiner/exists": "./builtin/refiner/exists.ts",
4243
"./builtin/refiner/noop": "./builtin/refiner/noop.ts",

0 commit comments

Comments
 (0)