Skip to content

Commit c795576

Browse files
#21: Fix NoClassDefFoundError in PyCham, WebStorm and other non-IDEA IDEs.
FilePathReferenceProvider is not a part of PyCharm, WebStorm default packages, it is available only in IntelliJ IDEA. FilePathReferenceProvider and some dependencies are copied to this project.
1 parent 35959f4 commit c795576

File tree

5 files changed

+439
-8
lines changed

5 files changed

+439
-8
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Available idea versions:
22
# https://www.jetbrains.com/intellij-repository/releases
33
# https://www.jetbrains.com/intellij-repository/snapshots
4-
version=0.4.0
4+
version=0.4.1
55
ideaVersion=145.258.11
66
# https://intellij-support.jetbrains.com/hc/en-us/articles/206544879-Selecting-the-JDK-version-the-IDE-will-run-under
77
# Java 8 is required to run IntelliJ IDEA starting from version 16

src/main/java/io/protostuff/jetbrains/plugin/psi/FileReferenceNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import com.intellij.psi.PsiFileSystemItem;
1010
import com.intellij.psi.PsiManager;
1111
import com.intellij.psi.PsiReference;
12-
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FilePathReferenceProvider;
1312
import io.protostuff.compiler.parser.Util;
13+
import io.protostuff.jetbrains.plugin.reference.file.FilePathReferenceProvider;
1414
import org.antlr.jetbrains.adapter.psi.ANTLRPsiNode;
1515
import org.jetbrains.annotations.NotNull;
1616
import org.jetbrains.annotations.Nullable;
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright 2000-2014 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.protostuff.jetbrains.plugin.reference.file;
17+
18+
import com.intellij.openapi.module.Module;
19+
import com.intellij.openapi.module.ModuleUtilCore;
20+
import com.intellij.openapi.roots.ModuleRootManager;
21+
import com.intellij.openapi.util.Condition;
22+
import com.intellij.openapi.util.TextRange;
23+
import com.intellij.openapi.vfs.VirtualFile;
24+
import com.intellij.psi.*;
25+
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference;
26+
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet;
27+
import com.intellij.util.ProcessingContext;
28+
import com.intellij.util.containers.ContainerUtil;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
31+
32+
import java.util.Collection;
33+
import java.util.Collections;
34+
import java.util.Set;
35+
36+
/**
37+
* @author cdr
38+
*/
39+
public class FilePathReferenceProvider extends PsiReferenceProvider {
40+
41+
private final boolean myEndingSlashNotAllowed;
42+
43+
public FilePathReferenceProvider() {
44+
this(true);
45+
}
46+
47+
public FilePathReferenceProvider(boolean endingSlashNotAllowed) {
48+
myEndingSlashNotAllowed = endingSlashNotAllowed;
49+
}
50+
51+
@NotNull
52+
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, String text, int offset, final boolean soft) {
53+
return getReferencesByElement(element, text, offset, soft, Module.EMPTY_ARRAY);
54+
}
55+
56+
@NotNull
57+
public PsiReference[] getReferencesByElement(@NotNull PsiElement element,
58+
String text,
59+
int offset,
60+
final boolean soft,
61+
@NotNull final Module... forModules) {
62+
return new FileReferenceSet(text, element, offset, this, true, myEndingSlashNotAllowed) {
63+
64+
65+
@Override
66+
protected boolean isSoft() {
67+
return soft;
68+
}
69+
70+
@Override
71+
public boolean isAbsolutePathReference() {
72+
return true;
73+
}
74+
75+
@Override
76+
public boolean couldBeConvertedTo(boolean relative) {
77+
return !relative;
78+
}
79+
80+
@Override
81+
public boolean absoluteUrlNeedsStartSlash() {
82+
final String s = getPathString();
83+
return s != null && !s.isEmpty() && s.charAt(0) == '/';
84+
}
85+
86+
@Override
87+
@NotNull
88+
public Collection<PsiFileSystemItem> computeDefaultContexts() {
89+
if (forModules.length > 0) {
90+
Set<PsiFileSystemItem> rootsForModules = ContainerUtil.newLinkedHashSet();
91+
for (Module forModule : forModules) {
92+
rootsForModules.addAll(getRoots(forModule, true));
93+
}
94+
return rootsForModules;
95+
}
96+
97+
return getRoots(ModuleUtilCore.findModuleForPsiElement(getElement()), true);
98+
}
99+
100+
@Override
101+
public FileReference createFileReference(final TextRange range, final int index, final String text) {
102+
return FilePathReferenceProvider.this.createFileReference(this, range, index, text);
103+
}
104+
105+
@Override
106+
protected Condition<PsiFileSystemItem> getReferenceCompletionFilter() {
107+
return new Condition<PsiFileSystemItem>() {
108+
@Override
109+
public boolean value(final PsiFileSystemItem element) {
110+
return isPsiElementAccepted(element);
111+
}
112+
};
113+
}
114+
}.getAllReferences();
115+
}
116+
117+
@Override
118+
public boolean acceptsTarget(@NotNull PsiElement target) {
119+
return target instanceof PsiFileSystemItem;
120+
}
121+
122+
protected boolean isPsiElementAccepted(PsiElement element) {
123+
return !(element instanceof PsiCompiledElement);
124+
}
125+
126+
protected FileReference createFileReference(FileReferenceSet referenceSet, final TextRange range, final int index, final String text) {
127+
return new FileReference(referenceSet, range, index, text);
128+
}
129+
130+
@Override
131+
@NotNull
132+
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
133+
String text = null;
134+
if (element instanceof PsiLiteralExpression) {
135+
Object value = ((PsiLiteralExpression) element).getValue();
136+
if (value instanceof String) {
137+
text = (String) value;
138+
}
139+
}
140+
//else if (element instanceof XmlAttributeValue) {
141+
// text = ((XmlAttributeValue)element).getValue();
142+
//}
143+
if (text == null) return PsiReference.EMPTY_ARRAY;
144+
return getReferencesByElement(element, text, 1, true);
145+
}
146+
147+
@NotNull
148+
public static Collection<PsiFileSystemItem> getRoots(@Nullable final Module thisModule, boolean includingClasses) {
149+
if (thisModule == null) return Collections.emptyList();
150+
151+
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(thisModule);
152+
Set<PsiFileSystemItem> result = ContainerUtil.newLinkedHashSet();
153+
final PsiManager psiManager = PsiManager.getInstance(thisModule.getProject());
154+
if (includingClasses) {
155+
VirtualFile[] libraryUrls = moduleRootManager.orderEntries().getAllLibrariesAndSdkClassesRoots();
156+
for (VirtualFile file : libraryUrls) {
157+
PsiDirectory directory = psiManager.findDirectory(file);
158+
if (directory != null) {
159+
result.add(directory);
160+
}
161+
}
162+
}
163+
164+
VirtualFile[] sourceRoots = moduleRootManager.orderEntries().recursively()
165+
.withoutSdk().withoutLibraries()
166+
.sources().usingCache().getRoots();
167+
for (VirtualFile root : sourceRoots) {
168+
final PsiDirectory directory = psiManager.findDirectory(root);
169+
if (directory != null) {
170+
result.add(directory);
171+
}
172+
}
173+
return result;
174+
}
175+
}

0 commit comments

Comments
 (0)