Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fixed #197:the pattern of new kt file name has been checked #204

Merged
merged 1 commit into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 72 additions & 68 deletions src/main/kotlin/wu/seal/jsontokotlin/ui/JsonInputDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import java.awt.Dimension
import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.awt.event.ActionEvent
import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
import java.net.URL
import javax.swing.*
import javax.swing.text.AttributeSet
import javax.swing.text.JTextComponent
import javax.swing.text.PlainDocument

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

val classNameInputContainer = createLinearLayoutVertical()
.apply {
val classNameTitle = JBLabel("Class Name: ")
classNameTitle.border = JBEmptyBorder(5, 0, 5, 0)
addComponentIntoVerticalBoxAlignmentLeft(classNameTitle)
addComponentIntoVerticalBoxAlignmentLeft(myField)
preferredSize = JBDimension(500, 56)
}
.apply {
val classNameTitle = JBLabel("Class Name:")
classNameTitle.border = JBEmptyBorder(5, 0, 5, 0)
addComponentIntoVerticalBoxAlignmentLeft(classNameTitle)
addComponentIntoVerticalBoxAlignmentLeft(myField)
preferredSize = JBDimension(500, 56)
}


val jsonInputContainer = createLinearLayoutVertical()
.apply {
preferredSize = JBDimension(700, 400)
border = JBEmptyBorder(5, 0, 5, 5)
val jsonTitle = JBLabel("JSON Text:")
jsonTitle.border = JBEmptyBorder(5, 0, 5, 0)
addComponentIntoVerticalBoxAlignmentLeft(jsonTitle)
addComponentIntoVerticalBoxAlignmentLeft(jsonContentEditor.component)
}
.apply {
preferredSize = JBDimension(700, 400)
border = JBEmptyBorder(5, 0, 5, 5)
val jsonTitle = JBLabel("JSON Text:")
jsonTitle.border = JBEmptyBorder(5, 0, 5, 0)
addComponentIntoVerticalBoxAlignmentLeft(jsonTitle)
addComponentIntoVerticalBoxAlignmentLeft(jsonContentEditor.component)
}


val centerContainer = JPanel()
.apply {
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer)
addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer)
}
.apply {
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer)
addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer)
}

val advancedButton = JButton("Advanced")
.apply {
horizontalAlignment = SwingConstants.CENTER
addActionListener(object : AbstractAction() {
override fun actionPerformed(e: ActionEvent) {
AdvancedDialog(false).show()
}
})
}
.apply {
horizontalAlignment = SwingConstants.CENTER
addActionListener(object : AbstractAction() {
override fun actionPerformed(e: ActionEvent) {
AdvancedDialog(false).show()
}
})
}

val formatButton = JButton("Format")
.apply {
horizontalAlignment = SwingConstants.CENTER
addActionListener(object : AbstractAction() {
override fun actionPerformed(p0: ActionEvent?) {
handleFormatJSONString()
}
})
}
.apply {
horizontalAlignment = SwingConstants.CENTER
addActionListener(object : AbstractAction() {
override fun actionPerformed(p0: ActionEvent?) {
handleFormatJSONString()
}
})
}

val settingContainer = JPanel()
.apply {
border = JBEmptyBorder(0, 5, 5, 7)
layout = BoxLayout(this, BoxLayout.LINE_AXIS)
add(advancedButton)
add(Box.createHorizontalGlue())
add(formatButton)
}
.apply {
border = JBEmptyBorder(0, 5, 5, 7)
layout = BoxLayout(this, BoxLayout.LINE_AXIS)
add(advancedButton)
add(Box.createHorizontalGlue())
add(formatButton)
}


return JPanel(BorderLayout())
.apply {
if (myMessage != null) {
add(createTextComponent(), BorderLayout.NORTH)
.apply {
if (myMessage != null) {
add(createTextComponent(), BorderLayout.NORTH)
}
add(centerContainer, BorderLayout.CENTER)
add(settingContainer, BorderLayout.SOUTH)
}
add(centerContainer, BorderLayout.CENTER)
add(settingContainer, BorderLayout.SOUTH)
}
}

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

editor.component
.apply {
isEnabled = true
preferredSize = Dimension(640, 480)
autoscrolls = true
}
.apply {
isEnabled = true
preferredSize = Dimension(640, 480)
autoscrolls = true
}


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

return JTextField()
.apply {
preferredSize = JBDimension(400, 40)
addKeyListener(object : KeyAdapter() {
override fun keyTyped(e: KeyEvent) {
if (e.keyChar == '˚') {
e.consume()
.apply {
preferredSize = JBDimension(400, 40)
document = object : PlainDocument() {
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
str ?: return
super.insertString(offs, str.filter { it.isLetterOrDigit() || it in listOf('_', '$') }.take(252), a)
}
}
})
}
}
}

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

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

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

return JPanel()
.apply {
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
}
.apply {
layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class KotlinDataClassFileGenerator(private val interceptors: List<IKotlinDataCla
}

executeCouldRollBackAction(project) {
val file = psiFileFactory.createFileFromText("$fileName.kt", KotlinFileType(), kotlinFileContent)
val file = psiFileFactory.createFileFromText("${fileName.trim('`')}.kt", KotlinFileType(), kotlinFileContent)
directory.add(file)
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/kotlin/extensions/ted/zeng/BugTest197.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package extensions.ted.zeng

import com.winterbe.expekt.should
import org.junit.Test

/**
* Created by ted on 2019-07-26 20:00.
*/
class BugTest197 {
private fun getClassName(name: String): String {
return name.let { if (it.first().isDigit() || it.contains('$')) "`$it`" else it }
}

@Test
fun getClassName() {
getClassName("abcd").should.equal("abcd")
getClassName("abc\$d").should.equal("`abc\$d`")
getClassName("1abcd").should.equal("`1abcd`")
getClassName("abcd1").should.equal("abcd1")
getClassName("abc_d1").should.equal("abc_d1")
}

@Test
fun firstClassNameCharTest() {
val chars = listOf('a', '_', '1', 'I', 'V', 'X', 'L', 'C', 'D', 'M', '@',
'β', '$', '+', ';', '.', '\\',
'/', ' ', '\t', '\n', '#', '¥',
'<', ',', '>', '-', '*', '&', '|',
':', ';', '"', '\'', '∑', '☑', '⒈',
'Ⅰ', 'I', '你', 'Д', 'ㅙ', 'す', 'ú',
'Φ', 'Ⅱ', 'Ⅲ', '❶', 'α', 'δ', 'À', 'û')
val (positive, negative) = chars.partition { it.isLetterOrDigit() || it in listOf('_', '$') }
println(positive)
println(negative)
}

}