Search in sources :

Example 1 with ProjectAndLibrariesScope

use of com.intellij.psi.search.ProjectAndLibrariesScope in project intellij-community by JetBrains.

the class VfsAwareMapIndexStorage method processKeys.

@Override
public boolean processKeys(@NotNull final Processor<Key> processor, GlobalSearchScope scope, final IdFilter idFilter) throws StorageException {
    l.lock();
    try {
        // this will ensure that all new keys are made into the map
        myCache.clear();
        if (myBuildKeyHashToVirtualFileMapping && idFilter != null) {
            TIntHashSet hashMaskSet = null;
            long l = System.currentTimeMillis();
            File fileWithCaches = getSavedProjectFileValueIds(myLastScannedId, scope);
            final boolean useCachedHashIds = ENABLE_CACHED_HASH_IDS && (scope instanceof ProjectScopeImpl || scope instanceof ProjectAndLibrariesScope) && fileWithCaches != null;
            int id = myKeyHashToVirtualFileMapping.getCurrentLength();
            if (useCachedHashIds && id == myLastScannedId) {
                if (ourInvalidatedSessionIds.remove(id) == null) {
                    try {
                        hashMaskSet = loadHashedIds(fileWithCaches);
                    } catch (IOException ignored) {
                    }
                }
            }
            if (hashMaskSet == null) {
                if (useCachedHashIds && myLastScannedId != 0) {
                    FileUtil.asyncDelete(fileWithCaches);
                }
                hashMaskSet = new TIntHashSet(1000);
                final TIntHashSet finalHashMaskSet = hashMaskSet;
                withLock(() -> {
                    myKeyHashToVirtualFileMapping.force();
                    ProgressManager.checkCanceled();
                    myKeyHashToVirtualFileMapping.processAll(key -> {
                        if (!idFilter.containsFileId(key[1]))
                            return true;
                        finalHashMaskSet.add(key[0]);
                        ProgressManager.checkCanceled();
                        return true;
                    }, IntPairInArrayKeyDescriptor.INSTANCE);
                });
                if (useCachedHashIds) {
                    saveHashedIds(hashMaskSet, id, scope);
                }
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Scanned keyHashToVirtualFileMapping of " + myBaseStorageFile + " for " + (System.currentTimeMillis() - l));
            }
            final TIntHashSet finalHashMaskSet = hashMaskSet;
            return myMap.processKeys(key -> {
                if (!finalHashMaskSet.contains(myKeyDescriptor.getHashCode(key)))
                    return true;
                return processor.process(key);
            });
        }
        return myMap.processKeys(processor);
    } catch (IOException e) {
        throw new StorageException(e);
    } catch (RuntimeException e) {
        return unwrapCauseAndRethrow(e);
    } finally {
        l.unlock();
    }
}
Also used : ProjectAndLibrariesScope(com.intellij.psi.search.ProjectAndLibrariesScope) ProjectScopeImpl(com.intellij.psi.search.ProjectScopeImpl) TIntHashSet(gnu.trove.TIntHashSet)

Example 2 with ProjectAndLibrariesScope

use of com.intellij.psi.search.ProjectAndLibrariesScope in project completable-reactor by ru-fix.

the class EditorPanelFactory method create.

