@Nullable private static PsiElement findParent( int syncStartOffset, int syncEndOffset, @NotNull AnchorTypeInfo type, PsiElement anchor) { TextRange range = anchor.getTextRange(); if (range.getStartOffset() != syncStartOffset) return null; while (range.getEndOffset() < syncEndOffset) { anchor = anchor.getParent(); if (anchor == null || anchor.getTextRange() == null) { return null; } range = anchor.getTextRange(); } while (range.getEndOffset() == syncEndOffset) { if (type.isAcceptable(anchor)) { return anchor; } anchor = anchor.getParent(); if (anchor == null || anchor.getTextRange() == null) break; range = anchor.getTextRange(); } return null; }
public static PsiElement findElementInside( @NotNull PsiFile file, int syncStartOffset, int syncEndOffset, @NotNull AnchorTypeInfo type) { PsiElement anchor = file.getViewProvider().findElementAt(syncStartOffset, type.getFileLanguage()); if (anchor == null && syncStartOffset == file.getTextLength()) { PsiElement lastChild = file.getViewProvider().getPsi(type.getFileLanguage()).getLastChild(); if (lastChild != null) { anchor = PsiTreeUtil.getDeepestLast(lastChild); } } if (anchor == null) return null; PsiElement result = findParent(syncStartOffset, syncEndOffset, type, anchor); if (syncEndOffset == syncStartOffset) { while (result == null && anchor.getTextRange().getStartOffset() == syncEndOffset) { anchor = PsiTreeUtil.prevLeaf(anchor, false); if (anchor == null) break; result = findParent(syncStartOffset, syncEndOffset, type, anchor); } } return result; }
@Override public PsiFile restoreFile() { return restoreFileFromVirtual(getVirtualFile(), getProject(), myType.getFileLanguage()); }