Skip to content

Commit

Permalink
Merge pull request #28 from ochotzas/master
Browse files Browse the repository at this point in the history
Refactored code to address code smells and improve readability
  • Loading branch information
BenHerbst committed Jul 8, 2023
2 parents 14800e1 + 1c6ae3a commit 555bf9e
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/org/idaesbasic/Main.kt
Expand Up @@ -7,7 +7,7 @@ import tornadofx.launch
class IdaesbasicApp : App(MainView::class, MainStyle::class) {

override fun start(stage: Stage) {
with(stage) {
stage.apply {
width = 1000.0
height = 800.0
}
Expand All @@ -21,4 +21,4 @@ object Main {
fun main(args: Array<String>) {
launch<IdaesbasicApp>(args)
}
}
}
72 changes: 34 additions & 38 deletions src/main/kotlin/org/idaesbasic/MainView.kt
Expand Up @@ -5,19 +5,19 @@ import javafx.beans.property.SimpleIntegerProperty
import javafx.event.Event
import javafx.geometry.Insets
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyCombination.*
import javafx.scene.input.KeyEvent
import javafx.scene.layout.Priority
import javafx.scene.paint.Color
import javafx.stage.FileChooser
import org.fxmisc.richtext.CodeArea
import org.fxmisc.richtext.LineNumberFactory
import org.fxmisc.wellbehaved.event.EventPattern.*
import org.fxmisc.wellbehaved.event.EventPattern.anyOf
import org.fxmisc.wellbehaved.event.EventPattern.keyPressed
import org.fxmisc.wellbehaved.event.InputMap
import org.fxmisc.wellbehaved.event.Nodes
import org.idaesbasic.buffer.NewBufferView
import org.idaesbasic.buffer.file.FileModel
import org.idaesbasic.buffer.run.ExecutationSetupView
import org.idaesbasic.buffer.run.ExecutionSetupView
import org.idaesbasic.buffer.run.RunConfigController
import org.idaesbasic.intelline.IntellineView
import org.idaesbasic.powerline.PowerLineView
Expand All @@ -30,7 +30,6 @@ import java.nio.file.Paths
import java.util.regex.Matcher
import java.util.regex.Pattern


class MainView : View() {
val controller: MainController by inject()

Expand Down Expand Up @@ -60,7 +59,7 @@ class MainView : View() {
iconColor = Color.web("#f8f8f2")
}
action {
if (controller.currentBufferIndexProperty.value +1 < controller.buffers.size) {
if (controller.currentBufferIndexProperty.value + 1 < controller.buffers.size) {
controller.currentBufferIndexProperty.value += 1
controller.openCurrentBufferIndexBuffer()
}
Expand Down Expand Up @@ -94,15 +93,15 @@ class MainView : View() {
action {
val currentEditor: Editor = controller.getCurrentBuffer() as Editor
showSaveDialogAndSaveText(
arrayOf(
extensions = arrayOf(
FileChooser.ExtensionFilter("All", "*"),
FileChooser.ExtensionFilter("Plain text", "*.txt"),
FileChooser.ExtensionFilter("Java class", "*.java"),
FileChooser.ExtensionFilter("Python", "*.py"),
FileChooser.ExtensionFilter("Kotlin class", "*.kt"),
),
currentEditor.root.text,
currentEditor.fileObject
FileChooser.ExtensionFilter("Kotlin class", "*.kt")
),
text = currentEditor.root.text,
file = currentEditor.fileObject
)
}
graphic = FontIcon().apply {
Expand Down Expand Up @@ -130,13 +129,13 @@ class MainView : View() {
prefHeight = prefWidth
val configsController = find(RunConfigController::class)
val contextMenu = contextmenu {
item ("Change config"){
item("Change config") {
action {
ExecutationSetupView().openWindow()
ExecutionSetupView().openWindow()
}
}
}
val runContextToggleGroup = togglegroup { }
val runContextToggleGroup = togglegroup { }
configsController.configs.onChange() {
// Update context menu to the changed config
it.next()
Expand Down Expand Up @@ -183,11 +182,9 @@ class MainView : View() {
private fun showSaveDialogAndSaveText(extensions: Array<FileChooser.ExtensionFilter>, text: String, file: FileModel) {
if (file.directory == null) {
val fileArray = chooseFile(
"Save file",
extensions,
null,
null,
FileChooserMode.Save
title = "Save file",
filters = extensions,
mode = FileChooserMode.Save
)
if (fileArray.isNotEmpty()) {
val newDirectory = fileArray[0]
Expand All @@ -213,7 +210,7 @@ class MainView : View() {
fun newBuffer() {
val newBuffer = NewBufferView()
controller.buffers.add(newBuffer)
controller.currentBufferIndexProperty.set(controller.buffers.size -1)
controller.currentBufferIndexProperty.set(controller.buffers.size - 1)
controller.openCurrentBufferIndexBuffer()
}

Expand All @@ -226,7 +223,7 @@ class MainViewModel : ItemViewModel<MainView>() {
val root = bind(MainView::root)
}

class Editor(file: FileModel): Fragment() {
class Editor(file: FileModel) : Fragment() {
override val root = CodeArea()
lateinit var fileObject: FileModel

Expand All @@ -237,42 +234,41 @@ class Editor(file: FileModel): Fragment() {
root.appendText(file.text)
val preventTab: InputMap<Event> = InputMap.consume(
anyOf(
// Prevent tab to replace with 4 spaces indention
// Prevent tab to replace with 4 spaces indentation
keyPressed(KeyCode.TAB)
)
)
Nodes.addInputMap(root, preventTab)
val whiteSpace: Pattern = Pattern.compile("^\\s+")
root.addEventHandler(KeyEvent.KEY_PRESSED) { KE ->
// Vim movement
if (KE.getCode() === KeyCode.H) {
if (KE.code === KeyCode.H) {
Platform.runLater {
val caretPosition: Int = root.getCaretPosition()
root.deleteText(caretPosition-1, caretPosition)
val caretPosition: Int = root.caretPosition
root.deleteText(caretPosition - 1, caretPosition)
root.moveTo(caretPosition - 2)
}
}
if (KE.getCode() === KeyCode.L) {
if (KE.code === KeyCode.L) {
Platform.runLater {
val caretPosition: Int = root.getCaretPosition()
root.deleteText(caretPosition-1, caretPosition)
val caretPosition: Int = root.caretPosition
root.deleteText(caretPosition - 1, caretPosition)
root.moveTo(caretPosition)
}
}
// Four spaces for tab
if (KE.getCode() === KeyCode.TAB) {
val caretPosition: Int = root.getCaretPosition()
val currentParagraph: Int = root.getCurrentParagraph()
if (KE.code === KeyCode.TAB) {
val caretPosition: Int = root.caretPosition
root.insertText(caretPosition, " ")
}
// Auto-indent after pressing enter from last line
if (KE.getCode() === KeyCode.ENTER) {
val caretPosition: Int = root.getCaretPosition()
val currentParagraph: Int = root.getCurrentParagraph()
val m0: Matcher = whiteSpace.matcher(root.getParagraph(currentParagraph - 1).getSegments().get(0))
// Auto-indent after pressing enter from the last line
if (KE.code === KeyCode.ENTER) {
val caretPosition: Int = root.caretPosition
val currentParagraph: Int = root.currentParagraph
val m0: Matcher = whiteSpace.matcher(root.getParagraph(currentParagraph - 1).getSegments()[0])
if (m0.find()) Platform.runLater { root.insertText(caretPosition, m0.group()) }
}
//TODO: Implement backpaces that removes 4 space intendations
//TODO: Implement backspaces that remove 4 space indentations
}
}
}
Expand All @@ -285,6 +281,7 @@ class MainController : Controller() {
fun getCurrentBuffer(): Fragment {
return buffers[currentBufferIndexProperty.get()]
}

fun openCurrentBufferIndexBuffer() {
find(MainView::class).switchCenterToBufferView(getCurrentBuffer())
}
Expand All @@ -300,5 +297,4 @@ class MainController : Controller() {
fun saveTextToFile(text: String, file: Path) {
Files.writeString(file, text)
}

}
}
36 changes: 18 additions & 18 deletions src/main/kotlin/org/idaesbasic/buffer/NewBufferView.kt
Expand Up @@ -8,24 +8,24 @@ import tornadofx.*
import java.nio.file.Files
import java.nio.file.Paths

class NewBufferView : Fragment () {
val loadDirectory = SimpleStringProperty()
val loadFile = SimpleStringProperty()
val controller = find(NewBufferController::class)
class NewBufferView : Fragment() {
private val loadDirectory = SimpleStringProperty()
private val loadFile = SimpleStringProperty()
private val controller: NewBufferController by inject()

override val root = squeezebox {
fold("New file", expanded = true) {
val newFileName = SimpleStringProperty()
form {
fieldset ("Basic file configuration"){
fieldset("Basic file configuration") {
field("Filename") {
textfield(newFileName)
}
field("Language") {
label("Plain text")
}
}
fieldset ("Advanced file configuration") {
fieldset("Advanced file configuration") {

}
button("Create") {
Expand All @@ -40,29 +40,29 @@ class NewBufferView : Fragment () {
}
fold("Load file", expanded = true) {
form {
fieldset ("Location") {
field ("Directory") {
fieldset("Location") {
field("Directory") {
hbox {
textfield (loadDirectory)
textfield(loadDirectory)
}
}
field ("File name") {
field("File name") {
hbox {
textfield (loadFile)
textfield(loadFile)
}
}
button ("Auto pick") {
button("Auto pick") {
action {
val extentions = arrayOf(
val extensions = arrayOf(
FileChooser.ExtensionFilter("All", "*"),
FileChooser.ExtensionFilter("Plain text", "*.txt"),
FileChooser.ExtensionFilter("Java class", "*.java"),
FileChooser.ExtensionFilter("Python", "*.py"),
FileChooser.ExtensionFilter("Kotlin class", "*.kt"),
FileChooser.ExtensionFilter("Kotlin class", "*.kt")
)
val fileArray = chooseFile(
"Save file",
extentions,
extensions,
null,
null,
FileChooserMode.Single
Expand All @@ -76,20 +76,20 @@ class NewBufferView : Fragment () {
}
button("Load file") {
action {
controller.loadFileInEditor(loadDirectory.value + loadFile.value)
controller.loadFileInEditor("${loadDirectory.value}${loadFile.value}")
}
}
}
}
}
}

class NewBufferController : Controller () {
class NewBufferController : Controller() {
fun loadFileInEditor(location: String) {
val mainView = find(MainView::class)
val openedFile = Paths.get(location)
val file = FileModel(openedFile.fileName.toString(), openedFile, Files.readString(openedFile))
mainView.newEditor(mainView.controller.currentBufferIndexProperty.get(), file)
mainView.controller.openCurrentBufferIndexBuffer()
}
}
}
38 changes: 0 additions & 38 deletions src/main/kotlin/org/idaesbasic/buffer/run/ExecutationSetupView.kt

This file was deleted.

39 changes: 39 additions & 0 deletions src/main/kotlin/org/idaesbasic/buffer/run/ExecutionSetupView.kt
@@ -0,0 +1,39 @@
package org.idaesbasic.buffer.run

import javafx.beans.property.SimpleStringProperty
import tornadofx.*

class ExecutionSetupView : View() {
private val controller: RunConfigController by inject()

private val configName = SimpleStringProperty()
private val configCommand = SimpleStringProperty()

override val root = squeezebox {
fold("Existing configs") {
listview(controller.configs) {
isEditable = true
cellFragment(RunConfigFragment::class)
}
}
fold("New config") {
form {
fieldset("Information") {
field("Name of config") {
textfield(configName)
}
field("Command") {
textfield(configCommand)
}
}
button("Create config") {
action {
controller.addConfig(RunConfigModel(configName.value, configCommand.value))
configName.set("")
configCommand.set("")
}
}
}
}
}
}

0 comments on commit 555bf9e

Please sign in to comment.