-
Notifications
You must be signed in to change notification settings - Fork 71
/
StringIndent_JEE_.ahk
219 lines (212 loc) · 6.54 KB
/
StringIndent_JEE_.ahk
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
;==================================================
;does not support:
;if {
;} else if {
;} else {
;'else DoThis' one-liners
;Label:
;HotkeyLabel::
;note: this function trims any trailing whitespace on a line
;requires: JEE_StrCount/JEE_StrEnds/JEE_StrRept/JEE_StrStarts
;vOpt: c (do not change indent: comments), g (do not change indent: general lines), i (show diagnostic info)
JEE_StrIndent(vText, vIndent:="`t", vOpt:="") {
static vListIf := "If ,If`t,IfEqual,IfExist,IfGreater,IfGreaterOrEqual,IfInString,IfLess,IfLessOrEqual,IfMsgBox,IfNotEqual,IfNotExist,IfNotInString,IfWinActive,IfWinExist,IfWinNotActive,IfWinNotExist"
static vListLoop := "For ,For`t,Loop,While"
static vListTry := "Catch,Finally,Try"
static vListOp := "&& ,|| ,AND ,OR ,&&`t,||`t,AND`t,OR`t"
;also: Else, start/end continuation sections, function definitions, labels
if !InStr(vOpt, "c")
vDoIndentCommentLines := 1
if !InStr(vOpt, "g")
vDoIndentGeneral := 1
if InStr(vOpt, "i")
vDoShowInfo := 1
oArray := StrSplit(vText, "`n", "`r")
vMem := "", oMemLastIf := [], oConSec := {}, oInfo := {}
Loop, % oArray.Length()
{
vTemp := LTrim(oArray[A_Index])
if vPos := RegExMatch(vTemp, "[ `t]*;")
vCode := SubStr(vTemp, 1, vPos-1), vComments := SubStr(vTemp, vPos)
else
vCode := vTemp, vComments := ""
;===============
;HANDLE CONTINUATION SECTIONS
if !vIsConSec && (SubStr(vTemp, 1, 1) = "(") && !InStr(vCode, ")")
{
vOutput .= JEE_StrRept(vIndent, vCountLast) A_LoopField "`r`n"
vIsConSec := 1
continue
}
if (SubStr(vTemp, 1, 1) = ")")
{
vOutput .= JEE_StrRept(vIndent, vCountLast) A_LoopField "`r`n"
vIsConSec := 0
continue
}
if vIsConSec
{
vOutput .= A_LoopField "`r`n"
oConSec[A_Index] := "" ;create key
continue
}
;===============
;IDENTIFY LINE CATEGORY
if JEE_StrStarts(vTemp, "if") && JEE_StrStarts(vTemp, vListIf, ",")
vCateg := "i"
else if JEE_StrStarts(vTemp, vListLoop, ",")
vCateg := "L"
else if JEE_StrStarts(vTemp, "else")
vCateg := "e"
else if JEE_StrStarts(vTemp, vListTry, ",")
vCateg := "t"
else if JEE_StrStarts(vTemp, vListOp, ",")
vCateg := "o"
else if JEE_StrStarts(vTemp, "{")
vCateg := "{"
else if JEE_StrStarts(vTemp, "}")
vCateg := "}"
else if JEE_StrStarts(vTemp, ";") || (vTemp = "") || vIsConSec
vCateg := ";"
else
vCateg := "x"
;===============
;BEFORE APPEND TEXT
if !(vCateg = ";") && !(vCateg = "o") && !(vCateg = "e")
&& JEE_StrEnds(vMem, "_")
vMem := SubStr(vMem, 1, -1)
if !(vCateg = ";") && !(vCateg = "o") && !(vCateg = "e")
&& JEE_StrEnds(vMem, "t")
&& ((vCateg = "x") || (vCateg = "i") || (vCateg = "L") || (vCateg = "}"))
vMem := RTrim(vMem, "t_")
if JEE_StrEnds(vMem, "t_") && !(vCategLast = "}")
&& (vCateg = "e")
vMem := SubStr(vMem, 1, -2)
if (vCateg = "e")
vMem := oMemLastIf[JEE_StrCount(vMem, "{")]
else if (vCateg = "i")
oMemLastIf[JEE_StrCount(vMem, "{")] := vMem
;===============
;APPEND TEXT
vTabCount := JEE_StrCount(vMem, "t") + JEE_StrCount(vMem, "{") - (vCateg = "o") - (vCateg = "{") - (vCateg = "}") + StrLen(vMem2)
(vDoShowInfo = 1) && oInfo[A_Index] := vCateg " " vMem "`t"
if (vDoIndentGeneral && vDoIndentCommentLines)
|| (vDoIndentGeneral && !vDoIndentCommentLines && !(vCateg = ";"))
|| (!vDoIndentGeneral && vDoIndentCommentLines && (vCateg = ";"))
oArray[A_Index] := JEE_StrRept(vIndent, vTabCount) vTemp
;===============
;AFTER APPEND TEXT
if (vCateg = "i") || (vCateg = "L") || (vCateg = "e")
vMem .= "t__"
else if (vCateg = "{")
vMem := SubStr(vMem, 1, -2) "{"
else if (vCateg = "}")
vMem := SubStr(vMem, 1, -1)
else if !(vCateg = ";")
vCategLast := vCateg
vCateg2 := "x"
;===============
}
if vDoIndentCommentLines
{
vLen := StrLen(vIndent)
vCountLast := 0
Loop, % vIndex := oArray.Length()+1
{
vIndex--
vTemp := oArray[vIndex]
if oConSec.HasKey(vIndex)
continue
vCount := 0
while JEE_StrStarts(vTemp, vIndent)
vTemp := SubStr(vTemp, vLen+1), vCount += 1
if JEE_StrStarts(vTemp, ";")
oArray[vIndex] := JEE_StrRept(vIndent, vCountLast) vTemp
else if JEE_StrStarts(vTemp, "}")
vCountLast := vCount+1
else if !(vTemp = "")
vCountLast := vCount
}
}
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2)
if vDoShowInfo
{
Loop, % oArray.Length()
vOutput .= RTrim((A_Index=1?"":"`r`n") oInfo[A_Index] oArray[A_Index])
}
else
{
Loop, % oArray.Length()
vOutput .= RTrim((A_Index=1?"":"`r`n") oArray[A_Index])
}
return vOutput
}
;==================================================
JEE_StrCount(ByRef vText, vNeedle, vCaseSen:=0) {
local vSCS, vCount
if (vNeedle = "")
return ""
vSCS := A_StringCaseSense
StringCaseSense, % vCaseSen ? "On" : "Off"
StrReplace(vText, vNeedle, "", vCount) ;seemed to be faster than line below
;StrReplace(vText, vNeedle, vNeedle, vCount)
StringCaseSense, % vSCS
return vCount
}
;==================================================
JEE_StrRept(vText, vNum) {
if (vNum <= 0)
return
return StrReplace(Format("{:" vNum "}","")," ",vText)
;return StrReplace(Format("{:0" vNum "}",0),0,vText)
}
;==================================================
;e.g. JEE_StrStarts(vText, "http,www", ",")
;e.g. JEE_StrStarts(vText, "http|www", "|")
;e.g. JEE_StrStarts(vText, "http")
JEE_StrStarts(ByRef vText, ByRef vNeedles, vDelim:="", vCaseSen:=0, ByRef vMatch:="") {
if (vDelim = "")
if (!vCaseSen && ("" SubStr(vText, 1, StrLen(vNeedles)) = vNeedles))
|| (vCaseSen && ("" SubStr(vText, 1, StrLen(vNeedles)) == vNeedles))
{
if IsByRef(vMatch)
vMatch := vNeedles
return 1
}
else
return 0
Loop, Parse, vNeedles, % vDelim
if (!vCaseSen && ("" SubStr(vText, 1, StrLen(A_LoopField)) = A_LoopField))
|| (vCaseSen && ("" SubStr(vText, 1, StrLen(A_LoopField)) == A_LoopField))
{
if IsByRef(vMatch)
vMatch := A_LoopField
return A_Index
}
return 0
}
;==================================================
JEE_StrEnds(ByRef vText, ByRef vNeedles, vDelim:="", vCaseSen:=0, ByRef vMatch:="") {
local vLen := StrLen(vText)
if (vDelim = "")
if (!vCaseSen && ("" SubStr(vText, vLen-StrLen(vNeedles)+1) = vNeedles))
|| (vCaseSen && ("" SubStr(vText, 1, vLen-StrLen(vNeedles)+1) == vNeedles))
{
if IsByRef(vMatch)
vMatch := vNeedles
return 1
}
else
return 0
Loop, Parse, vNeedles, % vDelim
if (!vCaseSen && ("" SubStr(vText, vLen-StrLen(A_LoopField)+1) = A_LoopField))
|| (vCaseSen && ("" SubStr(vText, vLen-StrLen(A_LoopField)+1) == A_LoopField))
{
if IsByRef(vMatch)
vMatch := A_LoopField
return A_Index
}
return 0
}
;==================================================