Skip to content

Commit b4f5191

Browse files
committed
enter between range block
1 parent 655d21e commit b4f5191

File tree

5 files changed

+78
-6
lines changed

5 files changed

+78
-6
lines changed

src/main/java/com/tang/intellij/lua/codeInsight/LuaEnterAfterUnmatchedBraceHandler.java renamed to src/main/java/com/tang/intellij/lua/editor/LuaEnterAfterUnmatchedBraceHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.tang.intellij.lua.codeInsight;
17+
package com.tang.intellij.lua.editor;
1818

1919
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate;
2020
import com.intellij.lang.ASTNode;

src/main/java/com/tang/intellij/lua/codeInsight/LuaEnterBetweenBracesHandler.java renamed to src/main/java/com/tang/intellij/lua/editor/LuaEnterBetweenBracesHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.tang.intellij.lua.codeInsight;
17+
package com.tang.intellij.lua.editor;
1818

1919
import com.intellij.codeInsight.editorActions.enter.EnterBetweenBracesHandler;
2020
import com.intellij.openapi.actionSystem.DataContext;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.codeInsight.editorActions.enter.EnterHandlerDelegate
20+
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegateAdapter
21+
import com.intellij.openapi.actionSystem.DataContext
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.psi.PsiDocumentManager
26+
import com.intellij.psi.PsiFile
27+
import com.intellij.psi.codeStyle.CodeStyleManager
28+
import com.intellij.util.IncorrectOperationException
29+
import com.intellij.util.text.CharArrayUtil
30+
import com.tang.intellij.lua.lang.LuaLanguage
31+
import com.tang.intellij.lua.psi.LuaBlock
32+
import com.tang.intellij.lua.psi.LuaIndentRange
33+
34+
class LuaEnterBetweenRangeBlockHandler : EnterHandlerDelegateAdapter() {
35+
36+
override fun preprocessEnter(psiFile: PsiFile,
37+
editor: Editor,
38+
caretOffsetRef: Ref<Int>,
39+
caretAdvance: Ref<Int>,
40+
dataContext: DataContext,
41+
originalHandler: EditorActionHandler?): EnterHandlerDelegate.Result {
42+
if (!psiFile.language.`is`(LuaLanguage.INSTANCE)) {
43+
return EnterHandlerDelegate.Result.Continue
44+
}
45+
46+
val caretOffset = caretOffsetRef.get()
47+
val document = editor.document
48+
val text = document.charsSequence
49+
val prevCharOffset = CharArrayUtil.shiftBackward(text, caretOffset - 1, " \t")
50+
val nextCharOffset = CharArrayUtil.shiftForward(text, caretOffset, " \t")
51+
52+
val prev = psiFile.findElementAt(prevCharOffset)
53+
val next = psiFile.findElementAt(nextCharOffset)
54+
if (prev != next &&
55+
prev != null &&
56+
next != null &&
57+
prev.parent == next.parent &&
58+
prev.parent is LuaIndentRange &&
59+
prev.nextSibling is LuaBlock) {
60+
originalHandler?.execute(editor, editor.caretModel.currentCaret, dataContext)
61+
PsiDocumentManager.getInstance(psiFile.project).commitDocument(editor.document)
62+
try {
63+
CodeStyleManager.getInstance(psiFile.project).adjustLineIndent(psiFile, editor.caretModel.offset)
64+
} catch (e: IncorrectOperationException) {
65+
}
66+
67+
return EnterHandlerDelegate.Result.DefaultForceIndent
68+
}
69+
return EnterHandlerDelegate.Result.Continue
70+
}
71+
}

src/main/java/com/tang/intellij/lua/codeInsight/LuaEnterInDocHandler.java renamed to src/main/java/com/tang/intellij/lua/editor/LuaEnterInDocHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.tang.intellij.lua.codeInsight;
17+
package com.tang.intellij.lua.editor;
1818

1919
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate;
2020
import com.intellij.lang.ASTNode;

src/main/resources/META-INF/plugin.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@
144144
<highlightUsagesHandlerFactory implementation="com.tang.intellij.lua.codeInsight.highlighting.LuaHighlightUsagesHandlerFactory"/>
145145

146146
<!--editor handlers-->
147-
<enterHandlerDelegate implementation="com.tang.intellij.lua.codeInsight.LuaEnterAfterUnmatchedBraceHandler" />
148-
<enterHandlerDelegate implementation="com.tang.intellij.lua.codeInsight.LuaEnterInDocHandler"/>
149-
<enterHandlerDelegate implementation="com.tang.intellij.lua.codeInsight.LuaEnterBetweenBracesHandler"/>
147+
<enterHandlerDelegate implementation="com.tang.intellij.lua.editor.LuaEnterAfterUnmatchedBraceHandler" />
148+
<enterHandlerDelegate implementation="com.tang.intellij.lua.editor.LuaEnterInDocHandler"/>
149+
<enterHandlerDelegate implementation="com.tang.intellij.lua.editor.LuaEnterBetweenBracesHandler"/>
150+
<enterHandlerDelegate implementation="com.tang.intellij.lua.editor.LuaEnterBetweenRangeBlockHandler"/>
150151
<backspaceHandlerDelegate implementation="com.tang.intellij.lua.codeInsight.LuaBackspaceHandlerDelegate"/>
151152
<typedHandler implementation="com.tang.intellij.lua.editor.LuaAutoPopupHandler"/>
152153
<typedHandler implementation="com.tang.intellij.lua.editor.LuaAutoIndentHandler"/>

0 commit comments

Comments
 (0)