Skip to content

Commit 73fe7d5

Browse files
committed
[Format] More smart indentation
1 parent 648f42d commit 73fe7d5

File tree

1 file changed

+83
-17
lines changed

1 file changed

+83
-17
lines changed

source/server/format.hexa

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ fun autoFormatWholeFile(file: String): String {
2323
let tokens = Lexer.tokenize(content, 'hexa.hexa', format: true)
2424
let count = tokens.line.length - 1
2525
let result = ['']
26+
var currentCall = [Token.Eof]
27+
var currentCallLevel = Token.Eof
2628
var lastLine = 1
2729
var lastToken = Token.Eof
2830
var lastLastToken = Token.Eof
29-
var lastLastGeneric = false
31+
var lastIndentation = ''
3032

3133
var tabs = ''
3234
var tabStack = ['']
@@ -123,15 +125,43 @@ fun autoFormatWholeFile(file: String): String {
123125
result.push('\n')
124126
}
125127

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
127131
result.push(tabs)
128132

129133
// `=\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+
) {
131138
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+
}
132161
}
133162

134163
shouldSpace = false
164+
lastIndentation = indentation
135165
}
136166

137167
// When evaluated to `true` it means `shouldSpace = false`
@@ -184,6 +214,8 @@ fun autoFormatWholeFile(file: String): String {
184214
lastToken != Token.Unequal and
185215
lastToken != Token.Less and
186216

217+
currentCallLevel != Token.KNew and
218+
187219
// Preserve cause `Class<T>()` and `a > (b)`
188220
not (
189221
(
@@ -212,6 +244,15 @@ fun autoFormatWholeFile(file: String): String {
212244
) and (
213245
(lastLastToken == Token.At and tokens.column[i] != getTokenLength(i - 1) + (tokens.column[i - 1] ?? 0))
214246
)
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+
)
215256
)
216257
)
217258
) or
@@ -312,25 +353,40 @@ fun autoFormatWholeFile(file: String): String {
312353
)
313354
) or
314355

315-
// `demo<T>` but not `demo <T>`
316356
(
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)
322378
)
323379
)
324380
) or
325-
lastLastGeneric or
381+
326382
// `T>`
327383
// `T>>`
328384
(
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)
334390
)
335391
)
336392
) or
@@ -349,12 +405,22 @@ fun autoFormatWholeFile(file: String): String {
349405
shouldSpace = false
350406
}
351407

352-
lastLastGeneric = token == Token.Less and not shouldSpace
353408

354409
if shouldSpace {
355410
result.push(' ')
356411
}
357412

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+
358424
switch token {
359425
case Comment: result.push(`/*` + tokens.value[i] + `*/`)
360426
case CommentLine:
@@ -372,7 +438,7 @@ fun autoFormatWholeFile(file: String): String {
372438
case QuotedString: switch tokens.meta[i] {
373439
// TODO FORMAT: should be here 1-tab less
374440
case SingleQuote: result.push(`'` + tokens.value[i] + `'`)
375-
case DoubleQuote: result.push(`"` + tokens.value[i] + `"`)
441+
case _: result.push(`"` + tokens.value[i] + `"`)
376442
}
377443
case _: result.push(Token.stringify(token, tokens.value[i], tokens.meta[i]))
378444
}

0 commit comments

Comments
 (0)