@@ -23,10 +23,12 @@ fun autoFormatWholeFile(file: String): String {
23
23
let tokens = Lexer.tokenize(content, 'hexa.hexa', format: true)
24
24
let count = tokens.line.length - 1
25
25
let result = ['']
26
+ var currentCall = [Token.Eof]
27
+ var currentCallLevel = Token.Eof
26
28
var lastLine = 1
27
29
var lastToken = Token.Eof
28
30
var lastLastToken = Token.Eof
29
- var lastLastGeneric = false
31
+ var lastIndentation = ''
30
32
31
33
var tabs = ''
32
34
var tabStack = ['']
@@ -123,15 +125,43 @@ fun autoFormatWholeFile(file: String): String {
123
125
result.push('\n')
124
126
}
125
127
126
- // TODO if bool? of array[i] not valid `if tabCase[depth] {}`, require == true
128
+ // TODO if `Bool?` of `array[i]` not valid `if tabCase[depth] {}`, require `== true`
129
+ // as `tabCase[depth] == true` cause it can be `null`
130
+ var indentation = tabs
127
131
result.push(tabs)
128
132
129
133
// `=\nvalue`
130
- if lastToken == Token.Assign {
134
+ // TODO weird indentation right here
135
+ if lastToken == Token.Assign or (
136
+ lastToken == Token.KReturn and token != Token.BlockClose
137
+ ) {
131
138
result.push('\t')
139
+ indentation += '\t'
140
+ }
141
+
142
+ // The idea here is `value1 and \n value2` should have `value2` with tab
143
+ if
144
+ lastToken == Token.Add or
145
+ lastToken == Token.Subtract or
146
+ lastToken == Token.Multiply or
147
+ lastToken == Token.LogicalAnd or
148
+ lastToken == Token.LogicalOr
149
+ {
150
+ if lastIndentation.length > indentation.length {
151
+ result.push('\t')
152
+ indentation += '\t'
153
+ }
154
+ }
155
+
156
+ if token == Token.KAs {
157
+ if lastIndentation != indentation {
158
+ result.push('\t')
159
+ indentation += '\t'
160
+ }
132
161
}
133
162
134
163
shouldSpace = false
164
+ lastIndentation = indentation
135
165
}
136
166
137
167
// When evaluated to `true` it means `shouldSpace = false`
@@ -184,6 +214,8 @@ fun autoFormatWholeFile(file: String): String {
184
214
lastToken != Token.Unequal and
185
215
lastToken != Token.Less and
186
216
217
+ currentCallLevel != Token.KNew and
218
+
187
219
// Preserve cause `Class<T>()` and `a > (b)`
188
220
not (
189
221
(
@@ -212,6 +244,15 @@ fun autoFormatWholeFile(file: String): String {
212
244
) and (
213
245
(lastLastToken == Token.At and tokens.column[i] != getTokenLength(i - 1) + (tokens.column[i - 1] ?? 0))
214
246
)
247
+ ) and
248
+
249
+ // `call()` but `var callback ()`
250
+ not (
251
+ (
252
+ lastToken == Token.Identifier
253
+ ) and (
254
+ ((lastLastToken == Token.KVar or lastLastToken == Token.KLet) and true)
255
+ )
215
256
)
216
257
)
217
258
) or
@@ -312,25 +353,40 @@ fun autoFormatWholeFile(file: String): String {
312
353
)
313
354
) or
314
355
315
- // `demo<T>` but not `demo <T>`
316
356
(
317
- token == Token.Less and (
318
- (
319
- lastToken == Token.Identifier or lastToken == Token.Title
320
- ) and (
321
- tokens.column[i] == getTokenLength(i - 1) + (tokens.column[i - 1] ?? 0)
357
+ token == Token.Less and
358
+ (
359
+ // Preserve cause `a < c` and `T<T>`
360
+ not (
361
+ tokens.column[i] != getTokenLength(i - 1) + (tokens.column[i - 1] ?? 0)
362
+ )
363
+ )
364
+ ) or
365
+
366
+ (
367
+ (
368
+ lastToken == Token.Less and (
369
+ token == Token.Identifier or
370
+ token == Token.At or
371
+ token == Token.Title
372
+ )
373
+ ) and
374
+ (
375
+ // Preserve cause `a < c` and `T<T>` and `T<name: T>`
376
+ not (
377
+ tokens.column[i] != getTokenLength(i - 1) + (tokens.column[i - 1] ?? 0)
322
378
)
323
379
)
324
380
) or
325
- lastLastGeneric or
381
+
326
382
// `T>`
327
383
// `T>>`
328
384
(
329
- (token == Token.Greater or token == Token.BitwiseRightShift) and (
330
- (
331
- lastToken == Token.Identifier or lastToken == Token.Title
332
- ) and (
333
- tokens.column[i] = = getTokenLength(i - 1) + (tokens.column[i - 1] ?? 0)
385
+ (token == Token.Greater or token == Token.BitwiseRightShift) and
386
+ (
387
+ // Preserve cause `a > c` and `T<T>`
388
+ not (
389
+ tokens.column[i] ! = getTokenLength(i - 1) + (tokens.column[i - 1] ?? 0)
334
390
)
335
391
)
336
392
) or
@@ -349,12 +405,22 @@ fun autoFormatWholeFile(file: String): String {
349
405
shouldSpace = false
350
406
}
351
407
352
- lastLastGeneric = token == Token.Less and not shouldSpace
353
408
354
409
if shouldSpace {
355
410
result.push(' ')
356
411
}
357
412
413
+ if token == Token.CallOpen {
414
+ // TODO `fun`
415
+ currentCall.push(lastToken)
416
+ currentCallLevel = lastToken
417
+ }
418
+
419
+ if token == Token.CallClose {
420
+ currentCall.pop()
421
+ currentCallLevel = currentCall[currentCall.length - 1]
422
+ }
423
+
358
424
switch token {
359
425
case Comment: result.push(`/*` + tokens.value[i] + `*/`)
360
426
case CommentLine:
@@ -372,7 +438,7 @@ fun autoFormatWholeFile(file: String): String {
372
438
case QuotedString: switch tokens.meta[i] {
373
439
// TODO FORMAT: should be here 1-tab less
374
440
case SingleQuote: result.push(`'` + tokens.value[i] + `'`)
375
- case DoubleQuote : result.push(`"` + tokens.value[i] + `"`)
441
+ case _ : result.push(`"` + tokens.value[i] + `"`)
376
442
}
377
443
case _: result.push(Token.stringify(token, tokens.value[i], tokens.meta[i]))
378
444
}
0 commit comments