use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.
the class MavenPathReferenceConverter method createReferences.
public static PsiReference[] createReferences(final DomElement genericDomValue, PsiElement element, @NotNull final Condition<PsiFileSystemItem> fileFilter, boolean isAbsolutePath) {
ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
TextRange range = manipulator.getRangeInElement(element);
String text = range.substring(element.getText());
FileReferenceSet set = new FileReferenceSet(text, element, range.getStartOffset(), null, SystemInfo.isFileSystemCaseSensitive, false) {
private MavenDomProjectModel model;
@Override
protected Condition<PsiFileSystemItem> getReferenceCompletionFilter() {
return fileFilter;
}
@Override
protected boolean isSoft() {
return true;
}
@Override
public FileReference createFileReference(TextRange range, int index, String text) {
return new FileReference(this, range, index, text) {
@Override
protected void innerResolveInContext(@NotNull String text, @NotNull PsiFileSystemItem context, Collection<ResolveResult> result, boolean caseSensitive) {
if (model == null) {
DomElement rootElement = DomUtil.getFileElement(genericDomValue).getRootElement();
if (rootElement instanceof MavenDomProjectModel) {
model = (MavenDomProjectModel) rootElement;
}
}
String resolvedText = model == null ? text : MavenPropertyResolver.resolve(text, model);
if (resolvedText.equals(text)) {
if (getIndex() == 0 && resolvedText.length() == 2 && resolvedText.charAt(1) == ':') {
// it's root on windows, e.g. "C:"
VirtualFile file = LocalFileSystem.getInstance().findFileByPath(resolvedText + '/');
if (file != null) {
PsiDirectory psiDirectory = context.getManager().findDirectory(file);
if (psiDirectory != null) {
result.add(new PsiElementResolveResult(psiDirectory));
}
}
} else {
super.innerResolveInContext(resolvedText, context, result, caseSensitive);
}
} else {
VirtualFile contextFile = context.getVirtualFile();
if (contextFile == null)
return;
VirtualFile file = null;
if (getIndex() == 0) {
file = LocalFileSystem.getInstance().findFileByPath(resolvedText);
}
if (file == null) {
file = LocalFileSystem.getInstance().findFileByPath(contextFile.getPath() + '/' + resolvedText);
}
if (file != null) {
PsiFileSystemItem res = file.isDirectory() ? context.getManager().findDirectory(file) : context.getManager().findFile(file);
if (res != null) {
result.add(new PsiElementResolveResult(res));
}
}
}
}
};
}
};
if (isAbsolutePath) {
set.addCustomization(FileReferenceSet.DEFAULT_PATH_EVALUATOR_OPTION, file -> {
VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) {
return FileReferenceSet.ABSOLUTE_TOP_LEVEL.fun(file);
}
virtualFile = VfsUtil.getRootFile(virtualFile);
PsiDirectory root = file.getManager().findDirectory(virtualFile);
if (root == null) {
return FileReferenceSet.ABSOLUTE_TOP_LEVEL.fun(file);
}
return Collections.<PsiFileSystemItem>singletonList(root);
});
}
return set.getAllReferences();
}
use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.
the class PyStringLiteralFileReferenceSet method getFileReferences.
@NotNull
private List<FileReference> getFileReferences(@NotNull PyStringLiteralExpression expression) {
final String value = expression.getStringValue();
final Matcher matcher = DELIMITERS.matcher(value);
int start = 0;
int index = 0;
final List<FileReference> results = new ArrayList<>();
while (matcher.find()) {
final String s = value.substring(start, matcher.start());
if (!s.isEmpty()) {
final TextRange range = TextRange.create(expression.valueOffsetToTextOffset(start), expression.valueOffsetToTextOffset(matcher.start()));
results.add(createFileReference(range, index++, s));
}
start = matcher.end();
}
final String s = value.substring(start);
if (!s.isEmpty()) {
final TextRange range = TextRange.create(expression.valueOffsetToTextOffset(start), expression.valueOffsetToTextOffset(value.length()));
results.add(createFileReference(range, index, s));
}
return results;
}
use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.
the class HtmlFileImpl method resolveFileReference.
@Nullable
@Override
public PsiFileSystemItem resolveFileReference(@NotNull FileReference reference, @NotNull String name) {
VirtualFile file = getVirtualFile();
if (!(file instanceof HttpVirtualFile)) {
return null;
}
VirtualFile parent = file;
if (!parent.isDirectory()) {
parent = parent.getParent();
if (parent == null) {
parent = file;
}
}
VirtualFile childFile = parent.findChild(name);
HttpFileSystem fileSystem = (HttpFileSystem) parent.getFileSystem();
if (childFile == null) {
childFile = fileSystem.createChild(parent, name, !reference.isLast());
}
if (childFile.isDirectory()) {
// pre create children
VirtualFile childParent = childFile;
FileReference[] references = reference.getFileReferenceSet().getAllReferences();
for (int i = reference.getIndex() + 1, n = references.length; i < n; i++) {
FileReference childReference = references[i];
childParent = fileSystem.createChild(childParent, childReference.decode(childReference.getText()), i != (n - 1));
}
return getManager().findDirectory(childFile);
} else {
return getManager().findFile(childFile);
}
}
use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.
the class MoveFilesOrDirectoriesProcessor method retargetUsages.
protected void retargetUsages(UsageInfo[] usages, Map<PsiElement, PsiElement> oldToNewMap) {
final List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
for (UsageInfo usageInfo : usages) {
if (usageInfo instanceof MyUsageInfo) {
final MyUsageInfo info = (MyUsageInfo) usageInfo;
final PsiElement element = myElementsToMove[info.myIndex];
if (info.getReference() instanceof FileReference || info.getReference() instanceof PsiDynaReference) {
final PsiElement usageElement = info.getElement();
if (usageElement != null) {
final PsiFile usageFile = usageElement.getContainingFile();
final PsiFile psiFile = usageFile.getViewProvider().getPsi(usageFile.getViewProvider().getBaseLanguage());
if (psiFile != null && psiFile.equals(element)) {
// already processed in MoveFilesOrDirectoriesUtil.doMoveFile
continue;
}
}
}
final PsiElement refElement = info.myReference.getElement();
if (refElement != null && refElement.isValid()) {
info.myReference.bindToElement(element);
}
} else if (usageInfo instanceof NonCodeUsageInfo) {
nonCodeUsages.add((NonCodeUsageInfo) usageInfo);
}
}
for (PsiFile movedFile : myFoundUsages.keySet()) {
MoveFileHandler.forElement(movedFile).retargetUsages(myFoundUsages.get(movedFile), oldToNewMap);
}
myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
}
use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.
the class ProjectIconsAccessor method resolveIconFile.
@Nullable
public VirtualFile resolveIconFile(PsiType type, @Nullable PsiExpression initializer) {
if (initializer == null || !initializer.isValid() || !isIconClassType(type)) {
return null;
}
final List<FileReference> refs = new ArrayList<>();
initializer.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof PsiLiteralExpression) {
for (PsiReference ref : element.getReferences()) {
if (ref instanceof FileReference) {
refs.add((FileReference) ref);
}
}
}
super.visitElement(element);
}
});
for (FileReference ref : refs) {
final PsiFileSystemItem psiFileSystemItem = ref.resolve();
VirtualFile file = null;
if (psiFileSystemItem == null) {
final ResolveResult[] results = ref.multiResolve(false);
for (ResolveResult result : results) {
final PsiElement element = result.getElement();
if (element instanceof PsiBinaryFile) {
file = ((PsiFile) element).getVirtualFile();
break;
}
}
} else {
file = psiFileSystemItem.getVirtualFile();
}
if (file == null || file.isDirectory() || !isIconFileExtension(file.getExtension()) || file.getLength() > ICON_MAX_SIZE) {
continue;
}
return file;
}
return null;
}
Aggregations