Search in sources :

Example 1 with Matcher

use of com.intellij.util.text.Matcher in project intellij-community by JetBrains.

the class VariantsCompletionAction method calcWords.

private static String[] calcWords(final String prefix, Editor editor) {
    final Matcher matcher = NameUtil.buildMatcher(prefix, 0, true, true);
    final Set<String> words = new HashSet<>();
    CharSequence chars = editor.getDocument().getCharsSequence();
    IdTableBuilding.scanWords(new IdTableBuilding.ScanWordProcessor() {

        @Override
        public void run(final CharSequence chars, @Nullable char[] charsArray, final int start, final int end) {
            final String word = chars.subSequence(start, end).toString();
            if (matcher.matches(word)) {
                words.add(word);
            }
        }
    }, chars, 0, chars.length());
    ArrayList<String> sortedWords = new ArrayList<>(words);
    Collections.sort(sortedWords);
    return ArrayUtil.toStringArray(sortedWords);
}
Also used : Matcher(com.intellij.util.text.Matcher) ArrayList(java.util.ArrayList) IdTableBuilding(com.intellij.psi.impl.cache.impl.id.IdTableBuilding) HashSet(java.util.HashSet)

Example 2 with Matcher

use of com.intellij.util.text.Matcher in project intellij-community by JetBrains.

the class ChooseByNameBase method calcSelectedIndex.

