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();
}
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);
}
}
});
}
}
}
}
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);
}
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());
}
}
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());
}
}
Aggregations