Skip to content

Commit

Permalink
feat: More descriptive location labels (instead of just .workspace) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Pasternak authored Oct 20, 2024
1 parent 9cebd40 commit 160a88e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/src/META-INF/blaze-base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
<postStartupActivity implementation="com.google.idea.blaze.base.formatter.ExternalFormatterCodeStyleManager$Installer"/>
<postStartupActivity implementation="com.google.idea.blaze.base.prefetch.PrefetchProjectInitializer" />
<postStartupActivity implementation="com.google.idea.blaze.base.project.OpenProjectViewStartupActivity"/>
<moduleRendererFactory implementation="com.google.idea.blaze.base.ui.BlazeModuleRendererFactory" order="first"/>


<toolWindow id="Blaze"
Expand Down
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;
}
}

0 comments on commit 160a88e

Please sign in to comment.