public static JFXPanel create(Project project, String graphModel) {
    JFXPanel swingFxPanel = new JFXPanel();
    GraphViewer graphViewer = new GraphViewer();
    graphViewer.setShortcut(ShortcutType.GOTO_SERIALIZATION_POINT, new Shortcut(true, KeyCode.B));
    graphViewer.registerListener(new GraphViewer.ActionListener() {

        @Override
        public void goToSource(@NotNull ReactorGraphModel.Source source) {
            ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runReadAction(() -> {
                PsiFile foundFile = null;
                if (source.fileName != null) {
                    foundFile = Arrays.stream(PsiShortNamesCache.getInstance(project).getFilesByName(source.fileName)).findAny().orElse(null);
                } else if (source.className != null) {
                    ProjectAndLibrariesScope searchScope = new ProjectAndLibrariesScope(project);
                    PsiClass payloadPsiClass = JavaPsiFacade.getInstance(project).findClass(source.className, searchScope);
                    if (payloadPsiClass != null) {
                        foundFile = payloadPsiClass.getContainingFile();
                    }
                }
                if (foundFile == null) {
                    log.warn("Can not find file for source: " + source);
                    return;
                }
                OpenFileDescriptor descriptor = new OpenFileDescriptor(project, foundFile.getVirtualFile(), source.fileNameLine != null ? source.fileNameLine - 1 : 0, 0);
                descriptor.navigate(true);
            }));
        }

        @Override
        public void goToSubgraph(String subgraphPayloadClass) {
            ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runReadAction(() -> {
                PsiFile foundFile = null;
                if (subgraphPayloadClass != null) {
                    foundFile = Arrays.stream(PsiShortNamesCache.getInstance(project).getFilesByName(subgraphPayloadClass + ".rg")).findAny().orElse(null);
                }
                if (foundFile == null) {
                    log.warn("Can not find file for subgraph: " + subgraphPayloadClass);
                    return;
                }
                OpenFileDescriptor descriptor = new OpenFileDescriptor(project, foundFile.getVirtualFile());
                descriptor.navigate(true);
            }));
        }

        @Override
        public void coordinatesChanged(List<GraphViewer.CoordinateItem> coordinateItems) {
            ReactorGraphModel graphModel = graphViewer.getGraphModel();
            final ReactorGraphModel.Source codeBlockFrom = graphModel.coordinatesSource;
            if (codeBlockFrom == null) {
                log.warn("Coordinates source start location not specified in model.");
                return;
            }
            ApplicationManager.getApplication().invokeLater(() -> {
                ApplicationManager.getApplication().runReadAction(() -> {
                    PsiFile[] foundFiles = PsiShortNamesCache.getInstance(project).getFilesByName(codeBlockFrom.fileName);
                    if (foundFiles.length == 0) {
                        log.warn("No file with name " + codeBlockFrom.fileName + " found");
                        return;
                    }
                    if (foundFiles.length > 1) {
                        log.warn("Found more than one file with name " + codeBlockFrom.fileName);
                    }
                    PsiFile foundFile = foundFiles[0];
                    Document document = PsiDocumentManager.getInstance(project).getDocument(foundFile);
                    TextRange textRange = new TextRange(document.getLineStartOffset(codeBlockFrom.fileNameLine - 1), document.getLineEndOffset(document.getLineCount() - 1));
                    String codeBlock = document.getText(textRange);
                    String newCodeBlock = codeUpdater.updateCoordinates(codeBlock, coordinateItems);
                    WriteCommandAction.runWriteCommandAction(project, () -> document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), newCodeBlock));
                });
            });
        }
    });
    // Because of https://bugs.openjdk.java.net/browse/JDK-8090517, it is important to disable implicit exit.
    Platform.setImplicitExit(false);
    Platform.runLater(() -> {
        try {
            graphViewer.openGraph(graphModel);
            swingFxPanel.setScene(graphViewer.getScene());
        } catch (Exception exc) {
            log.error("Failed to open graph.", exc);
        }
    });
    return swingFxPanel;
}
Also used : JFXPanel(javafx.embed.swing.JFXPanel) PsiClass(com.intellij.psi.PsiClass) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) GraphViewer(ru.fix.completable.reactor.graph.viewer.GraphViewer) ProjectAndLibrariesScope(com.intellij.psi.search.ProjectAndLibrariesScope) Shortcut(ru.fix.completable.reactor.graph.viewer.Shortcut) ReactorGraphModel(ru.fix.completable.reactor.api.ReactorGraphModel) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 3 with ProjectAndLibrariesScope

use of com.intellij.psi.search.ProjectAndLibrariesScope in project flutter-intellij by flutter.

the class FlutterReloadManager method hasErrors.

private boolean hasErrors(@NotNull Project project, @Nullable Module module, @NotNull Document document) {
    // For 2017.1, we use the IntelliJ parser and look for syntax errors in the current document.
    // For 2017.2 and later, we instead rely on the analysis server's results for files in the app's module.
    final DartAnalysisServerService analysisServerService = DartAnalysisServerService.getInstance(project);
    // TODO(devoncarew): Remove the use of reflection when our minimum revs to 2017.2.
    final Method getErrorsMethod = ReflectionUtil.getMethod(analysisServerService.getClass(), "getErrors", SearchScope.class);
    if (getErrorsMethod == null) {
        final PsiErrorElement firstError = ApplicationManager.getApplication().runReadAction((Computable<PsiErrorElement>) () -> {
            final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
            return PsiTreeUtil.findChildOfType(psiFile, PsiErrorElement.class, false);
        });
        return firstError != null;
    } else {
        final GlobalSearchScope scope = module == null ? new ProjectAndLibrariesScope(project) : module.getModuleContentScope();
        try {
            // List<DartServerData.DartError> errors = analysisServerService.getErrors(scope);
            // noinspection unchecked
            List<DartServerData.DartError> errors = (List<DartServerData.DartError>) getErrorsMethod.invoke(analysisServerService, scope);
            errors = errors.stream().filter(error -> shouldBlockReload(error, project, module)).collect(Collectors.toList());
            return !errors.isEmpty();
        } catch (IllegalAccessException | InvocationTargetException e) {
            return false;
        }
    }
}
Also used : Method(java.lang.reflect.Method) DartServerData(com.jetbrains.lang.dart.analyzer.DartServerData) InvocationTargetException(java.lang.reflect.InvocationTargetException) PsiErrorElement(com.intellij.psi.PsiErrorElement) ProjectAndLibrariesScope(com.intellij.psi.search.ProjectAndLibrariesScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)

Aggregations

ProjectAndLibrariesScope (com.intellij.psi.search.ProjectAndLibrariesScope)3 PsiFile (com.intellij.psi.PsiFile)2 Document (com.intellij.openapi.editor.Document)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiClass (com.intellij.psi.PsiClass)1 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 ProjectScopeImpl (com.intellij.psi.search.ProjectScopeImpl)1 DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)1 DartServerData (com.jetbrains.lang.dart.analyzer.DartServerData)1 TIntHashSet (gnu.trove.TIntHashSet)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 JFXPanel (javafx.embed.swing.JFXPanel)1 ReactorGraphModel (ru.fix.completable.reactor.api.ReactorGraphModel)1 GraphViewer (ru.fix.completable.reactor.graph.viewer.GraphViewer)1 Shortcut (ru.fix.completable.reactor.graph.viewer.Shortcut)1