Provide placeholder completion cell editor
This commit is contained in:
parent
525b76b208
commit
d15d54e801
|
|
@ -26,6 +26,9 @@ class FilenameTemplateSettingsComponent(val filenameTemplates: ArrayList<Filenam
|
||||||
activeColumn.resizable = false
|
activeColumn.resizable = false
|
||||||
activeColumn.maxWidth = determineRequiredWidth(activeColumn)
|
activeColumn.maxWidth = determineRequiredWidth(activeColumn)
|
||||||
|
|
||||||
|
val templateColumn = filenameTemplateTable.columnModel.getColumn(3)
|
||||||
|
templateColumn.cellEditor = PlaceholderCompletionCellEditor()
|
||||||
|
|
||||||
val filenameTemplateTableScrollPane = JBScrollPane(filenameTemplateTable)
|
val filenameTemplateTableScrollPane = JBScrollPane(filenameTemplateTable)
|
||||||
|
|
||||||
panel.add(filenameTemplateTableScrollPane, BorderLayout.CENTER)
|
panel.add(filenameTemplateTableScrollPane, BorderLayout.CENTER)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
package de.idetools.filename_template.settings
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.completion.CompletionResultSet
|
||||||
|
import com.intellij.codeInsight.completion.InsertHandler
|
||||||
|
import com.intellij.codeInsight.completion.InsertionContext
|
||||||
|
import com.intellij.codeInsight.lookup.LookupElement
|
||||||
|
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
||||||
|
import com.intellij.openapi.project.ProjectManager
|
||||||
|
import com.intellij.util.TextFieldCompletionProviderDumbAware
|
||||||
|
import com.intellij.util.textCompletion.TextFieldWithCompletion
|
||||||
|
import java.awt.Component
|
||||||
|
import javax.swing.AbstractCellEditor
|
||||||
|
import javax.swing.JTable
|
||||||
|
import javax.swing.table.TableCellEditor
|
||||||
|
|
||||||
|
internal class PlaceholderCompletionCellEditor : AbstractCellEditor(), TableCellEditor {
|
||||||
|
|
||||||
|
private var component = TextFieldWithCompletion(
|
||||||
|
ProjectManager.getInstance().defaultProject,
|
||||||
|
PlaceholderCompletionProvider(),
|
||||||
|
"",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun getCellEditorValue(): Any {
|
||||||
|
return component.text
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getTableCellEditorComponent(
|
||||||
|
table: JTable,
|
||||||
|
value: Any,
|
||||||
|
isSelected: Boolean,
|
||||||
|
rowIndex: Int,
|
||||||
|
vColIndex: Int
|
||||||
|
): Component {
|
||||||
|
component.text = value as String
|
||||||
|
return component
|
||||||
|
}
|
||||||
|
|
||||||
|
class PlaceholderCompletionProvider : TextFieldCompletionProviderDumbAware(true) {
|
||||||
|
object PlaceholderInsertHandler : InsertHandler<LookupElement> {
|
||||||
|
override fun handleInsert(context: InsertionContext, lookupElement: LookupElement) {
|
||||||
|
val text = context.document.text
|
||||||
|
if (context.tailOffset < text.length && text[context.tailOffset] == '}') {
|
||||||
|
context.document.replaceString(context.tailOffset - 1, context.tailOffset + 1, "}")
|
||||||
|
}
|
||||||
|
if (0 < context.startOffset && text[context.startOffset - 1] == '{') {
|
||||||
|
context.document.replaceString(context.startOffset - 1, context.startOffset + 1, "{")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getPrefix(currentTextPrefix: String): String {
|
||||||
|
if (-1 < currentTextPrefix.indexOf('{')) {
|
||||||
|
return currentTextPrefix.removeRange(0, currentTextPrefix.lastIndexOf('{'))
|
||||||
|
}
|
||||||
|
return currentTextPrefix
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addCompletionVariants(text: String, offset: Int, prefix: String, result: CompletionResultSet) {
|
||||||
|
listOf("|", "0", "DATE", "EPOCH", "HOST", "PROJECT", "USER").forEach {
|
||||||
|
result.addElement(LookupElementBuilder.create("{${it}}").withInsertHandler(PlaceholderInsertHandler))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user