11// The module 'vscode' contains the VS Code extensibility API
22// Import the module and reference it with the alias vscode in your code below
3- import { workspace } from 'vscode' ;
43import * as vscode from 'vscode' ;
54import { GitExtension } from './types/git' ;
65import CommitType from './config/commit-type' ;
76import { CommitDetailType , CommitDetailQuickPickOptions , MaxSubjectWords } from './config/commit-detail' ;
87import CommitInputType from './config/commit-input' ;
98import CommitTemplate from './config/template-type' ;
9+ import { Angular } from './config/default-temp' ;
1010export interface GitMessage {
1111 [ index : string ] : string ;
12+ templateName :string ;
13+ templateContent :string ;
14+ icon :string ;
1215 type : string ;
1316 scope : string ;
1417 subject : string ;
1518 body : string ;
1619 footer : string ;
1720}
18- //是否展现 Emoji图标 show Emoji or not
19- const isShowEmoji = workspace . getConfiguration ( 'GitCommitPlugin' ) . get < boolean > ( 'ShowEmoji' ) ;
21+
2022// this method is called when your extension is activated
2123// your extension is activated the very first time the command is executed
2224export function activate ( context : vscode . ExtensionContext ) {
@@ -47,8 +49,8 @@ export function activate(context: vscode.ExtensionContext) {
4749 }
4850 //组合信息 Portfolio information
4951 function messageCombine ( config : GitMessage ) {
50- let result = config . templateContent ;
51- result = isShowEmoji ? result . replace ( / < i c o n > / g, config . icon ) : result . replace ( / < i c o n > / g, '' ) ;
52+ let result = config . templateContent || Angular . templateContent ;
53+ result = config . icon ? result . replace ( / < i c o n > / g, config . icon ) : result . replace ( / < i c o n > / g, '' ) ;
5254 result = config . type !== '' ? result . replace ( / < t y p e > / g, config . type ) : result . replace ( / < t y p e > / g, '' ) ;
5355 result = config . scope !== '' ? result . replace ( / < s c o p e > / g, config . scope ) : result . replace ( / < s c o p e > / g, '' ) ;
5456 result = config . subject !== '' ? result . replace ( / < s u b j e c t > / g, config . subject ) : result . replace ( / < s u b j e c t > / g, '' ) ;
@@ -57,9 +59,6 @@ export function activate(context: vscode.ExtensionContext) {
5759 result = result . replace ( / < e n t e r > / g, '\n\n' ) ;
5860 result = result . replace ( / < s p a c e > / g, ' ' ) ;
5961 return result . trim ( ) ;
60- // return [`${config.type}${config.scope ? '(' + config.scope + ')' : ''}: ${config.subject} -- ${config.templateName}`, config.body, config.footer]
61- // .filter((item) => item)
62- // .join('\n\n');
6362 }
6463 const gitExtension = getGitExtension ( ) ;
6564 if ( ! gitExtension ?. enabled ) {
@@ -69,7 +68,6 @@ export function activate(context: vscode.ExtensionContext) {
6968
7069 //获取当前的 git仓库实例 Get git repo instance
7170 let repo : any = gitExtension . getAPI ( 1 ) . repositories [ 0 ] ;
72- console . log ( repo , 'repo' ) ;
7371
7472 //输入提交详情 Input message detail
7573 const inputMessageDetail = ( _key : string | number ) => {
@@ -94,9 +92,26 @@ export function activate(context: vscode.ExtensionContext) {
9492 recursiveInputMessage ( startMessageInput ) ;
9593 } ) ;
9694 } ;
95+ //是否存在模板 If has template
96+ const existTemplete = ( ) => {
97+ return Array . isArray ( CommitTemplate ) && CommitTemplate . length > 0 ;
98+ } ;
99+ //完成输入 Complete input message
100+ const completeInputMessage = ( select ?:boolean ) => {
101+ vscode . commands . executeCommand ( 'workbench.view.scm' ) ;
102+ if ( existTemplete ( ) && ! select ) {
103+ const defaultTemp = CommitTemplate . find ( ( item ) => item . default ) ;
104+ if ( defaultTemp !== undefined ) {
105+ message_config . templateName = defaultTemp . templateName ;
106+ message_config . templateContent = defaultTemp . templateContent ;
107+ }
108+ }
109+ repo . inputBox . value = messageCombine ( message_config ) ;
110+ } ;
97111 // 递归输入信息 Recursive input message
98112 const recursiveInputMessage = ( startMessageInput ?: ( ) => void ) => {
99113 CommitDetailQuickPickOptions . placeHolder = '搜索提交描述(Search Commit Describe)' ;
114+
100115 const _CommitDetailType : Array < CommitDetailType > = JSON . parse ( JSON . stringify ( CommitDetailType ) ) ;
101116 _CommitDetailType . map ( ( item : any ) => {
102117 if ( item . isEdit ) {
@@ -109,8 +124,7 @@ export function activate(context: vscode.ExtensionContext) {
109124 if ( label !== '' ) {
110125 const _key = select ?. key || 'body' ;
111126 if ( _key === 'complete' ) {
112- vscode . commands . executeCommand ( 'workbench.view.scm' ) ;
113- repo . inputBox . value = messageCombine ( message_config ) ;
127+ completeInputMessage ( ) ;
114128 clearMessage ( ) ;
115129 return false ;
116130 }
@@ -119,6 +133,10 @@ export function activate(context: vscode.ExtensionContext) {
119133 clearMessage ( ) ;
120134 return false ;
121135 }
136+ if ( _key === 'template' ) {
137+ SelectTemplate ( ) ;
138+ return false ;
139+ }
122140 inputMessageDetail ( _key ) ;
123141 } else {
124142 clearMessage ( ) ;
@@ -127,10 +145,13 @@ export function activate(context: vscode.ExtensionContext) {
127145 } ;
128146 //开始输入 Start input
129147 const startMessageInput = ( ) => {
130- CommitDetailQuickPickOptions . placeHolder = '搜索 Git 提交类型(Search Commit Type)' ;
148+ CommitDetailQuickPickOptions . placeHolder = '搜索 Git 提交类型(Search Commit Type)' ;
131149 vscode . window . showQuickPick ( CommitType , CommitDetailQuickPickOptions ) . then ( ( select ) => {
132- const label = ( select && select . label ) || '' ;
150+ let label = ( select && select . label ) || '' ;
133151 const icon = ( select && select . icon ) || '' ;
152+ if ( typeof icon === 'string' && icon . length > 0 ) {
153+ label = label . split ( ' ' ) [ 1 ] ;
154+ }
134155 message_config . type = label ;
135156 message_config . icon = icon ;
136157 if ( label !== '' ) {
@@ -139,7 +160,7 @@ export function activate(context: vscode.ExtensionContext) {
139160 } ) ;
140161 } ;
141162 //选择commit 提交的模板
142- const SelectTemplate = ( ) => {
163+ const SelectTemplate = ( ) => {
143164 CommitDetailQuickPickOptions . placeHolder = '选择提交使用的模板' ;
144165 vscode . window
145166 . showQuickPick ( CommitTemplate , CommitDetailQuickPickOptions ) . then ( ( select ) => {
@@ -148,8 +169,8 @@ export function activate(context: vscode.ExtensionContext) {
148169 message_config . templateName = templateName ;
149170 message_config . templateContent = templateContent ;
150171 if ( templateName !== '' ) {
151- startMessageInput ( ) ;
152- // recursiveInputMessage(startMessageInput );
172+ completeInputMessage ( true ) ;
173+ clearMessage ( ) ;
153174 }
154175 } ) ;
155176 } ;
@@ -161,7 +182,7 @@ export function activate(context: vscode.ExtensionContext) {
161182 return repo . rootUri . path === uri . _rootUri . path ;
162183 } ) ;
163184 }
164- SelectTemplate ( ) ;
185+ startMessageInput ( ) ;
165186 } ) ;
166187 context . subscriptions . push ( disposable ) ;
167188}
0 commit comments