Search in sources :

Example 36 with PsiExpression

use of com.intellij.psi.PsiExpression 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 37 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class InstructionVisitor method visitMethodCall.

public DfaInstructionState[] visitMethodCall(MethodCallInstruction instruction, DataFlowRunner runner, DfaMemoryState memState) {
    //noinspection UnusedDeclaration
    for (PsiExpression arg : instruction.getArgs()) {
        memState.pop();
    }
    //qualifier
    memState.pop();
    memState.push(DfaUnknownValue.getInstance());
    return nextInstruction(instruction, runner, memState);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression)

Example 38 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class SmartCompletionTemplateItem method getType.

@Override
public PsiType getType() {
    final Template template = getTemplate();
    String text = template.getTemplateText();
    StringBuilder resultingText = new StringBuilder(text);
    int segmentsCount = template.getSegmentsCount();
    for (int j = segmentsCount - 1; j >= 0; j--) {
        if (template.getSegmentName(j).equals(TemplateImpl.END)) {
            continue;
        }
        resultingText.insert(template.getSegmentOffset(j), "xxx");
    }
    try {
        final PsiExpression templateExpression = JavaPsiFacade.getElementFactory(myContext.getProject()).createExpressionFromText(resultingText.toString(), myContext);
        return templateExpression.getType();
    } catch (IncorrectOperationException e) {
        // can happen when text of the template does not form an expression
        return null;
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Template(com.intellij.codeInsight.template.Template)

Example 39 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class WrapReturnValue method fixUsage.

@Override
public void fixUsage() throws IncorrectOperationException {
    PsiExpression returnValue = myStatement.getReturnValue();
    assert returnValue != null;
    String newExpression = "new " + myType + '(' + returnValue.getText() + ')';
    MutationUtils.replaceExpression(newExpression, returnValue);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression)

Example 40 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class JavaPsiFormattingTest method testReferenceExpressionSpaceInsertion.

public void testReferenceExpressionSpaceInsertion() {
    String text = "if(x&y);";
    PsiFile file = createFile("A.java", "class C{{\n" + text + "\n}}");
    PsiElement element = file.findElementAt(file.getText().indexOf(text));
    PsiIfStatement statement = PsiTreeUtil.getParentOfType(element, PsiIfStatement.class);
    assertNotNull(statement);
    Ref<PsiElement> result = Ref.create();
    CommandProcessor.getInstance().executeCommand(getProject(), () -> WriteAction.run(() -> result.set(CodeStyleManager.getInstance(getProject()).reformat(statement))), "", null);
    PsiExpression expr = ((PsiIfStatement) result.get()).getCondition();
    assertEquals("PsiBinaryExpression:x & y\n" + "  PsiReferenceExpression:x\n" + "    PsiReferenceParameterList\n" + "      <empty list>\n" + "    PsiIdentifier:x('x')\n" + "  PsiWhiteSpace(' ')\n" + "  PsiJavaToken:AND('&')\n" + "  PsiWhiteSpace(' ')\n" + "  PsiReferenceExpression:y\n" + "    PsiReferenceParameterList\n" + "      <empty list>\n" + "    PsiIdentifier:y('y')\n", DebugUtil.psiToString(expr, false));
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiIfStatement(com.intellij.psi.PsiIfStatement) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiExpression (com.intellij.psi.PsiExpression)72 PsiElement (com.intellij.psi.PsiElement)28 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)13 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)11 Nullable (org.jetbrains.annotations.Nullable)9 PsiReference (com.intellij.psi.PsiReference)8 PsiType (com.intellij.psi.PsiType)8 NonNls (org.jetbrains.annotations.NonNls)8 PsiClass (com.intellij.psi.PsiClass)7 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiNewExpression (com.intellij.psi.PsiNewExpression)6 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)6 PsiStatement (com.intellij.psi.PsiStatement)6 Project (com.intellij.openapi.project.Project)5 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiElementFactory (com.intellij.psi.PsiElementFactory)5