Skip to content

Commit

Permalink
feat: Support sting type tasks.depends code completion (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
134130 authored Jan 5, 2025
1 parent d60d257 commit ddd4af2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@ import org.toml.lang.psi.TomlTable
/**
* ```
* [tasks.foo]
* ...
* run = "..."
* ```
*
* then
*
* ```
* [tasks.<task-name>]
* depends = [ "f<caret>" ]
* #^ Provides completion for "foo"
* ```
*
* or
*
* ```
* [tasks.<task-name>]
* depends = "f<caret>"
* #^ Provides completion for "foo"
* ```
*/
class MiseTomlTaskCompletionProvider : CompletionProvider<CompletionParameters>() {
override fun addCompletions(
Expand All @@ -31,14 +44,14 @@ class MiseTomlTaskCompletionProvider : CompletionProvider<CompletionParameters>(
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<TomlTable>() ?: 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(
Expand All @@ -48,6 +61,4 @@ class MiseTomlTaskCompletionProvider : CompletionProvider<CompletionParameters>(
)
}
}

// TODO: Need to support literal string completion
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ object MiseTomlPsiPatterns {
)
val inTaskDependsArray = miseTomlPsiElement<PsiElement>().inside(onTaskDependsArray)

Check notice on line 81 in modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Function or property has platform type

Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable.

/**
* ```
* [tasks]
* foo = { version = "*", depends = "" }
* #^
* ```
*
* ```
* [tasks.foo]
* depends = ""
* #^
* ```
*/
private val onTaskDependsString =
StandardPatterns.or(
miseTomlStringLiteral()
.withParent(taskProperty("depends")),
miseTomlStringLiteral()
.withParent(taskProperty("depends_post")),
)
val inTaskDependsString = miseTomlPsiElement<PsiElement>().inside(onTaskDependsString)

Check notice on line 103 in modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Function or property has platform type

Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable.

inline fun <reified I : PsiElement> psiElement() = PlatformPatterns.psiElement(I::class.java)

Check notice on line 105 in modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Function or property has platform type

Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable.

Check notice on line 105 in modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Function 'psiElement' could be private

fun <T : Any, Self : ObjectPattern<T, Self>> ObjectPattern<T, Self>.with(

Check notice on line 107 in modules/core/src/main/kotlin/com/github/l34130/mise/core/lang/psi/MiseTomlPsiPatterns.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Function 'with' could be private
Expand Down

0 comments on commit ddd4af2

Please sign in to comment.