com.intellij.psi.PsiStatement Java Examples

The following examples show how to use com.intellij.psi.PsiStatement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtractMethodPanel.java    From IntelliJDeodorant with MIT License 6 votes vote down vote up
/**
 * Collects statements that can be extracted into a separate method.
 */
public SmartList<PsiStatement> getStatementsToExtract(ASTSlice slice) {
    Set<PDGNode> nodes = slice.getSliceNodes();
    SmartList<PsiStatement> statementsToExtract = new SmartList<>();

    for (PDGNode pdgNode : nodes) {
        boolean isNotChild = true;
        for (PDGNode node : nodes) {
            if (isChild(node.getASTStatement(), pdgNode.getASTStatement())) {
                isNotChild = false;
            }
        }
        if (isNotChild) {
            statementsToExtract.add(pdgNode.getASTStatement());
        }
    }
    return statementsToExtract;
}
 
Example #2
Source File: ExtractMethodPanel.java    From IntelliJDeodorant with MIT License 6 votes vote down vote up
/**
 * Checks that the slice can be extracted into a separate method without compilation errors.
 */
private boolean canBeExtracted(ASTSlice slice) {
    SmartList<PsiStatement> statementsToExtract = getStatementsToExtract(slice);

    MyExtractMethodProcessor processor = new MyExtractMethodProcessor(scope.getProject(),
            null, statementsToExtract.toArray(new PsiElement[0]), slice.getLocalVariableCriterion().getType(),
            IntelliJDeodorantBundle.message("extract.method.refactoring.name"), "", HelpID.EXTRACT_METHOD,
            slice.getSourceTypeDeclaration(), slice.getLocalVariableCriterion());

    processor.setOutputVariable();

    try {
        processor.setShowErrorDialogs(false);
        return processor.prepare();

    } catch (PrepareFailedException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #3
Source File: DeclarationStatementTranslator.java    From java2typescript with Apache License 2.0 6 votes vote down vote up
public static void translate(PsiDeclarationStatement stmt, TranslationContext ctx) {
    for (int i = 0; i < stmt.getDeclaredElements().length; i++) {

        if(i > 0) {
            ctx.append(", ");
        }

        PsiElement element1 = stmt.getDeclaredElements()[i];
        if (element1 instanceof PsiStatement) {
            StatementTranslator.translate((PsiStatement) element1, ctx);
        } else if (element1 instanceof PsiLocalVariable) {
            LocalVariableTranslator.translate((PsiLocalVariable) element1, ctx);
        } else {
            System.err.println("Not managed " + element1);
        }
    }
}
 
Example #4
Source File: RequiredPropLineMarkerProviderTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void markStatement() {
  testHelper.getPsiClass(
      psiClasses -> {
        assertThat(psiClasses).hasSize(2);

        PsiClass underTest = psiClasses.get(0);
        PsiClass component = psiClasses.get(1);

        // For testing environment
        Function<PsiMethodCallExpression, PsiClass> resolver = ignored -> component;
        RequiredPropLineMarkerProvider provider = new RequiredPropLineMarkerProvider(resolver);
        List<PsiElement> statements =
            new ArrayList<>(PsiTreeUtil.findChildrenOfAnyType(underTest, PsiStatement.class));

        assertThat(provider.getLineMarkerInfo(statements.get(0))).isNotNull();
        assertThat(provider.getLineMarkerInfo(statements.get(1))).isNull();

        return true;
      },
      "RequiredPropAnnotatorTest.java",
      "RequiredPropAnnotatorComponent.java");
}
 
Example #5
Source File: ExtractMethodPanel.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
/**
 * Extracts statements into new method.
 *
 * @param slice computation slice.
 * @return callback to run when "Refactor" button is selected.
 */
private Runnable doExtract(ASTSlice slice) {
    return () -> {
        Editor editor = FileEditorManager.getInstance(slice.getSourceMethodDeclaration().getProject()).getSelectedTextEditor();
        SmartList<PsiStatement> statementsToExtract = getStatementsToExtract(slice);

        MyExtractMethodProcessor processor = new MyExtractMethodProcessor(slice.getSourceMethodDeclaration().getProject(),
                editor, statementsToExtract.toArray(new PsiElement[0]), slice.getLocalVariableCriterion().getType(),
                "", "", HelpID.EXTRACT_METHOD,
                slice.getSourceTypeDeclaration(), slice.getLocalVariableCriterion());

        processor.setOutputVariable();

        try {
            processor.setShowErrorDialogs(true);
            if (processor.prepare()) {
                ExtractMethodHandler.invokeOnElements(slice.getSourceMethodDeclaration().getProject(), processor,
                        slice.getSourceMethodDeclaration().getContainingFile(), true);
                if (editor != null && processor.getExtractedMethod() != null) {
                    IntelliJDeodorantCounterCollector.getInstance().extractMethodRefactoringApplied(editor.getProject(),
                            slice, processor.getExtractedMethod());
                }
            }
        } catch (PrepareFailedException e) {
            e.printStackTrace();
        }
    };
}
 
Example #6
Source File: ExtractMethodPanel.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
/**
 * Opens definition of method and highlights statements, which should be extracted.
 *
 * @param sourceMethod method from which code is proposed to be extracted into separate method.
 * @param scope        scope of the current project.
 * @param slice        computation slice.
 */
private static void openDefinition(@Nullable PsiMethod sourceMethod, AnalysisScope scope, ASTSlice slice) {
    new Task.Backgroundable(scope.getProject(), "Search Definition") {
        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
        }

        @Override
        public void onSuccess() {
            if (sourceMethod != null) {
                Set<SmartPsiElementPointer<PsiElement>> statements = slice.getSliceStatements();
                PsiStatement psiStatement = (PsiStatement) statements.iterator().next().getElement();
                if (psiStatement != null && psiStatement.isValid()) {
                    EditorHelper.openInEditor(psiStatement);
                    Editor editor = FileEditorManager.getInstance(sourceMethod.getProject()).getSelectedTextEditor();
                    if (editor != null) {
                        TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
                        editor.getMarkupModel().removeAllHighlighters();
                        statements.stream()
                                .filter(statement -> statement.getElement() != null)
                                .forEach(statement ->
                                        editor.getMarkupModel().addRangeHighlighter(statement.getElement().getTextRange().getStartOffset(),
                                                statement.getElement().getTextRange().getEndOffset(), HighlighterLayer.SELECTION,
                                                attributes, HighlighterTargetArea.EXACT_RANGE));
                    }
                }
            }
        }
    }.queue();
}
 
Example #7
Source File: CodeBlockTranslator.java    From java2typescript with Apache License 2.0 5 votes vote down vote up
public static void translate(PsiCodeBlock block, TranslationContext ctx) {
    ctx.append("{\n");
    ctx.increaseIdent();
    for (PsiStatement statement : block.getStatements()) {
        StatementTranslator.translate(statement, ctx);
    }
    ctx.decreaseIdent();
    ctx.print("}");
}
 
Example #8
Source File: RequiredPropAnnotatorTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void annotateStatement() {
  testHelper.getPsiClass(
      psiClasses -> {
        assertEquals(2, psiClasses.size());

        PsiClass underTest = psiClasses.get(0);
        PsiClass component = psiClasses.get(1);

        // For testing environment
        Function<PsiMethodCallExpression, PsiClass> resolver =
            psiMethodCallExpression -> component;
        RequiredPropAnnotator annotator = new RequiredPropAnnotator(resolver);
        TestHolder holder = new TestHolder();
        Collection<PsiStatement> statements =
            PsiTreeUtil.findChildrenOfAnyType(underTest, PsiStatement.class);
        // Simulates IDE behavior of traversing Psi elements
        for (PsiStatement statement : statements) {
          annotator.annotate(statement, holder);
        }

        assertEquals(3, holder.errorMessages.size());
        for (String errorMessage : holder.errorMessages) {
          assertEquals(
              "The following props are not "
                  + "marked as optional and were not supplied: testRequiredPropName",
              errorMessage);
        }
        assertEquals(3, holder.errorElements.size());
        for (PsiElement errorElement : holder.errorElements) {
          assertEquals("RequiredPropAnnotatorComponent.create", errorElement.getText());
        }
        return true;
      },
      "RequiredPropAnnotatorTest.java",
      "RequiredPropAnnotatorComponent.java");
}
 
Example #9
Source File: AbstractStatement.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
public PsiElement getStatement() {
    PsiElement element = this.statement.getElement();
    if (element instanceof PsiStatement || element instanceof PsiCodeBlock) {
        return element;
    } else {
        return null;
    }
}
 
Example #10
Source File: InstanceOfVariableDeclarationStatement.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
    if (statement instanceof PsiDeclarationStatement) {
        PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) statement;
        PsiElement[] declaredElements = declarationStatement.getDeclaredElements();
        for (PsiElement element : declaredElements) {
            if (element instanceof PsiVariable) {
                return true;
            }
        }
    }
    return false;
}
 
