Search in sources :

Example 16 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class MethodOrClosureScopeChooser method create.

/**
   * @param callback is invoked if any scope was chosen. The first arg is this scope and the second arg is a psielement to search for (super method of chosen method or
   *                 variable if the scope is a closure)
   */
public static JBPopup create(List<? extends GrParametersOwner> scopes, final Editor editor, final JBPopupOwner popupRef, final PairFunction<GrParametersOwner, PsiElement, Object> callback) {
    final JPanel panel = new JPanel(new BorderLayout());
    final JCheckBox superMethod = new JCheckBox(USE_SUPER_METHOD_OF, true);
    superMethod.setMnemonic('U');
    panel.add(superMethod, BorderLayout.SOUTH);
    final JBList list = new JBList(scopes.toArray());
    list.setVisibleRowCount(5);
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            final String text;
            if (value instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) value;
                text = PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
                final int flags = Iconable.ICON_FLAG_VISIBILITY;
                final Icon icon = method.getIcon(flags);
                if (icon != null)
                    setIcon(icon);
            } else {
                LOG.assertTrue(value instanceof GrClosableBlock);
                setIcon(JetgroovyIcons.Groovy.Groovy_16x16);
                text = "{...}";
            }
            setText(text);
            return this;
        }
    });
    list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    final List<RangeHighlighter> highlighters = new ArrayList<>();
    final TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    list.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(final ListSelectionEvent e) {
            final GrParametersOwner selectedMethod = (GrParametersOwner) list.getSelectedValue();
            if (selectedMethod == null)
                return;
            dropHighlighters(highlighters);
            updateView(selectedMethod, editor, attributes, highlighters, superMethod);
        }
    });
    updateView(scopes.get(0), editor, attributes, highlighters, superMethod);
    final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(list);
    scrollPane.setBorder(null);
    panel.add(scrollPane, BorderLayout.CENTER);
    final List<Pair<ActionListener, KeyStroke>> keyboardActions = Collections.singletonList(Pair.<ActionListener, KeyStroke>create(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final GrParametersOwner ToSearchIn = (GrParametersOwner) list.getSelectedValue();
            final JBPopup popup = popupRef.get();
            if (popup != null && popup.isVisible()) {
                popup.cancel();
            }
            final PsiElement toSearchFor;
            if (ToSearchIn instanceof GrMethod) {
                final GrMethod method = (GrMethod) ToSearchIn;
                toSearchFor = superMethod.isEnabled() && superMethod.isSelected() ? method.findDeepestSuperMethod() : method;
            } else {
                toSearchFor = superMethod.isEnabled() && superMethod.isSelected() ? ToSearchIn.getParent() : null;
            }
            IdeFocusManager.findInstance().doWhenFocusSettlesDown(() -> callback.fun(ToSearchIn, toSearchFor), ModalityState.current());
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)));
    return JBPopupFactory.getInstance().createComponentPopupBuilder(panel, list).setTitle("Introduce parameter to").setMovable(false).setResizable(false).setRequestFocus(true).setKeyboardActions(keyboardActions).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            dropHighlighters(highlighters);
        }
    }).createPopup();
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) ListSelectionEvent(javax.swing.event.ListSelectionEvent) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) ListSelectionListener(javax.swing.event.ListSelectionListener) ActionListener(java.awt.event.ActionListener) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) JBList(com.intellij.ui.components.JBList)

Example 17 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class PyUnusedLocalInspectionVisitor method visitPyStringLiteralExpression.

