Skip to content

Commit

Permalink
project relative imports
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-db committed Mar 8, 2024
1 parent 5169334 commit 53ee61c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.databricks"
version = "1.2"
version = "1.3"

repositories {
mavenCentral()
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/jsonnetplugin/JsonnetImportopReference.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.jsonnetplugin;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


public class JsonnetImportopReference extends PsiReferenceBase<PsiElement> implements PsiPolyVariantReference {

public JsonnetImportopReference(@NotNull PsiElement element, TextRange textRange) {
Expand All @@ -16,6 +18,7 @@ public JsonnetImportopReference(@NotNull PsiElement element, TextRange textRange
@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
VirtualFile projectRoot = ProjectUtil.guessProjectDir(getElement().getProject());
String importText = this.getElement().getLastChild().getText();
if (importText.startsWith("'") || importText.startsWith("\"")) {
importText = importText.substring(1);
Expand All @@ -24,20 +27,30 @@ public ResolveResult[] multiResolve(boolean incompleteCode) {
importText = importText.substring(0, importText.length() - 1);
}

// try and find the import relative to the file that is doing the importing
VirtualFile vf = getElement()
.getContainingFile()
.getOriginalFile()
.getVirtualFile()
.getParent()
.findFileByRelativePath(importText);

// try and find the import relative to the workspace root
if (vf == null && projectRoot != null) {
vf = projectRoot.findFileByRelativePath(importText);
}

// try and find the import relative to bazel output (for generated files)
if (vf == null && projectRoot != null) {
vf = projectRoot.findFileByRelativePath("bazel-bin/" + importText);
}

if (vf != null) {
PsiFile myPsiFile = PsiManager.getInstance(myElement.getProject()).findFile(vf);
if (myPsiFile != null) return new ResolveResult[]{new PsiElementResolveResult(myPsiFile)};
else return new ResolveResult[]{};
} else {
return new ResolveResult[]{};

}

}
Expand Down

0 comments on commit 53ee61c

Please sign in to comment.