-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #13: Adds reference contributor for USD references
This commit adds a new psi.referenceContributor that provides support for references on USD asset/prim path references. You can now Ctrl+Click reference paths, woohoo!! This commit also includes the needed structures to support this new behavior, including an applicationService/Configurable to specify one's usdresolve installation path as well as a localInspection to highlight potentially unresolvable asset references if one's usdresolve installation path isn't configured. The former structure is also the hopeful backbone for future feature additions (like usdc/usdz file support with usdcat).
- Loading branch information
Showing
19 changed files
with
734 additions
and
8 deletions.
There are no files selected for viewing
18 changes: 16 additions & 2 deletions
18
src/main/gen/com/justint/usdidea/lang/psi/impl/usdReferenceItemImpl.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 7 additions & 1 deletion
8
src/main/gen/com/justint/usdidea/lang/psi/usdReferenceItem.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
src/main/java/com/justint/usdidea/inspections/UsdresolvePathInspection.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,72 @@ | ||
package com.justint.usdidea.inspections; | ||
|
||
import com.intellij.codeInspection.*; | ||
import com.intellij.openapi.options.ShowSettingsUtil; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiElementVisitor; | ||
import com.justint.usdidea.lang.psi.usdReferenceItem; | ||
import com.justint.usdidea.settings.USDSettingsConfigurable; | ||
import com.justint.usdidea.settings.USDSettingsState; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.File; | ||
|
||
public class UsdresolvePathInspection extends LocalInspectionTool { | ||
|
||
@NotNull | ||
@Override | ||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) { | ||
// Don't bother the user if they've already configured usdresolve | ||
if (USDSettingsState.getInstance().isUsdresolvePathValid()) return PsiElementVisitor.EMPTY_VISITOR; | ||
else return new PsiElementVisitor() { | ||
|
||
@Override | ||
public void visitElement(@NotNull PsiElement element) { | ||
if (element instanceof usdReferenceItem) { | ||
usdReferenceItem referenceItem = (usdReferenceItem)element; | ||
PsiElement assetReference = referenceItem.getAssetReference(); | ||
if (assetReference != null) { | ||
String assetPathString = assetReference.getText().split("@")[1]; | ||
File assetFile = new File(assetPathString); | ||
if (!isFileRelative(assetPathString) && !assetFile.exists()) { | ||
String description = "Cannot resolve asset reference '" + assetReference.getText() + "' without configuration of usdresolve install path"; | ||
holder.registerProblem( | ||
element, | ||
description, | ||
ProblemHighlightType.WARNING, | ||
new OpenUSDSettingsQuickFix()); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private boolean isFileRelative(@NotNull String assetPathString) { | ||
return assetPathString.contains("./") || assetPathString.contains("../"); | ||
} | ||
|
||
|
||
private static class OpenUSDSettingsQuickFix implements LocalQuickFix { | ||
@NotNull | ||
@Override | ||
public String getFamilyName() { | ||
return "Configure usdresolve path"; | ||
} | ||
|
||
@Override | ||
public boolean startInWriteAction() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) { | ||
showUSDSettingsDialog(project); | ||
} | ||
|
||
public static void showUSDSettingsDialog(@NotNull Project project) { | ||
ShowSettingsUtil.getInstance().showSettingsDialog(project, USDSettingsConfigurable.class); | ||
} | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/justint/usdidea/lang/USDReferenceContributor.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,15 @@ | ||
package com.justint.usdidea.lang; | ||
|
||
import com.intellij.patterns.PlatformPatterns; | ||
import com.intellij.psi.PsiReferenceContributor; | ||
import com.intellij.psi.PsiReferenceRegistrar; | ||
import com.justint.usdidea.lang.psi.impl.USDReferenceReferenceProvider; | ||
import com.justint.usdidea.lang.psi.usdReferenceItem; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class USDReferenceContributor extends PsiReferenceContributor { | ||
@Override | ||
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) { | ||
registrar.registerReferenceProvider(PlatformPatterns.psiElement(usdReferenceItem.class), USDReferenceReferenceProvider.INSTANCE); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/justint/usdidea/lang/psi/USDNamedElement.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,7 @@ | ||
package com.justint.usdidea.lang.psi; | ||
|
||
import com.intellij.psi.PsiNameIdentifierOwner; | ||
|
||
public interface USDNamedElement extends PsiNameIdentifierOwner { | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/com/justint/usdidea/lang/psi/USDReferenceReference.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,102 @@ | ||
package com.justint.usdidea.lang.psi; | ||
|
||
import com.intellij.execution.ExecutionException; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.openapi.vfs.VirtualFileManager; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiManager; | ||
import com.intellij.psi.PsiReferenceBase; | ||
import com.justint.usdidea.lang.psi.impl.USDPsiImplUtil; | ||
import com.justint.usdidea.util.binscripts.USDResolve; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.File; | ||
import java.nio.file.InvalidPathException; | ||
import java.nio.file.Paths; | ||
|
||
public class USDReferenceReference extends PsiReferenceBase<PsiElement> { | ||
public USDReferenceReference(@NotNull PsiElement element, TextRange rangeInElement) { | ||
super(element, rangeInElement); | ||
} | ||
|
||
@Nullable | ||
private PsiFile getResolvedReferenceAssetFile(String referenceLayerPath, File sourceElementFile) { | ||
USDResolve resolver = new USDResolve(referenceLayerPath, sourceElementFile.getParent(), myElement.getProject()); | ||
boolean resolverIsValid = resolver.isUSDInstallValid(); | ||
|
||
String targetLayerPath = null; | ||
|
||
// Attempt to resolve this path as a literal filesystem path first | ||
try { | ||
File targetLayerFile = new File(Paths.get(sourceElementFile.getParent(), referenceLayerPath).toString()); | ||
targetLayerPath = targetLayerFile.getAbsolutePath(); | ||
} | ||
catch (InvalidPathException e) { | ||
// It may be a custom URL path; we'll try the resolver next if possible | ||
} | ||
if (resolverIsValid) { | ||
try { | ||
// Attempt to derive this path from `usdresolve`, if possible | ||
targetLayerPath = resolver.resolvePath(); | ||
} catch (ExecutionException | InterruptedException e) { | ||
// Resolver failed/was interrupted | ||
return null; | ||
} | ||
} | ||
|
||
if (targetLayerPath == null) return null; | ||
|
||
VirtualFile layerVf = VirtualFileManager.getInstance().findFileByUrl("file://" + targetLayerPath); | ||
if (layerVf == null) return null; | ||
|
||
return PsiManager.getInstance(myElement.getProject()).findFile(layerVf); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public PsiElement resolve() { | ||
PsiFile resolvedAssetFile = myElement.getContainingFile(); | ||
String referencePath = myElement.getText(); | ||
|
||
boolean hasAssetPath = false; | ||
boolean hasPrimPath = false; | ||
|
||
if (referencePath.contains("@")) { | ||
hasAssetPath = true; | ||
} | ||
if (referencePath.contains("<")) { | ||
hasPrimPath = true; | ||
} | ||
|
||
if (!hasAssetPath && !hasPrimPath) { | ||
return null; | ||
} | ||
if (hasAssetPath) { | ||
String assetPath = referencePath.split("@")[1]; | ||
|
||
String sourceElementFilePath = myElement.getContainingFile().getVirtualFile().getPath(); | ||
File sourceElementFile = new File(sourceElementFilePath); | ||
|
||
resolvedAssetFile = getResolvedReferenceAssetFile(assetPath, sourceElementFile); | ||
if (resolvedAssetFile == null) return null; | ||
} | ||
if (hasPrimPath) { | ||
if (resolvedAssetFile instanceof USDFile) { | ||
String primPath = referencePath.split("<")[1].replace(">", ""); | ||
PsiElement foundPrim = USDPsiImplUtil.findPrimInLayerFromPath((USDFile)resolvedAssetFile, primPath); | ||
if (foundPrim != null) return foundPrim; | ||
else return null; | ||
} | ||
else { | ||
// We have a prim path pointing into a non-USD layer asset file... strange, we'll just return no result | ||
return null; | ||
} | ||
} | ||
else { | ||
return resolvedAssetFile; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/justint/usdidea/lang/psi/impl/USDNamedElementImpl.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,18 @@ | ||
package com.justint.usdidea.lang.psi.impl; | ||
|
||
import com.intellij.extapi.psi.ASTWrapperPsiElement; | ||
import com.intellij.lang.ASTNode; | ||
import com.intellij.psi.PsiReference; | ||
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public abstract class USDNamedElementImpl extends ASTWrapperPsiElement { | ||
public USDNamedElementImpl(@NotNull ASTNode node) { | ||
super(node); | ||
} | ||
|
||
@Override | ||
public PsiReference[] getReferences() { | ||
return ReferenceProvidersRegistry.getReferencesFromProviders(this); | ||
} | ||
} |
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
Oops, something went wrong.