@Override
public void visitPyStringLiteralExpression(PyStringLiteralExpression pyString) {
    final ScopeOwner owner = ScopeUtil.getScopeOwner(pyString);
    if (owner != null && !(owner instanceof PsiFile)) {
        final PyStatement instrAnchor = PsiTreeUtil.getParentOfType(pyString, PyStatement.class);
        if (instrAnchor == null)
            return;
        final Instruction[] instructions = ControlFlowCache.getControlFlow(owner).getInstructions();
        final int startInstruction = ControlFlowUtil.findInstructionNumberByElement(instructions, instrAnchor);
        if (startInstruction < 0)
            return;
        final Project project = pyString.getProject();
        final List<Pair<PsiElement, TextRange>> pairs = InjectedLanguageManager.getInstance(project).getInjectedPsiFiles(pyString);
        if (pairs != null) {
            for (Pair<PsiElement, TextRange> pair : pairs) {
                pair.getFirst().accept(new PyRecursiveElementVisitor() {

                    @Override
                    public void visitPyReferenceExpression(PyReferenceExpression expr) {
                        final PyExpression qualifier = expr.getQualifier();
                        if (qualifier != null) {
                            qualifier.accept(this);
                            return;
                        }
                        final String name = expr.getName();
                        if (name != null) {
                            analyzeReadsInScope(name, owner, instructions, startInstruction, pyString);
                        }
                    }
                });
            }
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) Project(com.intellij.openapi.project.Project) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 18 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class ProcessWithConsoleRunner method getHighlightedStringsInConsole.

/**
   * Gets highlighted information from test console. Some parts of output (like file links) may be highlighted, and you need to check them.
   *
   * @return pair of [[ranges], [texts]] where range is [from,to] in doc. for each region, and "text" is text extracted from this region.
   * For example assume that in document "spam eggs ham" words "ham" and "spam" are highlighted.
   * You should have 2 ranges (0, 4) and (10, 13) and 2 strings (spam and ham)
   */
@NotNull
public Pair<List<Pair<Integer, Integer>>, List<String>> getHighlightedStringsInConsole() {
    final List<String> resultStrings = new ArrayList<>();
    final List<Pair<Integer, Integer>> resultRanges = new ArrayList<>();
    ApplicationManager.getApplication().invokeAndWait(() -> {
        myConsole.flushDeferredText();
        final Editor editor = myConsole.getEditor();
        for (final RangeHighlighter highlighter : editor.getMarkupModel().getAllHighlighters()) {
            if (highlighter instanceof RangeHighlighterEx) {
                final int start = ((RangeHighlighterEx) highlighter).getAffectedAreaStartOffset();
                final int end = ((RangeHighlighterEx) highlighter).getAffectedAreaEndOffset();
                resultRanges.add(Pair.create(start, end));
                resultStrings.add(editor.getDocument().getText().substring(start, end));
            }
        }
    }, ModalityState.NON_MODAL);
    return Pair.create(resultRanges, resultStrings);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) ArrayList(java.util.ArrayList) Editor(com.intellij.openapi.editor.Editor) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class PyAssignmentMappingTest method testTupleMapped.

public void testTupleMapped() throws Exception {
    Map<String, PsiElement> marks = loadTest();
    final int PAIR_NUM = 2;
    Assert.assertEquals(PAIR_NUM * 2, marks.size());
    PsiElement[] srcs = new PsiElement[PAIR_NUM];
    PsiElement[] dsts = new PsiElement[PAIR_NUM];
    for (int i = 0; i < PAIR_NUM; i += 1) {
        // ident -> target expr
        PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(dst instanceof PyTargetExpression);
        dsts[i] = dst;
        // ident -> target expr
        PsiElement src = marks.get("<src" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(src instanceof PyExpression);
        srcs[i] = src;
    }
    // tuple expr -> assignment
    PyAssignmentStatement stmt = (PyAssignmentStatement) srcs[0].getParent().getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(PAIR_NUM, mapping.size());
    for (int i = 0; i < PAIR_NUM; i += 1) {
        Pair<PyExpression, PyExpression> pair = mapping.get(i);
        Assert.assertEquals(dsts[i], pair.getFirst());
        Assert.assertEquals(srcs[i], pair.getSecond());
    }
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyExpression(com.jetbrains.python.psi.PyExpression) PyAssignmentStatement(com.jetbrains.python.psi.PyAssignmentStatement) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 20 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class PyAssignmentMappingTest method testTuplePack.

public void testTuplePack() throws Exception {
    Map<String, PsiElement> marks = loadTest();
    final int SRC_NUM = 2;
    Assert.assertEquals(SRC_NUM + 1, marks.size());
    PsiElement[] srcs = new PsiElement[SRC_NUM];
    for (int i = 0; i < SRC_NUM; i += 1) {
        // ident -> target expr
        PsiElement src = marks.get("<src" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(src instanceof PyExpression);
        srcs[i] = src;
    }
    // ident -> target expr
    PsiElement dst = marks.get("<dst>").getParent();
    PyAssignmentStatement stmt = (PyAssignmentStatement) dst.getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(1, mapping.size());
    Pair<PyExpression, PyExpression> pair = mapping.get(0);
    Assert.assertEquals(dst, pair.getFirst());
    for (PsiElement src : srcs) {
        // numeric expr -> tuple
        Assert.assertEquals(src.getParent(), pair.getSecond());
    }
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PyAssignmentStatement(com.jetbrains.python.psi.PyAssignmentStatement) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Aggregations

Pair (com.intellij.openapi.util.Pair)391 NotNull (org.jetbrains.annotations.NotNull)131 ArrayList (java.util.ArrayList)83 VirtualFile (com.intellij.openapi.vfs.VirtualFile)68 Project (com.intellij.openapi.project.Project)60 Nullable (org.jetbrains.annotations.Nullable)59 PsiElement (com.intellij.psi.PsiElement)47 TextRange (com.intellij.openapi.util.TextRange)43 File (java.io.File)42 List (java.util.List)37 Module (com.intellij.openapi.module.Module)34 ContainerUtil (com.intellij.util.containers.ContainerUtil)26 PsiFile (com.intellij.psi.PsiFile)25 StringUtil (com.intellij.openapi.util.text.StringUtil)20 IOException (java.io.IOException)19 java.util (java.util)19 ApplicationManager (com.intellij.openapi.application.ApplicationManager)18 THashSet (gnu.trove.THashSet)17 Map (java.util.Map)17 Document (com.intellij.openapi.editor.Document)15