Skip to content

Commit 75be965

Browse files
committed
resolve commands in depends
1 parent 4b9a148 commit 75be965

4 files changed

Lines changed: 70 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# intellij-lets Changelog
44

55
## [Unreleased]
6+
### Added
7+
8+
- resolve commands in `depends`
9+
610
### Updated
711

812
- make go to definition work for gitignore `mixins` files (with '-' (dash) at the beginning)

example/lets.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ commands:
88
test:
99
depends: [build]
1010
cmd: echo Test
11-
11+
12+
hello: echo Test
13+
1214
run:
13-
depends: [test]
15+
depends: [test, hello]
1416
cmd: echo Run

src/main/kotlin/com/github/kindermax/intellijlets/LetsReferenceContributor.kt

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import com.intellij.psi.search.GlobalSearchScope
1010
import com.intellij.psi.util.PsiTreeUtil
1111
import com.intellij.util.PathUtil
1212
import com.intellij.util.ProcessingContext
13+
import org.jetbrains.yaml.psi.YAMLFile
1314
import org.jetbrains.yaml.psi.YAMLKeyValue
15+
import org.jetbrains.yaml.psi.YAMLMapping
1416
import org.jetbrains.yaml.psi.YAMLScalar
1517

1618
open class LetsReferenceContributor : PsiReferenceContributor() {
@@ -20,16 +22,39 @@ open class LetsReferenceContributor : PsiReferenceContributor() {
2022
object : PsiReferenceProvider() {
2123
override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> {
2224
val yamlKeyValue = PsiTreeUtil.getParentOfType(element, YAMLKeyValue::class.java) ?: return emptyArray()
23-
if (yamlKeyValue.keyText == "mixins") {
24-
return arrayOf(LetsMixinReference(element as YAMLScalar))
25+
26+
return when (yamlKeyValue.keyText) {
27+
"mixins" -> arrayOf(LetsMixinReference(element as YAMLScalar))
28+
"depends" -> arrayOf(LetsDependsReference(element as YAMLScalar))
29+
else -> emptyArray()
2530
}
26-
return emptyArray()
2731
}
2832
}
2933
)
3034
}
3135
}
3236

37+
class LetsDependsReference(element: YAMLScalar) : PsiReferenceBase<YAMLScalar>(element) {
38+
override fun resolve(): PsiElement? {
39+
val project = myElement.project
40+
val commandName = myElement.textValue // Extracts the command name inside `depends`
41+
42+
// Locate the command declaration in the same YAML file
43+
val yamlFile = myElement.containingFile as? YAMLFile ?: return null
44+
return PsiTreeUtil.findChildrenOfType(yamlFile, YAMLKeyValue::class.java)
45+
.firstOrNull { it.keyText == commandName && it.parent is YAMLMapping }
46+
}
47+
48+
override fun getVariants(): Array<Any> {
49+
val yamlFile = myElement.containingFile as? YAMLFile ?: return emptyArray()
50+
51+
// Collect all available command names for autocompletion
52+
val commands = PsiTreeUtil.findChildrenOfType(yamlFile, YAMLKeyValue::class.java)
53+
.mapNotNull { it.keyText }
54+
55+
return commands.map { LookupElementBuilder.create(it) }.toTypedArray()
56+
}
57+
}
3358

3459
class LetsMixinReference(element: YAMLScalar) : PsiReferenceBase<YAMLScalar>(element) {
3560
/**
@@ -60,15 +85,15 @@ class LetsMixinReference(element: YAMLScalar) : PsiReferenceBase<YAMLScalar>(ele
6085
return yamlFiles.toTypedArray()
6186
}
6287

63-
/**
88+
/**
6489
* Searches for the mixin file anywhere in the project.
6590
* Supports both top-level files ("lets.build.yaml") and nested files ("lets/lets.docs.yaml").
6691
*/
6792
private fun findMixinFile(project: Project, mixinPath: String): VirtualFile? {
6893
// Normalize paths (handle both "lets.mixin.yaml" and "lets/lets.mixin.yaml")
6994
val normalizedPath = mixinPath.trimStart('/')
7095
// Normalize gitignored files (e.g. "-lets.mixin.yaml" -> "lets.mixin.yaml")
71-
.removePrefix("-")
96+
.removePrefix("-")
7297

7398
// Look for an exact match in the project
7499
return FilenameIndex.getVirtualFilesByName(

src/test/kotlin/com/github/kindermax/intellijlets/reference/ReferenceTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.github.kindermax.intellijlets.reference
22

33
import com.intellij.psi.PsiFile
44
import com.intellij.testFramework.fixtures.BasePlatformTestCase
5+
import org.jetbrains.yaml.psi.YAMLKeyValue
56

67
open class MinixsReferenceTest : BasePlatformTestCase() {
78
fun testMixinFileReference() {
@@ -120,3 +121,34 @@ open class MinixsReferenceTest : BasePlatformTestCase() {
120121
}
121122
}
122123

124+
open class DependsReferenceTest : BasePlatformTestCase() {
125+
fun testDependsCommandReference() {
126+
myFixture.configureByText(
127+
"lets.yaml",
128+
"""
129+
shell: bash
130+
131+
commands:
132+
test:
133+
cmd: echo Test
134+
135+
run:
136+
depends: [<caret>test]
137+
cmd: echo Run
138+
""".trimIndent()
139+
)
140+
141+
val ref = myFixture.getReferenceAtCaretPosition("lets.yaml")
142+
assertNotNull("Reference should not be null", ref)
143+
144+
val resolvedElement = ref!!.resolve()
145+
assertNotNull("Resolved element should not be null", resolvedElement)
146+
147+
val resolvedFile = resolvedElement?.containingFile
148+
assertEquals("lets.yaml", resolvedFile?.name)
149+
150+
val resolvedKey = resolvedElement as? YAMLKeyValue
151+
assertNotNull("Resolved element should be a YAMLKeyValue", resolvedKey)
152+
assertEquals("test", resolvedKey!!.keyText)
153+
}
154+
}

0 commit comments

Comments
 (0)