-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added navigation/completion/rename support for Nix paths and path-lik…
…e strings.
- Loading branch information
kef
committed
May 31, 2022
1 parent
41cd249
commit 202cf94
Showing
7 changed files
with
129 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
22 changes: 22 additions & 0 deletions
22
src/main/java/org/nixos/idea/psi/impl/NixLiteralReferencingElementImpl.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,22 @@ | ||
package org.nixos.idea.psi.impl; | ||
|
||
import com.intellij.lang.ASTNode; | ||
import com.intellij.psi.PsiReference; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.nixos.idea.reference.ReferenceUtil; | ||
|
||
public class NixLiteralReferencingElementImpl extends NixExprSimpleImpl { | ||
public NixLiteralReferencingElementImpl(@NotNull ASTNode node) { | ||
super(node); | ||
} | ||
|
||
@Override | ||
public PsiReference @NotNull [] getReferences() { | ||
return ReferenceUtil.getReferences(this); | ||
} | ||
|
||
@Override | ||
public PsiReference getReference() { | ||
return ReferenceUtil.getReference(this); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/nixos/idea/psi/impl/NixStringReferencingElementImpl.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,22 @@ | ||
package org.nixos.idea.psi.impl; | ||
|
||
import com.intellij.lang.ASTNode; | ||
import com.intellij.psi.PsiReference; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.nixos.idea.reference.ReferenceUtil; | ||
|
||
public class NixStringReferencingElementImpl extends NixStringPartImpl { | ||
public NixStringReferencingElementImpl(@NotNull ASTNode node) { | ||
super(node); | ||
} | ||
|
||
@Override | ||
public PsiReference @NotNull [] getReferences() { | ||
return ReferenceUtil.getReferences(this); | ||
} | ||
|
||
@Override | ||
public PsiReference getReference() { | ||
return ReferenceUtil.getReference(this); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/org/nixos/idea/reference/NixReferenceContributor.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,53 @@ | ||
package org.nixos.idea.reference; | ||
|
||
import com.intellij.psi.*; | ||
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference; | ||
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet; | ||
import com.intellij.util.ProcessingContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.nixos.idea.psi.NixLiteral; | ||
import org.nixos.idea.psi.NixStringText; | ||
|
||
import java.util.Arrays; | ||
|
||
import static com.intellij.patterns.PlatformPatterns.psiElement; | ||
import static com.intellij.patterns.StandardPatterns.or; | ||
import static com.intellij.patterns.StandardPatterns.string; | ||
|
||
public class NixReferenceContributor extends PsiReferenceContributor { | ||
|
||
// Same as the pattern in Nix.flex. | ||
private static final String NIX_PATH_REGEX = "[a-zA-Z0-9._+-]*(\\/[a-zA-Z0-9._+-]+)+\\/?"; | ||
|
||
@Override | ||
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar psiReferenceRegistrar) { | ||
psiReferenceRegistrar.registerReferenceProvider( | ||
or( | ||
psiElement(NixLiteral.class), | ||
psiElement(NixStringText.class) | ||
.withText(string().matches(NIX_PATH_REGEX)) | ||
), | ||
new NixReferenceProvider() | ||
); | ||
} | ||
|
||
private static class NixReferenceProvider extends PsiReferenceProvider { | ||
|
||
@Override | ||
public boolean acceptsTarget(@NotNull PsiElement target) { | ||
return target instanceof PsiFileSystemItem; | ||
} | ||
|
||
@Override | ||
public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement psiElement, | ||
@NotNull ProcessingContext processingContext) { | ||
FileReferenceSet fileReferenceSet = new FileReferenceSet(psiElement); | ||
FileReference[] references = fileReferenceSet.getAllReferences(); | ||
FileReference lastReference = fileReferenceSet.getLastReference(); | ||
FileReference defaultNixReference = fileReferenceSet.createFileReference(lastReference.getRangeInElement(), lastReference.getIndex() + 1, "default.nix"); | ||
PsiReference[] allReferences = Arrays.copyOf(references, references.length + 1); | ||
allReferences[allReferences.length - 1] = defaultNixReference; | ||
return allReferences; | ||
} | ||
} | ||
} |
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 org.nixos.idea.reference; | ||
|
||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiReference; | ||
import com.intellij.psi.PsiReferenceService; | ||
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry; | ||
import com.intellij.util.ArrayUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class ReferenceUtil { | ||
public static PsiReference @NotNull [] getReferences(@NotNull PsiElement element) { | ||
return ReferenceProvidersRegistry.getReferencesFromProviders(element, PsiReferenceService.Hints.NO_HINTS); | ||
} | ||
|
||
public static PsiReference getReference(@NotNull PsiElement element) { | ||
return ArrayUtil.getFirstElement(getReferences(element)); | ||
} | ||
} |
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
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