@tanishqkancharla/diffmap 0.1.3 → 0.1.4

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.
@@ -3,6 +3,9 @@ import type { CodeViewDiffItem, CodeViewLineSelection } from "@pierre/diffs";
3
3
  import type { SourceReference } from "../annotations.js";
4
4
  import type { SourceDefinition, DefinitionResponse } from "../definitions.js";
5
5
  import { DiffmapDefinitionError } from "../errors.js";
6
+ import { useGitHubPin } from "../github/pin.ts";
7
+ import { fetchPinnedBlob, findSymbolLineRange } from "../github/fetchRepo.ts";
8
+ import { useViewerMode } from "./viewerMode.ts";
6
9
 
7
10
  export async function requestSource(
8
11
  endpoint: "source" | "definition",
@@ -42,14 +45,55 @@ export function useSourceReference(reference: SourceReference | undefined) {
42
45
  reference: SourceReference;
43
46
  result: SourceDefinition | Error;
44
47
  }>();
48
+ const pin = useGitHubPin();
49
+ const mode = useViewerMode();
45
50
  useEffect(() => {
46
51
  if (reference?.kind !== "file") return;
52
+ if (mode === "gist") return;
53
+ let active = true;
54
+ if (pin !== undefined) {
55
+ // Pinned GitHub spec: load this path at the spec commit SHA, never main.
56
+ // oxlint-disable-next-line typescript/no-floating-promises -- The effect owns its asynchronous source result.
57
+ void fetchPinnedBlob(pin, reference.path).then((contents) => {
58
+ if (!active) return;
59
+ if (contents instanceof Error) {
60
+ const message =
61
+ contents.message === "not-found"
62
+ ? `Could not read ${reference.path} at this commit.`
63
+ : contents.message;
64
+ setResolution({
65
+ reference,
66
+ result: new DiffmapDefinitionError({ reason: message }),
67
+ });
68
+ return;
69
+ }
70
+ const lineCount = Math.max(1, contents.split(/\r?\n/).length);
71
+ const range =
72
+ reference.symbol !== undefined
73
+ ? findSymbolLineRange(contents, reference.symbol)
74
+ : {
75
+ start: reference.start ?? 1,
76
+ end: reference.end ?? lineCount,
77
+ };
78
+ setResolution({
79
+ reference,
80
+ result: {
81
+ path: reference.path,
82
+ contents,
83
+ start: range.start,
84
+ end: range.end,
85
+ },
86
+ });
87
+ });
88
+ return () => {
89
+ active = false;
90
+ };
91
+ }
47
92
  const params = new URLSearchParams({ path: reference.path });
48
93
  if (reference.symbol !== undefined) params.set("symbol", reference.symbol);
49
94
  if (reference.start !== undefined)
50
95
  params.set("start", String(reference.start));
51
96
  if (reference.end !== undefined) params.set("end", String(reference.end));
52
- let active = true;
53
97
  // oxlint-disable-next-line typescript/no-floating-promises -- The effect owns its asynchronous source result.
54
98
  void requestSource("source", params).then((result) => {
55
99
  if (active) setResolution({ reference, result });
@@ -57,7 +101,7 @@ export function useSourceReference(reference: SourceReference | undefined) {
57
101
  return () => {
58
102
  active = false;
59
103
  };
60
- }, [reference]);
104
+ }, [reference, pin, mode]);
61
105
  return resolution?.reference === reference ? resolution?.result : undefined;
62
106
  }
63
107
 
@@ -1,6 +1,6 @@
1
1
  import { createContext, useContext } from "react";
2
2
 
3
- export type ViewerMode = "local" | "gist";
3
+ export type ViewerMode = "local" | "gist" | "github";
4
4
 
5
5
  export const ViewerModeContext = createContext<ViewerMode>("local");
6
6
 
@@ -1,4 +1,5 @@
1
1
  declare module "virtual:diffmap" {
2
2
  import type { ViewerDocument } from "../parseViewer.js";
3
- export const viewerDocument: ViewerDocument;
3
+ export const viewerDocument: ViewerDocument | null;
4
+ export const parseError: string | null;
4
5
  }