OXYGENHUB
◆ main oxygen-vscode / src / api.ts
4.8 KiB · last changed Stop the Source Control refresh loop; remotes carry their CA (0.13.0) 1 month ago source blame history raw
1
// The API this extension exports to others (spec §9, "extension API"): the
2
// same idea as the built-in Git extension's `getAPI()`. A forge extension
3
// such as oxygenhub-vscode consumes it instead of running oxy itself, and
4
// registers a remote provider so this extension can offer "Open on …"
5
// links without knowing any hub.
6
//
7
// Pure functions live here so they can be tested with plain Node; the
8
// vscode-facing object is assembled in extension.ts.
9
10
/** What a remote-aware extension registers. */
11
export interface RemoteProvider {
12
  /** A stable id, e.g. `oxygenhub`. */
13
  readonly id: string;
14
  /** Shown in menus: "Open on OxygenHub". */
15
  readonly label: string;
16
  /** Whether this provider understands a remote URL. */
17
  matches(remoteUrl: string): boolean;
18
  /** A web URL for a thing in that remote, or undefined when the provider has none. */
19
  urlFor(remoteUrl: string, target: RemoteTarget): string | undefined;
20
}
21
22
export type RemoteTarget =
23
  | { kind: "repository" }
24
  | { kind: "bookmark"; name: string }
25
  | { kind: "change"; hash: string }
26
  | { kind: "file"; reference: string; path: string; line?: number; endLine?: number }
27
  | { kind: "proposals" }
28
  | { kind: "issues" };
29
30
/** A minimal view of one repository, stable across versions. */
31
export interface RepositoryHandle {
32
  readonly root: string;
33
  /** `oxy status --json` as last refreshed, or undefined before the first refresh. */
34
  readonly status: unknown;
35
  readonly bookmarks: { name: string; heads: string[]; current?: boolean }[];
36
  readonly currentBookmark: string | undefined;
37
  /** The repository's remotes, with the CA file each names for a self-signed hub. */
38
  remotes(): Promise<{ name: string; url: string; ca?: string }[]>;
39
  /** Run an oxy command in this repository and parse its JSON output. */
40
  json<T>(args: string[]): Promise<T>;
41
  refresh(): Promise<void>;
42
}
43
44
export interface OxygenAPI {
45
  readonly version: 1;
46
  readonly repositories: readonly RepositoryHandle[];
47
  /** Fires with the repository whose status changed. */
48
  readonly onDidChangeStatus: (listener: (repo: RepositoryHandle) => void) => { dispose(): void };
49
  /** Fires when repositories are discovered or removed. */
50
  readonly onDidChangeRepositories: (listener: () => void) => { dispose(): void };
51
  getRepository(fsPath: string): RepositoryHandle | undefined;
52
  registerRemoteProvider(provider: RemoteProvider): { dispose(): void };
53
  /** The providers registered so far, for callers that build their own menus. */
54
  remoteProviders(): readonly RemoteProvider[];
55
}
56
57
/** The provider (and remote) to use for a repository's remotes, if any. */
58
export function pickProvider(providers: readonly RemoteProvider[], remotes: { name: string; url: string }[]): { provider: RemoteProvider; remote: { name: string; url: string } } | undefined {
59
  // Prefer a remote called origin or earth, then any.
60
  const ordered = [...remotes].sort((a, b) => rank(a.name) - rank(b.name));
61
  for (const remote of ordered) {
62
    for (const provider of providers) {
63
      if (provider.matches(remote.url)) {
64
        return { provider, remote };
65
      }
66
    }
67
  }
68
  return undefined;
69
}
70
71
function rank(name: string): number {
72
  return name === "origin" ? 0 : name === "hub" ? 1 : 2;
73
}
74
75
/** The repository handle a folder path belongs to: the longest root that prefixes it. */
76
export function repositoryFor(roots: string[], fsPath: string): string | undefined {
77
  let best: string | undefined;
78
  for (const r of roots) {
79
    if ((fsPath === r || fsPath.startsWith(r.endsWith("/") ? r : r + "/")) && (!best || r.length > best.length)) {
80
      best = r;
81
    }
82
  }
83
  return best;
84
}
85
86
/**
87
 * A state address for permalinks, as OxygenHub writes it: the state's head
88
 * hashes, sorted and joined with `+`. Undefined without heads.
89
 */
90
export function stateSpec(heads: readonly string[]): string | undefined {
91
  const hex = [...new Set(heads.map((h) => h.toLowerCase()))].filter((h) => /^[0-9a-f]{64}$/.test(h)).sort();
92
  return hex.length > 0 && hex.length === new Set(heads).size ? hex.join("+") : undefined;
93
}
94
95
/**
96
 * The 1-based lines to link: the selection when the clicked line is inside it
97
 * (or nothing was clicked), otherwise the clicked line alone. `selection` is
98
 * 1-based and inclusive; a selection ending at column 0 of a line does not
99
 * include that line.
100
 */
101
export function linkedLines(clicked: number | undefined, selection: { start: number; end: number; endColumn: number; empty: boolean }): { line: number; endLine?: number } {
102
  let end = selection.end;
103
  if (!selection.empty && selection.endColumn === 0 && end > selection.start) {
104
    end -= 1;
105
  }
106
  if (clicked !== undefined && (selection.empty || clicked < selection.start || clicked > end)) {
107
    return { line: clicked };
108
  }
109
  if (selection.empty || end === selection.start) {
110
    return { line: selection.start };
111
  }
112
  return { line: selection.start, endLine: end };
113
}