Examples of GoIfStatement


Examples of ro.redeul.google.go.lang.psi.statements.GoIfStatement

    protected boolean satisfiedBy(PsiElement element) {
        if (!isNodeOfType(element, GoTokenTypes.kIF)) {
            return false;
        }

        GoIfStatement ifStatement = findParentOfType(element, GoIfStatement.class);
        if (ifStatement == null || ifStatement.getElseBlock() != null || ifStatement.getElseIfStatement() != null) {
            return false;
        }

        if (ifStatement.getExpression() == null) {
            return false;
        }

        GoBlockStatement thenBlock = ifStatement.getThenBlock();
        if (thenBlock == null ||
            !isNodeOfType(thenBlock.getFirstChild(), GoTokenTypes.pLCURLY) ||
            !isNodeOfType(thenBlock.getLastChild(), GoTokenTypes.pRCURLY)) {
            return false;
        }

        GoStatement[] statements = thenBlock.getStatements();
        if (statements.length != 1 || !(statements[0] instanceof GoIfStatement)) {
            return false;
        }

        GoIfStatement inner = (GoIfStatement) statements[0];
        return inner.getExpression() != null;
    }
View Full Code Here

Examples of ro.redeul.google.go.lang.psi.statements.GoIfStatement

    @Override
    protected void processIntention(@NotNull PsiElement element, Editor editor)
            throws IntentionExecutionException {
        final Document document = editor.getDocument();
        GoIfStatement outer = findParentOfType(element, GoIfStatement.class);
        RangeMarker reformatRange = document.createRangeMarker(outer.getTextRange());
        GoIfStatement inner = (GoIfStatement) outer.getThenBlock().getStatements()[0];
        if (outer.getSimpleStatement() != null && inner.getSimpleStatement() != null) {
            SmartPsiElementPointer<GoIfStatement> outerPointer = createSmartElementPointer(outer);
            MoveSimpleStatementOutIntention.moveSimpleStatementOut(editor, outer);
            outer = outerPointer.getElement();
            if (outer == null) {
                return;
            }
            inner = (GoIfStatement) outer.getThenBlock().getStatements()[0];
        }

        // outer if range is text range from "if" to "{", inclusive, and surrounding whitespaces are also included.
        final RangeMarker outerIfRange = getOuterIfRange(document, outer);
        final RangeMarker rightCurlyRange = getRightCurlyRange(document, outer);
        if (outerIfRange == null || rightCurlyRange == null) {
            return;
        }

        final int innerEnd = inner.getThenBlock().getTextOffset();
        final String simpleStatementAndExpression = getSimpleStatementAndExpression(outer, inner);

        final GoIfStatement finalInner = inner;
        WriteCommandAction writeCommandAction = new WriteCommandAction(element.getContainingFile().getProject()) {
            @Override
            protected void run(@NotNull Result result) throws Throwable {
                document.replaceString(finalInner.getTextOffset(), innerEnd, simpleStatementAndExpression);

                document.deleteString(outerIfRange.getStartOffset(), outerIfRange.getEndOffset());
                document.deleteString(rightCurlyRange.getStartOffset(), rightCurlyRange.getEndOffset());
            }
        };
View Full Code Here

Examples of ro.redeul.google.go.lang.psi.statements.GoIfStatement

    protected boolean satisfiedBy(PsiElement element) {
        if (element instanceof GoIfStatement) {
            return false;
        }

        GoIfStatement stmt = findParentOfType(element, GoIfStatement.class);
        if (stmt == null) {
            return false;
        }

        GoExpr condition = stmt.getExpression();
        GoBlockStatement thenBlock = stmt.getThenBlock();
        return !(condition == null || thenBlock == null || element.getTextOffset() >= thenBlock.getTextOffset()) && stmt.getElseIfStatement() == null;

    }
View Full Code Here

Examples of ro.redeul.google.go.lang.psi.statements.GoIfStatement

               isInSwitchStatement(element) ||
               isInForClauseStatement(element);
    }

    private static boolean isInIfStatement(PsiElement element) {
        GoIfStatement stmt = findParentOfType(element, GoIfStatement.class);
        if (stmt == null) {
            return false;
        }

        GoSimpleStatement ss = stmt.getSimpleStatement();
        return ss != null && ss.getTextRange().contains(element.getTextRange());
    }
View Full Code Here

