Skip to content

Commit f6ea236

Browse files
authored
feat: automatically trigger autocompletion in view files on $ sign (#45)
2 parents 86b24dd + bd03dee commit f6ea236

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
<psi.referenceContributor
7171
language="PHP"
7272
implementation="com.github.tempest.framework.db.references.QueryBuilderReferenceContributor"/>
73+
<typedHandler
74+
implementation="com.github.tempest.framework.views.completion.ViewVariableTypedHandler"/>
7375
</extensions>
7476
<extensions defaultExtensionNs="com.jetbrains.php">
7577
<libraryRoot id="tempest.meta-storm" path="meta-storm" runtime="false" />

0 commit comments

Comments
 (0)