Skip to content

Commit 2cbceb6

Browse files
authored
Merge pull request #204 from TedZen/3.4.0-bugfix#197
bug fixed #197:the pattern of new kt file name has been checked
2 parents 074e94c + 94b18f2 commit 2cbceb6

File tree

3 files changed

+110
-69
lines changed

3 files changed

+110
-69
lines changed

src/main/kotlin/wu/seal/jsontokotlin/ui/JsonInputDialog.kt

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import java.awt.Dimension
2323
import java.awt.Toolkit
2424
import java.awt.datatransfer.DataFlavor
2525
import java.awt.event.ActionEvent
26-
import java.awt.event.KeyAdapter
27-
import java.awt.event.KeyEvent
2826
import java.net.URL
2927
import javax.swing.*
28+
import javax.swing.text.AttributeSet
3029
import javax.swing.text.JTextComponent
30+
import javax.swing.text.PlainDocument
3131

3232
/**
3333
* Dialog widget relative
@@ -74,71 +74,71 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
7474
myInputValidator.jsonInputEditor = jsonContentEditor
7575

7676
val classNameInputContainer = createLinearLayoutVertical()
77-
.apply {
78-
val classNameTitle = JBLabel("Class Name: ")
79-
classNameTitle.border = JBEmptyBorder(5, 0, 5, 0)
80-
addComponentIntoVerticalBoxAlignmentLeft(classNameTitle)
81-
addComponentIntoVerticalBoxAlignmentLeft(myField)
82-
preferredSize = JBDimension(500, 56)
83-
}
77+
.apply {
78+
val classNameTitle = JBLabel("Class Name:")
79+
classNameTitle.border = JBEmptyBorder(5, 0, 5, 0)
80+
addComponentIntoVerticalBoxAlignmentLeft(classNameTitle)
81+
addComponentIntoVerticalBoxAlignmentLeft(myField)
82+
preferredSize = JBDimension(500, 56)
83+
}
8484

8585

8686
val jsonInputContainer = createLinearLayoutVertical()
87-
.apply {
88-
preferredSize = JBDimension(700, 400)
89-
border = JBEmptyBorder(5, 0, 5, 5)
90-
val jsonTitle = JBLabel("JSON Text:")
91-
jsonTitle.border = JBEmptyBorder(5, 0, 5, 0)
92-
addComponentIntoVerticalBoxAlignmentLeft(jsonTitle)
93-
addComponentIntoVerticalBoxAlignmentLeft(jsonContentEditor.component)
94-
}
87+
.apply {
88+
preferredSize = JBDimension(700, 400)
89+
border = JBEmptyBorder(5, 0, 5, 5)
90+
val jsonTitle = JBLabel("JSON Text:")
91+
jsonTitle.border = JBEmptyBorder(5, 0, 5, 0)
92+
addComponentIntoVerticalBoxAlignmentLeft(jsonTitle)
93+
addComponentIntoVerticalBoxAlignmentLeft(jsonContentEditor.component)
94+
}
9595

9696

9797
val centerContainer = JPanel()
98-
.apply {
99-
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
100-
addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer)
101-
addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer)
102-
}
98+
.apply {
99+
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
100+
addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer)
101+
addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer)
102+
}
103103

104104
val advancedButton = JButton("Advanced")
105-
.apply {
106-
horizontalAlignment = SwingConstants.CENTER
107-
addActionListener(object : AbstractAction() {
108-
override fun actionPerformed(e: ActionEvent) {
109-
AdvancedDialog(false).show()
110-
}
111-
})
112-
}
105+
.apply {
106+
horizontalAlignment = SwingConstants.CENTER
107+
addActionListener(object : AbstractAction() {
108+
override fun actionPerformed(e: ActionEvent) {
109+
AdvancedDialog(false).show()
110+
}
111+
})
112+
}
113113

114114
val formatButton = JButton("Format")
115-
.apply {
116-
horizontalAlignment = SwingConstants.CENTER
117-
addActionListener(object : AbstractAction() {
118-
override fun actionPerformed(p0: ActionEvent?) {
119-
handleFormatJSONString()
120-
}
121-
})
122-
}
115+
.apply {
116+
horizontalAlignment = SwingConstants.CENTER
117+
addActionListener(object : AbstractAction() {
118+
override fun actionPerformed(p0: ActionEvent?) {
119+
handleFormatJSONString()
120+
}
121+
})
122+
}
123123

124124
val settingContainer = JPanel()
125-
.apply {
126-
border = JBEmptyBorder(0, 5, 5, 7)
127-
layout = BoxLayout(this, BoxLayout.LINE_AXIS)
128-
add(advancedButton)
129-
add(Box.createHorizontalGlue())
130-
add(formatButton)
131-
}
125+
.apply {
126+
border = JBEmptyBorder(0, 5, 5, 7)
127+
layout = BoxLayout(this, BoxLayout.LINE_AXIS)
128+
add(advancedButton)
129+
add(Box.createHorizontalGlue())
130+
add(formatButton)
131+
}
132132

133133

134134
return JPanel(BorderLayout())
135-
.apply {
136-
if (myMessage != null) {
137-
add(createTextComponent(), BorderLayout.NORTH)
135+
.apply {
136+
if (myMessage != null) {
137+
add(createTextComponent(), BorderLayout.NORTH)
138+
}
139+
add(centerContainer, BorderLayout.CENTER)
140+
add(settingContainer, BorderLayout.SOUTH)
138141
}
139-
add(centerContainer, BorderLayout.CENTER)
140-
add(settingContainer, BorderLayout.SOUTH)
141-
}
142142
}
143143

144144
private fun createJsonContentEditor(): Editor {
@@ -155,11 +155,11 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
155155
val editor = editorFactory.createEditor(document, null, JsonFileType.INSTANCE, false)
156156

157157
editor.component
158-
.apply {
159-
isEnabled = true
160-
preferredSize = Dimension(640, 480)
161-
autoscrolls = true
162-
}
158+
.apply {
159+
isEnabled = true
160+
preferredSize = Dimension(640, 480)
161+
autoscrolls = true
162+
}
163163

164164

165165
val contentComponent = editor.contentComponent
@@ -176,16 +176,15 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
176176
override fun createTextFieldComponent(): JTextComponent {
177177

178178
return JTextField()
179-
.apply {
180-
preferredSize = JBDimension(400, 40)
181-
addKeyListener(object : KeyAdapter() {
182-
override fun keyTyped(e: KeyEvent) {
183-
if (e.keyChar == '˚') {
184-
e.consume()
179+
.apply {
180+
preferredSize = JBDimension(400, 40)
181+
document = object : PlainDocument() {
182+
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
183+
str ?: return
184+
super.insertString(offs, str.filter { it.isLetterOrDigit() || it in listOf('_', '$') }.take(252), a)
185185
}
186186
}
187-
})
188-
}
187+
}
189188
}
190189

191190
private fun createPasteFromClipboardMenuItem() = JMenuItem("Paste from clipboard").apply {
@@ -228,7 +227,12 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
228227
/**
229228
* get the user input class name
230229
*/
231-
fun getClassName(): String = if (exitCode == 0) this.myField.text.trim() else ""
230+
fun getClassName(): String {
231+
return if (exitCode == 0) {
232+
val name = myField.text.trim()
233+
name.let { if (it.first().isDigit() || it.contains('$')) "`$it`" else it }
234+
} else ""
235+
}
232236

