@NotNull public TypeInfo applyAnnotations(@NotNull StubBase<?> owner) { PsiModifierListStub modifierList = (PsiModifierListStub)owner.findChildStubByType(JavaStubElementTypes.MODIFIER_LIST); if (modifierList == null) return this; List<PsiAnnotationStub> annotationStubs = null; for (StubElement child : modifierList.getChildrenStubs()) { if (!(child instanceof PsiAnnotationStub)) continue; PsiAnnotationStub annotationStub = (PsiAnnotationStub)child; if (PsiImplUtil.isTypeAnnotation(annotationStub.getPsiElement())) { if (annotationStubs == null) annotationStubs = new SmartList<PsiAnnotationStub>(); annotationStubs.add(annotationStub); } } PsiAnnotationStub[] stubArray = PsiAnnotationStub.EMPTY_ARRAY; if (annotationStubs != null) stubArray = annotationStubs.toArray(new PsiAnnotationStub[annotationStubs.size()]); return new TypeInfo(text, arrayCount, isEllipsis, stubArray); }
public static int calcStubIndex(@NotNull StubBasedPsiElement psi) { if (psi instanceof PsiFile) { return 0; } final StubElement liveStub = psi.getStub(); if (liveStub != null) { return ((StubBase)liveStub).id; } PsiFileImpl file = (PsiFileImpl)psi.getContainingFile(); final StubTree stubTree = file.calcStubTree(); for (StubElement<?> stb : stubTree.getPlainList()) { if (stb.getPsi() == psi) { return ((StubBase)stb).id; } } return -1; // it is possible via custom stub builder intentionally not producing stubs for stubbed elements }
public static int calcStubIndex(StubBasedPsiElement psi) { if (psi instanceof PsiFile) { return 0; } final StubElement liveStub = psi.getStub(); if (liveStub != null) { return ((StubBase)liveStub).id; } PsiFileImpl file = (PsiFileImpl)psi.getContainingFile(); final StubTree stubTree = file.calcStubTree(); for (StubElement<?> stb : stubTree.getPlainList()) { if (stb.getPsi() == psi) { return ((StubBase)stb).id; } } return -1; // it is possible via custom stub builder intentionally not producing stubs for stubbed elements }
public static int calcStubIndex(@Nonnull StubBasedPsiElement psi) { if (psi instanceof PsiFile) { return 0; } final StubElement liveStub = psi.getStub(); if (liveStub != null) { return ((StubBase)liveStub).id; } PsiFileImpl file = (PsiFileImpl)psi.getContainingFile(); final StubTree stubTree = file.calcStubTree(); for (StubElement<?> stb : stubTree.getPlainList()) { if (stb.getPsi() == psi) { return ((StubBase)stb).id; } } return -1; // it is possible via custom stub builder intentionally not producing stubs for stubbed elements }
@Nullable @Contract("null -> null") public static PsiElement getStubOrPsiParent(@Nullable PsiElement element) { if (element instanceof StubBasedPsiElement) { StubBase stub = (StubBase)((StubBasedPsiElement)element).getStub(); if (stub != null) { //noinspection unchecked final StubElement parentStub = stub.getParentStub(); return parentStub != null ? parentStub.getPsi() : null; } } return element != null ? element.getParent() : null; }
@Nullable @Contract("null, _ -> null") public static <E extends PsiElement> E getStubOrPsiParentOfType(@Nullable PsiElement element, @NotNull Class<E> parentClass) { if (element instanceof StubBasedPsiElement) { StubBase stub = (StubBase)((StubBasedPsiElement)element).getStub(); if (stub != null) { //noinspection unchecked return (E)stub.getParentStubOfType(parentClass); } } return getParentOfType(element, parentClass); }
@NotNull public TypeInfo applyAnnotations(@NotNull StubBase<?> owner) { PsiModifierListStub modifierList = (PsiModifierListStub)owner.findChildStubByType(JavaStubElementTypes.MODIFIER_LIST); if (modifierList == null) return this; List<PsiAnnotationStub> annotationStubs = ContainerUtil.newArrayList(); for (StubElement child : modifierList.getChildrenStubs()) { if (!(child instanceof PsiAnnotationStub)) continue; PsiAnnotationStub annotationStub = (PsiAnnotationStub)child; if (PsiImplUtil.findApplicableTarget(annotationStub.getPsiElement(), TargetType.TYPE_USE) == TargetType.TYPE_USE) { annotationStubs.add(annotationStub); } } return new TypeInfo(text, arrayCount, isEllipsis, annotationStubs); }
@Nullable @Contract("null, _ -> null") public static <E extends PsiElement> E getStubOrPsiParentOfType(@Nullable PsiElement element, @Nonnull Class<E> parentClass) { if (element instanceof StubBasedPsiElement) { StubBase stub = (StubBase)((StubBasedPsiElement)element).getStub(); if (stub != null) { //noinspection unchecked return (E)stub.getParentStubOfType(parentClass); } } return getParentOfType(element, parentClass); }
@NotNull public TypeInfo applyAnnotations(@NotNull StubBase<?> owner) { PsiModifierListStub modifierList = (PsiModifierListStub) owner.findChildStubByType(JavaStubElementTypes.MODIFIER_LIST); if(modifierList == null) { return this; } List<PsiAnnotationStub> annotationStubs = null; for(StubElement child : modifierList.getChildrenStubs()) { if(!(child instanceof PsiAnnotationStub)) { continue; } PsiAnnotationStub annotationStub = (PsiAnnotationStub) child; if(PsiImplUtil.isTypeAnnotation(annotationStub.getPsiElement())) { if(annotationStubs == null) { annotationStubs = new SmartList<PsiAnnotationStub>(); } annotationStubs.add(annotationStub); } } PsiAnnotationStub[] stubArray = PsiAnnotationStub.EMPTY_ARRAY; if(annotationStubs != null) { stubArray = annotationStubs.toArray(new PsiAnnotationStub[annotationStubs.size()]); } return new TypeInfo(text, arrayCount, isEllipsis, stubArray); }
@Nullable public static PsiElement restoreFromStubIndex(PsiFileWithStubSupport fileImpl, int index, @NotNull IStubElementType elementType, boolean throwIfNull) { if (fileImpl == null) { if (throwIfNull) throw new AssertionError("Null file"); return null; } StubTree tree = fileImpl.getStubTree(); boolean foreign = tree == null; if (foreign) { if (fileImpl instanceof PsiFileImpl) { // Note: as far as this is a realization of StubIndexReference fileImpl#getContentElementType() must be instance of IStubFileElementType tree = ((PsiFileImpl)fileImpl).calcStubTree(); } else { if (throwIfNull) throw new AssertionError("Not PsiFileImpl: " + fileImpl.getClass()); return null; } } List<StubElement<?>> list = tree.getPlainList(); if (index >= list.size()) { if (throwIfNull) throw new AssertionError("Too large index: " + index + ">=" + list.size()); return null; } StubElement stub = list.get(index); if (stub.getStubType() != elementType) { if (throwIfNull) throw new AssertionError("Element type mismatch: " + stub.getStubType() + "!=" + elementType); return null; } if (foreign) { final PsiElement cachedPsi = ((StubBase)stub).getCachedPsi(); if (cachedPsi != null) return cachedPsi; final ASTNode ast = fileImpl.findTreeForStub(tree, stub); if (ast != null) { return ast.getPsi(); } if (throwIfNull) throw new AssertionError("No AST for stub"); return null; } return stub.getPsi(); }
public static void main(String[] args) throws ClassNotFoundException { String source = args.length == 0 ? DEFAULT_SOURCE : args[0]; PsiFile psi = LightPsi.parseFile("dummy", source, new SchemaParserDefinition()); String psiDump = DebugUtil.psiToString(psi, true, false).trim(); System.out.println(psiDump); // load file parsing related classes try { LightPsi.parseFile(new File("."), new SchemaParserDefinition()); } catch (IOException e) { // e.printStackTrace(); } // force some class loading @SuppressWarnings({"unused", "MismatchedReadAndWriteOfArray"}) Class<?>[] classes = new Class[]{ PsiTreeUtil.class, StubBasedPsiElementBase.class, StubBase.class, StubOutputStream.class, StubInputStream.class, IndexSink.class, ASTWrapperPsiElement.class, PsiElementProcessor.class, ParserDefinition.SpaceRequirements.class, PsiDirectory.class, }; String[] extraClasses = { "com.intellij.lang.parser.GeneratedParserUtilBase$CompletionState", "com.intellij.lang.parser.GeneratedParserUtilBase$Hooks", "com.intellij.lang.parser.GeneratedParserUtilBase$DummyBlock", "com.intellij.psi.impl.source.SubstrateRef$StubRef", "com.intellij.extapi.psi.StubBasedPsiElementBase$1", }; for (String extraClass : extraClasses) { Class.forName(extraClass); } }