From cd4443c6ebc804eb9cb861203c873592e50ad782 Mon Sep 17 00:00:00 2001 From: Quentin Date: Sun, 19 May 2024 22:23:42 +0200 Subject: [PATCH] feat: support custom filetype associations (e.g. `dev.Dockerfile`) (#32) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Quentin Guidée --- CHANGELOG.md | 1 + .../jetbrains_icons/IconProvider.kt | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e926945..0b24539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Different colors for Java filetypes (e.g. `Class`: Red, `Interface`: Green, `Record`: Mauve, `Enum`: Yellow and `Annotation`: Green) ([#35](https://github.com/catppuccin/jetbrains-icons/pull/35)) - Ability to disable different colors for Java filetypes in settings panel. ([#35](https://github.com/catppuccin/jetbrains-icons/pull/35)) +- Add Docker icon to all files with string `Dockerfile` (e.g. `dev.Dockerfile`) ### Changed diff --git a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/IconProvider.kt b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/IconProvider.kt index 8f784b8..617cb2b 100644 --- a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/IconProvider.kt +++ b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/IconProvider.kt @@ -1,31 +1,46 @@ package com.github.catppuccin.jetbrains_icons -import com.github.catppuccin.jetbrains_icons.settings.PluginSettingsState import com.intellij.ide.IconProvider import com.intellij.psi.PsiElement +import com.intellij.psi.PsiManager import com.intellij.psi.util.PsiUtilCore import javax.swing.Icon class IconProvider : IconProvider() { private var icons = IconPack.instance.icons + private val fileTypes = mapOf( + "Dockerfile" to icons.docker, + ) + override fun getIcon(element: PsiElement, flags: Int): Icon { - val file = PsiUtilCore.getVirtualFile(element) + val virtualFile = PsiUtilCore.getVirtualFile(element) + + // File types + if (virtualFile != null) { + val file = PsiManager.getInstance(element.project).findFile(virtualFile) + if (file != null) { + val icon = fileTypes[file.fileType.name] + if (icon != null) { + return icon + } + } + } // Folders - if (file?.isDirectory == true) { - return icons.FOLDER_TO_ICONS[file.name.lowercase()] ?: icons._folder + if (virtualFile?.isDirectory == true) { + return icons.FOLDER_TO_ICONS[virtualFile.name.lowercase()] ?: icons._folder } // Files - val icon = icons.FILE_TO_ICONS[file?.name?.lowercase()] + val icon = icons.FILE_TO_ICONS[virtualFile?.name?.lowercase()] if (icon != null) { return icon } // Extensions // if the file is abc.test.tsx, try abc.test.tsx, then test.tsx, then tsx - val parts = file?.name?.split(".") + val parts = virtualFile?.name?.split(".") if (parts != null) { for (i in parts.indices) { val path = parts.subList(i, parts.size).joinToString(".") @@ -36,7 +51,7 @@ class IconProvider : IconProvider() { } } - if (file?.fileType?.isBinary == true) { + if (virtualFile?.fileType?.isBinary == true) { return icons.binary }