[Editor] Enable auto-indentation and change indent size to 3
This commit is contained in:
@@ -4,6 +4,8 @@ import com.bartlomiejpluta.base.editor.code.highlighting.SyntaxHighlighter
|
|||||||
import javafx.beans.property.Property
|
import javafx.beans.property.Property
|
||||||
import javafx.beans.value.ObservableValue
|
import javafx.beans.value.ObservableValue
|
||||||
import javafx.concurrent.Task
|
import javafx.concurrent.Task
|
||||||
|
import javafx.scene.input.KeyCode
|
||||||
|
import javafx.scene.input.KeyEvent
|
||||||
import javafx.scene.layout.StackPane
|
import javafx.scene.layout.StackPane
|
||||||
import org.fxmisc.flowless.VirtualizedScrollPane
|
import org.fxmisc.flowless.VirtualizedScrollPane
|
||||||
import org.fxmisc.richtext.CodeArea
|
import org.fxmisc.richtext.CodeArea
|
||||||
@@ -39,9 +41,22 @@ class CodeEditor(private val highlighter: ObservableValue<out SyntaxHighlighter>
|
|||||||
.subscribe(this::applyHighlighting)
|
.subscribe(this::applyHighlighting)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
initAutoIndents()
|
||||||
|
|
||||||
children += VirtualizedScrollPane(editor)
|
children += VirtualizedScrollPane(editor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun initAutoIndents() {
|
||||||
|
editor.addEventHandler(KeyEvent.KEY_PRESSED) { event ->
|
||||||
|
if (event.code === KeyCode.ENTER) {
|
||||||
|
WHITESPACE.find(editor.getParagraph(editor.currentParagraph - 1).segments[0])?.apply {
|
||||||
|
editor.insertText(editor.caretPosition, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun computeHighlightingAsync(): Task<StyleSpans<Collection<String>>> {
|
private fun computeHighlightingAsync(): Task<StyleSpans<Collection<String>>> {
|
||||||
val code = editor.text
|
val code = editor.text
|
||||||
|
|
||||||
@@ -58,4 +73,8 @@ class CodeEditor(private val highlighter: ObservableValue<out SyntaxHighlighter>
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getUserAgentStylesheet(): String = highlighter.value.stylesheet.base64URL.toExternalForm()
|
override fun getUserAgentStylesheet(): String = highlighter.value.stylesheet.base64URL.toExternalForm()
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val WHITESPACE = "^\\s+".toRegex()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,11 @@ class JavaSyntaxHighlightingStylesheet : Stylesheet() {
|
|||||||
val string by cssclass()
|
val string by cssclass()
|
||||||
val comment by cssclass()
|
val comment by cssclass()
|
||||||
val paragraphBox by cssclass()
|
val paragraphBox by cssclass()
|
||||||
|
val paragraphText by cssclass()
|
||||||
|
|
||||||
val hasCaret by csspseudoclass()
|
val hasCaret by csspseudoclass()
|
||||||
|
|
||||||
|
val tabSize by cssproperty<Int>("-fx-tab-size")
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@@ -52,5 +56,9 @@ class JavaSyntaxHighlightingStylesheet : Stylesheet() {
|
|||||||
paragraphBox and hasCaret {
|
paragraphBox and hasCaret {
|
||||||
backgroundColor = multi(c("#f2f9fc"))
|
backgroundColor = multi(c("#f2f9fc"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
paragraphText {
|
||||||
|
tabSize.value = 3
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user