@VisibleForTesting
public int calcSelectedIndex(Object[] modelElements, String trimmedText) {
    if (myModel instanceof Comparator) {
        return 0;
    }
    Matcher matcher = buildPatternMatcher(transformPattern(trimmedText));
    final String statContext = statisticsContext();
    Comparator<Object> itemComparator = Comparator.comparing(e -> trimmedText.equalsIgnoreCase(myModel.getElementName(e))).thenComparing(e -> matchingDegree(matcher, e)).thenComparing(e -> getUseCount(statContext, e)).reversed();
    int bestPosition = 0;
    while (bestPosition < modelElements.length - 1 && isSpecialElement(modelElements[bestPosition])) bestPosition++;
    for (int i = 1; i < modelElements.length; i++) {
        final Object modelElement = modelElements[i];
        if (isSpecialElement(modelElement))
            continue;
        if (itemComparator.compare(modelElement, modelElements[bestPosition]) < 0) {
            bestPosition = i;
        }
    }
    return bestPosition;
}
Also used : ToolWindowManager(com.intellij.openapi.wm.ToolWindowManager) AllIcons(com.intellij.icons.AllIcons) ReadTask(com.intellij.openapi.progress.util.ReadTask) ModalityState(com.intellij.openapi.application.ModalityState) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Task(com.intellij.openapi.progress.Task) TooManyUsagesStatus(com.intellij.openapi.progress.util.TooManyUsagesStatus) Disposer(com.intellij.openapi.util.Disposer) Matcher(com.intellij.util.text.Matcher) Logger(com.intellij.openapi.diagnostic.Logger) ListSelectionEvent(javax.swing.event.ListSelectionEvent) AttributeSet(javax.swing.text.AttributeSet) ProgressManager(com.intellij.openapi.progress.ProgressManager) DumbService(com.intellij.openapi.project.DumbService) ToolWindow(com.intellij.openapi.wm.ToolWindow) StatisticsInfo(com.intellij.psi.statistics.StatisticsInfo) WindowManager(com.intellij.openapi.wm.WindowManager) PopupOwner(com.intellij.ui.popup.PopupOwner) MacIntelliJTextFieldUI(com.intellij.ide.ui.laf.intellij.MacIntelliJTextFieldUI) AbstractPopup(com.intellij.ui.popup.AbstractPopup) BadLocationException(javax.swing.text.BadLocationException) com.intellij.openapi.ui.popup(com.intellij.openapi.ui.popup) PlainDocument(javax.swing.text.PlainDocument) KeymapUtil(com.intellij.openapi.keymap.KeymapUtil) com.intellij.ui(com.intellij.ui) UsageViewManagerImpl(com.intellij.usages.impl.UsageViewManagerImpl) UnknownFileType(com.intellij.openapi.fileTypes.UnknownFileType) IdeBundle(com.intellij.ide.IdeBundle) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) CopyReferenceAction(com.intellij.ide.actions.CopyReferenceAction) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) com.intellij.util.ui(com.intellij.util.ui) java.awt.event(java.awt.event) ApplicationManager(com.intellij.openapi.application.ApplicationManager) EmptyBorder(javax.swing.border.EmptyBorder) Registry(com.intellij.openapi.util.registry.Registry) WindowManagerEx(com.intellij.openapi.wm.ex.WindowManagerEx) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) java.util(java.util) KeymapManager(com.intellij.openapi.keymap.KeymapManager) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) MatcherHolder(com.intellij.util.text.MatcherHolder) NonNls(org.jetbrains.annotations.NonNls) UsageInfo(com.intellij.usageView.UsageInfo) MacIntelliJTextBorder(com.intellij.ide.ui.laf.intellij.MacIntelliJTextBorder) ProgressIndicatorUtils(com.intellij.openapi.progress.util.ProgressIndicatorUtils) DaemonCodeAnalyzer(com.intellij.codeInsight.daemon.DaemonCodeAnalyzer) ContainerUtil(com.intellij.util.containers.ContainerUtil) NameUtil(com.intellij.psi.codeStyle.NameUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) StatisticsManager(com.intellij.psi.statistics.StatisticsManager) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor) DarculaTextFieldUI(com.intellij.ide.ui.laf.darcula.ui.DarculaTextFieldUI) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PopupPositionManager(com.intellij.ui.popup.PopupPositionManager) DocumentEvent(javax.swing.event.DocumentEvent) DataManager(com.intellij.ide.DataManager) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) JBList(com.intellij.ui.components.JBList) Patches(com.intellij.Patches) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.usages(com.intellij.usages) GotoFileAction(com.intellij.ide.actions.GotoFileAction) FileTypeManagerEx(com.intellij.openapi.fileTypes.ex.FileTypeManagerEx) Disposable(com.intellij.openapi.Disposable) SystemInfo(com.intellij.openapi.util.SystemInfo) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) Pair(com.intellij.openapi.util.Pair) MnemonicHelper(com.intellij.openapi.MnemonicHelper) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ListSelectionListener(javax.swing.event.ListSelectionListener) DarculaTextBorder(com.intellij.ide.ui.laf.darcula.ui.DarculaTextBorder) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) Matcher(com.intellij.util.text.Matcher) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Matcher

use of com.intellij.util.text.Matcher in project intellij-community by JetBrains.

the class GotoActionItemProvider method processIntentions.

