1
1
// The module 'vscode' contains the VS Code extensibility API
2
2
// Import the module and reference it with the alias vscode in your code below
3
- import { workspace } from 'vscode' ;
4
3
import * as vscode from 'vscode' ;
5
4
import { GitExtension } from './types/git' ;
6
5
import CommitType from './config/commit-type' ;
7
6
import { CommitDetailType , CommitDetailQuickPickOptions , MaxSubjectWords } from './config/commit-detail' ;
8
7
import CommitInputType from './config/commit-input' ;
9
8
import CommitTemplate from './config/template-type' ;
9
+ import { Angular } from './config/default-temp' ;
10
10
export interface GitMessage {
11
11
[ index : string ] : string ;
12
+ templateName :string ;
13
+ templateContent :string ;
14
+ icon :string ;
12
15
type : string ;
13
16
scope : string ;
14
17
subject : string ;
15
18
body : string ;
16
19
footer : string ;
17
20
}
18
- //是否展现 Emoji图标 show Emoji or not
19
- const isShowEmoji = workspace . getConfiguration ( 'GitCommitPlugin' ) . get < boolean > ( 'ShowEmoji' ) ;
21
+
20
22
// this method is called when your extension is activated
21
23
// your extension is activated the very first time the command is executed
22
24
export function activate ( context : vscode . ExtensionContext ) {
@@ -47,8 +49,8 @@ export function activate(context: vscode.ExtensionContext) {
47
49
}
48
50
//组合信息 Portfolio information
49
51
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, '' ) ;
52
54
result = config . type !== '' ? result . replace ( / < t y p e > / g, config . type ) : result . replace ( / < t y p e > / g, '' ) ;
53
55
result = config . scope !== '' ? result . replace ( / < s c o p e > / g, config . scope ) : result . replace ( / < s c o p e > / g, '' ) ;
54
56
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) {
57
59
result = result . replace ( / < e n t e r > / g, '\n\n' ) ;
58
60
result = result . replace ( / < s p a c e > / g, ' ' ) ;
59
61
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');
63
62
}
64
63
const gitExtension = getGitExtension ( ) ;
65
64
if ( ! gitExtension ?. enabled ) {
@@ -69,7 +68,6 @@ export function activate(context: vscode.ExtensionContext) {
69
68
70
69
//获取当前的 git仓库实例 Get git repo instance
71
70
let repo : any = gitExtension . getAPI ( 1 ) . repositories [ 0 ] ;
72
- console . log ( repo , 'repo' ) ;
73
71
74
72
//输入提交详情 Input message detail
75
73
const inputMessageDetail = ( _key : string | number ) => {
@@ -94,9 +92,26 @@ export function activate(context: vscode.ExtensionContext) {
94
92
recursiveInputMessage ( startMessageInput ) ;
95
93
} ) ;
96
94
} ;
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
+ } ;
97
111
// 递归输入信息 Recursive input message
98
112
const recursiveInputMessage = ( startMessageInput ?: ( ) => void ) => {
99
113
CommitDetailQuickPickOptions . placeHolder = '搜索提交描述(Search Commit Describe)' ;
114
+
100
115
const _CommitDetailType : Array < CommitDetailType > = JSON . parse ( JSON . stringify ( CommitDetailType ) ) ;
101
116
_CommitDetailType . map ( ( item : any ) => {
102
117
if ( item . isEdit ) {
@@ -109,8 +124,7 @@ export function activate(context: vscode.ExtensionContext) {
109
124
if ( label !== '' ) {
110
125
const _key = select ?. key || 'body' ;
111
126
if ( _key === 'complete' ) {
112
- vscode . commands . executeCommand ( 'workbench.view.scm' ) ;
113
- repo . inputBox . value = messageCombine ( message_config ) ;
127
+ completeInputMessage ( ) ;
114
128
clearMessage ( ) ;
115
129
return false ;
116
130
}
@@ -119,6 +133,10 @@ export function activate(context: vscode.ExtensionContext) {
119
133
clearMessage ( ) ;
120
134
return false ;
121
135
}
136
+ if ( _key === 'template' ) {
137
+ SelectTemplate ( ) ;
138
+ return false ;
139
+ }
122
140
inputMessageDetail ( _key ) ;
123
141
} else {
124
142
clearMessage ( ) ;
@@ -127,10 +145,13 @@ export function activate(context: vscode.ExtensionContext) {
127
145
} ;
128
146
//开始输入 Start input
129
147
const startMessageInput = ( ) => {
130
- CommitDetailQuickPickOptions . placeHolder = '搜索 Git 提交类型(Search Commit Type)' ;
148
+ CommitDetailQuickPickOptions . placeHolder = '搜索 Git 提交类型(Search Commit Type)' ;
131
149
vscode . window . showQuickPick ( CommitType , CommitDetailQuickPickOptions ) . then ( ( select ) => {
132
- const label = ( select && select . label ) || '' ;
150
+ let label = ( select && select . label ) || '' ;
133
151
const icon = ( select && select . icon ) || '' ;
152
+ if ( typeof icon === 'string' && icon . length > 0 ) {
153
+ label = label . split ( ' ' ) [ 1 ] ;
154
+ }
134
155
message_config . type = label ;
135
156
message_config . icon = icon ;
136
157
if ( label !== '' ) {
@@ -139,7 +160,7 @@ export function activate(context: vscode.ExtensionContext) {
139
160
} ) ;
140
161
} ;
141
162
//选择commit 提交的模板
142
- const SelectTemplate = ( ) => {
163
+ const SelectTemplate = ( ) => {
143
164
CommitDetailQuickPickOptions . placeHolder = '选择提交使用的模板' ;
144
165
vscode . window
145
166
. showQuickPick ( CommitTemplate , CommitDetailQuickPickOptions ) . then ( ( select ) => {
@@ -148,8 +169,8 @@ export function activate(context: vscode.ExtensionContext) {
148
169
message_config . templateName = templateName ;
149
170
message_config . templateContent = templateContent ;
150
171
if ( templateName !== '' ) {
151
- startMessageInput ( ) ;
152
- // recursiveInputMessage(startMessageInput );
172
+ completeInputMessage ( true ) ;
173
+ clearMessage ( ) ;
153
174
}
154
175
} ) ;
155
176
} ;
@@ -161,7 +182,7 @@ export function activate(context: vscode.ExtensionContext) {
161
182
return repo . rootUri . path === uri . _rootUri . path ;
162
183
} ) ;
163
184
}
164
- SelectTemplate ( ) ;
185
+ startMessageInput ( ) ;
165
186
} ) ;
166
187
context . subscriptions . push ( disposable ) ;
167
188
}
0 commit comments