Examples of ro.redeul.google.go.lang.psi.statements.GoIfStatement

    }

    @Override
    protected void processIntention(@NotNull PsiElement element, Editor editor)
            throws IntentionExecutionException {
        GoIfStatement ifStatement = findParentOfType(element, GoIfStatement.class);
        if (ifStatement != null) {
            moveSimpleStatementOut(editor, ifStatement);
            return;
        }
View Full Code Here

Examples of ro.redeul.google.go.lang.psi.statements.GoIfStatement

    }

    @Override
    protected void processIntention(@NotNull PsiElement element, Editor editor)
            throws IncorrectOperationException {
        GoIfStatement stmt = findParentOfType(element, GoIfStatement.class);
        if (stmt == null) {
            throw new IncorrectOperationException(GoIntentionsBundle.message("error.if.statement.not.found"));
        }

        PsiFile file = stmt.getContainingFile();
        if (file == null) {
            return;
        }

        final Document document = editor.getDocument();
        RangeMarker stmtRange = document.createRangeMarker(stmt.getTextRange());

        GoExpr condition = stmt.getExpression();
        boolean parentIsFunctionDeclaration = isFunctionBlock(stmt.getParent());
        GoBlockStatement thenBlock = stmt.getThenBlock();

        final int rightCurlyPosition = blockRightCurlyPosition(thenBlock);
        final int leftCurlyPosition = blockLeftCurlyPosition(thenBlock);
        if (rightCurlyPosition < 0 || leftCurlyPosition < 0) {
            return;
        }

        GoBlockStatement elseBlock = stmt.getElseBlock();
        boolean hasElseBlock = elseBlock != null;

        List<PsiElement> siblings = getSiblings(stmt);
        if (hasElseBlock) {
            swapThenAndElseBlock(document, condition, thenBlock, elseBlock);
        } else if (parentIsFunctionDeclaration &&
                   !containsNonSpaceCommentOrReturnElements(siblings)) {
            String returnDeclaration = "return";
            if (onlyOneReturnStatement(siblings)) {
                returnDeclaration = concatenateElements(siblings);
            }

            final String finalReturnDeclaration = returnDeclaration;
            WriteCommandAction writeCommandAction = new WriteCommandAction(element.getContainingFile().getProject()) {
                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    document.deleteString(rightCurlyPosition, rightCurlyPosition + 1);
                    document.insertString(leftCurlyPosition + 1, "\n" + finalReturnDeclaration + "\n}");
                }
            };
            writeCommandAction.execute();


            flipCondition(document, condition);

            GoSimpleStatement simpleStatement = stmt.getSimpleStatement();
            if (simpleStatement != null) {
                extractSimpleStatement(stmt, document, condition, simpleStatement);
            }
        } else {
            WriteCommandAction writeCommandAction = new WriteCommandAction(element.getContainingFile().getProject()) {
View Full Code Here

Examples of ro.redeul.google.go.lang.psi.statements.GoIfStatement

        PsiElement grandpa = parent.getParent();
        if (!(grandpa instanceof GoIfStatement)) {
            return false;
        }

        GoIfStatement ifStmt = (GoIfStatement) grandpa;
        return ifStmt.getThenBlock() != null &&
               ifStmt.getElseBlock() == null &&
               ifStmt.getElseIfStatement() == null;
    }
View Full Code Here

Examples of ro.redeul.google.go.lang.psi.statements.GoIfStatement

    @Override
    protected void processIntention(@NotNull PsiElement element, Editor editor)
            throws IncorrectOperationException {
        PsiElement parent = element.getParent();
        GoIfStatement ifStmt = (GoIfStatement) parent.getParent();
        PsiElement leftExpr = parent.getFirstChild();
        PsiElement rightExpr = parent.getLastChild();
        if (leftExpr == null || rightExpr == null ||
            !isNodeOfType(leftExpr, GoElementTypes.REL_EXPRESSION) ||
            !isNodeOfType(rightExpr, GoElementTypes.REL_EXPRESSION)) {
            return;
        }

        GoBlockStatement then = ifStmt.getThenBlock();
        TextRange thenRange = then.getTextRange();
        Document doc = editor.getDocument();
        RangeMarker thenRangeMarker = doc.createRangeMarker(thenRange);

        int lineStartOffset = doc.getLineStartOffset(doc.getLineNumber(thenRange.getEndOffset()));
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.