Skip to content

Commit 82c66dc

Browse files
committed
Refactor FormatterContent.kt to streamline input handling and improve output generation logic
1 parent 530f694 commit 82c66dc

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

  • src/main/kotlin/com/github/cnrture/quickprojectwizard/toolwindow/manager/formatter

src/main/kotlin/com/github/cnrture/quickprojectwizard/toolwindow/manager/formatter/FormatterContent.kt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ fun FormatterContent() {
4848
var selectedFormat by remember { mutableStateOf(settings.getFormatterSelectedFormat()) }
4949
var inputText by remember {
5050
mutableStateOf(
51-
settings.getFormatterInputText().ifEmpty {
51+
if (settings.getFormatterInputText().isNotEmpty()) {
52+
settings.getFormatterInputText()
53+
} else {
5254
if (selectedFormat == "JSON") getSampleJson() else getSampleXml()
5355
}
5456
)
@@ -62,7 +64,7 @@ fun FormatterContent() {
6264
settings.saveFormatterState(selectedFormat, inputText, errorMessage)
6365
}
6466

65-
LaunchedEffect(inputText, selectedFormat) {
67+
LaunchedEffect(inputText) {
6668
if (inputText.isNotEmpty()) {
6769
val result = if (selectedFormat == "JSON") {
6870
formatJson(inputText)
@@ -71,11 +73,7 @@ fun FormatterContent() {
7173
}
7274

7375
if (result.second) {
74-
val formattedInput = result.first
75-
if (formattedInput != inputText) {
76-
inputText = formattedInput
77-
}
78-
outputText = formattedInput
76+
outputText = result.first
7977
} else {
8078
outputText = ""
8179
}
@@ -85,15 +83,24 @@ fun FormatterContent() {
8583
}
8684
}
8785

86+
// Handle format changes to regenerate output
8887
LaunchedEffect(selectedFormat) {
89-
inputText = if (selectedFormat == "JSON") {
90-
getSampleJson()
91-
} else {
92-
getSampleXml()
88+
if (inputText.isNotEmpty()) {
89+
val result = if (selectedFormat == "JSON") {
90+
formatJson(inputText)
91+
} else {
92+
formatXml(inputText)
93+
}
94+
95+
if (result.second) {
96+
outputText = result.first
97+
} else {
98+
outputText = ""
99+
}
100+
101+
isValidInput = result.second
102+
errorMessage = result.third
93103
}
94-
outputText = ""
95-
errorMessage = ""
96-
isValidInput = true
97104
}
98105

99106
Column(
@@ -514,8 +521,11 @@ private fun SyntaxHighlightedText(
514521
trimmedText.startsWith("\"") -> QPWTheme.colors.green
515522
trimmedText.matches(Regex("\\d+")) || trimmedText.matches(Regex("\\d+\\.\\d+")) -> QPWTheme.colors.red
516523
trimmedText in listOf("true", "false") -> QPWTheme.colors.red
524+
517525
trimmedText == "null" -> QPWTheme.colors.lightGray
526+
518527
trimmedText.contains("{") || trimmedText.contains("}") || trimmedText.contains("[") || trimmedText.contains("]") -> QPWTheme.colors.white
528+
519529
else -> QPWTheme.colors.white
520530
}
521531

0 commit comments

Comments
 (0)