Search in sources :

Example 1 with FileBasedIndex

use of com.intellij.util.indexing.FileBasedIndex in project intellij-community by JetBrains.

the class DefaultXmlTagNameProvider method getRootTagsVariants.

private static List<LookupElement> getRootTagsVariants(final XmlTag tag, final List<LookupElement> elements) {
    elements.add(LookupElementBuilder.create("?xml version=\"1.0\" encoding=\"\" ?>").withPresentableText("<?xml version=\"1.0\" encoding=\"\" ?>").withInsertHandler(new InsertHandler<LookupElement>() {

        @Override
        public void handleInsert(InsertionContext context, LookupElement item) {
            int offset = context.getEditor().getCaretModel().getOffset();
            context.getEditor().getCaretModel().moveToOffset(offset - 4);
            AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
        }
    }));
    final FileBasedIndex fbi = FileBasedIndex.getInstance();
    Collection<String> result = new ArrayList<>();
    Processor<String> processor = Processors.cancelableCollectProcessor(result);
    fbi.processAllKeys(XmlNamespaceIndex.NAME, processor, tag.getProject());
    final GlobalSearchScope scope = new EverythingGlobalScope();
    for (final String ns : result) {
        if (ns.startsWith("file://"))
            continue;
        fbi.processValues(XmlNamespaceIndex.NAME, ns, null, new FileBasedIndex.ValueProcessor<XsdNamespaceBuilder>() {

            @Override
            public boolean process(final VirtualFile file, XsdNamespaceBuilder value) {
                List<String> tags = value.getRootTags();
                for (String s : tags) {
                    elements.add(LookupElementBuilder.create(s).withTypeText(ns).withInsertHandler(new XmlTagInsertHandler() {

                        @Override
                        public void handleInsert(InsertionContext context, LookupElement item) {
                            final Editor editor = context.getEditor();
                            final Document document = context.getDocument();
                            final int caretOffset = editor.getCaretModel().getOffset();
                            final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
                            caretMarker.setGreedyToRight(true);
                            XmlFile psiFile = (XmlFile) context.getFile();
                            boolean incomplete = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) == null && XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) == null;
                            XmlNamespaceHelper.getHelper(psiFile).insertNamespaceDeclaration(psiFile, editor, Collections.singleton(ns), null, null);
                            editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
                            XmlTag rootTag = psiFile.getRootTag();
                            if (incomplete) {
                                XmlToken token = XmlUtil.getTokenOfType(rootTag, XmlTokenType.XML_EMPTY_ELEMENT_END);
                                // remove tag end added by smart attribute adder :(
                                if (token != null)
                                    token.delete();
                                PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
                                super.handleInsert(context, item);
                            }
                        }
                    }));
                }
                return true;
            }
        }, scope);
    }
    return elements;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope) XsdNamespaceBuilder(com.intellij.xml.index.XsdNamespaceBuilder) RangeMarker(com.intellij.openapi.editor.RangeMarker) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Document(com.intellij.openapi.editor.Document) XmlToken(com.intellij.psi.xml.XmlToken) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Editor(com.intellij.openapi.editor.Editor) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with FileBasedIndex

use of com.intellij.util.indexing.FileBasedIndex in project intellij-community by JetBrains.

the class AntHectorConfigurable method createComponent.

