Skip to content

Commit 1327ba5

Browse files
committed
Fix function indent
1 parent 5dc990b commit 1327ba5

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/main/java/com/tang/intellij/lua/editor/LuaLineIndentProvider.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.intellij.psi.PsiDocumentManager;
2525
import com.intellij.psi.PsiElement;
2626
import com.intellij.psi.PsiFile;
27+
import com.intellij.psi.TokenType;
2728
import com.intellij.psi.codeStyle.lineIndent.LineIndentProvider;
2829
import com.intellij.psi.tree.IElementType;
2930
import com.tang.intellij.lua.lang.LuaLanguage;
@@ -127,7 +128,8 @@ private boolean isWhitespaceOrComment(PsiElement element) {
127128
return
128129
elementType == LuaTypes.SHORT_COMMENT ||
129130
elementType == LuaTypes.DOC_COMMENT ||
130-
elementType == LuaTypes.BLOCK_COMMENT;
131+
elementType == LuaTypes.BLOCK_COMMENT ||
132+
elementType == TokenType.WHITE_SPACE;
131133
}
132134

133135
private String calculateIndentBasedOnPreviousLine(Editor editor, Project project, PsiElement element, String baseIndent) {
@@ -140,18 +142,18 @@ private String calculateIndentBasedOnPreviousLine(Editor editor, Project project
140142
}
141143

142144
// 查找包含该元素的语句或块
143-
// PsiElement parent = element;
144-
// while (parent != null) {
145-
// if (shouldIncreaseIndent(parent)) {
146-
// return baseIndent + indentUnit;
147-
// }
148-
//
149-
// if (parent instanceof LuaBlock) {
150-
// break;
151-
// }
152-
//
153-
// parent = parent.getParent();
154-
// }
145+
PsiElement parent = element;
146+
while (parent != null) {
147+
if (shouldIncreaseIndent(parent)) {
148+
return baseIndent + indentUnit;
149+
}
150+
151+
if (parent instanceof LuaBlock) {
152+
break;
153+
}
154+
155+
parent = parent.getParent();
156+
}
155157

156158
// 检查元素本身的类型
157159
IElementType elementType = element.getNode().getElementType();
@@ -175,24 +177,13 @@ private String calculateIndentBasedOnPreviousLine(Editor editor, Project project
175177
}
176178

177179
private boolean shouldIncreaseIndent(PsiElement element) {
178-
if (element instanceof LuaBlock) {
179-
// 检查是否是新开始的块
180-
PsiElement parent = element.getParent();
181-
return parent instanceof LuaIfStat ||
182-
parent instanceof LuaWhileStat ||
183-
parent instanceof LuaForAStat ||
184-
parent instanceof LuaForBStat ||
185-
parent instanceof LuaRepeatStat ||
186-
parent instanceof LuaFuncBody ||
187-
parent instanceof LuaDoStat;
188-
}
189-
190-
// 对于table,只有在开始新的table时才增加缩进
191-
if (element instanceof LuaTableExpr) {
192-
return true;
193-
}
194-
195-
return false;
180+
return element instanceof LuaIfStat ||
181+
element instanceof LuaWhileStat ||
182+
element instanceof LuaForAStat ||
183+
element instanceof LuaForBStat ||
184+
element instanceof LuaRepeatStat ||
185+
element instanceof LuaFuncBody ||
186+
element instanceof LuaDoStat;
196187
}
197188

198189
private @Nullable String calculateTableIndent(PsiElement element, String baseIndent, String indentUnit) {

0 commit comments

Comments
 (0)