233237
override fun getInputString(): String = if (exitCode == 0) jsonContentEditor.document.text.trim() else ""
234238

@@ -267,7 +271,7 @@ class JsonInputDialog(classsName: String, private val project: Project) : Messag
267271
fun createLinearLayoutVertical(): JPanel {
268272

269273
return JPanel()
270-
.apply {
271-
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
272-
}
274+
.apply {
275+
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
276+
}
273277
}

src/main/kotlin/wu/seal/jsontokotlin/utils/KotlinDataClassFileGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class KotlinDataClassFileGenerator(private val interceptors: List<IKotlinDataCla
231231
}
232232

233233
executeCouldRollBackAction(project) {
234-
val file = psiFileFactory.createFileFromText("$fileName.kt", KotlinFileType(), kotlinFileContent)
234+
val file = psiFileFactory.createFileFromText("${fileName.trim('`')}.kt", KotlinFileType(), kotlinFileContent)
235235
directory.add(file)
236236
}
237237
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package extensions.ted.zeng
2+
3+
import com.winterbe.expekt.should
4+
import org.junit.Test
5+
6+
/**
7+
* Created by ted on 2019-07-26 20:00.
8+
*/
9+
class BugTest197 {
10+
private fun getClassName(name: String): String {
11+
return name.let { if (it.first().isDigit() || it.contains('$')) "`$it`" else it }
12+
}
13+
14+
@Test
15+
fun getClassName() {
16+
getClassName("abcd").should.equal("abcd")
17+
getClassName("abc\$d").should.equal("`abc\$d`")
18+
getClassName("1abcd").should.equal("`1abcd`")
19+
getClassName("abcd1").should.equal("abcd1")
20+
getClassName("abc_d1").should.equal("abc_d1")
21+
}
22+
23+
@Test
24+
fun firstClassNameCharTest() {
25+
val chars = listOf('a', '_', '1', 'I', 'V', 'X', 'L', 'C', 'D', 'M', '@',
26+
'β', '$', '+', ';', '.', '\\',
27+
'/', ' ', '\t', '\n', '#', '',
28+
'<', ',', '>', '-', '*', '&', '|',
29+
':', ';', '"', '\'', '', '', '',
30+
'', 'I', '', 'Д', '', '', 'ú',
31+
'Φ', '', '', '', 'α', 'δ', 'À', 'û')
32+
val (positive, negative) = chars.partition { it.isLetterOrDigit() || it in listOf('_', '$') }
33+
println(positive)
34+
println(negative)
35+
}
36+
37+
}

0 commit comments

Comments
 (0)