Skip to content

Commit

Permalink
Inject requirements fragments for arrays under PEP 735's `dependency-…
Browse files Browse the repository at this point in the history
…groups`
  • Loading branch information
InSyncWithFoo committed Oct 12, 2024
1 parent a09f885 commit 7530159
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/insyncwithfoo/ryecharm/TOML.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal val TomlTable.absoluteName: TOMLPath
get() = TOMLPath(header.key?.text.orEmpty())


private val TomlKey.name: String
private val TomlKey.segmentedName: String
get() = segments.joinToString(".") { it.text }


Expand All @@ -43,9 +43,9 @@ private val TomlKey.table: TomlTable?

internal val TomlKey.absoluteName: TOMLPath
get() = when {
parent === containingFile -> TOMLPath(name)
table == null -> TOMLPath(name)
else -> table!!.absoluteName + TOMLPath(name)
parent === containingFile -> TOMLPath(segmentedName)
table == null -> TOMLPath(segmentedName)
else -> table!!.absoluteName + TOMLPath(segmentedName)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import org.toml.lang.psi.TomlLiteral
* * `uv.upgrade-package`
* * `uv.pip.upgrade-package`
*
* As a bonus, `pyproject.toml`'s
* `project.optional-dependencies` is also supported.
* As a bonus, `pyproject.toml`'s `project.optional-dependencies`
* and `dependency-groups` (PEP 735) are also supported.
*/
internal class RequirementsInjector : LanguageInjectionContributor {

Expand Down Expand Up @@ -64,8 +64,8 @@ internal class RequirementsInjector : LanguageInjectionContributor {
val virtualFile = file.virtualFile ?: return null
val absoluteName = key.absoluteName

if (virtualFile.isPyprojectToml && absoluteName isChildOf "project.optional-dependencies") {
return injection
if (virtualFile.isPyprojectToml) {
return getPyprojectTomlInjection(absoluteName)
}

val relativeName = when {
Expand Down Expand Up @@ -93,4 +93,10 @@ internal class RequirementsInjector : LanguageInjectionContributor {
return injection
}

private fun getPyprojectTomlInjection(keyAbsoluteName: TOMLPath) = when {
keyAbsoluteName isChildOf "project.optional-dependencies" -> injection
keyAbsoluteName isChildOf "dependency-groups" -> injection
else -> null
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ internal class RequirementsInjectorTest : LanguageInjectionTestCase() {
assertEquals(2, fragments.size)
}

@Test
fun `test dependency-groups`() = fileBasedTest("dependencyGroups/pyproject.toml") {
assertEquals(1, fragments.size)
}

@Test
fun `test other keys`() {
val directoriesAndKeyNames = mapOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependency-groups]
foo = [
"ruff==0.6.9",
{ include-group = "bar" }
]

0 comments on commit 7530159

Please sign in to comment.