Skip to content

Commit

Permalink
Merge pull request #514 from FWDekker/new-uuids
Browse files Browse the repository at this point in the history
New UUIDs
  • Loading branch information
FWDekker authored Jan 3, 2024
2 parents aa0a143 + 0fbe43c commit 48f983e
Show file tree
Hide file tree
Showing 29 changed files with 540 additions and 237 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Changelog
## 3.2.0 -- [Unreleased]
### Added
* Added support for UUID versions 6, 7, and 8. ([#513](https://github.com/FWDekker/intellij-randomness/issues/513))

### Fixed
* Disabled uppercase option for integers when base is 10 or lower.


## 3.1.0 -- 2023-12-08
### Added
* Added ability to generate non-matching strings.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,7 @@ In chronological order of contribution:
[reporting a compatibility issue with the IntelliJ EAP](https://github.com/FWDekker/intellij-randomness/issues/459)!
* Thanks to [Vitaly Provodin](https://github.com/vprovodin) for
[also reporting that compatibility issue](https://github.com/FWDekker/intellij-randomness/issues/460)!
* Thanks to [Juraj Jurčo](https://github.com/JiangHongTiao) for
[suggesting adding support for newer UUID versions](https://github.com/FWDekker/intellij-randomness/issues/513)!

If I should add, remove, or change anything here, just open an issue or email me!
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import io.gitlab.arturbosch.detekt.Detekt
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
Expand Down Expand Up @@ -201,7 +200,7 @@ tasks {
}
}

// TODO: Remove this workaround for https://github.com/Kotlin/dokka/issues/3398 once it's fixed
// TODO[Remove workaround]: After https://github.com/Kotlin/dokka/issues/3398 has been fixed
abstract class DokkaHtmlPost : DefaultTask() {
private val buildDir = project.layout.buildDirectory

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
group=com.fwdekker
# Version number should also be updated in `com.fwdekker.randomness.PersistentSettings.Companion.CURRENT_VERSION`.
version=3.1.0
version=3.2.0

# Compatibility
# * `pluginSinceBuild`:
Expand All @@ -18,7 +18,7 @@ pluginVerifierIdeVersions=IC-2023.1.5, IC-2023.2.5, IC-2023.3, CL-2023.1.5, CL-2
# Targets
# * Java:
# Java version should be the one used by the oldest Randomness-supported version of IntelliJ. See also
# https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html#intellij-platform-based-products-of-recent-ide-versions.
# https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html#platformVersions.
#
# * Kotlin
# * `kotlinVersion` is the same as `kotlinApiVersion`.
Expand Down Expand Up @@ -50,5 +50,5 @@ org.gradle.configuration-cache=true
# Kotlin
kotlin.code.style=official
kotlin.stdlib.default.dependency=false
# TODO: Workaround for https://jb.gg/intellij-platform-kotlin-oom, will not be necessary once user IDEs use Kotlin 1.9.0
# TODO[Remove workaround]: See https://jb.gg/intellij-platform-kotlin-oom; remove when `pluginSinceBuild=2024.2`
kotlin.incremental.useClasspathSnapshot=false
14 changes: 6 additions & 8 deletions src/main/kotlin/com/fwdekker/randomness/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,16 @@ class PersistentSettings : PersistentStateComponent<Element> {
* Silently upgrades the format of the settings contained in [element] to the format of the latest version.
*/
private fun upgrade(element: Element): Element {
val elementVersion = element.getAttributeValueByName("version")?.let { Version.parse(it) }
val elementVersion = element.getPropertyValue("version")?.let { Version.parse(it) }

when {
elementVersion == null -> Unit

// Placeholder to show how an upgrade might work. Remove this once an actual upgrade has been added.
elementVersion < Version.parse("0.0.0-placeholder") ->
element.getContentByPath("templateList", null, "templates", null)?.getElements()
?.forEachIndexed { idx, template -> template.setAttributeValueByName("name", "Template$idx") }
elementVersion < Version.parse("3.0.0") -> error("Unsupported Randomness config version $elementVersion.")
elementVersion < Version.parse("3.2.0") ->
element.getSchemes().filter { it.name == "UuidScheme" }.forEach { it.renameProperty("type", "version") }
}

element.setAttributeValueByName("version", CURRENT_VERSION)
element.setPropertyValue("version", CURRENT_VERSION)
return element
}

Expand All @@ -126,6 +124,6 @@ class PersistentSettings : PersistentStateComponent<Element> {
/**
* The currently-running version of Randomness.
*/
const val CURRENT_VERSION: String = "3.1.0" // Synchronize this with the version in `gradle.properties`
const val CURRENT_VERSION: String = "3.2.0" // Synchronize this with the version in `gradle.properties`
}
}
93 changes: 67 additions & 26 deletions src/main/kotlin/com/fwdekker/randomness/XmlHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,87 @@ import org.jdom.Element


/**
* Returns a list of all [Element]s contained in this [Element].
* Adds a property with given [name] and [value].
*/
fun Element.getElements(): List<Element> =
content().toList().filterIsInstance<Element>()
fun Element.addProperty(name: String, value: String? = null) {
Element("option")
.also {
it.setAttribute("name", name)
if (value != null) it.setAttribute("value", value)
}
.also(::addContent)
}

/**
* Returns the single child with attribute `name="[name]"`, or `null` if there is not exactly one such child.
*/
fun Element.getProperty(name: String): Element? =
getMultiProperty(name).singleOrNull()

/**
* Returns all children with attribute `name="[name]"`.
*/
fun Element.getMultiProperty(name: String): List<Element> =
children.filter { it.getAttribute("name")?.value == name }

/**
* Traverses a path of [Element]s based on their [names] by monadically either calling [getProperty] (if the name is not
* `null`) or taking the single child (if the name is `null`).
*/
fun Element.getPropertyByPath(vararg names: String?): Element? =
names.fold(this as? Element) { acc, name ->
if (name == null) acc?.children?.singleOrNull()
else acc?.getProperty(name)
}

/**
* Returns the [Element] contained in this [Element] that has attribute `name="[name]"`, or `null` if no single such
* [Element] exists.
* Returns the value of the `value` attribute of property [name], or `null` if the property does not exist or if the
* property has no value.
*/
fun Element.getContentByName(name: String): Element? =
getContent<Element> { (it as Element).getAttribute("name")?.value == name }.singleOrNull()
fun Element.getPropertyValue(name: String): String? =
getProperty(name)?.getAttribute("value")?.value

/**
* Returns the value of the `value` attribute of the single [Element] contained in this [Element] that has attribute
* `name="[name]"`, or `null` if no single such [Element] exists.
* Sets the value of the `value` attribute of property [name], or adds the property if it does not exist.
*/
fun Element.getAttributeValueByName(name: String): String? =
getContentByName(name)?.getAttribute("value")?.value
fun Element.setPropertyValue(name: String, value: String) {
getMultiProperty(name)
.also {
if (it.isEmpty()) addProperty(name, value)
else if (it.size == 1) it[0].setAttribute("value", value)
}
}

/**
* Sets the value of the `value` attribute of the single [Element] contained in this [Element] that has attribute
* `name="[name]"` to [value], or does nothing if no single such [Element] exists.
* Renames the property from [oldName] to [newName].
*
* Requires that there is not already a property with [newName]. If there is no property with [oldName], nothing
* happens. If there are multiple properties with [oldName], they are all renamed to [newName].
*/
fun Element.setAttributeValueByName(name: String, value: String) {
getContentByName(name)?.setAttribute("value", value)
fun Element.renameProperty(oldName: String, newName: String) {
require(getMultiProperty(newName).isEmpty()) { "Attribute '$newName' is already in use." }

getMultiProperty(oldName).forEach { it.setAttribute("name", newName) }
}


/**
* Returns the single [Element] that is contained in this [Element], or `null` if this [Element] does not contain
* exactly one [Element].
* Assuming this is the [Element] representation of a [Settings] instance, returns the [Element] of the contained
* [com.fwdekker.randomness.template.TemplateList], or `null` if no such [Element] could be found.
*/
fun Element.getSingleContent(): Element? =
content.singleOrNull() as? Element
fun Element.getTemplateList(): Element? =
getPropertyByPath("templateList", null)

/**
* Traverses a path of [Element]s based on their [names] by monadically calling either [getContentByName] (if the name
* is not `null`) or [getSingleContent] (if the name is `null`).
* Assuming this is the [Element] representation of a [Settings] instance, returns the list of [Element]s of the
* contained [com.fwdekker.randomness.template.Template]s.
*/
fun Element.getContentByPath(vararg names: String?): Element? =
names.fold(this as? Element) { acc, name ->
if (name == null) acc?.getSingleContent()
else acc?.getContentByName(name)
}
fun Element.getTemplates(): List<Element> =
getPropertyByPath("templateList", null, "templates", null)?.children ?: emptyList()

/**
* Assuming this is the [Element] representation of a [Settings] instance, returns the list of [Element]s of the
* contained [com.fwdekker.randomness.Scheme]s.
*/
fun Element.getSchemes(): List<Element> =
getTemplates().mapNotNull { it.getProperty("schemes") }.flatMap { it.children }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.fwdekker.randomness.ui.disableMnemonic
import com.fwdekker.randomness.ui.isEditable
import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withName
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.panel
Expand Down Expand Up @@ -44,7 +43,7 @@ class AffixDecoratorEditor(
.bindSelected(scheme::enabled)
.also { enabledCheckBox = it }

cell(ComboBox(presets.toTypedArray()))
comboBox(presets)
.enabledIf(enabledIf?.and(enabledCheckBox.selected) ?: enabledCheckBox.selected)
.isEditable(true)
.withName(camelConcat(namePrefix, "affixDescriptor"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ data class ArrayDecorator(
/**
* The preset values for the [separator] field.
*/
val PRESET_SEPARATORS = arrayOf(", ", "; ", "\\n")
val PRESET_SEPARATORS = listOf(", ", "; ", "\\n")

/**
* The default value of the [separator] field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.fwdekker.randomness.ui.isEditable
import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withFixedWidth
import com.fwdekker.randomness.ui.withName
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.BottomGap
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.bindSelected
Expand Down Expand Up @@ -86,7 +85,7 @@ class ArrayDecoratorEditor(
.bindSelected(scheme::separatorEnabled)
.also { separatorEnabledCheckBox = it.component }

cell(ComboBox(PRESET_SEPARATORS))
comboBox(PRESET_SEPARATORS)
.enabledIf(isEnabled.and(separatorEnabledCheckBox.selected))
.isEditable(true)
.withName("arraySeparator")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ data class DecimalScheme(
/**
* The preset values for the [decimalSeparator] field.
*/
val PRESET_DECIMAL_SEPARATORS = arrayOf(",", ".")
val PRESET_DECIMAL_SEPARATORS = listOf(",", ".")

/**
* The default value of the [decimalSeparator] field.
Expand All @@ -144,7 +144,7 @@ data class DecimalScheme(
/**
* The preset values for the [groupingSeparator] field.
*/
val PRESET_GROUPING_SEPARATORS = arrayOf(".", ",", "_")
val PRESET_GROUPING_SEPARATORS = listOf(".", ",", "_")

/**
* The default value of the [groupingSeparator] field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withFilter
import com.fwdekker.randomness.ui.withFixedWidth
import com.fwdekker.randomness.ui.withName
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.bindSelected
Expand Down Expand Up @@ -79,7 +78,7 @@ class DecimalSchemeEditor(scheme: DecimalScheme = DecimalScheme()) : SchemeEdito
}

row(Bundle("decimal.ui.format.decimal_separator_option")) {
cell(ComboBox(PRESET_DECIMAL_SEPARATORS))
comboBox(PRESET_DECIMAL_SEPARATORS)
.isEditable(true)
.withFilter(MinMaxLengthDocumentFilter(1, 1))
.withName("decimalSeparator")
Expand All @@ -95,7 +94,7 @@ class DecimalSchemeEditor(scheme: DecimalScheme = DecimalScheme()) : SchemeEdito
.bindSelected(scheme::groupingSeparatorEnabled)
.also { groupingSeparatorEnabled = it }

cell(ComboBox(PRESET_GROUPING_SEPARATORS))
comboBox(PRESET_GROUPING_SEPARATORS)
.enabledIf(groupingSeparatorEnabled.selected)
.isEditable(true)
.withFilter(MinMaxLengthDocumentFilter(1, 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ data class IntegerScheme(
/**
* The preset values for the [groupingSeparator] descriptor.
*/
val PRESET_GROUPING_SEPARATORS = arrayOf(".", ",", "_")
val PRESET_GROUPING_SEPARATORS = listOf(".", ",", "_")

/**
* The default value of the [groupingSeparator] field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withFilter
import com.fwdekker.randomness.ui.withFixedWidth
import com.fwdekker.randomness.ui.withName
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.panel
Expand Down Expand Up @@ -77,6 +76,7 @@ class IntegerSchemeEditor(scheme: IntegerScheme = IntegerScheme()) : SchemeEdito

row {
checkBox(Bundle("integer.ui.format.uppercase_option"))
.enabledIf(base.hasValue { it > DECIMAL_BASE })
.loadMnemonic()
.withName("isUppercase")
.bindSelected(scheme::isUppercase)
Expand All @@ -89,7 +89,7 @@ class IntegerSchemeEditor(scheme: IntegerScheme = IntegerScheme()) : SchemeEdito
.bindSelected(scheme::groupingSeparatorEnabled)
.also { groupingSeparatorEnabled = it.component }

cell(ComboBox(PRESET_GROUPING_SEPARATORS))
comboBox(PRESET_GROUPING_SEPARATORS)
.enabledIf(base.hasValue { it == DECIMAL_BASE }.and(groupingSeparatorEnabled.selected))
.withName("groupingSeparator")
.isEditable(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ data class StringScheme(
/**
* The preset values for the [capitalization] field.
*/
val PRESET_CAPITALIZATION = arrayOf(
val PRESET_CAPITALIZATION = listOf(
CapitalizationMode.RETAIN,
CapitalizationMode.LOWER,
CapitalizationMode.UPPER,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.fwdekker.randomness.string

import com.fwdekker.randomness.Bundle
import com.fwdekker.randomness.CapitalizationMode
import com.fwdekker.randomness.SchemeEditor
import com.fwdekker.randomness.array.ArrayDecoratorEditor
import com.fwdekker.randomness.string.StringScheme.Companion.PRESET_CAPITALIZATION
import com.fwdekker.randomness.ui.UIConstants
import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withFixedWidth
import com.fwdekker.randomness.ui.withName
import com.fwdekker.randomness.ui.withSimpleRenderer
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.BottomGap
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.bindText
import com.intellij.ui.dsl.builder.listCellRenderer
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.builder.toNullableProperty
import com.intellij.ui.layout.selected
Expand Down Expand Up @@ -56,8 +54,7 @@ class StringSchemeEditor(scheme: StringScheme = StringScheme()) : SchemeEditor<S
}.bottomGap(BottomGap.SMALL)

row(Bundle("string.ui.value.capitalization_option")) {
cell(ComboBox(PRESET_CAPITALIZATION))
.withSimpleRenderer(CapitalizationMode::toLocalizedString)
comboBox(PRESET_CAPITALIZATION, listCellRenderer { it.toLocalizedString() })
.withName("capitalization")
.bindItem(scheme::capitalization.toNullableProperty())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ data class TemplateReference(
/**
* The preset values for the [capitalization] field.
*/
val PRESET_CAPITALIZATION = arrayOf(
val PRESET_CAPITALIZATION = listOf(
CapitalizationMode.RETAIN,
CapitalizationMode.LOWER,
CapitalizationMode.UPPER,
Expand Down
Loading

0 comments on commit 48f983e

Please sign in to comment.