Skip to content

Commit 6411bb1

Browse files
committed
修复格式化bug
1 parent 41347d3 commit 6411bb1

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

EmmyLua-LS/src/main/kotlin/com/tang/vscode/formatter/FormattingFormatter.kt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -965,15 +965,21 @@ class FormattingFormatter(val file: ILuaFile, val psi: PsiFile) {
965965

966966

967967
private fun printBinaryExpr(element: FormattingElement) {
968-
var currentLine = file.getLine(element.textRange.startOffset).first
968+
// 二元表达式的布局 可能是 aaa and bbb
969+
// 也可能是 aaa or
970+
// bbb
971+
// 还可能是 aaa
972+
// or bbb
969973
var lastElement: FormattingElement? = null
970974
element.children.forEach {
971-
val line = file.getLine(it.textRange.startOffset).first
972-
if (line > currentLine) {
973-
currentLine = line
974-
if (lastElement?.type != FormattingType.Comment) {
975-
//则换行
976-
ctx.print(lineSeparator)
975+
lastElement?.let {lastElement->
976+
val lastElementLine = file.getLine(lastElement.textRange.endOffset).first
977+
val line = file.getLine(it.textRange.startOffset).first
978+
if (line > lastElementLine) {
979+
if (lastElement.type != FormattingType.Comment) {
980+
//则换行
981+
ctx.print(lineSeparator)
982+
}
977983
}
978984
}
979985

@@ -1407,6 +1413,7 @@ class FormattingFormatter(val file: ILuaFile, val psi: PsiFile) {
14071413

14081414
private fun printCallArgs(element: FormattingElement) {
14091415
var firstArg = true
1416+
var isAlignment = false
14101417
// 上一个参数
14111418
var lastArgOrBracket: FormattingElement? = null;
14121419
element.children.forEach {
@@ -1419,7 +1426,6 @@ class FormattingFormatter(val file: ILuaFile, val psi: PsiFile) {
14191426
}
14201427
"(" -> {
14211428
ctx.print(text)
1422-
ctx.enterBlockEnv()
14231429
lastArgOrBracket = it
14241430
}
14251431
")" -> {
@@ -1428,7 +1434,9 @@ class FormattingFormatter(val file: ILuaFile, val psi: PsiFile) {
14281434
ctx.print(lineSeparator)
14291435
}
14301436
}
1431-
ctx.exitBlockEnv()
1437+
if (isAlignment) {
1438+
ctx.exitBlockEnv()
1439+
}
14321440
ctx.print(text)
14331441
}
14341442
else -> {
@@ -1437,21 +1445,18 @@ class FormattingFormatter(val file: ILuaFile, val psi: PsiFile) {
14371445
}
14381446
}
14391447
else -> {
1440-
if (firstArg) {
1441-
if (FormattingOptions.blankBeforeFirstArg) {
1442-
ctx.print(FormattingOptions.emptyWhite)
1443-
}
1444-
firstArg = false
1445-
}
1446-
14471448
lastArgOrBracket?.let { arg ->
14481449
if (file.getLine(arg.textRange.endOffset).first != file.getLine(it.textRange.startOffset).first) {
14491450
ctx.print(lineSeparator)
14501451
}
14511452
}
1452-
1453-
14541453
printElement(it)
1454+
1455+
if (firstArg) {
1456+
ctx.enterBlockEnv()
1457+
isAlignment = true
1458+
firstArg = false
1459+
}
14551460
lastArgOrBracket = it
14561461
}
14571462
}

0 commit comments

Comments
 (0)