Example #11
Source File: CFGNode.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
PsiStatement getASTStatement() {
    if (statement.getStatement() instanceof PsiStatement) {
        return (PsiStatement) statement.getStatement();
    } else {
        return null;
    }
}
 
Example #12
Source File: InnerBuilderUtils.java    From innerbuilder with Apache License 2.0 4 votes vote down vote up
static PsiStatement createReturnThis(@NotNull PsiElementFactory psiElementFactory, @Nullable PsiElement context) {
    return psiElementFactory.createStatementFromText("return this;", context);
}
 
Example #13
Source File: HaxeBlockStatementPsiMixinImpl.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public PsiStatement[] getStatements() {
  // TODO: Implement
  return new PsiStatement[0];
}
 
Example #14
Source File: TryStatementObject.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public TryStatementObject(PsiStatement statement, AbstractMethodFragment parent) {
    super(statement, StatementType.TRY, parent);
    this.catchClauses = new SmartList<>();
}
 
Example #15
Source File: SynchronizedStatementObject.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
SynchronizedStatementObject(PsiStatement statement, AbstractMethodFragment parent) {
    super(statement, StatementType.SYNCHRONIZED, parent);
    AbstractExpression abstractExpression = new AbstractExpression(
            ((PsiSynchronizedStatement) statement).getLockExpression(), this);
    this.addExpression(abstractExpression);
}
 
