Skip to content

Commit f0acc60

Browse files
committed
feat: add absolutePath renderer
1 parent 982df82 commit f0acc60

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

builtin/renderer/absolute_path.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 { defineRenderer, type Renderer } from "../../renderer.ts";
6+
7+
type Detail = {
8+
path: string;
9+
};
10+
11+
/**
12+
* Options for the absolutePath renderer.
13+
*/
14+
export type AbsolutePathOptions = {
15+
/**
16+
* The base directory to calculate the absolute path. If not specified, the
17+
* current working directory is used.
18+
*/
19+
base?: string;
20+
};
21+
22+
/**
23+
* Creates a Renderer that replace file path in the label to absolute paths.
24+
*
25+
* @returns A Renderer that replace file path in the label to absolute paths.
26+
*/
27+
export function absolutePath(
28+
options: AbsolutePathOptions = {},
29+
): Renderer<Detail> {
30+
return defineRenderer(async (denops, { items }, { signal }) => {
31+
// Get the current working directory
32+
const base = options.base ?? await fn.getcwd(denops);
33+
signal?.throwIfAborted();
34+
35+
// Convert relative path in label to absolute path
36+
items.forEach((item) => {
37+
const abspath = isAbsolute(item.detail.path)
38+
? item.detail.path
39+
: join(base, item.detail.path);
40+
item.label = item.label.replace(item.detail.path, abspath);
41+
});
42+
});
43+
}

builtin/renderer/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 "./helptag.ts";
34
export * from "./nerdfont.ts";
45
export * from "./noop.ts";

deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"./builtin/refiner/regexp": "./builtin/refiner/regexp.ts",
4545
"./builtin/refiner/relative-path": "./builtin/refiner/relative_path.ts",
4646
"./builtin/renderer": "./builtin/renderer/mod.ts",
47+
"./builtin/renderer/absolute-path": "./builtin/renderer/absolute_path.ts",
4748
"./builtin/renderer/helptag": "./builtin/renderer/helptag.ts",
4849
"./builtin/renderer/nerdfont": "./builtin/renderer/nerdfont.ts",
4950
"./builtin/renderer/noop": "./builtin/renderer/noop.ts",

0 commit comments

Comments
 (0)