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(), ")");
}
}
}
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}");
}
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);
}
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);
}
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(), "{}");
}
Aggregations