private boolean processIntentions(String pattern, Processor<MatchedValue> consumer, DataContext dataContext) {
    MinusculeMatcher matcher = NameUtil.buildMatcher("*" + pattern, NameUtil.MatchingCaseSensitivity.NONE);
    Map<String, ApplyIntentionAction> intentionMap = myIntentions.getValue();
    JBIterable<ActionWrapper> intentions = JBIterable.from(intentionMap.keySet()).transform(intentionText -> {
        ApplyIntentionAction intentionAction = intentionMap.get(intentionText);
        if (myModel.actionMatches(pattern, matcher, intentionAction) == MatchMode.NONE)
            return null;
        return new ActionWrapper(intentionAction, intentionText, MatchMode.INTENTION, dataContext);
    }).filter(Condition.NOT_NULL);
    return processItems(pattern, intentions, consumer);
}
Also used : CollectConsumer(com.intellij.util.CollectConsumer) java.util(java.util) JBIterable(com.intellij.util.containers.JBIterable) ApplyIntentionAction(com.intellij.ide.actions.ApplyIntentionAction) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) ContainerUtil(com.intellij.util.containers.ContainerUtil) ActionFromOptionDescriptorProvider(com.intellij.ide.ui.search.ActionFromOptionDescriptorProvider) NameUtil(com.intellij.psi.codeStyle.NameUtil) SearchableOptionsRegistrarImpl(com.intellij.ide.ui.search.SearchableOptionsRegistrarImpl) Project(com.intellij.openapi.project.Project) SearchableOptionsRegistrar(com.intellij.ide.ui.search.SearchableOptionsRegistrar) Matcher(com.intellij.util.text.Matcher) DataManager(com.intellij.ide.DataManager) ProgressManager(com.intellij.openapi.progress.ProgressManager) StringUtil(com.intellij.openapi.util.text.StringUtil) NotNullLazyValue(com.intellij.openapi.util.NotNullLazyValue) ActionManagerImpl(com.intellij.openapi.actionSystem.impl.ActionManagerImpl) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) GotoActionModel(com.intellij.ide.util.gotoByName.GotoActionModel) OptionDescription(com.intellij.ide.ui.search.OptionDescription) Processor(com.intellij.util.Processor) OptionsTopHitProvider(com.intellij.ide.ui.OptionsTopHitProvider) NotNull(org.jetbrains.annotations.NotNull) SearchTopHitProvider(com.intellij.ide.SearchTopHitProvider) Condition(com.intellij.openapi.util.Condition) ApplyIntentionAction(com.intellij.ide.actions.ApplyIntentionAction) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher)

Example 4 with Matcher

use of com.intellij.util.text.Matcher in project intellij-community by JetBrains.

the class InspectionListCellRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean sel, boolean focus) {
    final BorderLayout layout = new BorderLayout();
    layout.setHgap(5);
    final JPanel panel = new JPanel(layout);
    panel.setOpaque(true);
    final Color bg = sel ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground();
    final Color fg = sel ? UIUtil.getListSelectionForeground() : UIUtil.getListForeground();
    panel.setBackground(bg);
    panel.setForeground(fg);
    if (value instanceof InspectionToolWrapper) {
        final InspectionToolWrapper toolWrapper = (InspectionToolWrapper) value;
        final String inspectionName = "  " + toolWrapper.getDisplayName();
        final String groupName = StringUtil.join(toolWrapper.getGroupPath(), " | ");
        final String matchingText = inspectionName + "|" + groupName;
        Matcher matcher = MatcherHolder.getAssociatedMatcher(list);
        List<TextRange> fragments = matcher == null ? null : ((MinusculeMatcher) matcher).matchingFragments(matchingText);
        List<TextRange> adjustedFragments = new ArrayList<>();
        if (fragments != null) {
            adjustedFragments.addAll(fragments);
        }
        final int splitPoint = adjustRanges(adjustedFragments, inspectionName.length() + 1);
        final SimpleColoredComponent c = new SimpleColoredComponent();
        final boolean matchHighlighting = Registry.is("ide.highlight.match.in.selected.only") && !sel;
        if (matchHighlighting) {
            c.append(inspectionName, myPlain);
        } else {
            final List<TextRange> ranges = adjustedFragments.subList(0, splitPoint);
            SpeedSearchUtil.appendColoredFragments(c, inspectionName, ranges, sel ? mySelected : myPlain, myHighlighted);
        }
        panel.add(c, BorderLayout.WEST);
        final SimpleColoredComponent group = new SimpleColoredComponent();
        if (matchHighlighting) {
            group.append(groupName, SimpleTextAttributes.GRAYED_ATTRIBUTES);
        } else {
            final SimpleTextAttributes attributes = sel ? mySelected : SimpleTextAttributes.GRAYED_ATTRIBUTES;
            final List<TextRange> ranges = adjustedFragments.subList(splitPoint, adjustedFragments.size());
            SpeedSearchUtil.appendColoredFragments(group, groupName, ranges, attributes, myHighlighted);
        }
        final JPanel right = new JPanel(new BorderLayout());
        right.setBackground(bg);
        right.setForeground(fg);
        right.add(group, BorderLayout.CENTER);
        final JLabel icon = new JLabel(getIcon(toolWrapper));
        icon.setBackground(bg);
        icon.setForeground(fg);
        right.add(icon, BorderLayout.EAST);
        panel.add(right, BorderLayout.EAST);
    } else {
        // E.g. "..." item
        return value == ChooseByNameBase.NON_PREFIX_SEPARATOR ? ChooseByNameBase.renderNonPrefixSeparatorComponent(UIUtil.getListBackground()) : super.getListCellRendererComponent(list, value, index, sel, focus);
    }
    return panel;
}
Also used : MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) Matcher(com.intellij.util.text.Matcher) JBColor(com.intellij.ui.JBColor) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) InspectionToolWrapper(com.intellij.codeInspection.ex.InspectionToolWrapper) SimpleColoredComponent(com.intellij.ui.SimpleColoredComponent)

