Skip to content

Commit

Permalink
Add Run Line Marker for paddle run tasks
Browse files Browse the repository at this point in the history
Fix a bug with version specifiers
  • Loading branch information
Oleg Smirnov committed Jul 5, 2022
1 parent 38c9097 commit 89314f0
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 55 deletions.
4 changes: 3 additions & 1 deletion example/paddle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ plugins:

tasks:
run:
- entrypoint: src/main/app.py
- entrypoint: src/main/main.py
id: main
- entrypoint: src/main/main.py
id: another

#executor:
# type: docker
Expand Down
7 changes: 1 addition & 6 deletions idea/src/main/kotlin/io/paddle/idea/PaddleManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.intellij.openapi.externalSystem.util.ExternalSystemConstants
import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.options.Configurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import com.intellij.openapi.util.Pair
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.VirtualFile
Expand All @@ -35,7 +34,7 @@ class PaddleManager : ExternalSystemManager<
>,
ExternalSystemUiAware,
ExternalSystemConfigurableAware,
ExternalSystemAutoImportAware, StartupActivity {
ExternalSystemAutoImportAware {

companion object {
val ID: ProjectSystemId = ProjectSystemId("Paddle")
Expand Down Expand Up @@ -102,8 +101,4 @@ class PaddleManager : ExternalSystemManager<
override fun getConfigurable(project: Project): Configurable {
return PaddleConfigurable(project)
}

override fun runActivity(project: Project) {
val localSettings = project.getService(PaddleLocalSettings::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.paddle.idea.execution

import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution.actions.ConfigurationFromContext
import com.intellij.execution.configurations.ConfigurationFactory
import com.intellij.openapi.externalSystem.service.execution.*
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import com.jetbrains.python.sdk.basePath
import io.paddle.idea.PaddleManager
import org.jetbrains.yaml.psi.YAMLKeyValue

class PaddleRunConfigurationProducer : AbstractExternalSystemRunConfigurationProducer() {
override fun getConfigurationFactory(): ConfigurationFactory {
return PaddleTaskConfigurationType.getInstance().factory
}

override fun setupConfigurationFromContext(
configuration: ExternalSystemRunConfiguration,
context: ConfigurationContext,
sourceElement: Ref<PsiElement>
): Boolean {
if (context.location is ExternalSystemTaskLocation) {
return super.setupConfigurationFromContext(configuration, context, sourceElement)
}

if (configuration.settings.externalSystemId != PaddleManager.ID || configuration !is PaddleRunConfiguration) return false
if (context.location?.psiElement?.parent !is YAMLKeyValue) return false
val runTaskId = (context.location?.psiElement?.parent as YAMLKeyValue).value?.text ?: return false
val module = context.location?.module ?: return false

configuration.settings.taskNames = listOf("run$$runTaskId")
configuration.settings.externalProjectPath = module.basePath
configuration.name = AbstractExternalSystemTaskConfigurationType.generateName(module.project, configuration.settings)

return true
}

override fun isConfigurationFromContext(configuration: ExternalSystemRunConfiguration, context: ConfigurationContext): Boolean {
if (context.location is ExternalSystemTaskLocation) {
return super.isConfigurationFromContext(configuration, context)
}

if (configuration.settings.externalSystemId != PaddleManager.ID || configuration !is PaddleRunConfiguration) return false
val module = context.location?.module ?: return false
if (configuration.settings.externalProjectPath != module.basePath) return false

if (context.location?.psiElement?.parent !is YAMLKeyValue) return false
val runTaskId = (context.location?.psiElement?.parent as YAMLKeyValue).value?.text ?: return false
val taskNames = configuration.settings.taskNames.takeIf { it.isNotEmpty() } ?: return false
if (taskNames.first() != "run$$runTaskId") return false

return true
}

override fun isPreferredConfiguration(self: ConfigurationFromContext?, other: ConfigurationFromContext): Boolean {
return other.isProducedBy(PaddleRunConfigurationProducer::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.paddle.idea.execution

import com.intellij.execution.lineMarker.ExecutorAction
import com.intellij.execution.lineMarker.RunLineMarkerContributor
import com.intellij.icons.AllIcons
import com.intellij.psi.PsiElement
import io.paddle.idea.utils.getSuperParent

class PaddleRunLineMarkerContributor : RunLineMarkerContributor() {
override fun getInfo(element: PsiElement): Info? {
if (element.containingFile.name != "paddle.yaml") return null

if (element.text.contains("id")
&& element.getSuperParent(5).text.startsWith("run")
&& element.getSuperParent(7).text.startsWith("tasks")
) {
val actions = ExecutorAction.getActions(Integer.MAX_VALUE)
return Info(AllIcons.RunConfigurations.TestState.Run, actions) { e ->
actions.mapNotNull { getText(it, e) }.joinToString("\n")
}
}

return null
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.paddle.idea.utils

import com.intellij.openapi.util.Key
import com.intellij.openapi.util.UserDataHolder
import com.intellij.psi.PsiElement

fun <T> UserDataHolder.getOrPut(key: Key<T>, factory: () -> T): T {
return getUserData(key) ?: run {
Expand All @@ -10,3 +11,9 @@ fun <T> UserDataHolder.getOrPut(key: Key<T>, factory: () -> T): T {
value
}
}

fun PsiElement.getSuperParent(level: Int): PsiElement {
var current = this
repeat(level) { current = current.parent }
return current
}
6 changes: 4 additions & 2 deletions idea/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<!-- Tasks Execution & Run Configurations -->
<configurationType implementation="io.paddle.idea.execution.PaddleTaskConfigurationType"/>
<runConfigurationProducer implementation="io.paddle.idea.execution.PaddleRuntimeConfigurationProducer"/>
<runConfigurationProducer implementation="io.paddle.idea.execution.PaddleRunConfigurationProducer"/>

<externalSystemOutputParserProvider implementation="io.paddle.idea.execution.parser.PaddleParserProvider"/>

Expand All @@ -63,6 +63,8 @@
<stepsBeforeRunProvider implementation="io.paddle.idea.execution.beforeRun.PaddleBeforeRunTaskProvider"/>
<externalSystem.beforeRunTaskImporter implementation="io.paddle.idea.execution.beforeRun.PaddleBeforeRunTaskImporter"/>

<runLineMarkerContributor language="yaml" implementationClass="io.paddle.idea.execution.PaddleRunLineMarkerContributor"/>

<!-- Run & Debug -->
<programRunner implementation="io.paddle.idea.execution.runners.PaddleTaskRunner"
order="first"/>
Expand All @@ -88,7 +90,7 @@
<completion.contributor language="yaml"
implementationClass="io.paddle.idea.completion.PyPackageVersionCompletionContributor"/>


<!-- Notifications -->
<notificationGroup id="Paddle" displayType="BALLOON"/>
</extensions>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PyPackageVersionSpecifier private constructor(val clauses: List<PyPackageV
return versionSpecifier.split(",").map { it.trim() }.map { clause ->
val relation = PyPackageVersionRelation.values().find { clause.startsWith(it.operator) }
if (relation == null) {
// relation is not specified, assuming it is meant to be == (EQ)
// the relation is not specified, assuming it is meant to be == (EQ)
PyPackageVersionClause(PyPackageVersionRelation.EQ, clause.trim())
} else {
val rawVersion = clause.substringAfter(relation.operator).trim()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import io.paddle.plugin.python.PyLocations
import io.paddle.plugin.python.dependencies.index.distributions.PyDistributionInfo
import io.paddle.plugin.python.extensions.Repositories
import io.paddle.plugin.python.utils.*
import io.paddle.project.PaddleProject
import kotlinx.coroutines.*
import java.util.*
import kotlin.concurrent.schedule

class PyPackageRepositories(
private val repositories: Set<PyPackageRepository>,
val primarySource: PyPackageRepository,
val project: PaddleProject,
useCachedIndex: Boolean = true,
downloadIndex: Boolean = false
) {
companion object {
private const val CACHE_SYNC_PERIOD_MS = 60000L

fun resolve(repoDescriptors: List<Repositories.Descriptor>): PyPackageRepositories {
fun resolve(repoDescriptors: List<Repositories.Descriptor>, project: PaddleProject): PyPackageRepositories {
val repositories = hashSetOf(PyPackageRepository.PYPI_REPOSITORY)
var primarySource = PyPackageRepository.PYPI_REPOSITORY

Expand All @@ -39,12 +41,21 @@ class PyPackageRepositories(
repositories.add(repo)
}

return PyPackageRepositories(repositories, primarySource)
return PyPackageRepositories(repositories, primarySource, project)
}

private fun updateIndex(repositories: Set<PyPackageRepository>) = runBlocking {
repositories.parallelForEach { it.updateIndex() }
repositories.forEach { it.saveCache() }
private fun updateIndex(repositories: Set<PyPackageRepository>, project: PaddleProject) = runBlocking {
repositories.parallelForEach {
try {
it.updateIndex()
it.saveCache()
} catch (e: Throwable) {
project.terminal.warn(
"Failed to update index for PyPI repository: ${it.urlSimple}. " +
"Autocompletion for package names will not be available at the moment."
)
}
}
}
}

Expand All @@ -57,11 +68,11 @@ class PyPackageRepositories(
?.let { repo.loadCache(it) }
?: newRepositories.add(repo)
}
updateIndex(newRepositories)
updateIndex(newRepositories, project)
}

if (downloadIndex) {
updateIndex(repositories)
updateIndex(repositories, project)
}

Timer("PyPackagesRepositoriesCacheSynchronizer", true)
Expand Down

0 comments on commit 89314f0

Please sign in to comment.