Search in sources :

Example 1 with ScopeHighlighter

use of com.intellij.codeInsight.unwrap.ScopeHighlighter in project intellij-community by JetBrains.

the class IntroduceTargetChooser method showIntroduceTargetChooser.

public static <T extends IntroduceTarget> void showIntroduceTargetChooser(@NotNull Editor editor, @NotNull List<T> expressions, @NotNull Pass<T> callback, @NotNull @Nls String title, int selection) {
    AtomicReference<ScopeHighlighter> highlighter = new AtomicReference<>(new ScopeHighlighter(editor));
    CollectionListModel<T> model = new CollectionListModel<>(expressions);
    JBList<T> list = new JBList<>(model);
    // Set the accessible name so that screen readers announce the list tile (e.g. "Expression Types list").
    AccessibleContextUtil.setName(list, title);
    list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (selection > -1)
        list.setSelectedIndex(selection);
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            IntroduceTarget expr = (T) value;
            if (expr.isValid()) {
                String text = expr.render();
                int firstNewLinePos = text.indexOf('\n');
                String trimmedText = text.substring(0, firstNewLinePos != -1 ? firstNewLinePos : Math.min(100, text.length()));
                if (trimmedText.length() != text.length())
                    trimmedText += " ...";
                setText(trimmedText);
            } else {
                setForeground(JBColor.RED);
                setText("Invalid");
            }
            return rendererComponent;
        }
    });
    list.addListSelectionListener(e -> {
        ScopeHighlighter h = highlighter.get();
        if (h == null)
            return;
        h.dropHighlight();
        T expr = list.getSelectedValue();
        if (expr != null && expr.isValid()) {
            TextRange range = expr.getTextRange();
            h.highlight(Pair.create(range, Collections.singletonList(range)));
        }
    });
    JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle(title).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
        T expr = list.getSelectedValue();
        if (expr != null && expr.isValid()) {
            callback.pass(expr);
        }
    }).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            highlighter.getAndSet(null).dropHighlight();
        }
    }).createPopup().showInBestPositionFor(editor);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) TextRange(com.intellij.openapi.util.TextRange) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) PsiIntroduceTarget(com.intellij.refactoring.introduce.PsiIntroduceTarget) IntroduceTarget(com.intellij.refactoring.introduce.IntroduceTarget) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) ScopeHighlighter(com.intellij.codeInsight.unwrap.ScopeHighlighter) JBList(com.intellij.ui.components.JBList) CollectionListModel(com.intellij.ui.CollectionListModel)

Example 2 with ScopeHighlighter

use of com.intellij.codeInsight.unwrap.ScopeHighlighter in project intellij-community by JetBrains.

the class IntentionHintComponent method recreateMyPopup.

