Search in sources :

Example 1 with TypeSelectorManager

use of com.intellij.refactoring.ui.TypeSelectorManager in project intellij-community by JetBrains.

the class SuggestedParamTypesTest method doTest.

private void doTest(String... types) throws Exception {
    configureByFile(BASE_PATH + getTestName(false) + ".java");
    final Editor editor = getEditor();
    final PsiFile file = getFile();
    final Project project = getProject();
    int startOffset = editor.getSelectionModel().getSelectionStart();
    int endOffset = editor.getSelectionModel().getSelectionEnd();
    PsiElement[] elements;
    PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
    if (expr != null) {
        elements = new PsiElement[] { expr };
    } else {
        elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
    }
    assertTrue(elements.length > 0);
    final ExtractMethodProcessor processor = new ExtractMethodProcessor(project, editor, elements, null, "Extract Method", "newMethod", null);
    processor.prepare();
    for (final VariableData data : processor.getInputVariables().getInputVariables()) {
        final PsiExpression[] occurrences = ParameterTablePanel.findVariableOccurrences(elements, data.variable);
        final TypeSelectorManager manager = new TypeSelectorManagerImpl(project, data.type, occurrences, true) {

            @Override
            protected boolean isUsedAfter() {
                return processor.isOutputVariable(data.variable);
            }
        };
        final JComponent component = manager.getTypeSelector().getComponent();
        if (types.length > 1) {
            assertTrue("One type suggested", component instanceof JComboBox);
            final DefaultComboBoxModel model = (DefaultComboBoxModel) ((JComboBox) component).getModel();
            assertEquals(types.length, model.getSize());
            for (int i = 0, typesLength = types.length; i < typesLength; i++) {
                String type = types[i];
                assertEquals(type, model.getElementAt(i).toString());
            }
        } else if (types.length == 1) {
            assertTrue("Multiple types suggested", component instanceof JLabel);
            assertEquals(types[0], ((JLabel) component).getText());
        }
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) ExtractMethodProcessor(com.intellij.refactoring.extractMethod.ExtractMethodProcessor) VariableData(com.intellij.refactoring.util.VariableData) Project(com.intellij.openapi.project.Project) TypeSelectorManager(com.intellij.refactoring.ui.TypeSelectorManager) TypeSelectorManagerImpl(com.intellij.refactoring.ui.TypeSelectorManagerImpl) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 2 with TypeSelectorManager

use of com.intellij.refactoring.ui.TypeSelectorManager in project intellij-community by JetBrains.

the class ParameterTablePanel method createSelector.

protected TypeSelector createSelector(final Project project, final VariableData data, PsiElement[] scopeElements) {
    final PsiVariable variable = data.variable;
    final PsiExpression[] occurrences = findVariableOccurrences(scopeElements, variable);
    final TypeSelectorManager manager = new TypeSelectorManagerImpl(project, data.type, occurrences, areTypesDirected()) {

        @Override
        protected boolean isUsedAfter() {
            return ParameterTablePanel.this.isUsedAfter(variable);
        }
    };
    return manager.getTypeSelector();
}
Also used : TypeSelectorManager(com.intellij.refactoring.ui.TypeSelectorManager) TypeSelectorManagerImpl(com.intellij.refactoring.ui.TypeSelectorManagerImpl)

Example 3 with TypeSelectorManager

use of com.intellij.refactoring.ui.TypeSelectorManager in project intellij-community by JetBrains.

the class SuggestedReturnTypesTest method doTest.

private void doTest(String... types) throws Exception {
    configureByFile(BASE_PATH + getTestName(false) + ".java");
    final Editor editor = getEditor();
    final PsiFile file = getFile();
    final Project project = getProject();
    int startOffset = editor.getSelectionModel().getSelectionStart();
    int endOffset = editor.getSelectionModel().getSelectionEnd();
    PsiElement[] elements;
    PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
    if (expr != null) {
        elements = new PsiElement[] { expr };
    } else {
        elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
    }
    assertTrue(elements.length > 0);
    final ExtractMethodProcessor processor = new ExtractMethodProcessor(project, editor, elements, null, "Extract Method", "newMethod", null);
    processor.prepare();
    final PsiExpression[] occurrences = processor.findOccurrences();
    final TypeSelectorManager manager = new TypeSelectorManagerImpl(project, processor.getReturnType(), occurrences, true);
    final JComponent component = manager.getTypeSelector().getComponent();
    if (types.length > 1) {
        assertTrue("One type suggested", component instanceof JComboBox);
        final DefaultComboBoxModel model = (DefaultComboBoxModel) ((JComboBox) component).getModel();
        assertEquals(types.length, model.getSize());
        for (int i = 0, typesLength = types.length; i < typesLength; i++) {
            String type = types[i];
            assertEquals(type, model.getElementAt(i).toString());
        }
    } else if (types.length == 1) {
        assertTrue("Multiple types suggested", component instanceof JLabel);
        assertEquals(types[0], ((JLabel) component).getText());
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) ExtractMethodProcessor(com.intellij.refactoring.extractMethod.ExtractMethodProcessor) Project(com.intellij.openapi.project.Project) TypeSelectorManager(com.intellij.refactoring.ui.TypeSelectorManager) TypeSelectorManagerImpl(com.intellij.refactoring.ui.TypeSelectorManagerImpl) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

TypeSelectorManager (com.intellij.refactoring.ui.TypeSelectorManager)3 TypeSelectorManagerImpl (com.intellij.refactoring.ui.TypeSelectorManagerImpl)3 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 PsiExpression (com.intellij.psi.PsiExpression)2 PsiFile (com.intellij.psi.PsiFile)2 ExtractMethodProcessor (com.intellij.refactoring.extractMethod.ExtractMethodProcessor)2 VariableData (com.intellij.refactoring.util.VariableData)1