diff --git a/package-lock.json b/package-lock.json index baeb5c7..351ce64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "git-commit-plugin", - "version": "1.2.1", + "version": "1.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "git-commit-plugin", - "version": "1.2.1", + "version": "1.5.0", "license": "MIT", "dependencies": { "vsce": "^1.95.0", @@ -22,7 +22,7 @@ "eslint": "^6.8.0", "glob": "^7.1.6", "mocha": "^7.0.1", - "typescript": "^3.7.5", + "typescript": "^5.8.3", "vscode-test": "^1.3.0" }, "engines": { @@ -3260,16 +3260,17 @@ } }, "node_modules/typescript": { - "version": "3.9.10", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.10.tgz", - "integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==", + "version": "5.8.3", + "resolved": "https://registry.npmmirror.com/typescript/-/typescript-5.8.3.tgz", + "integrity": "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==", "dev": true, + "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { - "node": ">=4.2.0" + "node": ">=14.17" } }, "node_modules/uc.micro": { @@ -6316,9 +6317,9 @@ } }, "typescript": { - "version": "3.9.10", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.10.tgz", - "integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==", + "version": "5.8.3", + "resolved": "https://registry.npmmirror.com/typescript/-/typescript-5.8.3.tgz", + "integrity": "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==", "dev": true }, "uc.micro": { diff --git a/package.json b/package.json index 67c0f37..b23473e 100644 --- a/package.json +++ b/package.json @@ -115,11 +115,11 @@ "eslint": "^6.8.0", "glob": "^7.1.6", "mocha": "^7.0.1", - "typescript": "^3.7.5", + "typescript": "^5.8.3", "vscode-test": "^1.3.0" }, "dependencies": { "vsce": "^1.95.0", "vscode-nls-i18n": "^0.2.4" } -} \ No newline at end of file +} diff --git a/src/extension.ts b/src/extension.ts index 8856cd3..00e5e2d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,7 +1,7 @@ // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; -import { GitExtension } from './types/git'; +import { GitExtension, API, Repository } from './types/git'; import GetCommitTypes, { CommitType } from './config/commit-type'; import { GetCommitDetailType, @@ -251,21 +251,79 @@ export async function activate(context: vscode.ExtensionContext) { //点击图标触发快捷选项 Click the icon to trigger shortcut options let disposable = vscode.commands.registerCommand( 'extension.showGitCommit', - (uri?) => { + async (uri?: vscode.Uri) => { + // 获取激活的 git 扩展 + const gitExtension = vscode.extensions.getExtension('vscode.git')?.exports; + if (!gitExtension) { + vscode.window.showErrorMessage('Git 扩展未激活'); + return; + } + + // 获取 Git API + const api: API = gitExtension.getAPI(1); + if (!api) { + vscode.window.showErrorMessage('无法获取 Git API'); + return; + } + + // 选第一个仓库作为默认 + let repo: Repository | undefined = api.repositories[0]; + if (!repo) { + vscode.window.showErrorMessage('未找到 Git 仓库'); + return; + } + + // 如果有 uri,找到对应的仓库 if (uri) { - //如果有多个repo 寻找当前的 进行填充 If there are multiple repos looking for the current to populate - repo = gitExtension.getAPI(1).repositories.find(repo => { - const uriRoot = uri._rootUri ? uri._rootUri : uri.rootUri; - return repo.rootUri.path === uriRoot?.path; + const uriRoot = (uri as any)._rootUri ?? uri; + const foundRepo = api.repositories.find(r => r.rootUri.path === uriRoot.path); + if (foundRepo) { + repo = foundRepo; + } + } + + // 取当前提交输入框内容(去空格) + const currentInput: string = repo.inputBox.value?.trim() ?? ''; + + if (currentInput.length > 0) { + // 从配置中获取所有提交类型,包括 icon + const commitTypes: CommitType[] = GetCommitTypes(); + + // 构造 prefix -> emoji 映射,取 icon 字符 + const emojiMap: Record = {}; + commitTypes.forEach(ct => { + if (ct.key && ct.icon) { + emojiMap[ct.key] = ct.icon.trim(); + } }); + + // 遍历映射,匹配输入框前缀 + for (const prefix in emojiMap) { + const emoji = emojiMap[prefix]; + const regex = new RegExp(`^${prefix}`, 'i'); + // 如果输入以 prefix 开头且尚未带 emoji,则添加 emoji + if (regex.test(currentInput) && !currentInput.startsWith(emoji)) { + repo.inputBox.value = `${emoji} ${currentInput}`; + vscode.window.showInformationMessage(`已添加表情:${emoji}`); + return; + } + } + + // 如果已有表情或不匹配,提示无需更改 + vscode.window.showInformationMessage('已存在提交信息,无需更改'); + return; } + + // 输入为空,走原始交互流程 if (FillSubjectWithCurrent) { const message = repo.inputBox.value; setMessageInput('subject', message); } + startMessageInput(); }, ); + context.subscriptions.push(disposable); }