|
| 1 | +/* |
| 2 | + * Copyright (c) 2017. tangzx([email protected]) |
| 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.codeInsight.editorActions.enter.EnterHandlerDelegateAdapter; |
| 20 | +import com.intellij.openapi.actionSystem.DataContext; |
| 21 | +import com.intellij.openapi.editor.Document; |
| 22 | +import com.intellij.openapi.editor.Editor; |
| 23 | +import com.intellij.openapi.editor.actionSystem.EditorActionHandler; |
| 24 | +import com.intellij.openapi.util.Ref; |
| 25 | +import com.intellij.openapi.util.TextRange; |
| 26 | +import com.intellij.psi.PsiDocumentManager; |
| 27 | +import com.intellij.psi.PsiElement; |
| 28 | +import com.intellij.psi.PsiFile; |
| 29 | +import com.tang.intellij.lua.lang.LuaLanguage; |
| 30 | +import com.tang.intellij.lua.psi.*; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | + |
| 33 | +/** |
| 34 | + * Handles Enter key press for Lua files, especially for 'end' keyword indentation |
| 35 | + */ |
| 36 | +public class LuaEnterHandlerDelegate extends EnterHandlerDelegateAdapter { |
| 37 | + |
| 38 | + @Override |
| 39 | + public Result preprocessEnter(@NotNull PsiFile file, |
| 40 | + @NotNull Editor editor, |
| 41 | + @NotNull Ref<Integer> caretOffset, |
| 42 | + @NotNull Ref<Integer> caretAdvance, |
| 43 | + @NotNull DataContext dataContext, |
| 44 | + EditorActionHandler originalHandler) { |
| 45 | + if (!file.getLanguage().is(LuaLanguage.INSTANCE)) { |
| 46 | + return Result.Continue; |
| 47 | + } |
| 48 | + |
| 49 | + int offset = caretOffset.get(); |
| 50 | + if (offset <= 0) { |
| 51 | + return Result.Continue; |
| 52 | + } |
| 53 | + |
| 54 | + Document document = editor.getDocument(); |
| 55 | + PsiDocumentManager psiDocManager = PsiDocumentManager.getInstance(file.getProject()); |
| 56 | + psiDocManager.commitDocument(document); |
| 57 | + |
| 58 | + // 获取光标前的元素 |
| 59 | + PsiElement element = file.findElementAt(offset - 1); |
| 60 | + if (element == null) { |
| 61 | + return Result.Continue; |
| 62 | + } |
| 63 | + |
| 64 | + // 检查是否在 'end' 或 'until' 关键字之后按 Enter |
| 65 | + if (isAfterEndKeyword(element, document, offset)) { |
| 66 | + // 找到对应的开始关键字的缩进 |
| 67 | + String targetIndent = findMatchingBlockIndent(element); |
| 68 | + if (targetIndent != null) { |
| 69 | + int currentLine = document.getLineNumber(offset); |
| 70 | + int lineStartOffset = document.getLineStartOffset(currentLine); |
| 71 | + int lineEndOffset = document.getLineEndOffset(currentLine); |
| 72 | + |
| 73 | + // 获取当前行内容 |
| 74 | + String lineText = document.getText(new TextRange(lineStartOffset, lineEndOffset)); |
| 75 | + |
| 76 | + // 提取当前行的缩进和关键字后面的内容 |
| 77 | + String keyword = lineText.contains("end") ? "end" : "until"; |
| 78 | + int keywordPos = lineText.indexOf(keyword); |
| 79 | + if (keywordPos >= 0) { |
| 80 | + String afterKeyword = lineText.substring(keywordPos + keyword.length()).trim(); |
| 81 | + |
| 82 | + // 替换整行为正确缩进的关键字 |
| 83 | + document.replaceString(lineStartOffset, lineEndOffset, |
| 84 | + targetIndent + keyword + (afterKeyword.isEmpty() ? "" : " " + afterKeyword)); |
| 85 | + |
| 86 | + // 更新光标位置到关键字之后 |
| 87 | + caretOffset.set(lineStartOffset + targetIndent.length() + keyword.length() + |
| 88 | + (afterKeyword.isEmpty() ? 0 : 1 + afterKeyword.length())); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return Result.Continue; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * 检查光标是否在 'end' 或 'until' 关键字之后 |
| 98 | + */ |
| 99 | + private boolean isAfterEndKeyword(PsiElement element, Document document, int offset) { |
| 100 | + // 简单检查:查看当前行文本是否以 'end' 或 'until' 结尾 |
| 101 | + int currentLine = document.getLineNumber(offset); |
| 102 | + int lineStartOffset = document.getLineStartOffset(currentLine); |
| 103 | + String lineText = document.getText(new TextRange(lineStartOffset, offset)).trim(); |
| 104 | + |
| 105 | + return lineText.equals("end") || lineText.equals("until"); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * 查找与 'end' 或 'until' 匹配的父级语句的缩进 |
| 110 | + * 对于 local f = function() ... end, end 应该对齐到 local 而不是 function |
| 111 | + */ |
| 112 | + private String findMatchingBlockIndent(PsiElement endElement) { |
| 113 | + // 向上遍历 PSI 树,找到包含此 end 的块结构 |
| 114 | + PsiElement current = endElement.getParent(); |
| 115 | + int maxIterations = 50; // 防止无限循环 |
| 116 | + int iterations = 0; |
| 117 | + |
| 118 | + while (current != null && iterations++ < maxIterations) { |
| 119 | + // 找到块结构 |
| 120 | + if (current instanceof LuaStatement || |
| 121 | + current instanceof LuaFuncDef || |
| 122 | + current instanceof LuaLocalFuncDef || |
| 123 | + current instanceof LuaClassMethodDef || |
| 124 | + current instanceof LuaLocalDef |
| 125 | + ) { |
| 126 | + |
| 127 | + // 获取包含该块的最外层语句 |
| 128 | + PsiElement statement = current; |
| 129 | + PsiElement parent = current.getParent(); |
| 130 | + |
| 131 | + // 向上查找,找到最顶层的语句 (例如 LuaLocalDef 包含 LuaLocalFuncDef) |
| 132 | + while (parent != null && !(parent instanceof LuaPsiFile) && !(parent instanceof LuaBlock)) { |
| 133 | + if (parent instanceof LuaExprStat || |
| 134 | + parent instanceof LuaLocalDef || |
| 135 | + parent instanceof LuaAssignStat) { |
| 136 | + statement = parent; |
| 137 | + } |
| 138 | + parent = parent.getParent(); |
| 139 | + } |
| 140 | + |
| 141 | + // 获取语句的缩进 |
| 142 | + int startOffset = statement.getTextRange().getStartOffset(); |
| 143 | + Document document = PsiDocumentManager.getInstance(current.getProject()) |
| 144 | + .getDocument(current.getContainingFile()); |
| 145 | + if (document != null) { |
| 146 | + int lineNumber = document.getLineNumber(startOffset); |
| 147 | + int lineStartOffset = document.getLineStartOffset(lineNumber); |
| 148 | + String lineText = document.getText(new TextRange(lineStartOffset, startOffset)); |
| 149 | + |
| 150 | + // 提取缩进(空格和制表符) |
| 151 | + StringBuilder indent = new StringBuilder(); |
| 152 | + for (char c : lineText.toCharArray()) { |
| 153 | + if (c == ' ' || c == '\t') { |
| 154 | + indent.append(c); |
| 155 | + } else { |
| 156 | + break; |
| 157 | + } |
| 158 | + } |
| 159 | + return indent.toString(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + current = current.getParent(); |
| 164 | + } |
| 165 | + |
| 166 | + return null; |
| 167 | + } |
| 168 | +} |
0 commit comments