|
| 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 | +} |
0 commit comments