This repository was archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
79 lines (66 loc) · 2.86 KB
/
Copy pathextension.js
File metadata and controls
79 lines (66 loc) · 2.86 KB
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
const vscode = require('vscode');
async function getRepoRootOfFile(workspace, document) {
// const workspaceFolder = workspace.getWorkspaceFolder(document.uri);
// const rootPath = workspaceFolder.uri.path;
const foundFiles = await workspace.findFiles('**/.arcconfig');
const argconfigDirs = foundFiles
.map((file) => file.path.replace(/\/.arcconfig$/, ''))
.filter((dir) => document.uri.path.startsWith(dir));
argconfigDirs.sort((a, b) => a.split('/').length - b.split('/').length);
return argconfigDirs[0]; //pick the shortest number of segments (top of tree)
}
function readFilePath(workspace, path) {
return workspace.fs
.readFile(vscode.Uri.file(path))
.then((buf) => String.fromCharCode.apply(null, new Uint16Array(buf)));
}
function removeLeadingSlash(path) {
return path.replace(/^\//, '');
}
function removeTrailingSlash(path) {
return path.replace(/\/$/, '');
}
function getDiffusionDomain(arcconfig) {
return arcconfig['phabricator.uri'];
}
function getProjectCallsign(arcconfig) {
// TODO: hit the api and try to find the repo based on the git-remote url
// Rely on settings via: `vscode.workspace.getConfiguration('open-in-diffusion')`
return arcconfig['repository.callsign'];
}
function documentToProjectFile(document, rootPath) {
return vscode.Uri.file(document.fileName).path.replace(rootPath, '');
}
function selectionToUrlRange(selection) {
return selection.isSingleLine
? `$${selection.start.line + 1}`
: `$${selection.start.line + 1}-${selection.end.line + 1}`;
}
async function assembleUrl(activeEditor, workspace) {
const repoRootPath = await getRepoRootOfFile(workspace, activeEditor.document);
const arcconfig = JSON.parse(await readFilePath(workspace, `${repoRootPath}/.arcconfig`));
const domain = removeTrailingSlash(getDiffusionDomain(arcconfig));
const callsign = removeLeadingSlash(removeTrailingSlash(getProjectCallsign(arcconfig)));
const projectFile = removeLeadingSlash(documentToProjectFile(activeEditor.document, repoRootPath));
const lineRange = selectionToUrlRange(activeEditor.selection);
return `${domain}/diffusion/${callsign}/browse/master/${projectFile}${lineRange}`;
}
module.exports = {
activate: function(context) {
context.subscriptions.push(
vscode.commands.registerCommand('open-in-diffusion.copy-phabricator-url', async () => {
const url = await assembleUrl(vscode.window.activeTextEditor, vscode.workspace);
await Promise.all([
vscode.env.clipboard.writeText(url),
vscode.window.showInformationMessage(`Copied ${url}`),
]);
})
);
context.subscriptions.push(
vscode.commands.registerCommand('open-in-diffusion.open-in-phabricator', async () => {
const url = await assembleUrl(vscode.window.activeTextEditor, vscode.workspace);
await vscode.env.openExternal(vscode.Uri.parse(url));
})
);
},
};