-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRegion.regex.ps1
89 lines (72 loc) · 2.27 KB
/
Region.regex.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
<#
.Synopsis
Matches a Collapsible Region in code
.Description
Matches a #region #endregion pair. Returns the Name of the Region and the Content.
#>
param(
# The RegionName. If no RegionName is provided, will match all top-level regions.
[Parameter()]
[string]
$RegionName = $(
'(?:.|\s)+?(?=\z|\s{0,}$)' # Matches anything until whitespace and the end of line.
# This prevents trailing whitespace from failing to pair the match, but allows whitespace within the region name
),
[ValidateSet('PowerShell', 'C#', 'C++', 'C', 'JavaScript', 'JSON', 'Java', 'TypeScript', '')]
[string]
$Language = 'PowerShell'
)
if ($inputObject -and $inputObject -is [IO.FileInfo]) {
if ('.h', '.cpp', '.c' -contains $inputObject.Extension) {
$Language = 'C'
}
elseif ('.cs', '.ps1', '.psm1', '.psd1' -contains $inputObject.Extension) {
$Language =
if ($inputObject.Extension -eq '.cs') { 'C#' } else { 'PowerShell' }
}
elseif ('.js', '.json', '.ts' -contains $inputObject.Extension) {
$Language = 'Javascript'
}
elseif ('.java' -contains $inputObject.Extension) {
$Language = 'Java'
}
}
if ($inputObject -and $inputObject -is [Management.Automation.CommandInfo] -or $inputObject -is [ScriptBlock]) {
$Language = 'PowerShell'
}
if (-not $PSBoundParameters.Language -and -not $Language) {
return
}
$regionStart, $regionEnd =
switch ($Language) {
C {
'\#\s{1,}pragma\s{1,}region\s{1,}', '\#\s{1,}pragma\s{1,}endregion\s{1,}'
}
{ $_ -match 'C#|PowerShell'} {
'\#region\s{1,}', '\#endregion\s{1,}'
}
JavaScript {
'//\#region\s{1,}', '//\#endregion\s{1,}'
}
Java {
'//region\s{1,}', '//endregion\s{1,}'
}
}
if ($PSBoundParameters['RegionName']) {
$RegionName = $RegionName -replace '\s', '\s'
}
@"
(?m)
^\s{0,} # Line start and whitespace
$regionStart # The region start
(?<Name>$RegionName)
(?<Content>
(?:.|\s)+?(?=
(?>
\z|
^\s{0,}$regionEnd$(if ($language -ne 'Java') { '\k<Name>' })
)
)
)
^\s{0,}$regionEnd$(if ($language -ne 'Java') { '\k<Name>' })
"@