OXYGENHUB
# f532c4266f21 oxygen-vscode / src / api.ts
4.7 KiB · last changed Line links: Open on Remote, Copy Remote Link and Copy Permalink from the gutter and the editor context menu, for a line or the selection, with permalinks pinned to the bookmark's state; 0.10.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
  remotes(): Promise<{ name: string; url: string }[]>;
38
  /** Run an oxy command in this repository and parse its JSON output. */
39
  json<T>(args: string[]): Promise<T>;
40
  refresh(): Promise<void>;
41
}
42
43
export interface OxygenAPI {
44
  readonly version: 1;
45
  readonly repositories: readonly RepositoryHandle[];
46
  /** Fires with the repository whose status changed. */
47
  readonly onDidChangeStatus: (listener: (repo: RepositoryHandle) => void) => { dispose(): void };
48
  /** Fires when repositories are discovered or removed. */
49
  readonly onDidChangeRepositories: (listener: () => void) => { dispose(): void };
50
  getRepository(fsPath: string): RepositoryHandle | undefined;
51
  registerRemoteProvider(provider: RemoteProvider): { dispose(): void };
52
  /** The providers registered so far, for callers that build their own menus. */
53
  remoteProviders(): readonly RemoteProvider[];
54
}
55
56
/** The provider (and remote) to use for a repository's remotes, if any. */
57
export function pickProvider(providers: readonly RemoteProvider[], remotes: { name: string; url: string }[]): { provider: RemoteProvider; remote: { name: string; url: string } } | undefined {
58
  // Prefer a remote called origin or earth, then any.
59
  const ordered = [...remotes].sort((a, b) => rank(a.name) - rank(b.name));
60
  for (const remote of ordered) {
61
    for (const provider of providers) {
62
      if (provider.matches(remote.url)) {
63
        return { provider, remote };
64
      }
65
    }
66
  }
67
  return undefined;
68
}
69
70
function rank(name: string): number {
71
  return name === "origin" ? 0 : name === "hub" ? 1 : 2;
72
}
73
74
/** The repository handle a folder path belongs to: the longest root that prefixes it. */
75
export function repositoryFor(roots: string[], fsPath: string): string | undefined {
76
  let best: string | undefined;
77
  for (const r of roots) {
78
    if ((fsPath === r || fsPath.startsWith(r.endsWith("/") ? r : r + "/")) && (!best || r.length > best.length)) {
79
      best = r;
80
    }
81
  }
82
  return best;
83
}
84
85
/**
86
 * A state address for permalinks, as OxygenHub writes it: the state's head
87
 * hashes, sorted and joined with `+`. Undefined without heads.
88
 */
89
export function stateSpec(heads: readonly string[]): string | undefined {
90
  const hex = [...new Set(heads.map((h) => h.toLowerCase()))].filter((h) => /^[0-9a-f]{64}$/.test(h)).sort();
91
  return hex.length > 0 && hex.length === new Set(heads).size ? hex.join("+") : undefined;
92
}
93
94
/**
95
 * The 1-based lines to link: the selection when the clicked line is inside it
96
 * (or nothing was clicked), otherwise the clicked line alone. `selection` is
97
 * 1-based and inclusive; a selection ending at column 0 of a line does not
98
 * include that line.
99
 */
100
export function linkedLines(clicked: number | undefined, selection: { start: number; end: number; endColumn: number; empty: boolean }): { line: number; endLine?: number } {
101
  let end = selection.end;
102
  if (!selection.empty && selection.endColumn === 0 && end > selection.start) {
103
    end -= 1;
104
  }
105
  if (clicked !== undefined && (selection.empty || clicked < selection.start || clicked > end)) {
106
    return { line: clicked };
107
  }
108
  if (selection.empty || end === selection.start) {
109
    return { line: selection.start };
110
  }
111
  return { line: selection.start, endLine: end };
112
}