-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: More descriptive location labels (instead of just .workspace) (#…
- Loading branch information
Tomasz Pasternak
authored
Oct 20, 2024
1 parent
9cebd40
commit 160a88e
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
base/src/com/google/idea/blaze/base/ui/BlazeModuleRendererFactory.java
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,55 @@ | ||
package com.google.idea.blaze.base.ui; | ||
|
||
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot; | ||
import com.google.idea.blaze.base.settings.Blaze; | ||
import com.google.idea.blaze.base.settings.BlazeImportSettings; | ||
import com.intellij.ide.util.ModuleRendererFactory; | ||
import com.intellij.openapi.module.ModuleType; | ||
import com.intellij.openapi.module.ModuleUtilCore; | ||
import com.intellij.openapi.roots.ProjectFileIndex; | ||
import com.intellij.openapi.roots.ProjectRootManager; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.util.PsiUtilCore; | ||
import com.intellij.util.TextWithIcon; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
|
||
public class BlazeModuleRendererFactory extends ModuleRendererFactory { | ||
@Override | ||
protected boolean handles(Object element) { | ||
if (element instanceof PsiElement psiElement && psiElement.isValid()) { | ||
var project = psiElement.getProject(); | ||
if (Blaze.getProjectType(project) == BlazeImportSettings.ProjectType.UNKNOWN) { | ||
return false; | ||
} | ||
var module = ModuleUtilCore.findModuleForPsiElement(psiElement); | ||
|
||
return module != null && module.getName().equals(".workspace"); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public @Nullable TextWithIcon getModuleTextWithIcon(Object element) { | ||
if (element instanceof PsiElement psiElement && psiElement.isValid()) { | ||
var project = psiElement.getProject(); | ||
var file = PsiUtilCore.getVirtualFile(psiElement); | ||
var module = Optional.ofNullable(ModuleUtilCore.findModuleForPsiElement(psiElement)); | ||
|
||
ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex(); | ||
if (file == null) { | ||
return null; | ||
} | ||
var sourceRoot = index.getSourceRootForFile(file); | ||
var relativeSourceRoot = WorkspaceRoot.fromProject(project).relativize(sourceRoot); | ||
if (relativeSourceRoot == null) { | ||
return null; | ||
} | ||
return new TextWithIcon( | ||
relativeSourceRoot.toString(), | ||
module.map(m -> ModuleType.get(m).getIcon()).orElse(null)); | ||
} | ||
return null; | ||
} | ||
} |