Add caret position placeholder

This commit is contained in:
Matthias Gugel 2021-06-20 09:45:50 +02:00
parent d72198746f
commit e7f3c6f80e
4 changed files with 22 additions and 3 deletions

View File

@ -30,7 +30,10 @@ class CreateFileWithFilenameTemplateAction(
val contentPanel = NewItemSimplePopupPanel() val contentPanel = NewItemSimplePopupPanel()
val nameField = contentPanel.textField val nameField = contentPanel.textField
nameField.text = placeholderProcessor.processTemplate(project, directory) val (filename, caretPosition) = placeholderProcessor.processTemplate(project, directory)
nameField.text = filename
nameField.caretPosition = caretPosition
val popup = NewItemPopupUtil.createNewItemPopup(IdeBundle.message("title.new.file"), contentPanel, nameField) val popup = NewItemPopupUtil.createNewItemPopup(IdeBundle.message("title.new.file"), contentPanel, nameField)
contentPanel.setApplyAction { event: InputEvent? -> contentPanel.setApplyAction { event: InputEvent? ->

View File

@ -6,7 +6,7 @@ import kotlin.math.max
class PlaceholderProcessor(private val template: String, private val placeholders: List<Placeholder>) { class PlaceholderProcessor(private val template: String, private val placeholders: List<Placeholder>) {
fun processTemplate(project: Project, directory: PsiDirectory): String { fun processTemplate(project: Project, directory: PsiDirectory): ProcessedTemplate {
var placeholderLevel = 0 var placeholderLevel = 0
var placeholder = "" var placeholder = ""
@ -45,6 +45,13 @@ class PlaceholderProcessor(private val template: String, private val placeholder
} }
} }
return context.filename val caretPosition = when {
context.filename.indexOf("{|}") != -1 -> context.filename.indexOf("{|}")
context.filename.indexOfFirst { it == '.' } != -1 -> context.filename.indexOfFirst { it == '.' }
else -> context.filename.length
}
context.filename = context.filename.replace("{|}", "")
return ProcessedTemplate(context.filename, caretPosition)
} }
} }

View File

@ -0,0 +1,6 @@
package de.idetools.filename_template.placeholders
data class ProcessedTemplate(
val filename: String,
val caretPosition: Int
)

View File

@ -64,6 +64,9 @@
The first available sequential number. Define the initial value and desired leading zeros in the template. The first available sequential number. Define the initial value and desired leading zeros in the template.
Example: notes({042}).md => notes(042).md, notes(043).md, ... Example: notes({042}).md => notes(042).md, notes(043).md, ...
</dd> </dd>
<dt>{|}</dt>
<dd>The caret position. If omitted the caret is positioned before the first '.' if available or at the end.</dd>
</dl> </dl>
</dd> </dd>
</dl> </dl>