|
| 1 | +/* |
| 2 | + * Copyright (c) 2017. tangzx(love.tangzx@qq.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.tang.intellij.lua.editor; |
| 18 | + |
| 19 | +import com.intellij.lang.Language; |
| 20 | +import com.intellij.openapi.editor.Document; |
| 21 | +import com.intellij.openapi.editor.Editor; |
| 22 | +import com.intellij.openapi.project.Project; |
| 23 | +import com.intellij.openapi.util.TextRange; |
| 24 | +import com.intellij.psi.PsiDocumentManager; |
| 25 | +import com.intellij.psi.PsiElement; |
| 26 | +import com.intellij.psi.PsiFile; |
| 27 | +import com.intellij.psi.codeStyle.lineIndent.LineIndentProvider; |
| 28 | +import com.intellij.psi.tree.IElementType; |
| 29 | +import com.tang.intellij.lua.lang.LuaLanguage; |
| 30 | +import com.tang.intellij.lua.psi.*; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | +import org.jetbrains.annotations.Nullable; |
| 33 | + |
| 34 | +/** |
| 35 | + * Provides smart line indentation for Lua files |
| 36 | + */ |
| 37 | +public class LuaLineIndentProvider implements LineIndentProvider { |
| 38 | + |
| 39 | + @Override |
| 40 | + public @Nullable String getLineIndent(@NotNull Project project, @NotNull Editor editor, @Nullable Language language, int offset) { |
| 41 | + if (!isSuitableFor(language)) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); |
| 46 | + if (file == null) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + return calculateIndent(project, editor, file, offset); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public boolean isSuitableFor(@Nullable Language language) { |
| 55 | + return language instanceof LuaLanguage; |
| 56 | + } |
| 57 | + |
| 58 | + private @Nullable String calculateIndent(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, int offset) { |
| 59 | + Document document = editor.getDocument(); |
| 60 | + |
| 61 | + if (offset == 0) { |
| 62 | + return ""; |
| 63 | + } |
| 64 | + |
| 65 | + // 获取当前行号 |
| 66 | + int lineNumber = document.getLineNumber(offset); |
| 67 | + if (lineNumber == 0) { |
| 68 | + return ""; |
| 69 | + } |
| 70 | + |
| 71 | + // 获取前一行的缩进 |
| 72 | + String previousLineIndent = getPreviousLineIndent(document, lineNumber); |
| 73 | + |
| 74 | + // 获取前一行的最后一个有意义的元素 |
| 75 | + PsiElement elementAtPrevLine = getLastMeaningfulElementInPreviousLine(file, document, lineNumber); |
| 76 | + |
| 77 | + if (elementAtPrevLine == null) { |
| 78 | + return previousLineIndent; |
| 79 | + } |
| 80 | + |
| 81 | + // 根据前一行的内容决定缩进 |
| 82 | + return calculateIndentBasedOnPreviousLine(editor, project, elementAtPrevLine, previousLineIndent); |
| 83 | + } |
| 84 | + |
| 85 | + private String getPreviousLineIndent(Document document, int currentLineNumber) { |
| 86 | + if (currentLineNumber <= 0) { |
| 87 | + return ""; |
| 88 | + } |
| 89 | + |
| 90 | + int prevLineStartOffset = document.getLineStartOffset(currentLineNumber - 1); |
| 91 | + int prevLineEndOffset = document.getLineEndOffset(currentLineNumber - 1); |
| 92 | + String prevLineText = document.getText(new TextRange(prevLineStartOffset, prevLineEndOffset)); |
| 93 | + |
| 94 | + // 提取前一行的缩进 |
| 95 | + StringBuilder indent = new StringBuilder(); |
| 96 | + for (char c : prevLineText.toCharArray()) { |
| 97 | + if (c == ' ' || c == '\t') { |
| 98 | + indent.append(c); |
| 99 | + } else { |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + return indent.toString(); |
| 104 | + } |
| 105 | + |
| 106 | + private @Nullable PsiElement getLastMeaningfulElementInPreviousLine(PsiFile file, Document document, int currentLineNumber) { |
| 107 | + if (currentLineNumber <= 0) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + int prevLineStartOffset = document.getLineStartOffset(currentLineNumber - 1); |
| 112 | + int prevLineEndOffset = document.getLineEndOffset(currentLineNumber - 1); |
| 113 | + |
| 114 | + // 从行尾开始向前查找有意义的元素 |
| 115 | + for (int offset = prevLineEndOffset - 1; offset >= prevLineStartOffset; offset--) { |
| 116 | + PsiElement element = file.findElementAt(offset); |
| 117 | + if (element != null && !isWhitespaceOrComment(element)) { |
| 118 | + return element; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + private boolean isWhitespaceOrComment(PsiElement element) { |
| 126 | + IElementType elementType = element.getNode().getElementType(); |
| 127 | + return |
| 128 | + elementType == LuaTypes.SHORT_COMMENT || |
| 129 | + elementType == LuaTypes.DOC_COMMENT || |
| 130 | + elementType == LuaTypes.BLOCK_COMMENT; |
| 131 | + } |
| 132 | + |
| 133 | + private String calculateIndentBasedOnPreviousLine(Editor editor, Project project, PsiElement element, String baseIndent) { |
| 134 | + String indentUnit = getIndentUnit(editor, project); |
| 135 | + |
| 136 | + // 检查是否是table相关的缩进 |
| 137 | + String tableIndent = calculateTableIndent(element, baseIndent, indentUnit); |
| 138 | + if (tableIndent != null) { |
| 139 | + return tableIndent; |
| 140 | + } |
| 141 | + |
| 142 | + // 查找包含该元素的语句或块 |
| 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 | +// } |
| 155 | + |
| 156 | + // 检查元素本身的类型 |
| 157 | + IElementType elementType = element.getNode().getElementType(); |
| 158 | + |
| 159 | + // 需要增加缩进的情况 |
| 160 | + if (elementType == LuaTypes.DO || |
| 161 | + elementType == LuaTypes.THEN || |
| 162 | + elementType == LuaTypes.ELSE || |
| 163 | + elementType == LuaTypes.ELSEIF || |
| 164 | + elementType == LuaTypes.FUNCTION || |
| 165 | + elementType == LuaTypes.LOCAL || |
| 166 | + elementType == LuaTypes.REPEAT || |
| 167 | + elementType == LuaTypes.LCURLY || |
| 168 | + elementType == LuaTypes.LPAREN || |
| 169 | + elementType == LuaTypes.AND || |
| 170 | + elementType == LuaTypes.OR |
| 171 | + ) { |
| 172 | + return baseIndent + indentUnit; |
| 173 | + } |
| 174 | + return baseIndent; |
| 175 | + } |
| 176 | + |
| 177 | + 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; |
| 196 | + } |
| 197 | + |
| 198 | + private @Nullable String calculateTableIndent(PsiElement element, String baseIndent, String indentUnit) { |
| 199 | + IElementType elementType = element.getNode().getElementType(); |
| 200 | + |
| 201 | + // 如果是在左大括号后,增加缩进 |
| 202 | + if (elementType == LuaTypes.LCURLY || elementType == LuaTypes.LPAREN) { |
| 203 | + return baseIndent + indentUnit; |
| 204 | + } |
| 205 | + |
| 206 | + // 如果是逗号或分号,并且它们直接在table内(不在嵌套的function等结构中) |
| 207 | + if (elementType == LuaTypes.COMMA || elementType == LuaTypes.SEMI) { |
| 208 | + if (isDirectTableSeparator(element)) { |
| 209 | + return baseIndent; // 在table field分隔符后保持当前缩进 |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return null; |
| 214 | + } |
| 215 | + |
| 216 | + private boolean isDirectTableSeparator(PsiElement separator) { |
| 217 | + // 向上查找,看是否是直接在table内的分隔符 |
| 218 | + PsiElement current = separator.getParent(); |
| 219 | + while (current != null) { |
| 220 | + if (current instanceof LuaTableExpr) { |
| 221 | + return true; |
| 222 | + } |
| 223 | + // 如果遇到了这些复杂结构,说明分隔符不是直接在table内 |
| 224 | + if (current instanceof LuaFuncBody || |
| 225 | + current instanceof LuaIfStat || |
| 226 | + current instanceof LuaWhileStat || |
| 227 | + current instanceof LuaForAStat || |
| 228 | + current instanceof LuaForBStat || |
| 229 | + current instanceof LuaRepeatStat || |
| 230 | + current instanceof LuaDoStat) { |
| 231 | + return false; |
| 232 | + } |
| 233 | + current = current.getParent(); |
| 234 | + } |
| 235 | + return false; |
| 236 | + } |
| 237 | + |
| 238 | + private String getIndentUnit(Editor editor, Project project) { |
| 239 | + var settings = editor.getSettings(); |
| 240 | + if (settings.isUseTabCharacter(project)) { |
| 241 | + return "\t"; |
| 242 | + } else { |
| 243 | + int indentSize = settings.getTabSize(project); |
| 244 | + if (indentSize <= 0) { |
| 245 | + indentSize = settings.getTabSize(project); |
| 246 | + } |
| 247 | + return " ".repeat(Math.max(0, indentSize)); |
| 248 | + } |
| 249 | + } |
| 250 | +} |
0 commit comments