Skip to content

Commit 859dcc2

Browse files
committed
Refactor for new architecture
1 parent 2ba274e commit 859dcc2

File tree

3 files changed

+19
-108
lines changed

3 files changed

+19
-108
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "com.idealista"
8-
version = "1.0-SNAPSHOT"
8+
version = "1.1"
99

1010
repositories {
1111
mavenCentral()

src/main/kotlin/com/idealista/togglehandler/CreateToggleDialog.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ class CreateToggleDialog(private val project: Project) : DialogWrapper(project)
3030
)
3131

3232
data object ToggleFile
33-
data object ToggleDocFile
3433
data object ServiceExtensionsFile
3534
data object RemoteSettingsDefaultsFile
3635

3736
private val TARGET_FILES = mapOf(
3837
"Toggle.kt" to ToggleFile,
39-
"ToggleDoc.kt" to ToggleDocFile,
4038
"ServiceExtensions.kt" to ServiceExtensionsFile,
4139
"RemoteSettingsDefaults.kt" to RemoteSettingsDefaultsFile
4240
)
@@ -247,8 +245,6 @@ class CreateToggleDialog(private val project: Project) : DialogWrapper(project)
247245
when (fileType) {
248246
ToggleFile -> { fileModifier.addToggleDefinition(file, toggleData) }
249247

250-
ToggleDocFile -> { fileModifier.addToggleDocumentation(file, toggleData) }
251-
252248
ServiceExtensionsFile -> { fileModifier.addServiceExtensions(file, toggleData) }
253249

254250
RemoteSettingsDefaultsFile -> { fileModifier.addRemoteSettingsDefaults(file, toggleData) }

src/main/kotlin/com/idealista/togglehandler/ToggleFileModifier.kt

Lines changed: 18 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -22,127 +22,42 @@ class ToggleFileModifier(private val project: Project) {
2222

2323
val fileText = document.text
2424
val snakeCaseName = convertToSnakeCase(toggleData.name)
25-
val newToggleDefinition =
26-
" data object ${toggleData.name} : Toggle(\"$snakeCaseName\", ${toggleData.isRemotelyConfigurable})"
27-
val lastDataObjectIndex = fileText.lastIndexOf("data object")
28-
29-
if (lastDataObjectIndex == -1) {
30-
Messages.showErrorDialog(
31-
project,
32-
"Could not find data object pattern in Toggle.kt",
33-
"Error"
34-
)
35-
36-
return
37-
}
38-
39-
val endOfLastDataObject = fileText.indexOf('\n', lastDataObjectIndex)
40-
41-
if (endOfLastDataObject == -1) {
42-
Messages.showErrorDialog(
43-
project,
44-
"Could not find insertion point in Toggle.kt",
45-
"Error"
46-
)
47-
48-
return
49-
}
50-
51-
val insertPosition = endOfLastDataObject + 1
52-
53-
WriteCommandAction.runWriteCommandAction(project) {
54-
try {
55-
document.insertString(insertPosition, "$newToggleDefinition\n")
56-
PsiDocumentManager.getInstance(project).commitDocument(document)
57-
} catch (e: Exception) {
58-
Messages.showErrorDialog(
59-
project,
60-
"Error adding toggle: ${e.message}",
61-
"Error"
62-
)
63-
}
64-
}
65-
}
66-
67-
fun addToggleDocumentation(file: PsiFile, toggleData: CreateToggleDialog.Companion.ToggleData) {
68-
val document = PsiDocumentManager.getInstance(project).getDocument(file)
69-
70-
if (document == null) {
71-
Messages.showErrorDialog(
72-
project,
73-
"Could not access document for file: ${file.name}",
74-
"Error"
75-
)
76-
77-
return
78-
}
79-
80-
val fileText = document.text
81-
val lastIdToggleIndex = fileText.lastIndexOf("IdToggle(")
82-
83-
if (lastIdToggleIndex == -1) {
84-
Messages.showErrorDialog(
85-
project,
86-
"Could not find IdToggle pattern in documentation file",
87-
"Error"
88-
)
89-
90-
return
91-
}
92-
93-
var currentIndex = lastIdToggleIndex
94-
var parenthesesCount = 0
95-
var foundOpeningParenthesis = false
96-
97-
while (currentIndex < fileText.length) {
98-
val char = fileText[currentIndex]
99-
when (char) {
100-
'(' -> {
101-
parenthesesCount++
102-
foundOpeningParenthesis = true
103-
}
104-
105-
')' -> {
106-
parenthesesCount--
107-
if (foundOpeningParenthesis && parenthesesCount == 0) {
108-
val nextCommaIndex = fileText.indexOf(',', currentIndex)
109-
if (nextCommaIndex != -1) {
110-
val nextNewlineIndex = fileText.indexOf('\n', nextCommaIndex)
111-
currentIndex = if (nextNewlineIndex != -1) {
112-
nextNewlineIndex + 1
113-
} else {
114-
fileText.length
115-
}
116-
}
117-
break
118-
}
119-
}
120-
}
121-
currentIndex++
122-
}
12325

12426
val description = if (toggleData.jiraTask.isNotEmpty()) {
12527
"${toggleData.jiraTask} ${toggleData.description}"
12628
} else {
127-
toggleData.description.ifEmpty { "DESCRIPTION_UNKNOWN" }
29+
toggleData.description.ifEmpty { "Información desconocida" }
12830
}
12931

130-
val newIdToggle = """, IdToggle(
131-
toggle = Toggle.${toggleData.name},
32+
val newToggleDefinition = """
33+
data object ${toggleData.name} : Toggle(
34+
name = "$snakeCaseName",
35+
isRemoteConfigurable = ${toggleData.isRemotelyConfigurable},
13236
activationDate = "${toggleData.activationDate}",
13337
activationVersion = "${toggleData.activationVersion}",
13438
deprecationDate = "${toggleData.deprecationDate}",
13539
description = "$description"
13640
)"""
13741

42+
val classToggleEndIndex = fileText.lastIndexOf("}")
43+
44+
if (classToggleEndIndex == -1) {
45+
Messages.showErrorDialog(
46+
project,
47+
"Could not find closing brace of Toggle class",
48+
"Error"
49+
)
50+
return
51+
}
52+
13853
WriteCommandAction.runWriteCommandAction(project) {
13954
try {
140-
document.insertString(currentIndex + 1, newIdToggle)
55+
document.insertString(classToggleEndIndex, "$newToggleDefinition\n")
14156
PsiDocumentManager.getInstance(project).commitDocument(document)
14257
} catch (e: Exception) {
14358
Messages.showErrorDialog(
14459
project,
145-
"Error adding toggle documentation: ${e.message}",
60+
"Error adding toggle: ${e.message}",
14661
"Error"
14762
)
14863
}

0 commit comments

Comments
 (0)