Search in sources :

Example 41 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class GrIfConditionFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (psiElement instanceof GrIfStatement) {
        final Document doc = editor.getDocument();
        final GrIfStatement ifStatement = (GrIfStatement) psiElement;
        final PsiElement rParen = ifStatement.getRParenth();
        final PsiElement lParen = ifStatement.getLParenth();
        final GrExpression condition = ifStatement.getCondition();
        if (condition == null) {
            if (lParen == null || rParen == null) {
                int stopOffset = doc.getLineEndOffset(doc.getLineNumber(ifStatement.getTextRange().getStartOffset()));
                final GrStatement then = ifStatement.getThenBranch();
                if (then != null) {
                    stopOffset = Math.min(stopOffset, then.getTextRange().getStartOffset());
                }
                stopOffset = Math.min(stopOffset, ifStatement.getTextRange().getEndOffset());
                doc.replaceString(ifStatement.getTextRange().getStartOffset(), stopOffset, "if ()");
                processor.registerUnresolvedError(ifStatement.getTextRange().getStartOffset() + "if (".length());
            } else {
                processor.registerUnresolvedError(lParen.getTextRange().getEndOffset());
            }
        } else if (rParen == null) {
            doc.insertString(condition.getTextRange().getEndOffset(), ")");
        }
    }
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 42 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class GrMethodBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof GrMethod))
        return;
    GrMethod method = (GrMethod) psiElement;
    final PsiClass aClass = method.getContainingClass();
    if (aClass != null && aClass.isInterface() || method.hasModifierProperty(PsiModifier.ABSTRACT))
        return;
    final GrCodeBlock body = method.getBlock();
    final Document doc = editor.getDocument();
    if (body != null) {
        // See IDEADEV-1093. This is quite hacky heuristic but it seem to be best we can do.
        String bodyText = body.getText();
        if (StringUtil.startsWithChar(bodyText, '{')) {
            final GrStatement[] statements = body.getStatements();
            if (statements.length > 0) {
            //          [todo]
            //          if (statements[0] instanceof PsiDeclarationStatement) {
            //            if (PsiTreeUtil.getDeepestLast(statements[0]) instanceof PsiErrorElement) {
            //              if (method.getContainingClass().getRBrace() == null) {
            //                doc.insertString(body.getTextRange().getStartOffset() + 1, "\n}");
            //              }
            //            }
            //          }
            }
        }
        return;
    }
    int endOffset = method.getTextRange().getEndOffset();
    if (StringUtil.endsWithChar(method.getText(), ';')) {
        doc.deleteString(endOffset - 1, endOffset);
        endOffset--;
    }
    doc.insertString(endOffset, "{\n}");
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) Document(com.intellij.openapi.editor.Document) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 43 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class GrSwitchBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    GrSwitchStatement switchStatement = PsiTreeUtil.getParentOfType(psiElement, GrSwitchStatement.class);
    if (switchStatement == null || switchStatement.getLBrace() != null)
        return;
    if (!PsiTreeUtil.isAncestor(switchStatement.getCondition(), psiElement, false))
        return;
    final Document doc = editor.getDocument();
    PsiElement lBrace = switchStatement.getLBrace();
    if (lBrace != null)
        return;
    PsiElement eltToInsertAfter = switchStatement.getRParenth();
    String text = "{\n}";
    if (eltToInsertAfter == null) {
        eltToInsertAfter = switchStatement.getCondition();
        text = "){\n}";
    }
    doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
Also used : GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 44 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class GrSynchronizedFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    GrSynchronizedStatement synchronizedStatement = PsiTreeUtil.getParentOfType(psiElement, GrSynchronizedStatement.class);
    if (synchronizedStatement == null || synchronizedStatement.getBody() != null)
        return;
    if (!PsiTreeUtil.isAncestor(synchronizedStatement.getMonitor(), psiElement, false))
        return;
    final Document doc = editor.getDocument();
    PsiElement eltToInsertAfter = synchronizedStatement.getRParenth();
    String text = "{\n}";
    if (eltToInsertAfter == null) {
        eltToInsertAfter = synchronizedStatement.getMonitor();
        text = "){\n}";
    }
    doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
Also used : GrSynchronizedStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSynchronizedStatement) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 45 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class GrWhileBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof GrWhileStatement))
        return;
    GrWhileStatement whileStatement = (GrWhileStatement) psiElement;
    final Document doc = editor.getDocument();
    PsiElement body = whileStatement.getBody();
    if (body instanceof GrBlockStatement)
        return;
    if (body != null && GrForBodyFixer.startLine(editor.getDocument(), body) == GrForBodyFixer.startLine(editor.getDocument(), whileStatement) && whileStatement.getCondition() != null)
        return;
    final PsiElement rParenth = whileStatement.getRParenth();
    assert rParenth != null;
    doc.insertString(rParenth.getTextRange().getEndOffset(), "{}");
}
Also used : GrWhileStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement) Document(com.intellij.openapi.editor.Document) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

Document (com.intellij.openapi.editor.Document)1075 VirtualFile (com.intellij.openapi.vfs.VirtualFile)246 PsiFile (com.intellij.psi.PsiFile)191 Project (com.intellij.openapi.project.Project)164 NotNull (org.jetbrains.annotations.NotNull)138 Nullable (org.jetbrains.annotations.Nullable)129 TextRange (com.intellij.openapi.util.TextRange)122 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)117 PsiElement (com.intellij.psi.PsiElement)107 Editor (com.intellij.openapi.editor.Editor)104 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)56 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)45 IncorrectOperationException (com.intellij.util.IncorrectOperationException)43 IOException (java.io.IOException)42 RangeMarker (com.intellij.openapi.editor.RangeMarker)35 MockVirtualFile (com.intellij.mock.MockVirtualFile)33 File (java.io.File)32 ArrayList (java.util.ArrayList)32 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)30 List (java.util.List)29