Skip to content

Commit

Permalink
Documentation for scripts (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-athome committed Jan 27, 2025
1 parent a335f51 commit f501cbf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.util.childrenOfType
import com.intellij.refactoring.suggested.createSmartPointer
import com.intellij.util.TextWithIcon
import it.casaricci.hass.plugin.HassKnownDomains
import it.casaricci.hass.plugin.entityId
import it.casaricci.hass.plugin.isActionCall
import it.casaricci.hass.plugin.isHassConfigFile
import it.casaricci.hass.plugin.*
import it.casaricci.hass.plugin.services.getDomainNameFromActionName
import org.jetbrains.yaml.psi.YAMLKeyValue
import org.jetbrains.yaml.psi.YAMLMapping
Expand All @@ -29,9 +26,8 @@ class HassDocumentationProvider : PsiDocumentationTargetProvider {
/**
* This part of the API is not really well documented yet - I guess because it's relatively new.
* This method is called in various situations and I didn't really knew how to detect each situation but by
* reverse-engineering the method arguments. The current code seems to handle all (2+1) situations well - for now.
* reverse-engineering the method arguments. The current code seems to handle all (3+1) situations well - for now.
*/
// TODO handle quick documentation on script itself
override fun documentationTarget(element: PsiElement, originalElement: PsiElement?): DocumentationTarget? {
// we still don't know when originalElement could be null, better safe than sorry
if (originalElement == null) {
Expand All @@ -41,7 +37,8 @@ class HassDocumentationProvider : PsiDocumentationTargetProvider {
// documentation request coming from a Home Assistant YAML file
if (isHassConfigFile(originalElement)) {
// documentation request coming from an action call
if (originalElement.parent is YAMLScalar && isActionCall(originalElement.parent as YAMLScalar)) {
if ((originalElement.parent is YAMLScalar && isActionCall(originalElement.parent as YAMLScalar) ||
isScriptDefinition(originalElement.parent))) {
val resolveReference: Boolean

// documentation request from a code completion popup
Expand All @@ -54,6 +51,11 @@ class HassDocumentationProvider : PsiDocumentationTargetProvider {
resolveReference = true
element.parent
}
// documentation request on the entity definition
else if (element.parent is YAMLKeyValue) {
resolveReference = false
element.parent
}
// unhandled case?
else {
resolveReference = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.lang.findUsages.FindUsagesProvider
import com.intellij.psi.PsiElement
import it.casaricci.hass.plugin.HassKnownDomains

Check warning on line 6 in src/main/kotlin/it/casaricci/hass/plugin/findUsages/HassScriptFindUsagesProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import it.casaricci.hass.plugin.MyBundle
import it.casaricci.hass.plugin.isScriptDefinition
import org.jetbrains.yaml.YAMLWordsScanner
import org.jetbrains.yaml.psi.YAMLKeyValue

Check warning on line 10 in src/main/kotlin/it/casaricci/hass/plugin/findUsages/HassScriptFindUsagesProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive

Expand Down Expand Up @@ -43,7 +44,5 @@ class HassScriptFindUsagesProvider : FindUsagesProvider {
return ""
}

private fun isMyElement(element: PsiElement): Boolean = element is YAMLKeyValue &&
element.parent.parent is YAMLKeyValue &&
(element.parent.parent as YAMLKeyValue).keyText == HassKnownDomains.SCRIPT
private fun isMyElement(element: PsiElement): Boolean = isScriptDefinition(element)
}
6 changes: 6 additions & 0 deletions src/main/kotlin/it/casaricci/hass/plugin/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ fun isActionCall(element: YAMLScalar): Boolean {
(element.parent as YAMLKeyValue).keyText == "service")
}

fun isScriptDefinition(element: PsiElement): Boolean {
return element is YAMLKeyValue &&
element.parentMapping?.parent is YAMLKeyValue &&
(element.parentMapping?.parent as YAMLKeyValue).keyText == HassKnownDomains.SCRIPT
}

/**
* An [InputStream] that updates a [ProgressIndicator] while being read.
* To be used with [com.intellij.util.io.HttpRequests], handles also [CountingGZIPInputStream] for compressed streams.
Expand Down

0 comments on commit f501cbf

Please sign in to comment.