@Override @Nullable public SmartPointerElementInfo createElementInfo(@NotNull PsiElement element, @NotNull PsiFile containingFile) { if (element instanceof StubBasedPsiElement && containingFile instanceof PsiFileWithStubSupport) { PsiFileWithStubSupport stubFile = (PsiFileWithStubSupport)containingFile; StubTree stubTree = stubFile.getStubTree(); if (stubTree != null) { // use stubs when tree is not loaded StubBasedPsiElement stubPsi = (StubBasedPsiElement)element; int stubId = PsiAnchor.calcStubIndex(stubPsi); IStubElementType myStubElementType = stubPsi.getElementType(); IElementType contentElementType = ((PsiFileImpl)containingFile).getContentElementType(); if (stubId != -1 && contentElementType instanceof IStubFileElementType) { // TemplateDataElementType is not IStubFileElementType return new AnchorElementInfo(element, stubFile, stubId, myStubElementType); } } } PsiElement anchor = getAnchor(element); if (anchor != null) { return new AnchorElementInfo(anchor, containingFile); } return null; }
@Override public Object createAnchor(Object element) { if (element instanceof PsiElement) { final PsiElement psiElement = (PsiElement)element; return ApplicationManager.getApplication().runReadAction(new Computable<Object>() { @Override public Object compute() { PsiAnchor anchor = psiElement.getUserData(PSI_ANCHORIZER_ANCHOR); if (!psiElement.isValid()) { return anchor != null ? anchor : psiElement; } if (anchor == null || anchor.retrieve() != psiElement) { psiElement.putUserData(PSI_ANCHORIZER_ANCHOR, anchor = PsiAnchor.create(psiElement)); } return anchor; } }); } return super.createAnchor(element); }
@Override public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) { if (element instanceof PsiNamedElement && element.isValid()) { PsiNamedElement named = (PsiNamedElement)element; String name = named.getName(); if (name != null) { PsiAnchor context = myCurrentContextAnchor; if (context == null && myCurrentFileContext != null) { myCurrentContextAnchor = context = PsiAnchor.create(myCurrentFileContext); } myResult.add(name, new ResultWithContext(named, context)); } } return true; }
@NotNull public PsiNamedElement getElement() { PsiElement element = myElement.retrieve(); if (element == null) { String message = "Anchor hasn't survived: " + myElement; if (myElement instanceof PsiAnchor.StubIndexReference) { message += "; diagnostics=" + ((PsiAnchor.StubIndexReference)myElement).diagnoseNull(); } throw new AssertionError(message); } return (PsiNamedElement)element; }
@Override public MyInfo[] collectInformation(@NotNull PsiFile file) { final WebReference[] references = collectWebReferences(file); final MyInfo[] infos = new MyInfo[references.length]; for (int i = 0; i < infos.length; i++) { final WebReference reference = references[i]; infos[i] = new MyInfo(PsiAnchor.create(reference.getElement()), reference.getRangeInElement(), reference.getValue()); } return infos; }
public PsiFragment(PsiElement element, int cost) { myElementAnchors = new PsiAnchor[]{createAnchor(element)}; myDuplicate = false; myChecked = false; myNested = false; myParents = null; myCost = cost; myLanguage = calcLanguage(element); }
protected PsiAnchor createAnchor(final PsiElement element) { return ApplicationManager.getApplication().runReadAction(new Computable<PsiAnchor>() { public PsiAnchor compute() { return PsiAnchor.create(element); } }); }
public PsiFragment(List<? extends PsiElement> elements, int from, int to) { myElementAnchors = new PsiAnchor[to - from + 1]; for (int i = from; i <= to; i++) { myElementAnchors[i - from] = createAnchor(elements.get(i)); } myDuplicate = false; myChecked = false; myNested = false; myParents = null; myLanguage = to >= from && from < elements.size() ? calcLanguage(elements.get(from)) : null; }
public String toString() { StringBuilder buffer = new StringBuilder(); for (PsiAnchor psiAnchor : myElementAnchors) { final PsiElement element = psiAnchor.retrieve(); if (element != null) { buffer.append(element.getText()); buffer.append("\n"); } } return buffer.toString(); }
public int[][] getOffsets() { final int[][] result = new int[myElementAnchors.length][2]; int idx = 0; for (PsiAnchor anchor : myElementAnchors) { result[idx][0] = anchor.getStartOffset(); result[idx++][1] = anchor.getEndOffset(); } return result; }
public IncludedXmlElement(@NotNull T original, @Nullable PsiElement parent) { super(original.getManager(), original.getLanguage()); //noinspection unchecked T realOriginal = original instanceof IncludedXmlElement ? ((IncludedXmlElement<T>)original).getOriginal() : original; myOriginal = PsiAnchor.create(realOriginal); myRef = new SoftReference<T>(realOriginal); myParent = parent; }
public CreateNSDeclarationIntentionFix(@NotNull final PsiElement element, @NotNull String namespacePrefix, @Nullable final XmlToken token) { myNamespacePrefix = namespacePrefix; myElement = PsiAnchor.create(element); myToken = token == null ? null : PsiAnchor.create(token); }
private static void processParents(@NotNull Processor<SchemaTypeDef> consumer, @NotNull SchemaTypeDef baseType, @NotNull SchemaTypeParentsSearch.SearchParameters parameters) { final Ref<SchemaTypeDef> currentBase = Ref.create(); final Queue<PsiAnchor> queue = new Queue<>(10); final Set<PsiAnchor> processed = ContainerUtil.newTroveSet(); final Processor<SchemaTypeDef> processor = new ReadActionProcessor<SchemaTypeDef>() { @Override public boolean processInReadAction(SchemaTypeDef inheritor) { if (!consumer.process(inheritor)) return false; if (inheritor == null) return false; queue.addLast(PsiAnchor.create(inheritor)); return true; } }; // seed ApplicationManager.getApplication().runReadAction(() -> { queue.addLast(PsiAnchor.create(baseType)); }); // BFS while (!queue.isEmpty()) { ProgressManager.checkCanceled(); final PsiAnchor anchor = queue.pullFirst(); if (!processed.add(anchor)) continue; SchemaTypeDef typeDef = ApplicationManager.getApplication().runReadAction( (Computable<SchemaTypeDef>) () -> (SchemaTypeDef) anchor.retrieve() ); if (typeDef == null) continue; currentBase.set(typeDef); if (!SchemaDirectTypeParentsSearch.search(typeDef).forEach(processor)) return; } }
@Override public MyInfo[] collectionInformation(@NotNull PsiFile file) { final WebReference[] references = collectWebReferences(file); final MyInfo[] infos = new MyInfo[references.length]; for (int i = 0; i < infos.length; i++) { final WebReference reference = references[i]; infos[i] = new MyInfo(PsiAnchor.create(reference.getElement()), reference.getRangeInElement(), reference.getValue()); } return infos; }
@Override @Nullable public Object retrieveElement(Object pointer) { if (pointer instanceof PsiAnchor) { PsiElement retrieve = ((PsiAnchor)pointer).retrieve(); if (retrieve == null) { //System.out.println("Null anchor: " + pointer); } return retrieve; } return super.retrieveElement(pointer); }
public CreateNSDeclarationIntentionFix(@NotNull final PsiElement element, final String namespacePrefix, @Nullable final XmlToken token) { myNamespacePrefix = namespacePrefix; myElement = PsiAnchor.create(element); myToken = token == null ? null : PsiAnchor.create(token); }
@Override @Nullable public PsiElement restoreElement() { long typeAndId = myStubElementTypeAndId; int stubId = (int)typeAndId; if (stubId != -1) { PsiFile file = restoreFile(); if (!(file instanceof PsiFileWithStubSupport)) return null; short index = (short)(typeAndId >> 32); IStubElementType stubElementType = (IStubElementType)IElementType.find(index); return PsiAnchor.restoreFromStubIndex((PsiFileWithStubSupport)file, stubId, stubElementType, false); } return super.restoreElement(); }
@Override public MyInfo[] collectInformation(@Nonnull PsiFile file) { final WebReference[] references = collectWebReferences(file); final MyInfo[] infos = new MyInfo[references.length]; for (int i = 0; i < infos.length; i++) { final WebReference reference = references[i]; infos[i] = new MyInfo(PsiAnchor.create(reference.getElement()), reference.getRangeInElement(), reference.getValue()); } return infos; }
@Override public boolean process(PsiClass inheritor) { ProgressManager.checkCanceled(); for(PsiClassType interfaceType : inheritor.getImplementsListTypes()) { ProgressManager.checkCanceled(); PsiClass anInterface = interfaceType.resolveGenerics().getElement(); if(anInterface != null && myCheckedInterfaces.add(PsiAnchor.create(anInterface))) { processInterface(inheritor, anInterface); } } return !myRemainingMethods.isEmpty(); }
public ResultWithContext(@NotNull PsiNamedElement element, @Nullable PsiAnchor fileContext) { myElement = PsiAnchor.create(element); myFileContext = fileContext; }
public ClsElementInfo(@NotNull PsiAnchor.StubIndexReference stubReference) { myStubIndexReference = stubReference; }
private MyInfo(PsiAnchor anchor, TextRange rangeInElement, String url) { myAnchor = anchor; myRangeInElement = rangeInElement; myUrl = url; }
public static <T extends DomElement> DomAnchor<T> createAnchor(@NotNull T t, boolean usePsi) { DomInvocationHandler handler = DomManagerImpl.getNotNullHandler(t); if (handler.getStub() != null) { return new StubAnchor<T>(handler); } if (usePsi) { final XmlElement element = t.getXmlElement(); if (element != null) { return new PsiBasedDomAnchor<T>(PsiAnchor.create(element), element.getProject()); } } final DomElement parent = t.getParent(); if (parent == null) { LOG.error("Parent null: " + t); } if (parent instanceof DomFileElementImpl) { final DomFileElementImpl fileElement = (DomFileElementImpl)parent; //noinspection unchecked return new RootAnchor<T>(fileElement.getFile(), fileElement.getRootElementClass()); } final DomAnchor<DomElement> parentAnchor = createAnchor(parent); final String name = t.getGenericInfo().getElementName(t); final AbstractDomChildrenDescription description = t.getChildDescription(); final List<? extends DomElement> values = description.getValues(parent); if (name != null) { int i = 0; for (DomElement value : values) { if (value.equals(t)) { return new NamedAnchor<T>(parentAnchor, description, name, i); } if (name.equals(value.getGenericInfo().getElementName(value))) { i++; } } } final int index = values.indexOf(t); if (index < 0) { diagnoseNegativeIndex2(t, parent, description, values); } return new IndexedAnchor<T>(parentAnchor, description, index); }
public PsiBasedDomAnchor(PsiAnchor anchor, Project project) { myAnchor = anchor; myProject = project; }
public CreatePropertyFix(PsiElement element, String key, final List<PropertiesFile> propertiesFiles) { myElement = element == null ? null : PsiAnchor.create(element); myKey = key; myPropertiesFiles = propertiesFiles; }
private static void processInheritors(@NotNull Processor<SchemaTypeDef> consumer, @NotNull SchemaTypeDef baseType, @NotNull SchemaTypeInheritorsSearch.SearchParameters parameters) { // see JavaClassInheritorsSearcher // final Project project = PsiUtilCore.getProjectInReadAction(baseType); final Ref<SchemaTypeDef> currentBase = Ref.create(); final Stack<PsiAnchor> stack = new Stack<>(); final Set<PsiAnchor> processed = ContainerUtil.newTroveSet(); final Processor<SchemaTypeDef> processor = new ReadActionProcessor<SchemaTypeDef>() { @Override public boolean processInReadAction(SchemaTypeDef inheritor) { if (!consumer.process(inheritor)) return false; stack.push(PsiAnchor.create(inheritor)); return true; } }; ApplicationManager.getApplication().runReadAction(() -> { stack.push(PsiAnchor.create(baseType)); }); // DFS while (!stack.isEmpty()) { ProgressManager.checkCanceled(); final PsiAnchor anchor = stack.pop(); if (!processed.add(anchor)) continue; SchemaTypeDef typeDef = ApplicationManager.getApplication().runReadAction( (Computable<SchemaTypeDef>) () -> (SchemaTypeDef) anchor.retrieve() ); if (typeDef == null) continue; currentBase.set(typeDef); if (!SchemaDirectTypeInheritorsSearch.search(typeDef).forEach(processor)) return; } }
public static <T extends DomElement> DomAnchor<T> createAnchor(@NotNull T t, boolean usePsi) { if (usePsi) { final XmlElement element = t.getXmlElement(); if (element != null) { return new PsiBasedDomAnchor<T>(PsiAnchor.create(element), element.getProject()); } } DomInvocationHandler handler = DomManagerImpl.getNotNullHandler(t); if (handler.getStub() != null) { return new StubAnchor<T>(handler); } final DomElement parent = t.getParent(); if (parent == null) { LOG.error("Parent null: " + t); } if (parent instanceof DomFileElementImpl) { final DomFileElementImpl fileElement = (DomFileElementImpl)parent; //noinspection unchecked return new RootAnchor<T>(fileElement.getFile(), fileElement.getRootElementClass()); } final DomAnchor<DomElement> parentAnchor = createAnchor(parent); final String name = t.getGenericInfo().getElementName(t); final AbstractDomChildrenDescription description = t.getChildDescription(); final List<? extends DomElement> values = description.getValues(parent); if (name != null) { int i = 0; for (DomElement value : values) { if (value.equals(t)) { return new NamedAnchor<T>(parentAnchor, description, name, i); } if (name.equals(value.getGenericInfo().getElementName(value))) { i++; } } } final int index = values.indexOf(t); if (index < 0) { diagnoseNegativeIndex2(t, parent, description, values); } return new IndexedAnchor<T>(parentAnchor, description, index); }
public ClsElementInfo(@Nonnull PsiAnchor.StubIndexReference stubReference) { myStubIndexReference = stubReference; }
PsiCrumb(PsiElement element, BreadcrumbsProvider provider) { super(provider.getElementIcon(element), provider.getElementInfo(element), provider.getElementTooltip(element)); anchor = PsiAnchor.create(element); }
SiblingInheritorSearcher(Collection<PsiMethod> methods, PsiClass containingClass) { myContainingClass = containingClass; myRemainingMethods = new HashSet<>(methods); myCheckedInterfaces.add(PsiAnchor.create(containingClass)); }