Skip to content

Commit

Permalink
Refactor code and handle parsing exceptions
Browse files Browse the repository at this point in the history
A refactor that includes an exception handling mechanism has been implemented in the CodeLang and Main files. The XML method in CodeLang has been simplified, and the allowed languages in the dropdown menu now load from an array in the Main file. Furthermore, an additional layer of safety has been added to code parser via a try-catch providing default behaviour in case any exception occurs.
  • Loading branch information
jacobrein committed Apr 25, 2024
1 parent bf391e9 commit ff3837f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
65 changes: 32 additions & 33 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ fun FrameWindowScope.App() {
when (language) {
CodeLang.Kotlin -> chosenVectorFile?.fileSpec?.toString()
CodeLang.Swift -> chosenVectorFile?.swiftFileSpec?.toString()
CodeLang.XML -> chosenVectorFile?.file?.readText()
else -> null
else -> chosenVectorFile?.file?.readText()
} ?: ""
}
}
Expand Down Expand Up @@ -310,34 +309,18 @@ fun FrameWindowScope.App() {
showDropDown,
onDismissRequest = { showDropDown = false }
) {
DropdownMenuItem(
text = { Text("Kotlin") },
onClick = {
language = CodeLang.Kotlin
showDropDown = false
}
)
DropdownMenuItem(
text = { Text("Swift") },
onClick = {
language = CodeLang.Swift
showDropDown = false
}
)
DropdownMenuItem(
text = { Text("Original") },
onClick = {
language = CodeLang.XML
showDropDown = false
}
)
allowedLanguages.forEach {
DropdownMenuItem(
text = { Text(it.name) },
onClick = {
language = it
showDropDown = false
}
)
}
}
OutlinedTextField(
when (language) {
CodeLang.Kotlin -> "Kotlin"
CodeLang.Swift -> "Swift"
else -> "Original"
},
language.name,
onValueChange = {},
readOnly = true,
singleLine = true,
Expand All @@ -363,16 +346,20 @@ fun FrameWindowScope.App() {
val themeState by remember { mutableStateOf(CodeThemeType.Monokai) }
val theme = remember(themeState) { themeState.theme }
val parsedCode = remember(svgText, language) {
if (language == CodeLang.XML) {
AnnotatedString(svgText)
} else {
runCatching {
parseCodeAsAnnotatedString(
parser = parser,
theme = theme,
lang = language,
code = svgText
)
}
}.fold(
onSuccess = { it },
onFailure = {
it.printStackTrace()
AnnotatedString(svgText)
}
)
}
Text(
parsedCode,
Expand Down Expand Up @@ -404,7 +391,12 @@ fun FrameWindowScope.App() {
)
}

enum class FileDialogMode(internal val id: Int) { Load(FileDialog.LOAD), Save(FileDialog.SAVE) }
enum class FileDialogMode(internal val id: Int) {
Load(FileDialog.LOAD),

@Suppress("unused")
Save(FileDialog.SAVE)
}

@Composable
private fun FileDialog(
Expand All @@ -430,6 +422,13 @@ private fun FileDialog(
dispose = FileDialog::dispose
)

private val allowedLanguages = listOf(
CodeLang.Kotlin,
CodeLang.Swift,
CodeLang.XML,
CodeLang.HTML,
)

fun main() {
Runtime.getRuntime().addShutdownHook(Thread { outputDirectory.deleteRecursively() })
application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum class CodeLang(val langProvider: Prettify.LangProvider?, val value: List<St
SML(null, listOf("ml")),
FSharp(null, listOf("fs")),
JSON(null, listOf("json")),
XML({ LangXML() }, LangXML.fileExtensions),
XML(null, LangXML.fileExtensions),
Proto(null, listOf("proto")),
RegEx(null, listOf("regex")),
Appollo({ LangAppollo() }, LangAppollo.fileExtensions),
Expand Down

0 comments on commit ff3837f

Please sign in to comment.