@narumitw/pi-inline 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +13 -32
- package/package.json +1 -1
- package/src/filelinks.ts +8 -33
- package/src/hover-preview.ts +16 -31
- package/src/inline-markup-extension.ts +4 -12
- package/src/settings.ts +7 -65
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @narumitw/pi-inline
|
|
2
2
|
|
|
3
|
+
## 0.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 502e95d: Preview local text files outside the workspace by hovering over Markdown links in fullscreen mode. The `externalPreviewRoots` setting is now ignored, including empty lists that previously disabled external previews. External images, symbolic links, and `node_modules` are still excluded. Hover previews stay on screen and are not sent to the model or stored in the session.
|
|
8
|
+
|
|
3
9
|
## 0.5.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Style badges and preview local file citations in Pi's terminal transcript withou
|
|
|
8
8
|
## ✨ Features
|
|
9
9
|
|
|
10
10
|
- Display decorative badges, linked badges, and local Markdown links in user and assistant messages.
|
|
11
|
-
- Hover over a file link in fullscreen mode to preview
|
|
11
|
+
- Hover over a file link in fullscreen mode to preview local text files, including files outside the workspace, or workspace images.
|
|
12
12
|
- Turn on filename lookup for wikilinks in the current session.
|
|
13
13
|
- Edit or turn off the system prompt hint for badges and file citations.
|
|
14
14
|
|
|
@@ -49,8 +49,10 @@ extension. Do not load both copies. Use `/inline` to open the settings menu.
|
|
|
49
49
|
|
|
50
50
|
Start Pi in a directory with a `README.md` file. Send a message containing
|
|
51
51
|
`[README](./README.md)` to see a file icon next to the label. In fullscreen mode,
|
|
52
|
-
hover over the link to preview the file.
|
|
53
|
-
|
|
52
|
+
hover over the link to preview the file. Absolute and relative paths outside the
|
|
53
|
+
workspace also work for text previews without any setup. If Pi detects Kitty
|
|
54
|
+
graphics support, hover over a workspace image link such as
|
|
55
|
+
`[Cat Photo](./cat.png)` to see the image.
|
|
54
56
|
|
|
55
57
|
## 💬 Commands
|
|
56
58
|
|
|
@@ -105,13 +107,12 @@ and refreshes five seconds after each scan.
|
|
|
105
107
|
|
|
106
108
|
## ⚙️ Settings
|
|
107
109
|
|
|
108
|
-
|
|
110
|
+
Prompt hint settings use `pi-inline.json` in Pi's agent directory (by default
|
|
109
111
|
`~/.pi/agent/`). Project settings are ignored. Use a regular file, not a
|
|
110
112
|
symbolic link. If its content is invalid, repair it before saving through the
|
|
111
113
|
menu. Settings writes are ordered within one Pi process, not between processes.
|
|
112
114
|
Finish saving before editing the file by hand or from another Pi process.
|
|
113
|
-
|
|
114
|
-
### Prompt hint
|
|
115
|
+
The former `externalPreviewRoots` field is ignored and can be removed.
|
|
115
116
|
|
|
116
117
|
`promptHintEnabled` accepts a boolean and defaults to `true`. `promptHintText`
|
|
117
118
|
accepts a non-blank string, including multiple lines. Line endings are saved as
|
|
@@ -123,34 +124,14 @@ Use [[label]] for badges.
|
|
|
123
124
|
Use Markdown links with workspace-relative paths for file citations, for example [file](./path/to/file#L12-L20).
|
|
124
125
|
```
|
|
125
126
|
|
|
126
|
-
### External text previews
|
|
127
|
-
|
|
128
|
-
To allow external text previews, set `externalPreviewRoots`:
|
|
129
|
-
|
|
130
|
-
```json
|
|
131
|
-
{
|
|
132
|
-
"externalPreviewRoots": ["/home/alex/.agents/plans"]
|
|
133
|
-
}
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
A conversation link can trigger a file read inside an approved directory when
|
|
137
|
-
you hover over it. Approve only narrow directories you control, not shared
|
|
138
|
-
directories. Use existing absolute directories without symbolic links. Do not
|
|
139
|
-
use globs, a filesystem root, your home directory, or a directory that contains
|
|
140
|
-
your home directory.
|
|
141
|
-
|
|
142
|
-
The default is `[]`. Invalid roots or an unreadable file leave external
|
|
143
|
-
previews off. Changes take effect on session start or `/reload`. To revoke
|
|
144
|
-
access, remove the field or set it to `[]`, then run `/reload`. Link icons do not
|
|
145
|
-
depend on this setting.
|
|
146
|
-
|
|
147
127
|
## 🔒 Security and privacy
|
|
148
128
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
129
|
+
Hovering over a local Markdown link can display a file Pi can read, even outside
|
|
130
|
+
the workspace. This includes hidden files and files that contain secrets. Avoid
|
|
131
|
+
hovering over untrusted links, especially when sharing your screen. Previews
|
|
132
|
+
exclude `node_modules` and symbolic links. External PNG, JPEG, GIF, and WebP
|
|
133
|
+
links can be opened but not previewed. The extension does not add preview
|
|
134
|
+
contents to model context or stored messages.
|
|
154
135
|
|
|
155
136
|
In the terminal UI, Pi sends the enabled prompt hint to the model provider and
|
|
156
137
|
can retain it in session history. Do not put secrets in the hint. Editing or
|
package/package.json
CHANGED
package/src/filelinks.ts
CHANGED
|
@@ -21,12 +21,7 @@ export interface PreviewExcerpt {
|
|
|
21
21
|
lineRange?: LineRange;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
path: string;
|
|
26
|
-
identity: Pick<BigIntStats, "dev" | "ino">;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function relativePreviewPath(root: string, path: string): string | undefined {
|
|
24
|
+
function relativePreviewPath(root: string, path: string): string | undefined {
|
|
30
25
|
const local = relative(root, path);
|
|
31
26
|
if (!local || isAbsolute(local) || local === ".." || local.startsWith(`..${sep}`)) return undefined;
|
|
32
27
|
if (local.split(sep).includes("node_modules")) return undefined;
|
|
@@ -49,11 +44,6 @@ function localLinkPath(root: string, href: string): string | undefined {
|
|
|
49
44
|
}
|
|
50
45
|
}
|
|
51
46
|
|
|
52
|
-
export function resolveLinkPath(root: string, href: string): string | undefined {
|
|
53
|
-
const path = localLinkPath(root, href);
|
|
54
|
-
return path && relativePreviewPath(root, path) ? path : undefined;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
47
|
/** Classifies local links for display without checking whether a file exists. */
|
|
58
48
|
export function classifyLinkPath(
|
|
59
49
|
root: string,
|
|
@@ -98,44 +88,29 @@ function selectLines(
|
|
|
98
88
|
};
|
|
99
89
|
}
|
|
100
90
|
|
|
101
|
-
function sameFile(a:
|
|
91
|
+
function sameFile(a: BigIntStats, b: BigIntStats): boolean {
|
|
102
92
|
return a.dev === b.dev && a.ino === b.ino;
|
|
103
93
|
}
|
|
104
94
|
|
|
105
|
-
async function isCurrentRoot(root: ExternalPreviewRoot, signal: AbortSignal): Promise<boolean> {
|
|
106
|
-
if (signal.aborted || (await realpath(root.path)) !== root.path || signal.aborted) return false;
|
|
107
|
-
const stat = await lstat(root.path, { bigint: true });
|
|
108
|
-
return !signal.aborted && stat.isDirectory() && sameFile(stat, root.identity);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
95
|
/** Opens a regular preview file without following symlinks. The caller owns the handle. */
|
|
112
|
-
export async function openPreviewFile(root: string
|
|
96
|
+
export async function openPreviewFile(root: string, path: string, signal: AbortSignal) {
|
|
113
97
|
try {
|
|
114
|
-
const
|
|
115
|
-
const rootPath = typeof root === "string" ? root : root.path;
|
|
116
|
-
const local = relativePreviewPath(rootPath, path);
|
|
98
|
+
const local = relativePreviewPath(root, path);
|
|
117
99
|
if (!local || signal.aborted) return undefined;
|
|
118
|
-
|
|
119
|
-
const directory = external ? rootPath : await realpath(rootPath);
|
|
100
|
+
const directory = await realpath(root);
|
|
120
101
|
if (signal.aborted) return undefined;
|
|
121
102
|
const candidate = resolve(directory, local);
|
|
122
103
|
if ((await realpath(candidate)) !== candidate || signal.aborted) return undefined;
|
|
123
104
|
const verified = await lstat(candidate, { bigint: true });
|
|
124
105
|
if (!verified.isFile() || signal.aborted) return undefined;
|
|
125
|
-
if (external && !(await isCurrentRoot(external, signal))) return undefined;
|
|
126
|
-
if (signal.aborted) return undefined;
|
|
127
106
|
|
|
128
107
|
const file = await open(candidate, constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
|
|
129
108
|
let keepOpen = false;
|
|
130
109
|
try {
|
|
131
110
|
if (signal.aborted) return undefined;
|
|
132
111
|
const stat = await file.stat({ bigint: true });
|
|
133
|
-
if (signal.aborted || !stat.isFile()) return undefined;
|
|
134
|
-
|
|
135
|
-
if (external) {
|
|
136
|
-
if (!sameFile(verified, stat) || !(await isCurrentRoot(external, signal)) || signal.aborted) return undefined;
|
|
137
|
-
if ((await realpath(candidate)) !== candidate || signal.aborted) return undefined;
|
|
138
|
-
}
|
|
112
|
+
if (signal.aborted || !stat.isFile() || !sameFile(verified, stat)) return undefined;
|
|
113
|
+
if ((await realpath(candidate)) !== candidate || signal.aborted) return undefined;
|
|
139
114
|
keepOpen = true;
|
|
140
115
|
return {
|
|
141
116
|
file,
|
|
@@ -152,7 +127,7 @@ export async function openPreviewFile(root: string | ExternalPreviewRoot, path:
|
|
|
152
127
|
}
|
|
153
128
|
|
|
154
129
|
export async function readExcerpt(
|
|
155
|
-
root: string
|
|
130
|
+
root: string,
|
|
156
131
|
path: string,
|
|
157
132
|
signal: AbortSignal,
|
|
158
133
|
lineRange?: LineRange,
|
package/src/hover-preview.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
1
|
+
import { parse, resolve } from "node:path";
|
|
2
2
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import {
|
|
4
4
|
type Component,
|
|
@@ -9,15 +9,7 @@ import {
|
|
|
9
9
|
TuiAltScreen,
|
|
10
10
|
} from "@earendil-works/pi-tui";
|
|
11
11
|
import type { createExternalLinkMarker } from "./external-link-marker.js";
|
|
12
|
-
import {
|
|
13
|
-
classifyLinkPath,
|
|
14
|
-
type ExternalPreviewRoot,
|
|
15
|
-
parseLineRange,
|
|
16
|
-
readExcerpt,
|
|
17
|
-
relativePreviewPath,
|
|
18
|
-
renderFilePreview,
|
|
19
|
-
resolveLinkPath,
|
|
20
|
-
} from "./filelinks.js";
|
|
12
|
+
import { classifyLinkPath, parseLineRange, readExcerpt, renderFilePreview } from "./filelinks.js";
|
|
21
13
|
import { prepareImagePreview, previewImageMime } from "./image-preview.js";
|
|
22
14
|
import {
|
|
23
15
|
hoverLinkLine,
|
|
@@ -39,7 +31,7 @@ interface Point {
|
|
|
39
31
|
interface Hover extends Point {
|
|
40
32
|
href: string;
|
|
41
33
|
path?: string;
|
|
42
|
-
|
|
34
|
+
external: boolean;
|
|
43
35
|
preview?: WikiPreview;
|
|
44
36
|
controller: AbortController;
|
|
45
37
|
}
|
|
@@ -49,10 +41,7 @@ export function createHoverPreview(
|
|
|
49
41
|
theme: Theme,
|
|
50
42
|
cwd: string,
|
|
51
43
|
findWikiPreview: (href: string) => WikiPreview | undefined,
|
|
52
|
-
|
|
53
|
-
marker: ReturnType<typeof createExternalLinkMarker>;
|
|
54
|
-
roots(): readonly ExternalPreviewRoot[];
|
|
55
|
-
},
|
|
44
|
+
marker: ReturnType<typeof createExternalLinkMarker>,
|
|
56
45
|
): Component & { dismiss(): void; dispose(): void } {
|
|
57
46
|
let hover: Hover | undefined;
|
|
58
47
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -73,17 +62,17 @@ export function createHoverPreview(
|
|
|
73
62
|
return tui instanceof TuiAltScreen ? (tui.getScreenLines()[row] ?? "") : "";
|
|
74
63
|
}
|
|
75
64
|
|
|
76
|
-
function stillOnLink({ row, col, href, preview,
|
|
65
|
+
function stillOnLink({ row, col, href, preview, external }: Hover): boolean {
|
|
77
66
|
const line = screenLine(row);
|
|
78
67
|
return (
|
|
79
68
|
getOsc8LinkAtColumn(hoverLinkLine(line), col) === href &&
|
|
80
69
|
findWikiPreview(href) === preview &&
|
|
81
|
-
(!
|
|
70
|
+
(!external || marker.matches(line, col, href))
|
|
82
71
|
);
|
|
83
72
|
}
|
|
84
73
|
|
|
85
74
|
async function show(target: Hover): Promise<void> {
|
|
86
|
-
const { preview, path, controller,
|
|
75
|
+
const { preview, path, controller, external } = target;
|
|
87
76
|
if (hover !== target) return;
|
|
88
77
|
if (
|
|
89
78
|
!viewport ||
|
|
@@ -98,7 +87,7 @@ export function createHoverPreview(
|
|
|
98
87
|
const { columns, rows } = tui.terminal;
|
|
99
88
|
const below = rows - target.row - 1;
|
|
100
89
|
const above = target.row;
|
|
101
|
-
const imageMime = !
|
|
90
|
+
const imageMime = !external && !preview && path ? previewImageMime(path) : undefined;
|
|
102
91
|
const placeBelow = below >= above || (!imageMime && below >= MAX_HEIGHT);
|
|
103
92
|
const availableRows = placeBelow ? below : above;
|
|
104
93
|
const width = imageMime ? columns : Math.min(columns, preview ? 56 : 60);
|
|
@@ -124,7 +113,7 @@ export function createHoverPreview(
|
|
|
124
113
|
} else {
|
|
125
114
|
if (!path) return;
|
|
126
115
|
const lineRange = preview ? undefined : parseLineRange(target.href);
|
|
127
|
-
const excerpt = await readExcerpt(
|
|
116
|
+
const excerpt = await readExcerpt(external ? parse(path).root : cwd, path, controller.signal, lineRange);
|
|
128
117
|
if (!excerpt) return;
|
|
129
118
|
draw = () =>
|
|
130
119
|
preview
|
|
@@ -179,24 +168,20 @@ export function createHoverPreview(
|
|
|
179
168
|
href === hover.href &&
|
|
180
169
|
point.row === hover.row &&
|
|
181
170
|
preview === hover.preview &&
|
|
182
|
-
(!hover.
|
|
171
|
+
(!hover.external || marker.matches(line, point.col, href))
|
|
183
172
|
) {
|
|
184
173
|
hover.col = point.col;
|
|
185
174
|
return;
|
|
186
175
|
}
|
|
187
176
|
hide();
|
|
188
177
|
if (!href || tui.hasOverlay()) return;
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
externalRoot = external.roots().find((root) => relativePreviewPath(root.path, local.path));
|
|
195
|
-
if (!externalRoot || !external.marker.matches(line, point.col, href)) return;
|
|
196
|
-
path = local.path;
|
|
197
|
-
}
|
|
178
|
+
const local = preview ? undefined : classifyLinkPath(cwd, href);
|
|
179
|
+
if (!preview && !local) return;
|
|
180
|
+
const external = local?.kind === "external";
|
|
181
|
+
if (external && (previewImageMime(local.path) || !marker.matches(line, point.col, href))) return;
|
|
182
|
+
const path = preview && "path" in preview ? preview.path : local?.path;
|
|
198
183
|
|
|
199
|
-
const target: Hover = { ...point, href, path,
|
|
184
|
+
const target: Hover = { ...point, href, path, external, preview, controller: new AbortController() };
|
|
200
185
|
hover = target;
|
|
201
186
|
viewport = { columns: tui.terminal.columns, rows: tui.terminal.rows };
|
|
202
187
|
timer = setTimeout(() => void show(target), HOVER_DELAY_MS);
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import type { TUI } from "@earendil-works/pi-tui";
|
|
3
3
|
import { createExternalLinkMarker } from "./external-link-marker.js";
|
|
4
|
-
import type { ExternalPreviewRoot } from "./filelinks.js";
|
|
5
4
|
import { createHoverPreview } from "./hover-preview.js";
|
|
6
5
|
import { renderInlineMarkup } from "./inline-markup.js";
|
|
7
|
-
import { loadPromptSettings,
|
|
6
|
+
import { loadPromptSettings, type PromptHintSettings, savePromptHint } from "./settings.js";
|
|
8
7
|
import { showInlineSettings } from "./settings-menu.js";
|
|
9
8
|
import { createWikilinks } from "./wikilinks.js";
|
|
10
9
|
|
|
@@ -48,14 +47,10 @@ export function inlineMarkupExtension(pi: ExtensionAPI): void {
|
|
|
48
47
|
if (mode !== "tui") return;
|
|
49
48
|
const settings = new AbortController();
|
|
50
49
|
const marker = createExternalLinkMarker();
|
|
51
|
-
let roots: readonly ExternalPreviewRoot[] = [];
|
|
52
50
|
ui.setWidget(
|
|
53
51
|
WIDGET_KEY,
|
|
54
52
|
(tui, theme) => {
|
|
55
|
-
const preview = createHoverPreview(tui, theme, cwd, (href) => current.wiki?.findPreview(href),
|
|
56
|
-
marker,
|
|
57
|
-
roots: () => roots,
|
|
58
|
-
});
|
|
53
|
+
const preview = createHoverPreview(tui, theme, cwd, (href) => current.wiki?.findPreview(href), marker);
|
|
59
54
|
const current: Session = {
|
|
60
55
|
cwd,
|
|
61
56
|
theme,
|
|
@@ -71,7 +66,6 @@ export function inlineMarkupExtension(pi: ExtensionAPI): void {
|
|
|
71
66
|
dispose() {
|
|
72
67
|
if (session === current) session = undefined;
|
|
73
68
|
settings.abort();
|
|
74
|
-
roots = [];
|
|
75
69
|
current.menu?.abort();
|
|
76
70
|
preview.dispose();
|
|
77
71
|
current.wiki?.dispose();
|
|
@@ -89,12 +83,10 @@ export function inlineMarkupExtension(pi: ExtensionAPI): void {
|
|
|
89
83
|
},
|
|
90
84
|
{ placement: "belowEditor" },
|
|
91
85
|
);
|
|
92
|
-
const snapshot = await
|
|
86
|
+
const snapshot = await loadPromptSettings(settings.signal);
|
|
93
87
|
if (settings.signal.aborted || session?.marker !== marker) return;
|
|
94
|
-
roots = snapshot.roots;
|
|
95
88
|
session.prompt = snapshot.prompt;
|
|
96
|
-
if (
|
|
97
|
-
ui.notify("pi-inline: Invalid or unreadable settings were ignored.", "warning");
|
|
89
|
+
if (snapshot.saveBlocked && hasUI) ui.notify("pi-inline: Invalid or unreadable settings were ignored.", "warning");
|
|
98
90
|
});
|
|
99
91
|
|
|
100
92
|
pi.on("session_shutdown", async (_event, ctx) => {
|
package/src/settings.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { type BigIntStats, constants } from "node:fs";
|
|
3
|
-
import { lstat, mkdir, open, readFile,
|
|
4
|
-
import {
|
|
5
|
-
import { dirname, isAbsolute, join, parse, resolve } from "node:path";
|
|
3
|
+
import { lstat, mkdir, open, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
6
5
|
import { getAgentDir, withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
7
6
|
import { parseTree } from "jsonc-parser";
|
|
8
|
-
import { type ExternalPreviewRoot, relativePreviewPath } from "./filelinks.js";
|
|
9
7
|
import { INLINE_FORMATTING_PROMPT } from "./prompt.js";
|
|
10
8
|
|
|
11
9
|
export type PromptHintSettings = { enabled: boolean; text: string };
|
|
@@ -14,7 +12,6 @@ export type PromptHintChange = { key: "promptHintEnabled"; value: boolean } | {
|
|
|
14
12
|
export const DEFAULT_PROMPT_HINT: PromptHintSettings = Object.freeze({ enabled: true, text: INLINE_FORMATTING_PROMPT });
|
|
15
13
|
|
|
16
14
|
type PromptSettings = { prompt: PromptHintSettings; saveBlocked?: true };
|
|
17
|
-
type Settings = PromptSettings & { roots: readonly ExternalPreviewRoot[]; invalid?: true };
|
|
18
15
|
type SettingsDocument = Record<string, unknown>;
|
|
19
16
|
|
|
20
17
|
type DocumentSnapshot = { document: SettingsDocument; source?: string; stat?: BigIntStats };
|
|
@@ -90,40 +87,6 @@ function readPromptHint(document: SettingsDocument): PromptHintSettings | undefi
|
|
|
90
87
|
return typeof enabled === "boolean" && text !== undefined ? { enabled, text } : undefined;
|
|
91
88
|
}
|
|
92
89
|
|
|
93
|
-
// Path syntax is a setting value. Filesystem availability and authorization are separate.
|
|
94
|
-
function readRootPaths(document: SettingsDocument): string[] | undefined {
|
|
95
|
-
if (!Object.hasOwn(document, "externalPreviewRoots")) return [];
|
|
96
|
-
const values = document.externalPreviewRoots;
|
|
97
|
-
if (!Array.isArray(values)) return undefined;
|
|
98
|
-
const paths: string[] = [];
|
|
99
|
-
for (const value of values) {
|
|
100
|
-
if (typeof value !== "string" || !isAbsolute(value) || /[*?[\]{}\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}]/u.test(value))
|
|
101
|
-
return undefined;
|
|
102
|
-
const path = resolve(value);
|
|
103
|
-
if (path === parse(path).root) return undefined;
|
|
104
|
-
paths.push(path);
|
|
105
|
-
}
|
|
106
|
-
return paths;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async function authorizeRoots(paths: readonly string[], signal: AbortSignal): Promise<ExternalPreviewRoot[]> {
|
|
110
|
-
if (!paths.length) return [];
|
|
111
|
-
signal.throwIfAborted();
|
|
112
|
-
const home = resolve(homedir());
|
|
113
|
-
const realHome = await realpath(home).catch(() => home);
|
|
114
|
-
const roots: ExternalPreviewRoot[] = [];
|
|
115
|
-
for (const path of paths) {
|
|
116
|
-
signal.throwIfAborted();
|
|
117
|
-
if (path === home || path === realHome || relativePreviewPath(path, realHome)) throw new Error();
|
|
118
|
-
const identity = await lstat(path, { bigint: true });
|
|
119
|
-
signal.throwIfAborted();
|
|
120
|
-
if (!identity.isDirectory() || (await realpath(path)) !== path) throw new Error();
|
|
121
|
-
signal.throwIfAborted();
|
|
122
|
-
roots.push({ path, identity });
|
|
123
|
-
}
|
|
124
|
-
return roots;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
90
|
async function readSettings(signal: AbortSignal): Promise<SettingsDocument | undefined> {
|
|
128
91
|
if (signal.aborted) return undefined;
|
|
129
92
|
const path = settingsPath();
|
|
@@ -135,33 +98,12 @@ async function readSettings(signal: AbortSignal): Promise<SettingsDocument | und
|
|
|
135
98
|
}
|
|
136
99
|
}
|
|
137
100
|
|
|
138
|
-
function describePrompt(prompt: PromptHintSettings | undefined, paths: readonly string[] | undefined): PromptSettings {
|
|
139
|
-
return {
|
|
140
|
-
prompt: prompt ?? { ...DEFAULT_PROMPT_HINT },
|
|
141
|
-
...(!prompt || !paths ? { saveBlocked: true } : {}),
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export async function loadSettings(signal: AbortSignal): Promise<Settings> {
|
|
146
|
-
const defaults = { roots: [], prompt: { ...DEFAULT_PROMPT_HINT } };
|
|
147
|
-
const document = await readSettings(signal);
|
|
148
|
-
if (signal.aborted) return defaults;
|
|
149
|
-
if (!document) return { ...defaults, invalid: true, saveBlocked: true };
|
|
150
|
-
const paths = readRootPaths(document);
|
|
151
|
-
const prompt = describePrompt(readPromptHint(document), paths);
|
|
152
|
-
if (!paths) return { ...prompt, roots: [], invalid: true };
|
|
153
|
-
try {
|
|
154
|
-
const roots = await authorizeRoots(paths, signal);
|
|
155
|
-
return signal.aborted ? defaults : { ...prompt, roots };
|
|
156
|
-
} catch {
|
|
157
|
-
return signal.aborted ? defaults : { ...prompt, roots: [], invalid: true };
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
101
|
export async function loadPromptSettings(signal: AbortSignal): Promise<PromptSettings> {
|
|
162
102
|
const document = await readSettings(signal);
|
|
163
|
-
|
|
164
|
-
|
|
103
|
+
const defaults = { prompt: { ...DEFAULT_PROMPT_HINT } };
|
|
104
|
+
if (signal.aborted) return defaults;
|
|
105
|
+
const prompt = document && readPromptHint(document);
|
|
106
|
+
return prompt ? { prompt } : { ...defaults, saveBlocked: true };
|
|
165
107
|
}
|
|
166
108
|
|
|
167
109
|
function editPromptSetting(source: string | undefined, key: PromptHintChange["key"], value: boolean | string): string {
|
|
@@ -255,7 +197,7 @@ export async function savePromptHint(
|
|
|
255
197
|
const snapshot = await readDocument(path, signal);
|
|
256
198
|
if (signal.aborted) return false;
|
|
257
199
|
const prompt = readPromptHint(snapshot.document);
|
|
258
|
-
if (!prompt
|
|
200
|
+
if (!prompt) throw new Error();
|
|
259
201
|
const current = enabled ? prompt.enabled : prompt.text;
|
|
260
202
|
if (current !== baseline) {
|
|
261
203
|
fail = stale;
|