Skip to content

Commit 1c6ae3a

Browse files
committed
Refactored code to address code smells and improve readability
1 parent 14800e1 commit 1c6ae3a

File tree

6 files changed

+108
-112
lines changed

6 files changed

+108
-112
lines changed

src/main/kotlin/org/idaesbasic/Main.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import tornadofx.launch
77
class IdaesbasicApp : App(MainView::class, MainStyle::class) {
88

99
override fun start(stage: Stage) {
10-
with(stage) {
10+
stage.apply {
1111
width = 1000.0
1212
height = 800.0
1313
}
@@ -21,4 +21,4 @@ object Main {
2121
fun main(args: Array<String>) {
2222
launch<IdaesbasicApp>(args)
2323
}
24-
}
24+
}

src/main/kotlin/org/idaesbasic/MainView.kt

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import javafx.beans.property.SimpleIntegerProperty
55
import javafx.event.Event
66
import javafx.geometry.Insets
77
import javafx.scene.input.KeyCode
8-
import javafx.scene.input.KeyCombination.*
98
import javafx.scene.input.KeyEvent
109
import javafx.scene.layout.Priority
1110
import javafx.scene.paint.Color
1211
import javafx.stage.FileChooser
1312
import org.fxmisc.richtext.CodeArea
1413
import org.fxmisc.richtext.LineNumberFactory
15-
import org.fxmisc.wellbehaved.event.EventPattern.*
14+
import org.fxmisc.wellbehaved.event.EventPattern.anyOf
15+
import org.fxmisc.wellbehaved.event.EventPattern.keyPressed
1616
import org.fxmisc.wellbehaved.event.InputMap
1717
import org.fxmisc.wellbehaved.event.Nodes
1818
import org.idaesbasic.buffer.NewBufferView
1919
import org.idaesbasic.buffer.file.FileModel
20-
import org.idaesbasic.buffer.run.ExecutationSetupView
20+
import org.idaesbasic.buffer.run.ExecutionSetupView
2121
import org.idaesbasic.buffer.run.RunConfigController
2222
import org.idaesbasic.intelline.IntellineView
2323
import org.idaesbasic.powerline.PowerLineView
@@ -30,7 +30,6 @@ import java.nio.file.Paths
3030
import java.util.regex.Matcher
3131
import java.util.regex.Pattern
3232

33-
3433
class MainView : View() {
3534
val controller: MainController by inject()
3635

@@ -60,7 +59,7 @@ class MainView : View() {
6059
iconColor = Color.web("#f8f8f2")
6160
}
6261
action {
63-
if (controller.currentBufferIndexProperty.value +1 < controller.buffers.size) {
62+
if (controller.currentBufferIndexProperty.value + 1 < controller.buffers.size) {
6463
controller.currentBufferIndexProperty.value += 1
6564
controller.openCurrentBufferIndexBuffer()
6665
}
@@ -94,15 +93,15 @@ class MainView : View() {
9493
action {
9594
val currentEditor: Editor = controller.getCurrentBuffer() as Editor
9695
showSaveDialogAndSaveText(
97-
arrayOf(
96+
extensions = arrayOf(
9897
FileChooser.ExtensionFilter("All", "*"),
9998
FileChooser.ExtensionFilter("Plain text", "*.txt"),
10099
FileChooser.ExtensionFilter("Java class", "*.java"),
101100
FileChooser.ExtensionFilter("Python", "*.py"),
102-
FileChooser.ExtensionFilter("Kotlin class", "*.kt"),
103-
),
104-
currentEditor.root.text,
105-
currentEditor.fileObject
101+
FileChooser.ExtensionFilter("Kotlin class", "*.kt")
102+
),
103+
text = currentEditor.root.text,
104+
file = currentEditor.fileObject
106105
)
107106
}
108107
graphic = FontIcon().apply {
@@ -130,13 +129,13 @@ class MainView : View() {
130129
prefHeight = prefWidth
131130
val configsController = find(RunConfigController::class)
132131
val contextMenu = contextmenu {
133-
item ("Change config"){
132+
item("Change config") {
134133
action {
135-
ExecutationSetupView().openWindow()
134+
ExecutionSetupView().openWindow()
136135
}
137136
}
138137
}
139-
val runContextToggleGroup = togglegroup { }
138+
val runContextToggleGroup = togglegroup { }
140139
configsController.configs.onChange() {
141140
// Update context menu to the changed config
142141
it.next()
@@ -183,11 +182,9 @@ class MainView : View() {
183182
private fun showSaveDialogAndSaveText(extensions: Array<FileChooser.ExtensionFilter>, text: String, file: FileModel) {
184183
if (file.directory == null) {
185184
val fileArray = chooseFile(
186-
"Save file",
187-
extensions,
188-
null,
189-
null,
190-
FileChooserMode.Save
185+
title = "Save file",
186+
filters = extensions,
187+
mode = FileChooserMode.Save
191188
)
192189
if (fileArray.isNotEmpty()) {
193190
val newDirectory = fileArray[0]
@@ -213,7 +210,7 @@ class MainView : View() {
213210
fun newBuffer() {
214211
val newBuffer = NewBufferView()
215212
controller.buffers.add(newBuffer)
216-
controller.currentBufferIndexProperty.set(controller.buffers.size -1)
213+
controller.currentBufferIndexProperty.set(controller.buffers.size - 1)
217214
controller.openCurrentBufferIndexBuffer()
218215
}
219216

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

229-
class Editor(file: FileModel): Fragment() {
226+
class Editor(file: FileModel) : Fragment() {
230227
override val root = CodeArea()
231228
lateinit var fileObject: FileModel
232229

@@ -237,42 +234,41 @@ class Editor(file: FileModel): Fragment() {
237234
root.appendText(file.text)
238235
val preventTab: InputMap<Event> = InputMap.consume(
239236
anyOf(
240-
// Prevent tab to replace with 4 spaces indention
237+
// Prevent tab to replace with 4 spaces indentation
241238
keyPressed(KeyCode.TAB)
242239
)
243240
)
244241
Nodes.addInputMap(root, preventTab)
245242
val whiteSpace: Pattern = Pattern.compile("^\\s+")
246243
root.addEventHandler(KeyEvent.KEY_PRESSED) { KE ->
247244
// Vim movement
248-
if (KE.getCode() === KeyCode.H) {
245+
if (KE.code === KeyCode.H) {
249246
Platform.runLater {
250-
val caretPosition: Int = root.getCaretPosition()
251-
root.deleteText(caretPosition-1, caretPosition)
247+
val caretPosition: Int = root.caretPosition
248+
root.deleteText(caretPosition - 1, caretPosition)
252249
root.moveTo(caretPosition - 2)
253250
}
254251
}
255-
if (KE.getCode() === KeyCode.L) {
252+
if (KE.code === KeyCode.L) {
256253
Platform.runLater {
257-
val caretPosition: Int = root.getCaretPosition()
258-
root.deleteText(caretPosition-1, caretPosition)
254+
val caretPosition: Int = root.caretPosition
255+
root.deleteText(caretPosition - 1, caretPosition)
259256
root.moveTo(caretPosition)
260257
}
261258
}
262259
// Four spaces for tab
263-
if (KE.getCode() === KeyCode.TAB) {
264-
val caretPosition: Int = root.getCaretPosition()
265-
val currentParagraph: Int = root.getCurrentParagraph()
260+
if (KE.code === KeyCode.TAB) {
261+
val caretPosition: Int = root.caretPosition
266262
root.insertText(caretPosition, " ")
267263
}
268-
// Auto-indent after pressing enter from last line
269-
if (KE.getCode() === KeyCode.ENTER) {
270-
val caretPosition: Int = root.getCaretPosition()
271-
val currentParagraph: Int = root.getCurrentParagraph()
272-
val m0: Matcher = whiteSpace.matcher(root.getParagraph(currentParagraph - 1).getSegments().get(0))
264+
// Auto-indent after pressing enter from the last line
265+
if (KE.code === KeyCode.ENTER) {
266+
val caretPosition: Int = root.caretPosition
267+
val currentParagraph: Int = root.currentParagraph
268+
val m0: Matcher = whiteSpace.matcher(root.getParagraph(currentParagraph - 1).getSegments()[0])
273269
if (m0.find()) Platform.runLater { root.insertText(caretPosition, m0.group()) }
274270
}
275-
//TODO: Implement backpaces that removes 4 space intendations
271+
//TODO: Implement backspaces that remove 4 space indentations
276272
}
277273
}
278274
}
@@ -285,6 +281,7 @@ class MainController : Controller() {
285281
fun getCurrentBuffer(): Fragment {
286282
return buffers[currentBufferIndexProperty.get()]
287283
}
284+
288285
fun openCurrentBufferIndexBuffer() {
289286
find(MainView::class).switchCenterToBufferView(getCurrentBuffer())
290287
}
@@ -300,5 +297,4 @@ class MainController : Controller() {
300297
fun saveTextToFile(text: String, file: Path) {
301298
Files.writeString(file, text)
302299
}
303-
304-
}
300+
}

src/main/kotlin/org/idaesbasic/buffer/NewBufferView.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ import tornadofx.*
88
import java.nio.file.Files
99
import java.nio.file.Paths
1010

11-
class NewBufferView : Fragment () {
12-
val loadDirectory = SimpleStringProperty()
13-
val loadFile = SimpleStringProperty()
14-
val controller = find(NewBufferController::class)
11+
class NewBufferView : Fragment() {
12+
private val loadDirectory = SimpleStringProperty()
13+
private val loadFile = SimpleStringProperty()
14+
private val controller: NewBufferController by inject()
1515

1616
override val root = squeezebox {
1717
fold("New file", expanded = true) {
1818
val newFileName = SimpleStringProperty()
1919
form {
20-
fieldset ("Basic file configuration"){
20+
fieldset("Basic file configuration") {
2121
field("Filename") {
2222
textfield(newFileName)
2323
}
2424
field("Language") {
2525
label("Plain text")
2626
}
2727
}
28-
fieldset ("Advanced file configuration") {
28+
fieldset("Advanced file configuration") {
2929

3030
}
3131
button("Create") {
@@ -40,29 +40,29 @@ class NewBufferView : Fragment () {
4040
}
4141
fold("Load file", expanded = true) {
4242
form {
43-
fieldset ("Location") {
44-
field ("Directory") {
43+
fieldset("Location") {
44+
field("Directory") {
4545
hbox {
46-
textfield (loadDirectory)
46+
textfield(loadDirectory)
4747
}
4848
}
49-
field ("File name") {
49+
field("File name") {
5050
hbox {
51-
textfield (loadFile)
51+
textfield(loadFile)
5252
}
5353
}
54-
button ("Auto pick") {
54+
button("Auto pick") {
5555
action {
56-
val extentions = arrayOf(
56+
val extensions = arrayOf(
5757
FileChooser.ExtensionFilter("All", "*"),
5858
FileChooser.ExtensionFilter("Plain text", "*.txt"),
5959
FileChooser.ExtensionFilter("Java class", "*.java"),
6060
FileChooser.ExtensionFilter("Python", "*.py"),
61-
FileChooser.ExtensionFilter("Kotlin class", "*.kt"),
61+
FileChooser.ExtensionFilter("Kotlin class", "*.kt")
6262
)
6363
val fileArray = chooseFile(
6464
"Save file",
65-
extentions,
65+
extensions,
6666
null,
6767
null,
6868
FileChooserMode.Single
@@ -76,20 +76,20 @@ class NewBufferView : Fragment () {
7676
}
7777
button("Load file") {
7878
action {
79-
controller.loadFileInEditor(loadDirectory.value + loadFile.value)
79+
controller.loadFileInEditor("${loadDirectory.value}${loadFile.value}")
8080
}
8181
}
8282
}
8383
}
8484
}
8585
}
8686

87-
class NewBufferController : Controller () {
87+
class NewBufferController : Controller() {
8888
fun loadFileInEditor(location: String) {
8989
val mainView = find(MainView::class)
9090
val openedFile = Paths.get(location)
9191
val file = FileModel(openedFile.fileName.toString(), openedFile, Files.readString(openedFile))
9292
mainView.newEditor(mainView.controller.currentBufferIndexProperty.get(), file)
9393
mainView.controller.openCurrentBufferIndexBuffer()
9494
}
95-
}
95+
}

src/main/kotlin/org/idaesbasic/buffer/run/ExecutationSetupView.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.idaesbasic.buffer.run
2+
3+
import javafx.beans.property.SimpleStringProperty
4+
import tornadofx.*
5+
6+
class ExecutionSetupView : View() {
7+
private val controller: RunConfigController by inject()
8+
9+
private val configName = SimpleStringProperty()
10+
private val configCommand = SimpleStringProperty()
11+
12+
override val root = squeezebox {
13+
fold("Existing configs") {
14+
listview(controller.configs) {
15+
isEditable = true
16+
cellFragment(RunConfigFragment::class)
17+
}
18+
}
19+
fold("New config") {
20+
form {
21+
fieldset("Information") {
22+
field("Name of config") {
23+
textfield(configName)
24+
}
25+
field("Command") {
26+
textfield(configCommand)
27+
}
28+
}
29+
button("Create config") {
30+
action {
31+
controller.addConfig(RunConfigModel(configName.value, configCommand.value))
32+
configName.set("")
33+
configCommand.set("")
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)