-
Notifications
You must be signed in to change notification settings - Fork 21
[Plugin] Display ImageVector kt Icons in Project View #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
egorikftp
merged 5 commits into
ComposeGears:main
from
t-regbs:feature/file-icon-preview
Dec 10, 2025
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6497472
feat: add image vector icon support and refactor completion contribut…
t-regbs 6ffc718
refactor: replace cached value retrieval with getOrCreateCachedIcon e…
t-regbs 5659989
refactor: consolidate parsing and icon conversion functions
t-regbs d8d8357
chore: fix formatting
t-regbs dc52ad1
refactor: update getOrCreateCachedIcon kdoc
t-regbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
29 changes: 29 additions & 0 deletions
29
...ea-plugin/src/main/kotlin/io/github/composegears/valkyrie/icon/ImageVectorIconProvider.kt
This file contains hidden or 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,29 @@ | ||
| package io.github.composegears.valkyrie.icon | ||
|
|
||
| import com.intellij.ide.IconProvider | ||
| import com.intellij.openapi.project.DumbAware | ||
| import com.intellij.psi.PsiElement | ||
| import com.intellij.psi.PsiFile | ||
| import io.github.composegears.valkyrie.util.getOrCreateCachedIcon | ||
| import io.github.composegears.valkyrie.util.hasImageVectorProperties | ||
| import javax.swing.Icon | ||
| import org.jetbrains.kotlin.psi.KtFile | ||
|
|
||
| class ImageVectorIconProvider : | ||
| IconProvider(), | ||
| DumbAware { | ||
|
|
||
| override fun getIcon(element: PsiElement, flags: Int): Icon? { | ||
| val ktFile = when (element) { | ||
| is KtFile -> element | ||
| is PsiFile -> null | ||
| else -> element.containingFile as? KtFile | ||
| } ?: return null | ||
|
|
||
| if (!ktFile.hasImageVectorProperties()) { | ||
| return null | ||
| } | ||
|
|
||
| return ktFile.getOrCreateCachedIcon() | ||
| } | ||
| } |
108 changes: 108 additions & 0 deletions
108
tools/idea-plugin/src/main/kotlin/io/github/composegears/valkyrie/util/ImageVectorPsiUtil.kt
This file contains hidden or 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,108 @@ | ||
| package io.github.composegears.valkyrie.util | ||
|
|
||
| import com.intellij.psi.util.CachedValueProvider | ||
| import com.intellij.psi.util.CachedValuesManager | ||
| import io.github.composegears.valkyrie.completion.ImageVectorIcon | ||
| import io.github.composegears.valkyrie.psi.imagevector.ImageVectorPsiParser | ||
| import io.github.composegears.valkyrie.sdk.ir.core.IrImageVector | ||
| import io.github.composegears.valkyrie.sdk.ir.util.aspectRatio | ||
| import io.github.composegears.valkyrie.sdk.ir.util.dominantShadeColor | ||
| import io.github.composegears.valkyrie.sdk.ir.xml.toVectorXmlString | ||
| import javax.swing.Icon | ||
| import org.jetbrains.kotlin.psi.KtFile | ||
| import org.jetbrains.kotlin.psi.KtProperty | ||
|
|
||
| /** | ||
| * Checks if this property is an ImageVector based on its type reference. | ||
| */ | ||
| fun KtProperty.isImageVector(): Boolean { | ||
| val typeText = typeReference?.text ?: return false | ||
| return typeText in IMAGE_VECTOR_TYPES | ||
| } | ||
|
|
||
| /** | ||
| * Checks if this Kotlin file contains ImageVector properties. | ||
| * | ||
| * A file is considered to contain ImageVector if it has at least one top-level | ||
| * property with an ImageVector type annotation. | ||
| */ | ||
| fun KtFile.hasImageVectorProperties(): Boolean { | ||
| val properties = declarations.filterIsInstance<KtProperty>() | ||
| if (properties.isEmpty()) return false | ||
|
|
||
| return properties.any { it.isImageVector() } | ||
| } | ||
|
|
||
| /** | ||
| * Gets or creates a cached gutter icon for an ImageVector property. | ||
| * | ||
| * This function uses the PSI caching mechanism to avoid recreating icons repeatedly | ||
| * for the same property, improving performance. | ||
| */ | ||
| fun KtFile.getOrCreateCachedIcon(): Icon? { | ||
| return CachedValuesManager | ||
| .getManager(project).getCachedValue(this) { | ||
| val icon = createImageVectorIcon() | ||
| CachedValueProvider.Result.create(icon, this) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets or creates a cached gutter icon for an ImageVector property. | ||
| * | ||
| * This function uses the PSI caching mechanism to avoid recreating icons repeatedly | ||
| * for the same property, improving performance. | ||
| */ | ||
| fun KtProperty.getOrCreateGutterIcon(): Icon? { | ||
| val cachedValuesManager = CachedValuesManager.getManager(project) | ||
|
|
||
| return cachedValuesManager.getCachedValue(this) { | ||
| val icon = createIcon() | ||
|
|
||
| CachedValueProvider.Result.create(icon, this) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates an icon representation from this Kotlin file containing an ImageVector. | ||
| * | ||
| * This function parses the ImageVector definition and renders it as a Swing Icon | ||
| * suitable for display in the IDE UI (project view, gutter, completion, etc.). | ||
| */ | ||
| private fun KtFile.createImageVectorIcon(): Icon? = ImageVectorPsiParser.parseToIrImageVector(this) | ||
| ?.toIcon() | ||
|
|
||
| private fun KtProperty.createIcon(): Icon? = parseImageVectorProperty(this) | ||
| ?.toIcon() | ||
|
|
||
| private fun IrImageVector.toIcon(): Icon = ImageVectorIcon( | ||
| vectorXml = toVectorXmlString(), | ||
| aspectRatio = aspectRatio, | ||
| dominantShade = dominantShadeColor, | ||
| ) | ||
|
|
||
| private fun parseImageVectorProperty(property: KtProperty): IrImageVector? { | ||
| // Try parsing the current file | ||
| val containingFile = property.containingKtFile | ||
| ImageVectorPsiParser.parseToIrImageVector(containingFile)?.let { return it } | ||
|
|
||
| // For properties from libraries, navigate to decompiled/attached source | ||
| val navigationElement = property.navigationElement | ||
| if (navigationElement is KtProperty && navigationElement != property) { | ||
| val sourceFile = navigationElement.containingKtFile | ||
|
|
||
| // Only parse if we have actual source code (not a stub) | ||
| if (COMPILED_CODE_MARKER !in sourceFile.text) { | ||
| return ImageVectorPsiParser.parseToIrImageVector(sourceFile) | ||
| } | ||
| } | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| private val IMAGE_VECTOR_TYPES = setOf( | ||
| "ImageVector", | ||
| "androidx.compose.ui.graphics.vector.ImageVector", | ||
| ) | ||
|
|
||
| private const val COMPILED_CODE_MARKER = "/* compiled code */" | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.