Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #271 from koxudaxi/fix_toml_header_problem
Browse files Browse the repository at this point in the history
Fix toml header problem
  • Loading branch information
koxudaxi committed Sep 7, 2021
2 parents 933843a + 2aac05d commit 5544965
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 29 deletions.
4 changes: 4 additions & 0 deletions resources/META-INF/plugin.xml
Expand Up @@ -5,6 +5,10 @@
<vendor email="koaxudai@gmail.com">Koudai Aono @koxudaxi</vendor>
<change-notes><![CDATA[
<h2>version 1.1.5-212</h2>
<p>Bug fixes</p>
<ul>
<li>Fix toml header problem [#271]</li>
</ul>
<p>Features</p>
<ul>
<li>Update versions [#270]</li>
Expand Down
Expand Up @@ -18,7 +18,7 @@ object PoetryExtrasLineMarkerContributor : RunLineMarkerContributor() {
}
val keyValue = element.parent as? TomlKeyValue ?: return null
val header = (keyValue.parent as? TomlTable)?.header ?: return null
if (header.keyText != "tool.poetry.extras") return null
if (header.key?.text != "tool.poetry.extras") return null
if (keyValue.key.text == null) return null
val value = keyValue.value as? TomlArray ?: return null
if (value.elements.isEmpty() || value.elements.any { it !is TomlLiteral || it.textLength < 3}) return null
Expand Down
Expand Up @@ -22,7 +22,7 @@ object PoetryScriptsLineMarkerContributor : RunLineMarkerContributor() {
}
val keyValue = element.parent as? TomlKeyValue ?: return null
val header = (keyValue.parent as? TomlTable)?.header ?: return null
if (header.keyText != "tool.poetry.scripts") return null
if (header.key?.text != "tool.poetry.scripts") return null
if (keyValue.key.text == null) return null
val value = keyValue.value as? TomlLiteral ?: return null
if (value.textLength < 3) return null
Expand Down
2 changes: 1 addition & 1 deletion src/com/koxudaxi/poetry/PoetryVersionInspection.kt
Expand Up @@ -32,7 +32,7 @@ class PoetryVersionInspection : LocalInspectionTool() {
if (file.virtualFile != module.pyProjectToml) return
file.children
.filter { element ->
(element as? TomlTable)?.header?.keyText in listOf("tool.poetry.dependencies", "tool.poetry.dev-dependencies")
(element as? TomlTable)?.header?.key?.text in listOf("tool.poetry.dependencies", "tool.poetry.dev-dependencies")
}.flatMap {
it.children.mapNotNull { line -> line as? TomlKeyValue }
}.forEach { keyValue ->
Expand Down
26 changes: 0 additions & 26 deletions src/com/koxudaxi/poetry/poetry.kt
Expand Up @@ -726,29 +726,3 @@ data class PoetryOutdatedVersion(
@SerializedName("currentVersion") var currentVersion: String,
@SerializedName("latestVersion") var latestVersion: String)


private fun tomlTableHeaderHasKey(): Boolean = TomlTableHeader::class.memberProperties.any { it.name == "key" }
var tomlTableHeaderHasKeyCache: Boolean? = null
val TomlTableHeader.keyText: String? get() = getKeyByTomlTableHeader(this)


private fun getKeyByTomlTableHeader(header: TomlTableHeader): String? {
return try {
when (tomlTableHeaderHasKeyCache) {
true -> header.key?.text
false -> (header::class.java.getMethod("getNames").invoke(header) as? List<*>)
?.filterIsInstance<TomlKey>()?.joinToString(".") { it.text }
else -> {
tomlTableHeaderHasKeyCache = tomlTableHeaderHasKey()
getKeyByTomlTableHeader(header)
}
}
} catch (e: Exception) {
val updatedTomlTableHeaderHasKey = tomlTableHeaderHasKey()
if (updatedTomlTableHeaderHasKey == tomlTableHeaderHasKeyCache) {
throw Exception("unsupported TomlTableHeader type")
}
tomlTableHeaderHasKeyCache = updatedTomlTableHeaderHasKey
getKeyByTomlTableHeader(header)
}
}

0 comments on commit 5544965

Please sign in to comment.