private void recreateMyPopup(@NotNull ListPopupStep step) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    if (myPopup != null) {
        Disposer.dispose(myPopup);
    }
    myPopup = JBPopupFactory.getInstance().createListPopup(step);
    if (myPopup instanceof WizardPopup) {
        Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts(IdeActions.ACTION_SHOW_INTENTION_ACTIONS);
        for (Shortcut shortcut : shortcuts) {
            if (shortcut instanceof KeyboardShortcut) {
                KeyboardShortcut keyboardShortcut = (KeyboardShortcut) shortcut;
                if (keyboardShortcut.getSecondKeyStroke() == null) {
                    ((WizardPopup) myPopup).registerAction("activateSelectedElement", keyboardShortcut.getFirstKeyStroke(), new AbstractAction() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            myPopup.handleSelect(true);
                        }
                    });
                }
            }
        }
    }
    boolean committed = PsiDocumentManager.getInstance(myFile.getProject()).isCommitted(myEditor.getDocument());
    final PsiFile injectedFile = committed ? InjectedLanguageUtil.findInjectedPsiNoCommit(myFile, myEditor.getCaretModel().getOffset()) : null;
    final Editor injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(myEditor, injectedFile);
    final ScopeHighlighter highlighter = new ScopeHighlighter(myEditor);
    final ScopeHighlighter injectionHighlighter = new ScopeHighlighter(injectedEditor);
    myPopup.addListener(new JBPopupListener.Adapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            highlighter.dropHighlight();
            injectionHighlighter.dropHighlight();
            myPopupShown = false;
        }
    });
    myPopup.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(@NotNull ListSelectionEvent e) {
            final Object source = e.getSource();
            highlighter.dropHighlight();
            injectionHighlighter.dropHighlight();
            if (source instanceof DataProvider) {
                final Object selectedItem = PlatformDataKeys.SELECTED_ITEM.getData((DataProvider) source);
                if (selectedItem instanceof IntentionActionWithTextCaching) {
                    final IntentionAction action = ((IntentionActionWithTextCaching) selectedItem).getAction();
                    if (action instanceof SuppressIntentionActionFromFix) {
                        if (injectedFile != null && ((SuppressIntentionActionFromFix) action).isShouldBeAppliedToInjectionHost() == ThreeState.NO) {
                            final PsiElement at = injectedFile.findElementAt(injectedEditor.getCaretModel().getOffset());
                            final PsiElement container = ((SuppressIntentionActionFromFix) action).getContainer(at);
                            if (container != null) {
                                injectionHighlighter.highlight(container, Collections.singletonList(container));
                            }
                        } else {
                            final PsiElement at = myFile.findElementAt(myEditor.getCaretModel().getOffset());
                            final PsiElement container = ((SuppressIntentionActionFromFix) action).getContainer(at);
                            if (container != null) {
                                highlighter.highlight(container, Collections.singletonList(container));
                            }
                        }
                    }
                }
            }
        }
    });
    if (myEditor.isOneLineMode()) {
        // hide popup on combobox popup show
        final Container ancestor = SwingUtilities.getAncestorOfClass(JComboBox.class, myEditor.getContentComponent());
        if (ancestor != null) {
            final JComboBox comboBox = (JComboBox) ancestor;
            myOuterComboboxPopupListener = new PopupMenuListenerAdapter() {

                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    hide();
                }
            };
            comboBox.addPopupMenuListener(myOuterComboboxPopupListener);
        }
    }
    Disposer.register(this, myPopup);
    Disposer.register(myPopup, new Disposable() {

        @Override
        public void dispose() {
            ApplicationManager.getApplication().assertIsDispatchThread();
        }
    });
}
Also used : ActionEvent(java.awt.event.ActionEvent) ListSelectionEvent(javax.swing.event.ListSelectionEvent) PopupMenuEvent(javax.swing.event.PopupMenuEvent) PsiFile(com.intellij.psi.PsiFile) ScopeHighlighter(com.intellij.codeInsight.unwrap.ScopeHighlighter) PsiElement(com.intellij.psi.PsiElement) Disposable(com.intellij.openapi.Disposable) SuppressIntentionActionFromFix(com.intellij.codeInspection.SuppressIntentionActionFromFix) PopupMenuListenerAdapter(com.intellij.ui.PopupMenuListenerAdapter) WizardPopup(com.intellij.ui.popup.WizardPopup) ListSelectionListener(javax.swing.event.ListSelectionListener) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) BaseRefactoringIntentionAction(com.intellij.refactoring.BaseRefactoringIntentionAction) Editor(com.intellij.openapi.editor.Editor)

Aggregations

ScopeHighlighter (com.intellij.codeInsight.unwrap.ScopeHighlighter)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 SuppressIntentionActionFromFix (com.intellij.codeInspection.SuppressIntentionActionFromFix)1 Disposable (com.intellij.openapi.Disposable)1 Editor (com.intellij.openapi.editor.Editor)1 JBPopupAdapter (com.intellij.openapi.ui.popup.JBPopupAdapter)1 LightweightWindowEvent (com.intellij.openapi.ui.popup.LightweightWindowEvent)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 BaseRefactoringIntentionAction (com.intellij.refactoring.BaseRefactoringIntentionAction)1 IntroduceTarget (com.intellij.refactoring.introduce.IntroduceTarget)1 PsiIntroduceTarget (com.intellij.refactoring.introduce.PsiIntroduceTarget)1 CollectionListModel (com.intellij.ui.CollectionListModel)1 PopupMenuListenerAdapter (com.intellij.ui.PopupMenuListenerAdapter)1 JBList (com.intellij.ui.components.JBList)1 WizardPopup (com.intellij.ui.popup.WizardPopup)1 ActionEvent (java.awt.event.ActionEvent)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1