Skip to content

Commit 8a991a0

Browse files
committed
Replace text as you type during code completion
1 parent 01b8174 commit 8a991a0

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/sql/directive/PercentDirectiveHandler.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.intellij.codeInsight.lookup.AutoCompletionPolicy
2121
import com.intellij.codeInsight.lookup.LookupElement
2222
import com.intellij.codeInsight.lookup.LookupElementBuilder
2323
import com.intellij.psi.PsiElement
24+
import com.intellij.psi.util.prevLeafs
2425

2526
class PercentDirectiveHandler(
2627
originalFile: PsiElement,
@@ -32,6 +33,11 @@ class PercentDirectiveHandler(
3233
element,
3334
result,
3435
) { bind ->
36+
val beforeTextChars =
37+
this.element.prevLeafs
38+
.takeWhile { prev -> prev.text != "%" }
39+
.toList()
40+
.size
3541
listOf(
3642
"if",
3743
"elseif",
@@ -46,7 +52,12 @@ class PercentDirectiveHandler(
4652
}.map {
4753
LookupElementBuilder
4854
.create(it)
49-
.withAutoCompletionPolicy(AutoCompletionPolicy.ALWAYS_AUTOCOMPLETE)
55+
.withInsertHandler { context, _ ->
56+
val start = context.startOffset - beforeTextChars
57+
val tail = context.tailOffset
58+
context.document.replaceString(start, tail, it)
59+
context.editor.caretModel.moveToOffset(start + it.length)
60+
}.withAutoCompletionPolicy(AutoCompletionPolicy.ALWAYS_AUTOCOMPLETE)
5061
}
5162
}
5263

0 commit comments

Comments
 (0)