-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insert equals and invoke completion automatically when completing tom…
…l key
- Loading branch information
Showing
2 changed files
with
28 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.demonwav.mcdev.toml.toml | ||
|
||
import com.intellij.codeInsight.AutoPopupController | ||
import com.intellij.codeInsight.completion.InsertHandler | ||
import com.intellij.codeInsight.completion.InsertionContext | ||
import com.intellij.codeInsight.lookup.LookupElement | ||
import com.intellij.psi.PsiDocumentManager | ||
import org.toml.lang.psi.TomlElementTypes | ||
import org.toml.lang.psi.TomlKeyValue | ||
import org.toml.lang.psi.ext.elementType | ||
|
||
/** Inserts `=` after the completed key if missing and invokes the completion popup for the value automatically */ | ||
class TomlKeyInsertionHandler(private val keyValue: TomlKeyValue) : InsertHandler<LookupElement> { | ||
override fun handleInsert(context: InsertionContext, item: LookupElement) { | ||
val hasEq = keyValue.children.any { it.elementType == TomlElementTypes.EQ } | ||
if (!hasEq) { | ||
context.document.insertString(context.tailOffset, " = ") | ||
PsiDocumentManager.getInstance(context.project).commitDocument(context.document) | ||
context.editor.caretModel.moveToOffset(context.tailOffset) // The tail offset is tracked automatically | ||
AutoPopupController.getInstance(context.project).scheduleAutoPopup(context.editor); | ||
} | ||
} | ||
} |