-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using an undocumented API, but what the hell, it works just fine.
- Loading branch information
1 parent
c22809f
commit 11cff94
Showing
9 changed files
with
191 additions
and
10 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/it/casaricci/hass/plugin/findUsages/HassAutomationFindUsagesProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package it.casaricci.hass.plugin.findUsages | ||
|
||
import com.intellij.lang.cacheBuilder.WordsScanner | ||
import com.intellij.lang.findUsages.FindUsagesProvider | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiNamedElement | ||
import it.casaricci.hass.plugin.MyBundle | ||
import it.casaricci.hass.plugin.language.HassAutomation | ||
import org.jetbrains.yaml.YAMLWordsScanner | ||
|
||
class HassAutomationFindUsagesProvider : FindUsagesProvider { | ||
|
||
override fun getWordsScanner(): WordsScanner { | ||
return YAMLWordsScanner() | ||
} | ||
|
||
override fun canFindUsagesFor(element: PsiElement): Boolean { | ||
return isMyElement(element) | ||
} | ||
|
||
override fun getHelpId(element: PsiElement): String? { | ||
return null | ||
} | ||
|
||
override fun getType(element: PsiElement): String { | ||
if (isMyElement(element)) { | ||
return MyBundle.message("hass.findUsages.haAutomation") | ||
} | ||
return "" | ||
} | ||
|
||
override fun getDescriptiveName(element: PsiElement): String { | ||
if (isMyElement(element)) { | ||
return (element as PsiNamedElement).name ?: "" | ||
} | ||
return "" | ||
} | ||
|
||
override fun getNodeText(element: PsiElement, useFullName: Boolean): String { | ||
return getDescriptiveName(element) | ||
} | ||
|
||
private fun isMyElement(element: PsiElement): Boolean { | ||
return element is HassAutomation | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/it/casaricci/hass/plugin/language/HassAutomation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package it.casaricci.hass.plugin.language | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiNamedElement | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.intellij.util.IncorrectOperationException | ||
import org.jetbrains.yaml.YAMLBundle | ||
import org.jetbrains.yaml.YAMLElementGenerator | ||
import org.jetbrains.yaml.psi.YAMLScalar | ||
import org.jetbrains.yaml.psi.impl.YAMLKeyValueImpl | ||
|
||
/** | ||
* Wraps the "alias" key value element of an automation, giving the element [PsiNamedElement] features. | ||
*/ | ||
class HassAutomation(private val element: YAMLScalar) : PsiNamedElement, YAMLScalar by element { | ||
|
||
/** | ||
* Automation name is actually the value of the "alias" key. | ||
*/ | ||
override fun getName(): String { | ||
return element.textValue | ||
} | ||
|
||
/** | ||
* Copied from [YAMLKeyValueImpl] + [org.jetbrains.yaml.YAMLUtil], although it seems like overkill. | ||
*/ | ||
override fun setName(name: String): PsiElement { | ||
if (name == element.textValue) { | ||
throw IncorrectOperationException(YAMLBundle.message("rename.same.name")) | ||
} | ||
|
||
val elementGenerator = YAMLElementGenerator.getInstance(element.project) | ||
|
||
val tempFile: PsiFile = elementGenerator.createDummyYamlWithText(name) | ||
val textElement = PsiTreeUtil.collectElementsOfType(tempFile, YAMLScalar::class.java).iterator().next() | ||
|
||
element.replace(textElement) | ||
return this | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/it/casaricci/hass/plugin/language/HassElementEvaluator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package it.casaricci.hass.plugin.language | ||
|
||
import com.intellij.codeInsight.TargetElementEvaluatorEx2 | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiNamedElement | ||
import com.intellij.psi.util.parentOfType | ||
import it.casaricci.hass.plugin.isAutomation | ||
import org.jetbrains.yaml.psi.YAMLScalar | ||
|
||
class HassElementEvaluator : TargetElementEvaluatorEx2() { | ||
|
||
/** | ||
* This method will be called for elements that don't inherit from [com.intellij.psi.PsiNamedElement]. | ||
* @return an actual [com.intellij.psi.PsiNamedElement] that can be named and used as a reference | ||
*/ | ||
override fun getNamedElement(element: PsiElement): PsiElement? { | ||
// handle text values (e.g. property values) | ||
val textElement = element.parentOfType<YAMLScalar>() | ||
if (textElement != null) { | ||
return wrapElement(textElement) | ||
} | ||
|
||
return null | ||
} | ||
|
||
/** | ||
* This method will be called for practically every element selected by the user, so it needs to be very fast. | ||
* @return an element wrapped with a custom class that we can recognize (if available, otherwise the same element | ||
* will be returned) | ||
*/ | ||
override fun adjustReferenceOrReferencedElement( | ||
file: PsiFile, | ||
editor: Editor, | ||
offset: Int, | ||
flags: Int, | ||
refElement: PsiElement? | ||
): PsiElement? { | ||
val wrappedElement = when (refElement) { | ||
is YAMLScalar -> { | ||
wrapElement(refElement) | ||
} | ||
|
||
else -> { | ||
null | ||
} | ||
} | ||
|
||
return wrappedElement ?: super.adjustReferenceOrReferencedElement(file, editor, offset, flags, refElement) | ||
} | ||
|
||
private fun wrapElement(element: YAMLScalar): PsiNamedElement? { | ||
if (isAutomation(element)) { | ||
return HassAutomation(element) | ||
} | ||
return null | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters