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

3.3.0 fix#176 #179

Merged
merged 2 commits into from
May 23, 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
7 changes: 0 additions & 7 deletions src/main/kotlin/wu/seal/jsontokotlin/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ object ConfigManager : IConfigManager {
TestConfig.enableMapType = value
} else PropertiesComponent.getInstance().setValue(ENABLE_MAP_TYP_KEY, value, false)

var enableAutoReformat: Boolean
get() = (TestConfig.isTestModel && TestConfig.enableAutoReformat)
|| PropertiesComponent.getInstance().getBoolean(ENABLE_AUTO_REFORMAT, true)
set(value) = if (TestConfig.isTestModel) {
TestConfig.enableAutoReformat = value
} else PropertiesComponent.getInstance().setValue(ENABLE_AUTO_REFORMAT, value, true)

var enableMinimalAnnotation: Boolean
get() = if (TestConfig.isTestModel) {
TestConfig.enableMinimalAnnotation
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/wu/seal/jsontokotlin/feedback/Datas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ data class ConfigInfo(
val customClassAnnotationFormatString: String = ConfigManager.customClassAnnotationFormatString,
val customPropertyAnnotationFormatString: String = ConfigManager.customPropertyAnnotationFormatString,
val enableMapType: Boolean = ConfigManager.enableMapType,
val enableAutoReformat: Boolean = ConfigManager.enableAutoReformat,
val enableMinimalAnnotation: Boolean = ConfigManager.enableMinimalAnnotation,
val parenClassTemplate: String = ConfigManager.parenClassTemplate,
val keywordPropertyValid: Boolean = ConfigManager.keywordPropertyValid,
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/wu/seal/jsontokotlin/test/TestConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ object TestConfig {
var indent: Int = 4

var enableMapType: Boolean = true
var enableAutoReformat: Boolean = true

var enableMinimalAnnotation = false

Expand Down Expand Up @@ -129,7 +128,7 @@ object TestConfig {
var indent: Int = 4

var enableMapType: Boolean = true
var enableAutoReformat: Boolean = true

var enableMinimalAnnotation = false

var parenClassTemplate = ""
Expand Down
6 changes: 0 additions & 6 deletions src/main/kotlin/wu/seal/jsontokotlin/ui/AdvancedOtherTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@ class AdvancedOtherTab(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPan
ConfigManager.enableMinimalAnnotation = enableMinimalAnnotation.isSelected
}


val enableAutoReformat =
JBCheckBox("Auto reformatting generated code according to code style.")
val enableAutoReformatNoteLable = JBLabel("(Note that the Indent option bellow would be ignored.)")
enableAutoReformatNoteLable.border = JBEmptyBorder(0, 25, 0, 0)
enableAutoReformat.isSelected = ConfigManager.enableAutoReformat
enableAutoReformat.addActionListener { ConfigManager.enableAutoReformat = enableAutoReformat.isSelected }


val indentJPanel = JPanel()
Expand Down Expand Up @@ -163,7 +158,6 @@ class AdvancedOtherTab(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPan

boxPanel.add(Box.createVerticalStrut(JBUI.scale(20)))

boxPanel.addComponentIntoVerticalBoxAlignmentLeft(enableAutoReformat)
boxPanel.addComponentIntoVerticalBoxAlignmentLeft(enableAutoReformatNoteLable)

boxPanel.add(Box.createVerticalStrut(JBUI.scale(20)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package wu.seal.jsontokotlin.utils

import com.intellij.codeInsight.actions.AbstractLayoutCodeProcessor
import com.intellij.codeInsight.actions.OptimizeImportsProcessor
import com.intellij.codeInsight.actions.RearrangeCodeProcessor
import com.intellij.codeInsight.actions.ReformatCodeProcessor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import wu.seal.jsontokotlin.ConfigManager
import wu.seal.jsontokotlin.codeelements.getDefaultValue
Expand Down Expand Up @@ -226,16 +221,7 @@ class KotlinDataClassFileGenerator(private val interceptors: List<IKotlinDataCla

executeCouldRollBackAction(project) {
val file = psiFileFactory.createFileFromText("$fileName.kt", KotlinFileType(), kotlinFileContent)

val fileAdded = directory.add(file)

if (ConfigManager.enableAutoReformat) {
var processor: AbstractLayoutCodeProcessor =
ReformatCodeProcessor(project, fileAdded as PsiFile, null, false)
processor = OptimizeImportsProcessor(processor)
processor = RearrangeCodeProcessor(processor)
processor.run()
}
directory.add(file)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ClassCodeParser(private val classBlockString: String) {
} else {
removeCommentAndTypeProperty.trim().split(" ")[0]
}
if (annotationPre.contains("@")) {
if (annotationPre.startsWith("@")) {
listOf(annotationPre)
} else
listOf("")
Expand All @@ -100,7 +100,7 @@ class ClassCodeParser(private val classBlockString: String) {
val keyword = noAnnotationString.split(" ").first()
keyword
}
stringBeforeLastColonWithoutComment.contains("@") -> {
stringBeforeLastColonWithoutComment.startsWith("@")-> {
val keyword = stringBeforeLastColonWithoutComment.split(" ")[1]
keyword
}
Expand All @@ -123,7 +123,7 @@ class ClassCodeParser(private val classBlockString: String) {
.joinToString(" ")
propertyName
}
stringBeforeLastColonWithoutComment.contains("@") -> {
stringBeforeLastColonWithoutComment.startsWith("@") -> {
val splits = stringBeforeLastColonWithoutComment.split(" ")
val propertyName =
splits.filterIndexed { index, _ -> listOf(0, 1).contains(index).not() }
Expand Down
88 changes: 88 additions & 0 deletions src/test/kotlin/wu/seal/jsontokotlin/regression/Issue176Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package wu.seal.jsontokotlin.regression

import com.winterbe.expekt.should
import org.junit.Before
import org.junit.Test
import wu.seal.jsontokotlin.TargetJsonConverter
import wu.seal.jsontokotlin.interceptor.InterceptorManager
import wu.seal.jsontokotlin.test.TestConfig
import wu.seal.jsontokotlin.utils.classblockparse.NormalClassesCodeParser


class Issue176Test {

val testJson = """data class Test(
val recipeInstructions: List<String>,
val recipeIngredient: List<String>,
val recipeCuisine: String, // italian
val cookingMethod: Any, // null
val cookTime: String, // PT0S
val prepTime: String, // PT0S
val totalTime: Any, // null
val recipeYield: Int, // 4
val nutrition: Nutrition,
val aggregateRating: AggregateRating,
val recipeCategory: String, // dinner, main
val keywords: String, // Mushrooms, sage, butter, spaghetti, simple pasta, Italian flavours, Italian food, Italian cooking, Italian recipes, five ingredient pasta, 5 ingredient pasta, Phoebe Wood, mushroom pasta, cacio e pepe pasta, sage mushrooms, crispy sage, Vegetarian, Easy vegetarian, Vegetarian recipes, Vegetarian dinner, Vegetable mains, Meat free Monday, Meat-free Monday, Meat free mains, portobello mushrooms, roasted mushrooms, Fast food, Quick meals, Easy dinner, Easy recipes, Easy cooking
val name: String, // Cacio e pepe with sage mushrooms
val description: String, // Need a break from meat tonight? This simple vegetarian pasta is a winner. With just five ingredients, it's the perfect mid-week recipe to add to your weekly meal plan, yum!
val datePublished: String, // 2016-06-10
val dateCreated: String, // 2016-06-17
val mainEntityOfPage: Any, // null
val dateModified: String, // 2019-01-23
val author: Author,
val publisher: Publisher,
val image: Image,
val video: Video,
val @context: String, // http://schema.org
val @type: String // Recipe
)"""

private val expected = """data class Test(
val @context: String, // http://schema.org
val @type: String, // Recipe
val aggregateRating: AggregateRating,
val author: Author,
val cookTime: String, // PT0S
val cookingMethod: Any, // null
val dateCreated: String, // 2016-06-17
val dateModified: String, // 2019-01-23
val datePublished: String, // 2016-06-10
val description: String, // Need a break from meat tonight? This simple vegetarian pasta is a winner. With just five ingredients, it's the perfect mid-week recipe to add to your weekly meal plan, yum!
val image: Image,
val keywords: String, // Mushrooms, sage, butter, spaghetti, simple pasta, Italian flavours, Italian food, Italian cooking, Italian recipes, five ingredient pasta, 5 ingredient pasta, Phoebe Wood, mushroom pasta, cacio e pepe pasta, sage mushrooms, crispy sage, Vegetarian, Easy vegetarian, Vegetarian recipes, Vegetarian dinner, Vegetable mains, Meat free Monday, Meat-free Monday, Meat free mains, portobello mushrooms, roasted mushrooms, Fast food, Quick meals, Easy dinner, Easy recipes, Easy cooking
val mainEntityOfPage: Any, // null
val name: String, // Cacio e pepe with sage mushrooms
val nutrition: Nutrition,
val prepTime: String, // PT0S
val publisher: Publisher,
val recipeCategory: String, // dinner, main
val recipeCuisine: String, // italian
val recipeIngredient: List<String>,
val recipeInstructions: List<String>,
val recipeYield: Int, // 4
val totalTime: Any, // null
val video: Video
)"""



/**
* init test environment before test
*/
@Before
fun setUp() {
TestConfig.setToTestInitState()
TestConfig.initWithDefaultValue = false
TestConfig.targetJsonConvertLib = TargetJsonConverter.None
}

/**
* test issue #700 of Github Project issue
*/
@Test
fun testIssue176() {
val generated = NormalClassesCodeParser(testJson).parse()[0].applyInterceptors(InterceptorManager.getEnabledKotlinDataClassInterceptors()).getCode()
generated.trim().should.be.equal(expected)
}
}