-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathextension.ts
103 lines (89 loc) · 3.05 KB
/
extension.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
96
97
98
99
100
101
102
103
import * as vscode from "vscode";
import { GitExtension, Repository } from "./api/git";
import Gitmoji from "./gitmoji/gitmoji";
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand(
"extension.Gitmoji",
(uri?) => {
const git = getGitExtension();
const language = getEnvLanguage();
if (!git) {
vscode.window.showErrorMessage("Unable to load Git Extension");
return;
}
let additionalEmojis: Array<any> =
vscode.workspace.getConfiguration().get("gitmoji.additionalEmojis") ||
[];
const showEmojiCode:
| boolean
| undefined = vscode.workspace
.getConfiguration()
.get("gitmoji.showEmojiCode");
let emojis = [];
let onlyUseAdditionalEmojis:
| boolean
| undefined = vscode.workspace
.getConfiguration()
.get("gitmoji.onlyUseAdditionalEmojis");
if (onlyUseAdditionalEmojis === true) {
emojis = [...additionalEmojis];
} else {
emojis = [...Gitmoji, ...additionalEmojis];
}
const items = emojis.map((emojiObj) => {
const { description, description_zh_cn, code, emoji } = emojiObj;
const displayDescription =
language === "zh-cn" ? description_zh_cn || description : description;
const displayCode = showEmojiCode ? code : "";
const label = `${emoji} ${displayDescription} ${displayCode}`;
return {
label,
code,
emoji,
};
});
vscode.window.showQuickPick(items).then(function (selected) {
if (selected) {
vscode.commands.executeCommand("workbench.view.scm");
let outputType = vscode.workspace
.getConfiguration()
.get("gitmoji.outputType");
if (uri) {
let selectedRepository = git.repositories.find((repository) => {
return repository.rootUri.path === uri._rootUri.path;
});
if (selectedRepository) {
if (outputType === "emoji") {
prefixCommit(selectedRepository, selected.emoji);
} else {
prefixCommit(selectedRepository, selected.code);
}
}
} else {
for (let repo of git.repositories) {
if (outputType === "emoji") {
prefixCommit(repo, selected.emoji);
} else {
prefixCommit(repo, selected.code);
}
}
}
}
});
}
);
context.subscriptions.push(disposable);
}
function getEnvLanguage() {
const language = vscode.env.language;
return language;
}
function prefixCommit(repository: Repository, prefix: String) {
repository.inputBox.value = `${prefix} ${repository.inputBox.value}`;
}
function getGitExtension() {
const vscodeGit = vscode.extensions.getExtension<GitExtension>("vscode.git");
const gitExtension = vscodeGit && vscodeGit.exports;
return gitExtension && gitExtension.getAPI(1);
}
export function deactivate() {}