Skip to content

Commit cb04382

Browse files
fbriconangelozerr
authored andcommitted
feat: display platform file icons on completion
Signed-off-by: Fred Bricon <[email protected]>
1 parent cab8091 commit cb04382

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/main/java/com/redhat/devtools/lsp4ij/ui/IconMapper.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
package com.redhat.devtools.lsp4ij.ui;
1515

1616
import com.intellij.icons.AllIcons;
17+
import com.intellij.openapi.fileTypes.FileType;
18+
import com.intellij.openapi.fileTypes.FileTypeManager;
19+
import com.intellij.openapi.fileTypes.UnknownFileType;
1720
import com.intellij.openapi.util.IconLoader;
1821
import com.intellij.util.ui.ColorIcon;
1922
import org.eclipse.lsp4j.CompletionItem;
@@ -28,6 +31,8 @@
2831
import java.util.Map;
2932
import java.util.concurrent.ConcurrentHashMap;
3033

34+
import static com.intellij.util.PathUtil.getFileExtension;
35+
3136
/**
3237
* Maps LSP4J kinds to Intellij Icons. See the <a href="https://jetbrains.design/intellij/resources/icons_list/" target="_blank">JetBrains icon list</a> for reference.
3338
*/
@@ -54,12 +59,36 @@ private IconMapper(){
5459
return null;
5560
}
5661
Icon icon = null;
57-
if (item.getKind() == CompletionItemKind.Color) {
62+
if (item.getKind() == CompletionItemKind.File) {
63+
icon = getFileIcon(item);
64+
} else if (item.getKind() == CompletionItemKind.Color) {
5865
icon = getColorIcon(item);
5966
}
6067
return icon == null? getIcon(item.getKind()) : icon;
6168
}
6269

70+
private static Icon getFileIcon(CompletionItem item) {
71+
FileType fileType = getFileType(item.getLabel());
72+
if (fileType == null) {
73+
fileType = getFileType(item.getDetail());
74+
}
75+
return fileType == null? null: fileType.getIcon();
76+
}
77+
78+
private static FileType getFileType(String fileOrPathName){
79+
if (fileOrPathName == null || fileOrPathName.isBlank()) {
80+
return null;
81+
}
82+
String extension = getFileExtension(fileOrPathName);
83+
if (extension != null) {
84+
FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(extension);
85+
if (!UnknownFileType.INSTANCE.equals(fileType)) {
86+
return fileType;
87+
}
88+
}
89+
return null;
90+
}
91+
6392
private static Icon getColorIcon(@NotNull CompletionItem item) {
6493
//While this method is private, we already know this is a Color kind, no need to check again
6594
String docString = getDocString(item.getDocumentation());

0 commit comments

Comments
 (0)