Skip to content

Commit

Permalink
feat: support custom filetype associations (e.g. dev.Dockerfile) (#32)
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Guidée <[email protected]>
  • Loading branch information
quentinguidee authored May 19, 2024
1 parent dc4f6c2 commit cd4443c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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(".")
Expand All @@ -36,7 +51,7 @@ class IconProvider : IconProvider() {
}
}

if (file?.fileType?.isBinary == true) {
if (virtualFile?.fileType?.isBinary == true) {
return icons.binary
}

Expand Down

0 comments on commit cd4443c

Please sign in to comment.