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 #174 from koxudaxi/support_toml_plugin_version_0_2…
Browse files Browse the repository at this point in the history
…_139_3615_203

Support Toml plugin version 0.2.139.3615-203 or earlier version
  • Loading branch information
koxudaxi committed Feb 5, 2021
2 parents 154aa72 + e095597 commit e95221d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
7 changes: 6 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<idea-plugin url="https://github.com/koxudaxi/poetry-pycharm-plugin" require-restart="true">
<id>com.koxudaxi.poetry</id>
<name>Poetry</name>
<version>1.0.4</version>
<version>1.0.5</version>
<vendor email="koaxudai@gmail.com">Koudai Aono @koxudaxi</vendor>
<change-notes><![CDATA[
<h2>version 1.0.5</h2>
<p>Bug fixes</p>
<ul>
<li>Support Toml plugin version 0.2.139.3615-203 or earlier version [#174]</li>
</ul>
<h2>version 1.0.4</h2>
<p>Bug fixes</p>
<ul>
Expand Down
4 changes: 2 additions & 2 deletions src/com/koxudaxi/poetry/PoetryExtrasLineMarkerContributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ object PoetryExtrasLineMarkerContributor : RunLineMarkerContributor() {
return null //Toml plugin is installed. But, PyCharm has not restarted yet.
}
val keyValue = element.parent as? TomlKeyValue ?: return null
val key = (keyValue.parent as? TomlTable)?.header?.key ?: return null
if (key.text != "tool.poetry.extras") return null
val header = (keyValue.parent as? TomlTable)?.header ?: return null
if (header.keyText != "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
4 changes: 2 additions & 2 deletions src/com/koxudaxi/poetry/PoetryScriptsLineMarkerContributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ object PoetryScriptsLineMarkerContributor : RunLineMarkerContributor() {
return null //Toml plugin is installed. But, PyCharm has not restarted yet.
}
val keyValue = element.parent as? TomlKeyValue ?: return null
val key = (keyValue.parent as? TomlTable)?.header?.key ?: return null
if (key.text != "tool.poetry.scripts") return null
val header = (keyValue.parent as? TomlTable)?.header ?: return null
if (header.keyText != "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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PoetryVersionInspection : LocalInspectionTool() {
if (file.virtualFile != module.pyProjectToml) return
file.children
.filter { element ->
(element as? TomlTable)?.header?.key?.text in listOf("tool.poetry.dependencies", "tool.poetry.dev-dependencies")
(element as? TomlTable)?.header?.keyText in listOf("tool.poetry.dependencies", "tool.poetry.dev-dependencies")
}.flatMap {
it.children.mapNotNull { line -> line as? TomlKeyValue }
}.forEach { keyValue ->
Expand Down
28 changes: 27 additions & 1 deletion src/com/koxudaxi/poetry/poetry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ import org.apache.tuweni.toml.TomlParseResult
import org.apache.tuweni.toml.TomlTable
import org.jetbrains.annotations.SystemDependent
import org.jetbrains.annotations.TestOnly
import org.toml.lang.psi.TomlKey
import org.toml.lang.psi.TomlTableHeader
import java.io.File
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import java.util.function.Supplier
import java.util.regex.Pattern
import kotlin.reflect.full.memberProperties

const val PY_PROJECT_TOML: String = "pyproject.toml"
const val POETRY_LOCK: String = "poetry.lock"
Expand Down Expand Up @@ -712,4 +715,27 @@ fun parsePoetryShowOutdated(input: String): Map<String, PoetryOutdatedVersion> {

data class PoetryOutdatedVersion(
@SerializedName("currentVersion") var currentVersion: String,
@SerializedName("latestVersion") var latestVersion: String)
@SerializedName("latestVersion") var latestVersion: String)


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


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

0 comments on commit e95221d

Please sign in to comment.