Example #16
Source File: InstanceOfTryStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiTryStatement;
}
 
Example #17
Source File: InstanceOfBreakStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiBreakStatement;
}
 
Example #18
Source File: InstanceOfWhileStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiWhileStatement;
}
 
Example #19
Source File: InstanceOfEnhancedForStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiForeachStatement;
}
 
Example #20
Source File: InstanceOfIfStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiIfStatement;
}
 
Example #21
Source File: InstanceOfReturnStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiReturnStatement;
}
 
Example #22
Source File: InstanceOfBranchingStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
    return statement instanceof PsiBreakStatement || statement instanceof PsiContinueStatement
            || statement instanceof PsiReturnStatement;
}
 
Example #23
Source File: InstanceOfContinueStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiContinueStatement;
}
 
Example #24
Source File: InstanceOfSwitchStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiSwitchStatement;
}
 
Example #25
Source File: InstanceOfDoStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiDoWhileStatement;
}
 
Example #26
Source File: InstanceOfForStatement.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean instanceOf(PsiStatement statement) {
	return statement instanceof PsiForStatement;
}
 
Example #27
Source File: StatementInstanceChecker.java    From IntelliJDeodorant with MIT License votes vote down vote up
boolean instanceOf(PsiStatement statement);