Search in sources :

Example 1 with VirtualFileWithId

use of com.intellij.openapi.vfs.VirtualFileWithId in project intellij-community by JetBrains.

the class FileDescriptionCachedValueProvider method _computeFileElement.

@Nullable
private DomFileElementImpl<T> _computeFileElement(final boolean fireEvents, @NotNull final XmlFileHeader rootTagName, @Nullable StringBuilder sb) {
    if (sb != null) {
        sb.append(rootTagName).append("\n");
    }
    if (!myXmlFile.isValid()) {
        return null;
    }
    if (sb != null) {
        sb.append("File is valid\n");
    }
    if (!(myXmlFile.getFileType() instanceof DomSupportEnabled)) {
        return null;
    }
    if (sb != null) {
        sb.append("File is of dom file type\n");
    }
    final DomFileDescription<T> description = findFileDescription(rootTagName, sb);
    final DomFileElementImpl oldValue = getLastValue();
    if (sb != null) {
        sb.append("last " + oldValue + "\n");
    }
    final List<DomEvent> events = fireEvents ? new SmartList<>() : Collections.<DomEvent>emptyList();
    if (oldValue != null) {
        if (fireEvents) {
            events.add(new DomEvent(oldValue, false));
        }
    }
    if (description == null) {
        return null;
    }
    final Class<T> rootElementClass = description.getRootElementClass();
    final XmlName xmlName = DomImplUtil.createXmlName(description.getRootTagName(), rootElementClass, null);
    assert xmlName != null;
    final EvaluatedXmlNameImpl rootTagName1 = EvaluatedXmlNameImpl.createEvaluatedXmlName(xmlName, xmlName.getNamespaceKey(), false);
    VirtualFile file = myXmlFile.getVirtualFile();
    FileStub stub = null;
    if (description.hasStubs() && file instanceof VirtualFileWithId && !isFileParsed()) {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        if (!XmlUtil.isStubBuilding()) {
            ObjectStubTree stubTree = StubTreeLoader.getInstance().readOrBuild(myXmlFile.getProject(), file, myXmlFile);
            if (stubTree != null) {
                stub = (FileStub) stubTree.getRoot();
            }
        }
    }
    DomFileElementImpl<T> result = new DomFileElementImpl<>(myXmlFile, rootElementClass, rootTagName1, myDomManager, description, stub);
    if (sb != null) {
        sb.append("success " + result + "\n");
    }
    if (fireEvents) {
        events.add(new DomEvent(result, true));
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileStub(com.intellij.util.xml.stubs.FileStub) DomSupportEnabled(com.intellij.ide.highlighter.DomSupportEnabled) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) DomEvent(com.intellij.util.xml.events.DomEvent) ObjectStubTree(com.intellij.psi.stubs.ObjectStubTree) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with VirtualFileWithId

use of com.intellij.openapi.vfs.VirtualFileWithId in project intellij-community by JetBrains.

the class DomElementClassIndex method hasStubElementsOfType.

public boolean hasStubElementsOfType(final DomFileElement domFileElement, final Class<? extends DomElement> clazz) {
    final VirtualFile file = domFileElement.getFile().getVirtualFile();
    if (!(file instanceof VirtualFileWithId))
        return false;
    final String clazzName = clazz.getName();
    final int virtualFileId = ((VirtualFileWithId) file).getId();
    CommonProcessors.FindFirstProcessor<? super PsiFile> processor = new CommonProcessors.FindFirstProcessor<>();
    StubIndex.getInstance().processElements(KEY, clazzName, domFileElement.getFile().getProject(), GlobalSearchScope.fileScope(domFileElement.getFile()), new IdFilter() {

        @Override
        public boolean containsFileId(int id) {
            return id == virtualFileId;
        }
    }, PsiFile.class, processor);
    return processor.isFound();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdFilter(com.intellij.util.indexing.IdFilter) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) CommonProcessors(com.intellij.util.CommonProcessors)

Example 3 with VirtualFileWithId

use of com.intellij.openapi.vfs.VirtualFileWithId in project intellij-community by JetBrains.

the class FileBasedIndexImpl method registerIndexableSet.

@Override
public void registerIndexableSet(@NotNull IndexableFileSet set, @Nullable Project project) {
    myIndexableSets.add(set);
    myIndexableSetToProjectMap.put(set, project);
    if (project != null) {
        ((PsiManagerImpl) PsiManager.getInstance(project)).addTreeChangePreprocessor(event -> {
            if (event.isGenericChange() && event.getCode() == PsiTreeChangeEventImpl.PsiEventType.CHILDREN_CHANGED) {
                PsiFile file = event.getFile();
                if (file != null) {
                    waitUntilIndicesAreInitialized();
                    VirtualFile virtualFile = file.getVirtualFile();
                    if (!clearUpToDateStateForPsiIndicesOfUnsavedDocuments(virtualFile)) {
                        if (virtualFile instanceof VirtualFileWithId) {
                            int fileId = ((VirtualFileWithId) virtualFile).getId();
                            boolean wasIndexed = false;
                            List<ID<?, ?>> candidates = getAffectedIndexCandidates(virtualFile);
                            for (ID<?, ?> psiBackedIndex : myPsiDependentIndices) {
                                if (!candidates.contains(psiBackedIndex))
                                    continue;
                                if (getInputFilter(psiBackedIndex).acceptInput(virtualFile)) {
                                    getIndex(psiBackedIndex).resetIndexedStateForFile(fileId);
                                    wasIndexed = true;
                                }
                            }
                            if (wasIndexed) {
                                myChangedFilesCollector.scheduleForUpdate(virtualFile);
                                IndexingStamp.flushCache(fileId);
                            }
                        }
                    }
                }
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) PsiManagerImpl(com.intellij.psi.impl.PsiManagerImpl) PsiFile(com.intellij.psi.PsiFile) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId)

Example 4 with VirtualFileWithId

use of com.intellij.openapi.vfs.VirtualFileWithId in project intellij-community by JetBrains.

the class JavaFileManagerImpl method doFindClasses.

@NotNull
private List<Pair<PsiClass, VirtualFile>> doFindClasses(@NotNull String qName, @NotNull final GlobalSearchScope scope) {
    final Collection<PsiClass> classes = JavaFullClassNameIndex.getInstance().get(qName.hashCode(), myManager.getProject(), scope);
    if (classes.isEmpty())
        return Collections.emptyList();
    List<Pair<PsiClass, VirtualFile>> result = new ArrayList<>(classes.size());
    for (PsiClass aClass : classes) {
        final String qualifiedName = aClass.getQualifiedName();
        if (qualifiedName == null || !qualifiedName.equals(qName))
            continue;
        PsiFile file = aClass.getContainingFile();
        if (file == null) {
            throw new AssertionError("No file for class: " + aClass + " of " + aClass.getClass());
        }
        final boolean valid = file.isValid();
        VirtualFile vFile = file.getVirtualFile();
        if (!valid) {
            LOG.error("Invalid file " + file + "; virtualFile:" + vFile + (vFile != null && !vFile.isValid() ? " (invalid)" : "") + "; id=" + (vFile == null ? 0 : ((VirtualFileWithId) vFile).getId()), new PsiInvalidElementAccessException(aClass));
            continue;
        }
        if (!hasAcceptablePackage(vFile))
            continue;
        result.add(Pair.create(aClass, vFile));
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with VirtualFileWithId

use of com.intellij.openapi.vfs.VirtualFileWithId in project intellij-community by JetBrains.

the class DuplicatesInspectionBase method checkFile.

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull final PsiFile psiFile, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    final VirtualFile virtualFile = psiFile.getVirtualFile();
    if (!(virtualFile instanceof VirtualFileWithId) || /*!isOnTheFly || */
    !DuplicatesIndex.ourEnabled)
        return ProblemDescriptor.EMPTY_ARRAY;
    final DuplicatesProfile profile = DuplicatesIndex.findDuplicatesProfile(psiFile.getFileType());
    if (profile == null)
        return ProblemDescriptor.EMPTY_ARRAY;
    final Ref<DuplicatedCodeProcessor> myProcessorRef = new Ref<>();
    final FileASTNode node = psiFile.getNode();
    boolean usingLightProfile = profile instanceof LightDuplicateProfile && node.getElementType() instanceof ILightStubFileElementType && DuplicatesIndex.ourEnabledLightProfiles;
    if (usingLightProfile) {
        LighterAST ast = node.getLighterAST();
        ((LightDuplicateProfile) profile).process(ast, new LightDuplicateProfile.Callback() {

            DuplicatedCodeProcessor<LighterASTNode> myProcessor;

            @Override
            public void process(int hash, int hash2, @NotNull final LighterAST ast, @NotNull final LighterASTNode... nodes) {
                class LightDuplicatedCodeProcessor extends DuplicatedCodeProcessor<LighterASTNode> {

                    private LightDuplicatedCodeProcessor(VirtualFile file, Project project) {
                        super(file, project, myFilterOutGeneratedCode);
                    }

                    @Override
                    protected TextRange getRangeInElement(LighterASTNode node) {
                        return null;
                    }

                    @Override
                    protected PsiElement getPsi(LighterASTNode node) {
                        return ((TreeBackedLighterAST) ast).unwrap(node).getPsi();
                    }

                    @Override
                    protected int getStartOffset(LighterASTNode node) {
                        return node.getStartOffset();
                    }

                    @Override
                    protected int getEndOffset(LighterASTNode node) {
                        return node.getEndOffset();
                    }

                    @Override
                    protected boolean isLightProfile() {
                        return true;
                    }
                }
                if (myProcessor == null) {
                    myProcessor = new LightDuplicatedCodeProcessor(virtualFile, psiFile.getProject());
                    myProcessorRef.set(myProcessor);
                }
                myProcessor.process(hash, hash2, nodes[0]);
            }
        });
    } else {
        final DuplocatorState state = profile.getDuplocatorState(psiFile.getLanguage());
        profile.createVisitor(new FragmentsCollector() {

            DuplicatedCodeProcessor<PsiFragment> myProcessor;

            @Override
            public void add(int hash, final int cost, @Nullable final PsiFragment frag) {
                if (!DuplicatesIndex.isIndexedFragment(frag, cost, profile, state)) {
                    return;
                }
                class OldDuplicatedCodeProcessor extends DuplicatedCodeProcessor<PsiFragment> {

                    private OldDuplicatedCodeProcessor(VirtualFile file, Project project) {
                        super(file, project, myFilterOutGeneratedCode);
                    }

                    @Override
                    protected TextRange getRangeInElement(PsiFragment node) {
                        PsiElement[] elements = node.getElements();
                        TextRange rangeInElement = null;
                        if (elements.length > 1) {
                            PsiElement lastElement = elements[elements.length - 1];
                            rangeInElement = new TextRange(elements[0].getStartOffsetInParent(), lastElement.getStartOffsetInParent() + lastElement.getTextLength());
                        }
                        return rangeInElement;
                    }

                    @Override
                    protected PsiElement getPsi(PsiFragment node) {
                        PsiElement[] elements = node.getElements();
                        return elements.length > 1 ? elements[0].getParent() : elements[0];
                    }

                    @Override
                    protected int getStartOffset(PsiFragment node) {
                        return node.getStartOffset();
                    }

                    @Override
                    protected int getEndOffset(PsiFragment node) {
                        return node.getEndOffset();
                    }

                    @Override
                    protected boolean isLightProfile() {
                        return false;
                    }
                }
                if (myProcessor == null) {
                    myProcessor = new OldDuplicatedCodeProcessor(virtualFile, psiFile.getProject());
                    myProcessorRef.set(myProcessor);
                }
                myProcessor.process(hash, 0, frag);
            }
        }, true).visitNode(psiFile);
    }
    DuplicatedCodeProcessor<?> processor = myProcessorRef.get();
    final SmartList<ProblemDescriptor> descriptors = new SmartList<>();
    if (processor != null) {
        final VirtualFile baseDir = psiFile.getProject().getBaseDir();
        for (Map.Entry<Integer, TextRange> entry : processor.reportedRanges.entrySet()) {
            final Integer offset = entry.getKey();
            if (!usingLightProfile && processor.fragmentSize.get(offset) < MIN_FRAGMENT_SIZE)
                continue;
            final VirtualFile file = processor.reportedFiles.get(offset);
            String path = null;
            if (file.equals(virtualFile))
                path = "this file";
            else if (baseDir != null) {
                path = VfsUtilCore.getRelativePath(file, baseDir);
            }
            if (path == null) {
                path = file.getPath();
            }
            String message = "Found duplicated code in " + path;
            PsiElement targetElement = processor.reportedPsi.get(offset);
            TextRange rangeInElement = entry.getValue();
            final int offsetInOtherFile = processor.reportedOffsetInOtherFiles.get(offset);
            LocalQuickFix fix = createNavigateToDupeFix(file, offsetInOtherFile);
            long hash = processor.fragmentHash.get(offset);
            LocalQuickFix viewAllDupesFix = hash != 0 ? createShowOtherDupesFix(virtualFile, offset, (int) hash, (int) (hash >> 32), psiFile.getProject()) : null;
            ProblemDescriptor descriptor = manager.createProblemDescriptor(targetElement, rangeInElement, message, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly, fix, viewAllDupesFix);
            descriptors.add(descriptor);
        }
    }
    return descriptors.isEmpty() ? null : descriptors.toArray(new ProblemDescriptor[descriptors.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DuplicatesProfile(com.intellij.dupLocator.DuplicatesProfile) ILightStubFileElementType(com.intellij.psi.tree.ILightStubFileElementType) FileASTNode(com.intellij.lang.FileASTNode) PsiFragment(com.intellij.dupLocator.util.PsiFragment) FragmentsCollector(com.intellij.dupLocator.treeHash.FragmentsCollector) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) LightDuplicateProfile(com.intellij.dupLocator.LightDuplicateProfile) PsiElement(com.intellij.psi.PsiElement) LighterASTNode(com.intellij.lang.LighterASTNode) TreeBackedLighterAST(com.intellij.lang.TreeBackedLighterAST) LighterAST(com.intellij.lang.LighterAST) TextRange(com.intellij.openapi.util.TextRange) DuplocatorState(com.intellij.dupLocator.DuplocatorState) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) TreeBackedLighterAST(com.intellij.lang.TreeBackedLighterAST) SmartList(com.intellij.util.SmartList) TIntLongHashMap(gnu.trove.TIntLongHashMap) Map(java.util.Map) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) TIntIntHashMap(gnu.trove.TIntIntHashMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

VirtualFileWithId (com.intellij.openapi.vfs.VirtualFileWithId)16 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)5 Project (com.intellij.openapi.project.Project)2 FileAttributes (com.intellij.openapi.util.io.FileAttributes)2 FileInfo (com.intellij.openapi.util.io.win32.FileInfo)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 THashMap (gnu.trove.THashMap)2 DuplicatesProfile (com.intellij.dupLocator.DuplicatesProfile)1 DuplocatorState (com.intellij.dupLocator.DuplocatorState)1 LightDuplicateProfile (com.intellij.dupLocator.LightDuplicateProfile)1 FragmentsCollector (com.intellij.dupLocator.treeHash.FragmentsCollector)1 PsiFragment (com.intellij.dupLocator.util.PsiFragment)1 StubBasedPsiElementBase (com.intellij.extapi.psi.StubBasedPsiElementBase)1 DomSupportEnabled (com.intellij.ide.highlighter.DomSupportEnabled)1 PsiNavigationSupport (com.intellij.ide.util.PsiNavigationSupport)1 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)1