From 256192111877482a2e2ceeda806064920008b59e Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sun, 12 May 2024 12:52:59 +0300 Subject: Version 2.1.0 Fixed a bunch of issues with the new changes Trident settings are now under "tools" section Updated README.md Renamed some UI elements --- .../me/kyren223/trident/utils/TridentUtils.kt | 103 ++++++++++++++------- 1 file changed, 68 insertions(+), 35 deletions(-) (limited to 'src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt') diff --git a/src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt b/src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt index b2302d7..f00e568 100644 --- a/src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt +++ b/src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt @@ -5,6 +5,8 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.project.guessProjectDir import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VirtualFile +import me.kyren223.trident.data.AutomaticMapping +import me.kyren223.trident.data.AutomaticReplacing import me.kyren223.trident.data.Settings object TridentList { @@ -17,42 +19,72 @@ object TridentList { return expandedPath.isNotBlank() && LocalFileSystem.getInstance().findFileByPath(expandedPath) != null } - private fun getShortPath(path: String): String? { - // For path `/path/to/file.ext` the result should be `$file` - // Unless for dot files, in which case it should be `$ext` - // Examples: - // - `/path/to/TridentUtils.kt` -> `$TridentUtils` - // - `/path/to/someBinary` -> `$someBinary` - // - `/path/to/.ideavimrc` -> `$ideavimrc` + private fun getModifiedPath(project: Project, path: String): String { + val automaticMapping = Settings.state.automaticMapping + val automaticReplacing = Settings.state.automaticReplacing + val mappings = TridentMappings.get(project) + + val hasReplacement = automaticReplacing != AutomaticReplacing.Disabled + && mappings.containsValue(path) + + if (hasReplacement) { + return mappings.filterValues { it == path }.keys.firstOrNull()!! + } + + if (automaticMapping == AutomaticMapping.Disabled) { + return if (automaticReplacing == AutomaticReplacing.Smart) { + return smartReplacement(path, mappings) + } else path + } + return path.split("/") .lastOrNull() ?.let { - it.split(".") - .let { parts -> - when (parts.size) { - 0 -> it - 1, 2 -> parts.firstOrNull() - else -> return null - } - } - } - ?.let { "\$${it}" } + if (automaticMapping == AutomaticMapping.Filename) { + return@let it + } + val dotIndex = it.lastIndexOf(".") + val filename = if (dotIndex == -1) it else it.substring(0, dotIndex).ifBlank { null } + val extension = if (dotIndex == -1) null else it.substring(dotIndex + 1) + return@let (filename ?: extension)!! + }?.let { + val key = "\$$it" + TridentMappings.append(project, key, path) + return@let key + } ?: path } - + + private fun smartReplacement(path: String, mappings: Map): String { + // This is technically redundant, + // The smart path is sorted based on length, so the longest path will be the first + // And the longest path will be the first path that exactly matches + val exactPath = mappings.filter { it.value == path }.keys.firstOrNull() + exactPath?.let { return it } + + var smartPath = path + for ((key, value) in mappings + .toList() + .sortedByDescending { it.second.length } + .associate { it.first to it.second }) { + smartPath = smartPath.replace(value, key) + } + return smartPath + } + fun append(project: Project, file: VirtualFile) { + println("TridentList.append - file: ${file.path}") val files = get(project).toMutableList() - - val automaticMapping = Settings.state.automaticMapping - val path = if (automaticMapping) { - val shortPath = getShortPath(file.path) ?: file.path - if (TridentMappings.exists(project, shortPath)) file.path else { - TridentMappings.append(project, shortPath, file.path) - shortPath - } - } else file.path - + val path = getModifiedPath(project, file.path) + println("TridentList.append - path: $path") files.add(path) + println("TridentList.append - add: " + files.joinToString(", ")) set(project, files) + println("TridentList.append - set") + + println("TridentList.append - TridentList: " + get(project).joinToString(", ")) + println("TridentList.append - TridentMappings: " + TridentMappings.get(project) + .map { "${it.key}=${it.value}" } + .joinToString(", ")) } fun set(project: Project, files: List) { @@ -92,18 +124,23 @@ object TridentMappings { private fun isValidEntry(key: String, value: String): Boolean { // Requirements: // - Key and value must not be blank - // - key must only contain alphanumeric characters and start with $ + // - key must start with $ + // - key must only contain alphanumeric characters, underscores, hyphens, and dots // - value must not contain = return key.isNotBlank() && value.isNotBlank() && - key.matches(Regex("""^\$[a-zA-Z0-9]+$""")) && + key.matches(Regex("""^\$[a-zA-Z0-9_.\-]+$""")) && value.matches(Regex("""^[^=]+$""")) } fun append(project: Project, key: String, value: String, overwrite: Boolean = false) { + println("TridentMappings.append - key: $key, value: $value") val map = get(project).toMutableMap() - if (map.contains(key) && !overwrite) return + if (map.containsKey(key) && !overwrite) return + println("TridentMappings.append - map: $map") map[key] = value + println("TridentMappings.append - map after: $map") set(project, map) + println("TridentMappings.append - set: ${get(project).map { "${it.key}=${it.value}" }.joinToString(", ")}") } fun set(project: Project, mappings: Map) { @@ -149,8 +186,4 @@ object TridentMappings { return expandedPath } - - fun exists(project: Project, key: String): Boolean { - return get(project).containsKey(key) - } } \ No newline at end of file -- cgit v1.3.1