@Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { final int offset = editor.getCaretModel().getOffset(); final PsiElement element = file.findElementAt(offset); if (element == null || element instanceof PsiWhiteSpace) { return false; } final PsiReference reference = file.findReferenceAt(offset); final FileReference fileReference = reference == null ? null : FileReference.findFileReference(reference); if (fileReference != null) { final FileReferenceSet set = fileReference.getFileReferenceSet(); final FileReference lastReference = set.getLastReference(); return set.couldBeConvertedTo(isConvertToRelative()) && lastReference != null && (!isConvertToRelative() && !set.isAbsolutePathReference() || isConvertToRelative() && set.isAbsolutePathReference()) && lastReference.resolve() != null; } return false; }
public void testInjectReference() throws Exception { myFixture.configureByText("foo.xml", "<foo xmlns=\"http://foo.bar\" \n" + " xxx=\"ba<caret>r\"/>"); assertNull(myFixture.getReferenceAtCaretPosition()); assertTrue(new InjectLanguageAction().isAvailable(getProject(), myFixture.getEditor(), myFixture.getFile())); assertFalse(new UnInjectLanguageAction().isAvailable(getProject(), myFixture.getEditor(), myFixture.getFile())); InjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile(), new FileReferenceInjector()); assertTrue(myFixture.getReferenceAtCaretPosition() instanceof FileReference); assertFalse(new InjectLanguageAction().isAvailable(getProject(), myFixture.getEditor(), myFixture.getFile())); assertTrue(new UnInjectLanguageAction().isAvailable(getProject(), myFixture.getEditor(), myFixture.getFile())); myFixture.configureByText("bar.xml", "<foo xmlns=\"<error descr=\"URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)\">http://foo.bar</error>\" \n" + " xxx=\"<error descr=\"Cannot resolve file 'bar'\">b<caret>ar</error>\"/>"); myFixture.testHighlighting(); UnInjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile()); assertNull(myFixture.getReferenceAtCaretPosition()); }
public void testSurviveSerialization() throws Exception { myFixture.configureByText("foo.xml", "<foo xmlns=\"http://foo.bar\" \n" + " xxx=\"ba<caret>r\"/>"); assertNull(myFixture.getReferenceAtCaretPosition()); InjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile(), new FileReferenceInjector()); assertTrue(myFixture.getReferenceAtCaretPosition() instanceof FileReference); Configuration configuration = Configuration.getInstance(); Element element = configuration.getState(); configuration.loadState(element); ((PsiModificationTrackerImpl)PsiManager.getInstance(getProject()).getModificationTracker()).incCounter(); assertTrue(myFixture.getReferenceAtCaretPosition() instanceof FileReference); UnInjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile()); assertNull(myFixture.getReferenceAtCaretPosition()); }
public void testInjectIntoJava() throws Exception { myFixture.configureByText("Foo.java", "class Foo {\n" + " String bar() {\n" + " return \"ba<caret>r.xml\";\n" + " } \n" + "}"); assertNull(getInjectedReferences()); InjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile(), new FileReferenceInjector()); PsiReference[] references = getInjectedReferences(); PsiReference reference = assertOneElement(references); assertTrue(reference instanceof FileReference); UnInjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile()); assertNull(getInjectedReferences()); }
@Nullable private static FileReference findFileReference(@NotNull final PsiReference original) { if (original instanceof PsiMultiReference) { final PsiMultiReference multiReference = (PsiMultiReference)original; for (PsiReference reference : multiReference.getReferences()) { if (reference instanceof FileReference) { return (FileReference)reference; } } } else if (original instanceof FileReferenceOwner) { final PsiFileReference fileReference = ((FileReferenceOwner)original).getLastFileReference(); if (fileReference instanceof FileReference) { return (FileReference)fileReference; } } return null; }
@Override public PsiFile resolveFile() { final String includeText = getIncludeText(); if(includeText == null) { return null; } final FileReference[] references = new FileReferenceSet(includeText, this, 0, null, SystemInfo.isFileSystemCaseSensitive).getAllReferences(); if(references != null && references.length > 0) { final PsiElement element = references[references.length - 1].resolve(); if(element instanceof PsiFile) { return (PsiFile) element; } } return null; }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { final int offset = editor.getCaretModel().getOffset(); final PsiElement element = file.findElementAt(offset); if(element == null || element instanceof PsiWhiteSpace) { return false; } final PsiReference reference = file.findReferenceAt(offset); final FileReference fileReference = reference == null ? null : FileReference.findFileReference(reference); if(fileReference != null) { final FileReferenceSet set = fileReference.getFileReferenceSet(); final FileReference lastReference = set.getLastReference(); return set.couldBeConvertedTo(isConvertToRelative()) && lastReference != null && (!isConvertToRelative() && !set.isAbsolutePathReference() || isConvertToRelative() && set.isAbsolutePathReference()) && lastReference.resolve() != null; } return false; }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { if(!FileModificationService.getInstance().prepareFileForWrite(file)) { return; } final PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset()); final FileReference fileReference = reference == null ? null : FileReference.findFileReference(reference); if(fileReference != null) { final FileReference lastReference = fileReference.getFileReferenceSet().getLastReference(); if(lastReference != null) { lastReference.bindToElement(lastReference.resolve(), !isConvertToRelative()); } } }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { if (!FileModificationService.getInstance().prepareFileForWrite(file)) return; final PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset()); final FileReference fileReference = reference == null ? null : FileReference.findFileReference(reference); if (fileReference != null) { final FileReference lastReference = fileReference.getFileReferenceSet().getLastReference(); if (lastReference != null) lastReference.bindToElement(lastReference.resolve(), !isConvertToRelative()); } }
@Override public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { for (PsiReference reference : myReferences) { if (reference instanceof FileReference) { return reference.bindToElement(element); } } return myElement; }
protected void retargetUsages(UsageInfo[] usages, Map<PsiElement, PsiElement> oldToNewMap) { final List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<NonCodeUsageInfo>(); 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)) { continue; // already processed in MoveFilesOrDirectoriesUtil.doMoveFile } } } 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()]); }
@Override protected void reparse() { //noinspection ConstantConditions if (myStringLiteralExpression != null) { final List<FileReference> references = getFileReferences(myStringLiteralExpression); myReferences = references.toArray(new FileReference[references.size()]); } }
@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); } }
@Override public boolean createReferences(@NotNull final PsiElement psiElement, @NotNull final List<PsiReference> references, final boolean soft) { final TextRange range = ElementManipulators.getValueTextRange(psiElement); final String elementText = psiElement.getText(); final int anchorOffset = elementText.indexOf('#'); if (anchorOffset == -1) { return false; } final boolean dynamic = isDynamic(psiElement, anchorOffset + 1, elementText); if (dynamic) { return false; } FileReference fileReference = null; if (range.getStartOffset() != anchorOffset) { fileReference = findFileReference(references); if (fileReference == null || fileReference.resolve() == null) { return false; } } final int pos = elementText.indexOf('?', anchorOffset); final String anchor; try { int endIndex = pos != -1 ? pos : range.getEndOffset(); if (endIndex <= anchorOffset) { endIndex = anchorOffset + 1; } anchor = elementText.substring(anchorOffset + 1, endIndex); } catch (StringIndexOutOfBoundsException e) { throw new RuntimeException(elementText, e); } final AnchorReferenceImpl anchorReference = new AnchorReferenceImpl(anchor, fileReference, psiElement, anchorOffset + 1, soft); references.add(anchorReference); return false; }
@Nullable private static FileReference findFileReference(final List<PsiReference> references) { FileReference fileReference = null; for (PsiReference reference : references) { if (reference instanceof FileReference) { fileReference = ((FileReference)reference).getFileReferenceSet().getLastReference(); break; } } return fileReference; }
AnchorReferenceImpl(final String anchor, @Nullable final FileReference psiReference, final PsiElement element, final int offset, final boolean soft) { myAnchor = anchor; myFileReference = psiReference; myElement = element; myOffset = offset; mySoft = soft; }
public static PsiReference[] restrict(FileReferenceSet set, final Condition<PsiFile> cond, final Boolean soft) { final FileReference[] references = set.getAllReferences(); return ContainerUtil.map2Array(references, PsiReference.class, new NotNullFunction<FileReference, PsiReference>() { @Override @NotNull public PsiReference fun(FileReference fileReference) { return new MyFileReference(fileReference, cond, soft); } }); }
public AntDomFileReferenceSet(final GenericAttributeValue attribValue, final String pathSubstring, int beginOffset, boolean validateFileRefs) { super(cutTrailingSlash(FileUtil.toSystemIndependentName(pathSubstring)), attribValue.getXmlAttributeValue(), ElementManipulators.getOffsetInElement(attribValue.getXmlAttributeValue()) + beginOffset, null, SystemInfo.isFileSystemCaseSensitive ); myValue = attribValue; for (FileReference reference : getAllReferences()) { if (reference instanceof AntDomReference) { ((AntDomReference)reference).setShouldBeSkippedByAnnotator(!validateFileRefs); } } }
public void testInjectIntoTagValue() throws Exception { myFixture.configureByText("foo.xml", "<foo xmlns=\"http://foo.bar\" <bar>x<caret>xx</bar>/>"); assertNull(myFixture.getReferenceAtCaretPosition()); InjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile(), new FileReferenceInjector()); assertTrue(myFixture.getReferenceAtCaretPosition() instanceof FileReference); UnInjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile()); assertNull(myFixture.getReferenceAtCaretPosition()); }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { final PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset()); final FileReference fileReference = reference == null ? null : findFileReference(reference); if (fileReference != null) { final FileReferenceSet set = fileReference.getFileReferenceSet(); final FileReference lastReference = set.getLastReference(); return set.couldBeConvertedTo(isConvertToRelative()) && lastReference != null && (!isConvertToRelative() && !set.isAbsolutePathReference() || isConvertToRelative() && set.isAbsolutePathReference()) && lastReference.resolve() != null; } return false; }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { if (!FileModificationService.getInstance().prepareFileForWrite(file)) return; final PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset()); final FileReference fileReference = reference == null ? null : findFileReference(reference); if (fileReference != null) { final FileReference lastReference = fileReference.getFileReferenceSet().getLastReference(); if (lastReference != null) lastReference.bindToElement(lastReference.resolve(), !isConvertToRelative()); } }
public boolean createReferences(@NotNull final PsiElement psiElement, final @NotNull List<PsiReference> references, final boolean soft) { final TextRange range = ElementManipulators.getValueTextRange(psiElement); final String elementText = psiElement.getText(); final int anchorOffset = elementText.indexOf('#'); if (anchorOffset == -1) { return false; } final boolean dynamic = isDynamic(psiElement, anchorOffset + 1, elementText); if (dynamic) { return false; } FileReference fileReference = null; if (range.getStartOffset() != anchorOffset) { fileReference = findFileReference(references); if (fileReference == null || fileReference.resolve() == null) { return false; } } final int pos = elementText.indexOf('?', anchorOffset); final String anchor; try { int endIndex = pos != -1 ? pos : range.getEndOffset(); if (endIndex <= anchorOffset) { endIndex = anchorOffset + 1; } anchor = elementText.substring(anchorOffset + 1, endIndex); } catch (StringIndexOutOfBoundsException e) { throw new RuntimeException(elementText, e); } final AnchorReference anchorReference = new AnchorReference(anchor, fileReference, psiElement, anchorOffset + 1, soft); references.add(anchorReference); return false; }
AnchorReference(final String anchor, @Nullable final FileReference psiReference, final PsiElement element, final int offset, final boolean soft) { myAnchor = anchor; myFileReference = psiReference; myElement = element; myOffset = offset; mySoft = soft; }
public static PsiReference[] restrict(FileReferenceSet set, final Condition<PsiFile> cond, final Boolean soft) { final FileReference[] references = set.getAllReferences(); return ContainerUtil.map2Array(references, PsiReference.class, new NotNullFunction<FileReference, PsiReference>() { @NotNull public PsiReference fun(FileReference fileReference) { return new MyFileReference(fileReference, cond, soft); } }); }
/** * Returns last reference of the current element's references. * * @return last {@link FileReference} */ @Nullable public FileReference getLastReference() { FileReference lastReference = super.getLastReference(); if (lastReference != null && lastReference.getCanonicalText().endsWith(getSeparatorString())) { return this.myReferences != null && this.myReferences.length > 1 ? this.myReferences[this.myReferences.length - 2] : null; } return lastReference; }
public AntDomFileReferenceSet(final GenericAttributeValue attribValue, final String pathSubstring, int beginOffset, boolean validateFileRefs) { super(cutTrailingSlash(FileUtil.toSystemIndependentName(pathSubstring)), attribValue.getXmlAttributeValue(), ElementManipulators.getOffsetInElement(attribValue.getXmlAttributeValue()) + beginOffset, null, SystemInfo.isFileSystemCaseSensitive); myValue = attribValue; for(FileReference reference : getAllReferences()) { if(reference instanceof AntDomReference) { ((AntDomReference) reference).setShouldBeSkippedByAnnotator(!validateFileRefs); } } }
@Override public PsiElement bindToElement(@Nonnull PsiElement element) throws IncorrectOperationException { for (PsiReference reference : myReferences) { if (reference instanceof FileReference) { return reference.bindToElement(element); } } return myElement; }
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)) { continue; // already processed in MoveFilesOrDirectoriesUtil.doMoveFile } } } 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()]); }
public boolean createReferences(@NotNull final PsiElement psiElement, final @NotNull List<PsiReference> references, final boolean soft) { final TextRange range = ElementManipulators.getValueTextRange(psiElement); final String elementText = psiElement.getText(); final int anchorOffset = elementText.indexOf('#'); if (anchorOffset == -1) { return false; } final boolean dynamic = isDynamic(psiElement, anchorOffset + 1, elementText); if (dynamic) { return false; } FileReference fileReference = null; if (range.getStartOffset() != anchorOffset) { fileReference = findFileReference(references); if (fileReference == null || fileReference.resolve() == null) { return false; } } final int pos = elementText.indexOf('?', anchorOffset); final String anchor; try { int endIndex = pos != -1 ? pos : range.getEndOffset(); if (endIndex <= anchorOffset) { endIndex = anchorOffset + 1; } anchor = elementText.substring(anchorOffset + 1, endIndex); } catch (StringIndexOutOfBoundsException e) { throw new RuntimeException(elementText, e); } final AnchorReferenceImpl anchorReferenceImpl = new AnchorReferenceImpl(anchor, fileReference, psiElement, anchorOffset + 1, soft); references.add(anchorReferenceImpl); return false; }
@Nullable static VtlFileReferenceSet findVtlFileReferenceSet(@NotNull PsiElement element) { for (PsiReference ref : element.getReferences()) { if (!(ref instanceof FileReference)) { continue; } FileReferenceSet refSet = ((FileReference)ref).getFileReferenceSet(); if (refSet instanceof VtlFileReferenceSet && refSet.getLastReference() != null) { return (VtlFileReferenceSet)refSet; } } return null; }
@NotNull public static FileReference[] getFileReferences(@NotNull String text, @NotNull PsiElement element, int startOffset, final boolean considerVelocityProperties) { FileReferenceSet set = considerVelocityProperties ? new VtlFileReferenceSet(text, element, startOffset) : new SoftFileReferenceSet(text, element, startOffset); return set.getAllReferences(); }
@Nullable private static <T extends PsiFile> T findFile(PsiComment comment, String text, @Nullable String filePath, Class<T> fileClass) { if(filePath == null) { return null; } final FileReference[] fileReferences = PsiUtil.getFileReferences(filePath, comment, text.indexOf(filePath), false); return PsiUtil.findFile(fileReferences, fileClass); }
private static boolean findAndAddReferencesByElement(@NotNull final PsiElement element, @NotNull String startMarker, @NotNull String endMarker, @NotNull Collection<PsiReference> collection) { final String text = element.getText(); TextRange range = PsiUtil.findRange(text, startMarker, endMarker); if (range == null) { return false; } final String filePath = range.substring(text); FileReference[] fileReferences = PsiUtil.getFileReferences(filePath, element, range.getStartOffset(), false); return collection.addAll(Arrays.asList(fileReferences)); }
protected FileReference createFileReference(FileReferenceSet referenceSet, final TextRange range, final int index, final String text) { return new FileReference(referenceSet, range, index, text); }