-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathextension.js
74 lines (57 loc) · 2 KB
/
extension.js
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
const vscode = require('vscode');
const Codeowners = require('codeowners');
const path = require('path');
const COMMAND_ID = 'vscode-codeowners.show-owners';
const STATUS_BAR_PRIORITY = 100;
const getOwners = () => {
if (!vscode.window.activeTextEditor) {
return [];
}
const { fileName, uri } = vscode.window.activeTextEditor.document;
const {
uri: { fsPath: workspacePath }
} = vscode.workspace.getWorkspaceFolder(uri);
let folder;
try {
folder = new Codeowners(workspacePath);
} catch {
// no CODEOWNERS file
return null;
}
const file = fileName.split(`${workspacePath}${path.sep}`)[1];
return folder.getOwner(file);
};
const activate = context => {
console.log('CODEOWNERS: activated');
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, STATUS_BAR_PRIORITY);
statusBarItem.command = COMMAND_ID;
context.subscriptions.push(statusBarItem);
context.subscriptions.push(
vscode.commands.registerCommand(COMMAND_ID, () => {
vscode.window.showQuickPick(getOwners());
})
);
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(() => {
const owners = getOwners();
if (!owners) {
statusBarItem.hide();
return;
}
if (owners.length > 2) {
statusBarItem.text = `CODEOWNERS: ${owners[0]} & ${owners.length - 1} others`;
} else if (owners.length === 2) {
statusBarItem.text = `CODEOWNERS: ${owners[0]} & 1 other`;
} else if (owners.length === 1) {
statusBarItem.text = `CODEOWNERS: ${owners[0]}`;
} else {
statusBarItem.text = 'CODEOWNERS: None';
}
statusBarItem.tooltip = 'Show CODEOWNERS';
statusBarItem.show();
})
);
};
exports.activate = activate;
const deactivate = () => {};
exports.deactivate = deactivate;