public JComponent createComponent() {
    final JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(IdeBorderFactory.createTitledBorder("File Context", false));
    myCombo = new ComboBox();
    myCombo.putClientProperty(CONTEXTS_COMBO_KEY, Boolean.TRUE);
    panel.add(new JLabel("Included into:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, JBUI.insets(5, 0), 0, 0));
    panel.add(myCombo, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, JBUI.insets(5, 5, 5, 0), 0, 0));
    final PsiManager psiManager = PsiManager.getInstance(myProject);
    final FileBasedIndex fbi = FileBasedIndex.getInstance();
    final Collection<VirtualFile> antFiles = fbi.getContainingFiles(AntImportsIndex.INDEX_NAME, AntImportsIndex.ANT_FILES_WITH_IMPORTS_KEY, myFileFilter);
    for (VirtualFile file : antFiles) {
        final PsiFile psiFile = psiManager.findFile(file);
        if (!(psiFile instanceof XmlFile)) {
            continue;
        }
        final XmlFile xmlFile = (XmlFile) psiFile;
        if (!xmlFile.equals(myFile) && AntDomFileDescription.isAntFile(xmlFile)) {
            final String path = PathUtil.getLocalPath(file);
            final XmlFile previous = myPathToFileMap.put(path, xmlFile);
            assert previous == null;
        }
    }
    final List<String> paths = new ArrayList<>(myPathToFileMap.keySet());
    Collections.sort(paths, (o1, o2) -> o1.compareTo(o2));
    myCombo.addItem(NONE);
    for (String path : paths) {
        myCombo.addItem(path);
    }
    final AntConfigurationBase antConfig = AntConfigurationBase.getInstance(myProject);
    final XmlFile currentContext = antConfig.getContextFile(myFile);
    if (currentContext != null) {
        final VirtualFile vFile = currentContext.getVirtualFile();
        assert vFile != null;
        final String path = PathUtil.getLocalPath(vFile);
        if (!FileUtil.pathsEqual(path, myLocalPath)) {
            myOriginalContext = path;
        }
    }
    myCombo.setSelectedItem(myOriginalContext);
    return panel;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) ComboBox(com.intellij.openapi.ui.ComboBox) PsiManager(com.intellij.psi.PsiManager) AntConfigurationBase(com.intellij.lang.ant.config.AntConfigurationBase) PsiFile(com.intellij.psi.PsiFile) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

Example 3 with FileBasedIndex

use of com.intellij.util.indexing.FileBasedIndex in project intellij-plugins by JetBrains.

the class AngularIndexUtil method multiResolveAngularNamedDefinitionIndex.

public static ResolveResult[] multiResolveAngularNamedDefinitionIndex(@NotNull final Project project, @NotNull final ID<String, AngularNamedItemDefinition> INDEX, @NotNull final String id, @NotNull final Condition<VirtualFile> filter, boolean dirtyResolve) {
    final FileBasedIndex instance = FileBasedIndex.getInstance();
    Collection<VirtualFile> files = instance.getContainingFiles(INDEX, id, GlobalSearchScope.allScope(project));
    if (files.isEmpty())
        return ResolveResult.EMPTY_ARRAY;
    final List<VirtualFile> filtered = ContainerUtil.filter(files, filter);
    if (filtered.isEmpty()) {
        if (!dirtyResolve)
            return ResolveResult.EMPTY_ARRAY;
    } else {
        files = filtered;
    }
    final List<JSImplicitElement> elements = new ArrayList<>();
    for (VirtualFile file : files) {
        final List<AngularNamedItemDefinition> values = instance.getValues(INDEX, id, GlobalSearchScope.fileScope(project, file));
        for (AngularNamedItemDefinition value : values) {
            JSQualifiedNameImpl qName = JSQualifiedNameImpl.fromQualifiedName(id);
            JSImplicitElementImpl.Builder elementBuilder = new JSImplicitElementImpl.Builder(qName, null);
            final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
            if (psiFile != null) {
                elements.add(new JSOffsetBasedImplicitElement(elementBuilder, (int) value.getStartOffset(), psiFile));
            }
        }
    }
    final List<ResolveResult> list = ContainerUtil.map(elements, JS_IMPLICIT_TO_RESOLVE_RESULT);
    return list.toArray(new ResolveResult[list.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JSQualifiedNameImpl(com.intellij.lang.javascript.psi.JSQualifiedNameImpl) JSImplicitElementImpl(com.intellij.lang.javascript.psi.stubs.impl.JSImplicitElementImpl) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) JSOffsetBasedImplicitElement(com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement) JSImplicitElement(com.intellij.lang.javascript.psi.stubs.JSImplicitElement) JSResolveResult(com.intellij.lang.javascript.psi.resolve.JSResolveResult) ResolveResult(com.intellij.psi.ResolveResult) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

Example 4 with FileBasedIndex

use of com.intellij.util.indexing.FileBasedIndex in project intellij-community by JetBrains.

the class FrameworkDetectionManager method doRunDetection.

private void doRunDetection() {
    Set<Integer> detectorsToProcess;
    synchronized (myLock) {
        detectorsToProcess = new HashSet<>(myDetectorsToProcess);
        detectorsToProcess.addAll(myDetectorsToProcess);
        myDetectorsToProcess.clear();
    }
    if (detectorsToProcess.isEmpty())
        return;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Starting framework detectors: " + detectorsToProcess);
    }
    final FileBasedIndex index = FileBasedIndex.getInstance();
    List<DetectedFrameworkDescription> newDescriptions = new ArrayList<>();
    List<DetectedFrameworkDescription> oldDescriptions = new ArrayList<>();
    final DetectionExcludesConfiguration excludesConfiguration = DetectionExcludesConfiguration.getInstance(myProject);
    for (Integer id : detectorsToProcess) {
        final List<? extends DetectedFrameworkDescription> frameworks = runDetector(id, index, excludesConfiguration, true);
        oldDescriptions.addAll(frameworks);
        final Collection<? extends DetectedFrameworkDescription> updated = myDetectedFrameworksData.updateFrameworksList(id, frameworks);
        newDescriptions.addAll(updated);
        oldDescriptions.removeAll(updated);
        if (LOG.isDebugEnabled()) {
            LOG.debug(frameworks.size() + " frameworks detected, " + updated.size() + " changed");
        }
    }
    Set<String> frameworkNames = new HashSet<>();
    for (final DetectedFrameworkDescription description : FrameworkDetectionUtil.removeDisabled(newDescriptions, oldDescriptions)) {
        frameworkNames.add(description.getDetector().getFrameworkType().getPresentableName());
    }
    if (!frameworkNames.isEmpty()) {
        String names = StringUtil.join(frameworkNames, ", ");
        final String text = ProjectBundle.message("framework.detected.info.text", names, frameworkNames.size());
        FRAMEWORK_DETECTION_NOTIFICATION.createNotification("Frameworks detected", text, NotificationType.INFORMATION, new NotificationListener() {

            @Override
            public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
                if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    showSetupFrameworksDialog(notification);
                }
            }
        }).notify(myProject);
    }
}
Also used : HyperlinkEvent(javax.swing.event.HyperlinkEvent) DetectedFrameworkDescription(com.intellij.framework.detection.DetectedFrameworkDescription) NotNull(org.jetbrains.annotations.NotNull) Notification(com.intellij.notification.Notification) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) DetectionExcludesConfiguration(com.intellij.framework.detection.DetectionExcludesConfiguration) NotificationListener(com.intellij.notification.NotificationListener)

