From ddd4af289012632392411ff665fa8362cdcd53bd Mon Sep 17 00:00:00 2001 From: JiHwan Oh Date: Sun, 5 Jan 2025 17:44:15 +0900 Subject: [PATCH] feat: Support sting type `tasks.depends` code completion (#127) --- .../MiseTomlCompletionContributor.kt | 7 +++++- .../MiseTomlTaskCompletionProvider.kt | 21 +++++++++++++----- .../mise/core/lang/psi/MiseTomlPsiPatterns.kt | 22 +++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/completion/MiseTomlCompletionContributor.kt b/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/completion/MiseTomlCompletionContributor.kt index a61ecce9..86a86e2d 100644 --- a/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/completion/MiseTomlCompletionContributor.kt +++ b/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/completion/MiseTomlCompletionContributor.kt @@ -3,9 +3,14 @@ package com.github.l34130.mise.core.lang.completion import com.github.l34130.mise.core.lang.psi.MiseTomlPsiPatterns import com.intellij.codeInsight.completion.CompletionContributor import com.intellij.codeInsight.completion.CompletionType +import com.intellij.patterns.StandardPatterns class MiseTomlCompletionContributor : CompletionContributor() { init { - extend(CompletionType.BASIC, MiseTomlPsiPatterns.inTaskDependsArray, MiseTomlTaskCompletionProvider()) + extend( + CompletionType.BASIC, + StandardPatterns.or(MiseTomlPsiPatterns.inTaskDependsArray, MiseTomlPsiPatterns.inTaskDependsString), + MiseTomlTaskCompletionProvider(), + ) } } diff --git a/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/completion/MiseTomlTaskCompletionProvider.kt b/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/completion/MiseTomlTaskCompletionProvider.kt index ed401b5b..dae41832 100644 --- a/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/completion/MiseTomlTaskCompletionProvider.kt +++ b/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/completion/MiseTomlTaskCompletionProvider.kt @@ -16,11 +16,24 @@ import org.toml.lang.psi.TomlTable /** * ``` * [tasks.foo] - * ... + * run = "..." + * ``` + * + * then * + * ``` * [tasks.] * depends = [ "f" ] * #^ Provides completion for "foo" + * ``` + * + * or + * + * ``` + * [tasks.] + * depends = "f" + * #^ Provides completion for "foo" + * ``` */ class MiseTomlTaskCompletionProvider : CompletionProvider() { override fun addCompletions( @@ -31,14 +44,14 @@ class MiseTomlTaskCompletionProvider : CompletionProvider( val element = parameters.position val miseTomlFile = element.containingFile as? MiseTomlFile ?: return - val dependsArray = (element.parent.parent as? TomlArray) ?: return + val dependsArray = (element.parent.parent as? TomlArray) val parentTable = element.parentOfType() ?: return val parentTaskName = parentTable.taskName for (task in miseTomlFile.allTasks()) { val taskName = task.name ?: continue - if (dependsArray.elements.any { it.stringValue == taskName }) continue + if (dependsArray?.elements?.any { it.stringValue == taskName } == true) continue if (taskName == parentTaskName) continue result.addElement( @@ -48,6 +61,4 @@ class MiseTomlTaskCompletionProvider : CompletionProvider( ) } } - - // TODO: Need to support literal string completion } diff --git a/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt b/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt index 7ba4e073..ef8c6045 100644 --- a/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt +++ b/modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt @@ -80,6 +80,28 @@ object MiseTomlPsiPatterns { ) val inTaskDependsArray = miseTomlPsiElement().inside(onTaskDependsArray) + /** + * ``` + * [tasks] + * foo = { version = "*", depends = "" } + * #^ + * ``` + * + * ``` + * [tasks.foo] + * depends = "" + * #^ + * ``` + */ + private val onTaskDependsString = + StandardPatterns.or( + miseTomlStringLiteral() + .withParent(taskProperty("depends")), + miseTomlStringLiteral() + .withParent(taskProperty("depends_post")), + ) + val inTaskDependsString = miseTomlPsiElement().inside(onTaskDependsString) + inline fun psiElement() = PlatformPatterns.psiElement(I::class.java) fun > ObjectPattern.with(