-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
custom.ts
95 lines (80 loc) · 2.81 KB
/
custom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import type { Range, Uri } from 'vscode';
import type { RemotesUrlsConfig } from '../../config';
import { interpolate } from '../../system/string';
import type { Repository } from '../models/repository';
import { RemoteProvider } from './remoteProvider';
export class CustomRemote extends RemoteProvider {
private readonly urls: RemotesUrlsConfig;
constructor(domain: string, path: string, urls: RemotesUrlsConfig, protocol?: string, name?: string) {
super(domain, path, protocol, name, true);
this.urls = urls;
}
get id() {
return 'custom';
}
get name() {
return this.formatName('Custom');
}
getLocalInfoFromRemoteUri(
_repository: Repository,
_uri: Uri,
): Promise<{ uri: Uri; startLine?: number; endLine?: number } | undefined> {
return Promise.resolve(undefined);
}
protected override getUrlForRepository(): string {
return this.encodeUrl(interpolate(this.urls.repository, this.getContext()));
}
protected getUrlForBranches(): string {
return this.encodeUrl(interpolate(this.urls.branches, this.getContext()));
}
protected getUrlForBranch(branch: string): string {
return this.encodeUrl(interpolate(this.urls.branch, this.getContext({ branch: branch })));
}
protected getUrlForCommit(sha: string): string {
return this.encodeUrl(interpolate(this.urls.commit, this.getContext({ id: sha })));
}
protected override getUrlForComparison(base: string, compare: string, notation: '..' | '...'): string | undefined {
if (this.urls.comparison == null) return undefined;
return this.encodeUrl(
interpolate(this.urls.comparison, this.getContext({ ref1: base, ref2: compare, notation: notation })),
);
}
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {
let line;
if (range != null) {
if (range.start.line === range.end.line) {
line = interpolate(this.urls.fileLine, { line: range.start.line });
} else {
line = interpolate(this.urls.fileRange, { start: range.start.line, end: range.end.line });
}
} else {
line = '';
}
let url;
if (sha) {
url = interpolate(this.urls.fileInCommit, this.getContext({ id: sha, file: fileName, line: line }));
} else if (branch) {
url = interpolate(this.urls.fileInBranch, this.getContext({ branch: branch, file: fileName, line: line }));
} else {
url = interpolate(this.urls.file, this.getContext({ file: fileName, line: line }));
}
const decodeHash = url.includes('#');
url = this.encodeUrl(url);
if (decodeHash) {
const index = url.lastIndexOf('%23');
if (index !== -1) {
url = `${url.substring(0, index)}#${url.substring(index + 3)}`;
}
}
return url;
}
private getContext(context?: Record<string, unknown>) {
const [repoBase, repoPath] = this.splitPath();
return {
repo: this.path,
repoBase: repoBase,
repoPath: repoPath,
...(context ?? {}),
};
}
}