@d3ara1n/pi-hashline-edit 0.5.2 → 0.5.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d3ara1n/pi-hashline-edit",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "type": "module",
5
5
  "description": "Hashline-style file editing for pi — line-anchored edits verified by content hash, replacing oldText/newText matching",
6
6
  "homepage": "https://github.com/d3ara1n/pi-extensions/tree/main/packages/pi-hashline-edit#readme",
@@ -42,7 +42,7 @@ import { Text } from "@earendil-works/pi-tui";
42
42
  import { spawn } from "node:child_process";
43
43
  import { createInterface } from "node:readline";
44
44
  import { access, constants, readFile, stat } from "node:fs/promises";
45
- import { basename, delimiter, join, relative } from "node:path";
45
+ import { delimiter, isAbsolute, join, relative, resolve, sep } from "node:path";
46
46
  import { hashFileLines } from "../core/hash.ts";
47
47
  import { splitLines } from "../core/lines.ts";
48
48
  import { getState } from "./state.ts";
@@ -411,11 +411,9 @@ export function makeGrepOverrideWithBackend(cwd: string, overrides: Partial<Grep
411
411
  })();
412
412
  const hashLen = state.config.hashLen;
413
413
 
414
- // Verify search paths upfront; remember dir-ness for relative display.
415
- const roots: { path: string; isDir: boolean }[] = [];
416
414
  for (const sp of searchPaths) {
417
415
  try {
418
- roots.push({ path: sp, isDir: (await stat(sp)).isDirectory() });
416
+ await stat(sp);
419
417
  } catch {
420
418
  throw new Error(`Path not found: ${sp}`);
421
419
  }
@@ -528,12 +526,11 @@ export function makeGrepOverrideWithBackend(cwd: string, overrides: Partial<Grep
528
526
  };
529
527
 
530
528
  const formatPath = (fp: string): string => {
531
- for (const root of roots) {
532
- if (!root.isDir) continue;
533
- const rel = relative(root.path, fp).replace(/\\/g, "/");
534
- if (rel && !rel.startsWith("..")) return rel;
535
- }
536
- return basename(fp);
529
+ const abs = resolve(cwd, fp);
530
+ const rel = relative(cwd, abs);
531
+ return rel && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel)
532
+ ? rel.replace(/\\/g, "/")
533
+ : abs;
537
534
  };
538
535
 
539
536
  const blocks: string[] = [];
@@ -4,11 +4,12 @@
4
4
  */
5
5
  import { test } from "node:test";
6
6
  import assert from "node:assert/strict";
7
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
7
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
8
8
  import { tmpdir } from "node:os";
9
9
  import { join } from "node:path";
10
10
  import { computeLineHash } from "../core/hash.ts";
11
11
  import { makeGrepOverrideWithBackend, type GrepBackend } from "./grep-tool.ts";
12
+ import { makeEditOverride } from "./edit-tool.ts";
12
13
  import { getState } from "./state.ts";
13
14
 
14
15
  type FakeOptions = {
@@ -111,6 +112,37 @@ test("formats parsed rg matches with full-line hash anchors", async () => {
111
112
  );
112
113
  });
113
114
 
115
+ test("grep in a subdirectory returns a path that edits the matching file", async () => {
116
+ await withDir(async (dir) =>
117
+ withEnabled(true, async () => {
118
+ await mkdir(join(dir, "src"));
119
+ const original = "export const status = 1;\n";
120
+ const rootFile = join(dir, "status.ts");
121
+ const matchedFile = join(dir, "src", "status.ts");
122
+ await writeFile(rootFile, original);
123
+ await writeFile(matchedFile, original);
124
+ const fake = fakeBackend({ lines: [rgMatch(matchedFile, 1, original)] });
125
+ const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
126
+ pattern: "status",
127
+ path: "src",
128
+ });
129
+ const output = text(result);
130
+ const displayPath = output.split(" · ")[0];
131
+ const edit: any = makeEditOverride(dir);
132
+ await call(edit, {
133
+ path: displayPath,
134
+ edits: [{
135
+ op: "replace",
136
+ anchor: { line: 1, hash: computeLineHash(1, original.trimEnd()) },
137
+ body: ["export const status = 2;"],
138
+ }],
139
+ });
140
+ assert.equal(await readFile(matchedFile, "utf-8"), "export const status = 2;\n");
141
+ assert.equal(await readFile(rootFile, "utf-8"), original);
142
+ }),
143
+ );
144
+ });
145
+
114
146
  test("applies all, exclude, context, and CRLF filtering after rg output", async () => {
115
147
  await withDir(async (dir) =>
116
148
  withEnabled(true, async () => {