forked from JeremySkinner/posh-hg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HgTabExpansion.ps1
342 lines (295 loc) · 8.19 KB
/
HgTabExpansion.ps1
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
$script:hgCommands = @()
$script:hgflowStreams = @()
function HgTabExpansion($lastBlock) {
switch -regex ($lastBlock) {
#handles hgtk help <cmd>
#handles hgtk <cmd>
'thg (help )?(\S*)$' {
thgCommands($matches[2]);
}
#handles hg update <branch name>
#handles hg merge <branch name>
'hg (up|update|merge|co|checkout|pu|pm|closemerge|closebranch) (\S*)$' {
findBranchOrBookmarkOrTags($matches[2])
}
#Handles hg pull -B <bookmark>
'hg pull (-\S* )*-(B) (\S*)$' {
hgRemoteBookmarks($matches[3])
hgLocalBookmarks($matches[3])
}
#Handles hg push -B <bookmark>
'hg push (-\S* )*-(B) (\S*)$' {
hgLocalBookmarks($matches[3])
}
#Handles hg bookmark <bookmark>
'hg (book|bookmark) (\S*)$' {
hgLocalBookmarks($matches[2])
}
#Handles hg push <path>
#Handles hg pull <path>
#Handles hg outgoing <path>
#Handles hg incoming <path>
'hg (push|pull|outgoing|incoming) (-\S* )*(\S*)$' {
hgRemotes($matches[3])
}
#handles hg help <cmd>
#handles hg <cmd>
'hg (help )?(\S*)$' {
hgCommands($matches[2]);
}
#handles hg <cmd> --<option>
'hg (\S+) (-\S* )*--(\S*)$' {
hgOptions $matches[1] $matches[3];
}
#handles hg revert <path>
'hg revert (\S*)$' {
hgFiles $matches[1] 'M|A|R|!'
}
#handles hg add <path>
'hg add (\S*)$' {
hgFiles $matches[1] '\?'
}
# handles hg diff <path>
'hg diff (\S*)$' {
hgFiles $matches[1] 'M'
}
# handles hg commit -(I|X) <path>
'hg commit (\S* )*-(I|X) (\S*)$' {
hgFiles $matches[3] 'M|A|R|!'
}
#handles hg flow * <branch name>
'hg flow (feature|release|hotfix|support) (\S*)$' {
findBranchOrBookmarkOrTags($matches[1]+"/"+$matches[2])
}
#handles hg flow *
'hg flow (\S*)$' {
hgflowStreams($matches[1])
hgLocalBranches($matches[1])
}
}
}
function hgFiles($filter, $pattern) {
hg status $(hg root) |
foreach {
if($_ -match "($pattern){1} (.*)") {
$matches[2]
}
} |
where { $_ -like "*$filter*" } |
foreach { if($_ -like '* *') { "'$_'" } else { $_ } }
}
function hgRemotes($filter) {
hg paths | foreach {
$path = $_.Split("=")[0].Trim();
if($filter -and $path.StartsWith($filter)) {
$path
} elseif(-not $filter) {
$path
}
}
}
# By default the hg command list is populated the first time hgCommands is invoked.
# Invoke PopulateHgCommands in your profile if you don't want the initial hit.
function hgCommands($filter) {
if($script:hgCommands.Length -eq 0) {
populateHgCommands
}
if($filter) {
$hgCommands | ? { $_.StartsWith($filter) } | % { $_.Trim() } | sort
}
else {
$hgCommands | % { $_.Trim() } | sort
}
}
# By default the hg command list is populated the first time hgCommands is invoked.
# Invoke PopulateHgCommands in your profile if you don't want the initial hit.
function PopulateHgCommands() {
$hgCommands = foreach($cmd in (hg help)) {
# Enable shelve
if($cmd -eq "enabled extensions:") {
continue
}
# Stop once after reaching the "Enabled Extensions" section of help.
# Not sure if there's a better way to do this...
if($cmd -eq "additional help topics:") {
break
}
if($cmd -match '^ (\S+) (.*)') {
$matches[1]
}
}
if($global:PoshHgSettings.ShowPatches) {
# MQ integration must be explicitly enabled as the user may not have the extension
$hgCommands += (hg help mq) | % {
if($_ -match '^ (\S+) (.*)') {
$matches[1]
}
}
}
# Add alias expansion
$hgCommands += (hg config alias) | % {
if($_ -match '^alias.(.*)=.*') {
$matches[1]
}
}
$hgCommands += "closemerge"
$script:hgCommands = $hgCommands
}
function findBranchOrBookmarkOrTags($filter){
hgLocalBranches($filter)
hgLocalTags($filter)
hgLocalBookmarks($filter)
}
function hgLocalBranches($filter) {
hg branches | foreach {
if($_ -match "(\S+) .*") {
if($filter -and $matches[1].StartsWith($filter, "CurrentCultureIgnoreCase")) {
$matches[1]
}
elseif(-not $filter) {
$matches[1]
}
}
}
}
function hgLocalTags($filter) {
hg tags | foreach {
if($_ -match "(\S+) .*") {
if($filter -and $matches[1].StartsWith($filter, "CurrentCultureIgnoreCase")) {
$matches[1]
}
elseif(-not $filter) {
$matches[1]
}
}
}
}
function bookmarkName($bookmark) {
$split = $bookmark.Split(" ");
if($bookmark.StartsWith("*")) {
$split[1]
}
else{
$split[0]
}
}
function hgLocalBookmarks($filter) {
hg bookmarks --quiet | foreach {
if($_ -match "(\S+) .*") {
$bookmark = bookmarkName($matches[0])
if($filter -and $bookmark.StartsWith($filter, "CurrentCultureIgnoreCase")) {
$bookmark
}
elseif(-not $filter) {
$bookmark
}
}
}
}
function hgRemoteBookmarks($filter) {
hg incoming -B | foreach {
if($_ -match "(\S+) .*") {
if($filter -and $matches[1].StartsWith($filter, "CurrentCultureIgnoreCase")) {
$matches[1]
}
elseif(-not $filter) {
$matches[1]
}
}
}
}
function hgOptions($cmd, $filter) {
$optList = @()
$output = hg help $cmd
foreach($line in $output) {
if($line -match '^ ((-\S)| ) --(\S+) .*$') {
$opt = $matches[3]
if($filter -and $opt.StartsWith($filter)) {
$optList += '--' + $opt.Trim()
}
elseif(-not $filter) {
$optList += '--' + $opt.Trim()
}
}
}
$optList | sort
}
function thgCommands($filter) {
$cmdList = @()
$output = thg help
foreach($line in $output) {
if($line -match '^ (\S+) (.*)') {
$cmd = $matches[1]
if($filter -and $cmd.StartsWith($filter)) {
$cmdList += $cmd.Trim()
}
elseif(-not $filter) {
$cmdList += $cmd.Trim()
}
}
}
$cmdList | sort
}
function hgflowStreams($filter) {
if($script:hgflowStreams.Length -eq 0) {
$hgflow = ((hg root) + "\.flow")
if (Test-Path $hgflow) {
populatehgflowStreams($hgflow)
} else {
$hgflow = ((hg root) + "\.hgflow")
if (Test-Path $hgflow) {
populatehgflowStreams($hgflow)
}
}
$script:hgflowStreams = $script:hgflowStreams
}
if($filter) {
$hgflowStreams | ? { $_.StartsWith($filter) } | % { $_.Trim() } | sort
}
else {
$hgflowStreams | % { $_.Trim() } | sort
}
}
function populatehgflowStreams($filename) {
$ini = @{}
switch -regex -file $filename
{
"^\[(.+)\]" # Section
{
$section = $matches[1]
$ini[$section] = @()
}
"(.+?)\s*=(.*)" # Key
{
$name,$value = $matches[1..2]
$ini[$section] += $name
}
}
# Supporting by 0.4 and 0.9 files
$script:hgflowStreams = if ($ini["Basic"]) { $ini["Basic"] } else { $ini["branchname"] }
}
$PowerTab_RegisterTabExpansion = if (Get-Module -Name powertab) { Get-Command Register-TabExpansion -Module powertab -ErrorAction SilentlyContinue }
if ($PowerTab_RegisterTabExpansion)
{
& $PowerTab_RegisterTabExpansion "hg.exe" -Type Command {
param($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces) # 1:
$line = $Context.Line
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
$TabExpansionHasOutput.Value = $true
HgTabExpansion $lastBlock
}
return
}
if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion TabExpansionBackup
}
# Set up tab expansion and include hg expansion
function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1]
switch -regex ($lastBlock) {
"^$(Get-AliasPattern hg) (.*)" { HgTabExpansion $lastBlock }
"^$(Get-AliasPattern tgh) (.*)" { HgTabExpansion $lastBlock }
# Fall back on existing tab expansion
default { if (Test-Path Function:\TabExpansionBackup) { TabExpansionBackup $line $lastWord } }
}
}