Skip to content

Commit 95ee299

Browse files
committed
auto insert " end" when typed '(' after 'function'
1 parent b4f5191 commit 95ee299

File tree

3 files changed

+55
-60
lines changed

3 files changed

+55
-60
lines changed

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

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.AutoPopupController
20+
import com.intellij.codeInsight.editorActions.TypedHandlerDelegate
21+
import com.intellij.openapi.editor.Editor
22+
import com.intellij.openapi.project.Project
23+
import com.intellij.psi.PsiDocumentManager
24+
import com.intellij.psi.PsiFile
25+
import com.tang.intellij.lua.psi.LuaFuncBody
26+
27+
/**
28+
29+
* Created by TangZX on 2016/11/28.
30+
*/
31+
class LuaTypedHandler : TypedHandlerDelegate() {
32+
33+
override fun checkAutoPopup(charTyped: Char, project: Project, editor: Editor, file: PsiFile): TypedHandlerDelegate.Result {
34+
if (charTyped == ':' || charTyped == '@') {
35+
AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null)
36+
return TypedHandlerDelegate.Result.STOP
37+
}
38+
return super.checkAutoPopup(charTyped, project, editor, file)
39+
}
40+
41+
override fun charTyped(c: Char, project: Project, editor: Editor, file: PsiFile): TypedHandlerDelegate.Result {
42+
// function() <caret> end 自动加上end
43+
if (c == '(') {
44+
PsiDocumentManager.getInstance(project).commitDocument(editor.document)
45+
val pos = editor.caretModel.offset
46+
val element = file.findElementAt(pos)
47+
if (element != null && element.parent is LuaFuncBody) {
48+
editor.document.insertString(pos + 1, " end")
49+
return TypedHandlerDelegate.Result.STOP
50+
}
51+
}
52+
return super.charTyped(c, project, editor, file)
53+
}
54+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
<enterHandlerDelegate implementation="com.tang.intellij.lua.editor.LuaEnterBetweenBracesHandler"/>
150150
<enterHandlerDelegate implementation="com.tang.intellij.lua.editor.LuaEnterBetweenRangeBlockHandler"/>
151151
<backspaceHandlerDelegate implementation="com.tang.intellij.lua.codeInsight.LuaBackspaceHandlerDelegate"/>
152-
<typedHandler implementation="com.tang.intellij.lua.editor.LuaAutoPopupHandler"/>
152+
<typedHandler implementation="com.tang.intellij.lua.editor.LuaTypedHandler"/>
153153
<typedHandler implementation="com.tang.intellij.lua.editor.LuaAutoIndentHandler"/>
154154

155155
<!--highlighting-->

0 commit comments

Comments
 (0)