Example 5 with FileBasedIndex

use of com.intellij.util.indexing.FileBasedIndex in project intellij-plugins by JetBrains.

the class AngularJavaScriptCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    if (AngularJSCompletionContributor.getElementLanguage(parameters).isKindOf(JavascriptLanguage.INSTANCE)) {
        PsiElement originalPosition = parameters.getOriginalPosition();
        if (originalPosition == null)
            return;
        final Project project = originalPosition.getProject();
        if (AngularJSReferencesContributor.UI_VIEW_PATTERN.accepts(originalPosition)) {
            final FileBasedIndex instance = FileBasedIndex.getInstance();
            final Collection<String> keys = instance.getAllKeys(AngularUiRouterViewsIndex.UI_ROUTER_VIEWS_CACHE_INDEX, project);
            addCompletionVariants(result, keys, " (angular-ui-router ui-view)");
        } else {
            originalPosition = originalPosition instanceof LeafPsiElement && ((LeafPsiElement) originalPosition).getElementType() == JSTokenTypes.STRING_LITERAL ? originalPosition.getParent() : originalPosition;
            if (AngularJSReferencesContributor.MODULE_PATTERN.accepts(originalPosition) || AngularJSReferencesContributor.MODULE_DEPENDENCY_PATTERN.accepts(originalPosition)) {
                final Collection<String> keys = AngularIndexUtil.getAllKeys(AngularModuleIndex.KEY, project);
                addCompletionVariants(result, keys, " (AngularJS module)");
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

Aggregations

FileBasedIndex (com.intellij.util.indexing.FileBasedIndex)27 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)13 NotNull (org.jetbrains.annotations.NotNull)10 PsiFile (com.intellij.psi.PsiFile)6 Project (com.intellij.openapi.project.Project)5 PsiElement (com.intellij.psi.PsiElement)4 JSElement (com.intellij.lang.javascript.psi.JSElement)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 DetectedFrameworkDescription (com.intellij.framework.detection.DetectedFrameworkDescription)2 DetectionExcludesConfiguration (com.intellij.framework.detection.DetectionExcludesConfiguration)2 JSOffsetBasedImplicitElement (com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement)2 JSImplicitElementImpl (com.intellij.lang.javascript.psi.stubs.impl.JSImplicitElementImpl)2 Module (com.intellij.openapi.module.Module)2 PsiManager (com.intellij.psi.PsiManager)2 XmlFile (com.intellij.psi.xml.XmlFile)2 HashMap (com.intellij.util.containers.HashMap)2 HashSet (com.intellij.util.containers.HashSet)2