Example 5 with Matcher

use of com.intellij.util.text.Matcher in project intellij-community by JetBrains.

the class PluginsTableRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if (myPluginDescriptor != null) {
        Couple<Color> colors = UIUtil.getCellColors(table, isSelected, row, column);
        Color fg = colors.getFirst();
        final Color background = colors.getSecond();
        Color grayedFg = isSelected ? fg : new JBColor(Gray._130, Gray._120);
        myPanel.setBackground(background);
        myName.setForeground(fg);
        myCategory.setForeground(grayedFg);
        myStatus.setForeground(grayedFg);
        myLastUpdated.setForeground(grayedFg);
        myDownloads.setForeground(grayedFg);
        myName.clear();
        myName.setOpaque(false);
        myCategory.clear();
        myCategory.setOpaque(false);
        String pluginName = myPluginDescriptor.getName() + "  ";
        Object query = table.getClientProperty(SpeedSearchSupply.SEARCH_QUERY_KEY);
        SimpleTextAttributes attr = new SimpleTextAttributes(UIUtil.getListBackground(isSelected), UIUtil.getListForeground(isSelected), JBColor.RED, SimpleTextAttributes.STYLE_PLAIN);
        Matcher matcher = NameUtil.buildMatcher("*" + query, NameUtil.MatchingCaseSensitivity.NONE);
        if (query instanceof String) {
            SpeedSearchUtil.appendColoredFragmentForMatcher(pluginName, myName, attr, matcher, UIUtil.getTableBackground(isSelected), true);
        } else {
            myName.append(pluginName);
        }
        String category = myPluginDescriptor.getCategory() == null ? null : StringUtil.toUpperCase(myPluginDescriptor.getCategory());
        if (category != null) {
            if (query instanceof String) {
                SpeedSearchUtil.appendColoredFragmentForMatcher(category, myCategory, attr, matcher, UIUtil.getTableBackground(isSelected), true);
            } else {
                myCategory.append(category);
            }
        } else if (!myPluginsView) {
            myCategory.append(AvailablePluginsManagerMain.N_A);
        }
        myStatus.setIcon(AllIcons.Nodes.Plugin);
        if (myPluginDescriptor.isBundled()) {
            myCategory.append(" [Bundled]");
            myStatus.setIcon(AllIcons.Nodes.PluginJB);
        }
        String vendor = myPluginDescriptor.getVendor();
        if (vendor != null && StringUtil.containsIgnoreCase(vendor, "jetbrains")) {
            myStatus.setIcon(AllIcons.Nodes.PluginJB);
        }
        String downloads = myPluginDescriptor.getDownloads();
        if (downloads != null && myPluginDescriptor instanceof PluginNode) {
            if (downloads.length() > 3) {
                downloads = new DecimalFormat("#,###").format(Integer.parseInt(downloads));
            }
            myDownloads.setText(downloads);
            myRating.setRate(((PluginNode) myPluginDescriptor).getRating());
            myLastUpdated.setText(DateFormatUtil.formatBetweenDates(((PluginNode) myPluginDescriptor).getDate(), System.currentTimeMillis()));
        }
        // plugin state-dependent rendering
        PluginId pluginId = myPluginDescriptor.getPluginId();
        IdeaPluginDescriptor installed = PluginManager.getPlugin(pluginId);
        if (installed != null && ((IdeaPluginDescriptorImpl) installed).isDeleted()) {
            // existing plugin uninstalled (both views)
            myStatus.setIcon(AllIcons.Nodes.PluginRestart);
            if (!isSelected)
                myName.setForeground(FileStatus.DELETED.getColor());
            myPanel.setToolTipText(IdeBundle.message("plugin.manager.uninstalled.tooltip"));
        } else if (ourState.wasInstalled(pluginId)) {
            // new plugin installed (both views)
            myStatus.setIcon(AllIcons.Nodes.PluginRestart);
            if (!isSelected)
                myName.setForeground(FileStatus.ADDED.getColor());
            myPanel.setToolTipText(IdeBundle.message("plugin.manager.installed.tooltip"));
        } else if (ourState.wasUpdated(pluginId)) {
            // existing plugin updated (both views)
            myStatus.setIcon(AllIcons.Nodes.PluginRestart);
            if (!isSelected)
                myName.setForeground(FileStatus.ADDED.getColor());
            myPanel.setToolTipText(IdeBundle.message("plugin.manager.updated.tooltip"));
        } else if (ourState.hasNewerVersion(pluginId)) {
            // existing plugin has a newer version (both views)
            myStatus.setIcon(AllIcons.Nodes.Pluginobsolete);
            if (!isSelected)
                myName.setForeground(FileStatus.MODIFIED.getColor());
            if (!myPluginsView && installed != null) {
                myPanel.setToolTipText(IdeBundle.message("plugin.manager.new.version.tooltip", installed.getVersion()));
            } else {
                myPanel.setToolTipText(IdeBundle.message("plugin.manager.update.available.tooltip"));
            }
        } else if (isIncompatible(myPluginDescriptor, table.getModel())) {
            // a plugin is incompatible with current installation (both views)
            if (!isSelected)
                myName.setForeground(JBColor.RED);
            myPanel.setToolTipText(whyIncompatible(myPluginDescriptor, table.getModel()));
        } else if (!myPluginDescriptor.isEnabled() && myPluginsView) {
            // a plugin is disabled (plugins view only)
            myStatus.setIcon(IconLoader.getDisabledIcon(myStatus.getIcon()));
        }
    }
    return myPanel;
}
Also used : Matcher(com.intellij.util.text.Matcher) JBColor(com.intellij.ui.JBColor) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) DecimalFormat(java.text.DecimalFormat) JBColor(com.intellij.ui.JBColor) PluginId(com.intellij.openapi.extensions.PluginId)

Aggregations

Matcher (com.intellij.util.text.Matcher)8 MinusculeMatcher (com.intellij.psi.codeStyle.MinusculeMatcher)6 DataManager (com.intellij.ide.DataManager)3 ActionFromOptionDescriptorProvider (com.intellij.ide.ui.search.ActionFromOptionDescriptorProvider)3 OptionDescription (com.intellij.ide.ui.search.OptionDescription)3 SearchableOptionsRegistrarImpl (com.intellij.ide.ui.search.SearchableOptionsRegistrarImpl)3 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 ProgressManager (com.intellij.openapi.progress.ProgressManager)3 Project (com.intellij.openapi.project.Project)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 NameUtil (com.intellij.psi.codeStyle.NameUtil)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)3 java.util (java.util)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 SearchTopHitProvider (com.intellij.ide.SearchTopHitProvider)2 ApplyIntentionAction (com.intellij.ide.actions.ApplyIntentionAction)2 OptionsTopHitProvider (com.intellij.ide.ui.OptionsTopHitProvider)2 SearchableOptionsRegistrar (com.intellij.ide.ui.search.SearchableOptionsRegistrar)2