|
| 1 | +package com.github.tempest.framework.views.completion |
| 2 | + |
| 3 | +import com.github.tempest.framework.TempestFrameworkUtil |
| 4 | +import com.intellij.codeInsight.AutoPopupController |
| 5 | +import com.intellij.codeInsight.editorActions.TypedHandlerDelegate |
| 6 | +import com.intellij.openapi.editor.Editor |
| 7 | +import com.intellij.openapi.project.Project |
| 8 | +import com.intellij.psi.PsiFile |
| 9 | + |
| 10 | +class ViewVariableTypedHandler : TypedHandlerDelegate() { |
| 11 | + override fun checkAutoPopup(charTyped: Char, project: Project, editor: Editor, file: PsiFile): Result { |
| 12 | + if (charTyped != '$') return Result.CONTINUE |
| 13 | + if (!file.name.endsWith(TempestFrameworkUtil.TEMPLATE_SUFFIX)) return Result.CONTINUE |
| 14 | + |
| 15 | + val offset = editor.caretModel.offset |
| 16 | + val text = editor.document.charsSequence |
| 17 | + |
| 18 | + if (isInsideTemplateTag(text, offset)) { |
| 19 | + AutoPopupController.getInstance(project).scheduleAutoPopup(editor) |
| 20 | + return Result.STOP |
| 21 | + } |
| 22 | + |
| 23 | + return Result.CONTINUE |
| 24 | + } |
| 25 | + |
| 26 | + private fun isInsideTemplateTag(text: CharSequence, offset: Int): Boolean { |
| 27 | + val textBefore = text.subSequence(0, offset).toString() |
| 28 | + |
| 29 | + val lastRawOpen = textBefore.lastIndexOf("{!!") |
| 30 | + val lastRawClose = textBefore.lastIndexOf("!!}") |
| 31 | + val lastEscapedOpen = textBefore.lastIndexOf("{{") |
| 32 | + val lastEscapedClose = textBefore.lastIndexOf("}}") |
| 33 | + |
| 34 | + val inRawTag = lastRawOpen > lastRawClose && lastRawOpen >= 0 |
| 35 | + val inEscapedTag = lastEscapedOpen > lastEscapedClose && lastEscapedOpen >= 0 |
| 36 | + |
| 37 | + if (!inRawTag && !inEscapedTag) return false |
| 38 | + |
| 39 | + val textAfter = text.subSequence(offset, text.length).toString() |
| 40 | + val expectedCloseTag = if (inRawTag && lastRawOpen > lastEscapedOpen) "!!}" else "}}" |
| 41 | + |
| 42 | + return textAfter.contains(expectedCloseTag) |
| 43 | + } |
| 44 | +} |
0 commit comments