mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
IDEA-69656 Complete Current Statement: Provide support for completing 'condition' section of 'for' statement
'Update' section of 'for' statement may be smart-completed now
This commit is contained in:
+4
-2
@@ -96,10 +96,12 @@ public class ForStatementFixer implements Fixer {
|
||||
final Project project = editor.getProject();
|
||||
int offset = lastValidForPart.getTextRange().getEndOffset();
|
||||
if (project != null && CodeStyleSettingsManager.getSettings(project).SPACE_AFTER_COMMA) {
|
||||
if (editor.getDocument().getCharsSequence().charAt(lastValidForPart.getTextRange().getEndOffset() - 1) != ';') {
|
||||
offset++;
|
||||
}
|
||||
for (PsiElement element = lastValidForPart.getNextSibling();
|
||||
element != null && element != forStatement.getRParenth() && element.getParent() == forStatement;
|
||||
element = element.getNextSibling())
|
||||
{
|
||||
element = element.getNextSibling()) {
|
||||
final ASTNode node = element.getNode();
|
||||
if (node != null && ElementType.WHITE_SPACE_BIT_SET.contains(node.getElementType()) && element.getTextLength() > 0) {
|
||||
offset++;
|
||||
|
||||
+96
-53
@@ -19,6 +19,7 @@ import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
@@ -26,6 +27,8 @@ import com.intellij.psi.impl.source.tree.ElementType;
|
||||
import com.intellij.psi.impl.source.tree.TreeUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
@@ -37,6 +40,11 @@ import com.intellij.util.IncorrectOperationException;
|
||||
@SuppressWarnings({"HardCodedStringLiteral"})
|
||||
public class SemicolonFixer implements Fixer {
|
||||
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
boolean fixed = fixReturn(editor, psiElement) || fixForUpdate(editor, psiElement, processor)
|
||||
|| fixAfterLastValidElement(editor, psiElement);
|
||||
}
|
||||
|
||||
private static boolean fixReturn(@NotNull Editor editor, @Nullable PsiElement psiElement) {
|
||||
if (psiElement instanceof PsiReturnStatement) {
|
||||
PsiMethod method = PsiTreeUtil.getParentOfType(psiElement, PsiMethod.class);
|
||||
if (method != null && PsiType.VOID.equals(method.getReturnType())) {
|
||||
@@ -44,65 +52,100 @@ public class SemicolonFixer implements Fixer {
|
||||
if (stmt.getReturnValue() != null) {
|
||||
Document doc = editor.getDocument();
|
||||
doc.insertString(stmt.getTextRange().getStartOffset() + "return".length(), ";");
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean fixForUpdate(@NotNull Editor editor, @Nullable PsiElement psiElement, @NotNull JavaSmartEnterProcessor processor) {
|
||||
if (!(psiElement instanceof PsiForStatement)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PsiForStatement forStatement = (PsiForStatement)psiElement;
|
||||
final PsiExpression condition = forStatement.getCondition();
|
||||
if (forStatement.getUpdate() != null || condition == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (psiElement instanceof PsiExpressionStatement ||
|
||||
psiElement instanceof PsiDeclarationStatement ||
|
||||
psiElement instanceof PsiImportStatementBase ||
|
||||
psiElement instanceof PsiDoWhileStatement ||
|
||||
psiElement instanceof PsiReturnStatement ||
|
||||
psiElement instanceof PsiThrowStatement ||
|
||||
psiElement instanceof PsiBreakStatement ||
|
||||
psiElement instanceof PsiContinueStatement ||
|
||||
psiElement instanceof PsiAssertStatement ||
|
||||
psiElement instanceof PsiField && !(psiElement instanceof PsiEnumConstant) ||
|
||||
psiElement instanceof PsiMethod && (((PsiMethod) psiElement).getContainingClass().isInterface() ||
|
||||
((PsiMethod) psiElement).hasModifierProperty(PsiModifier.ABSTRACT))) {
|
||||
String text = psiElement.getText();
|
||||
|
||||
int tailLength = 0;
|
||||
ASTNode leaf = TreeUtil.findLastLeaf(psiElement.getNode());
|
||||
while (leaf != null && ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(leaf.getElementType())) {
|
||||
tailLength += leaf.getTextLength();
|
||||
leaf = TreeUtil.prevLeaf(leaf);
|
||||
}
|
||||
|
||||
if (tailLength > 0) {
|
||||
text = text.substring(0, text.length() - tailLength);
|
||||
}
|
||||
|
||||
if (leaf == null) {
|
||||
return;
|
||||
}
|
||||
int insertionOffset = leaf.getTextRange().getEndOffset();
|
||||
Document doc = editor.getDocument();
|
||||
if (psiElement instanceof PsiField && ((PsiField) psiElement).hasModifierProperty(PsiModifier.ABSTRACT)) {
|
||||
// abstract rarely seem to be field. It is rather incomplete method.
|
||||
doc.insertString(insertionOffset, "()");
|
||||
insertionOffset += "()".length();
|
||||
}
|
||||
|
||||
if (!StringUtil.endsWithChar(text, ';')) {
|
||||
final PsiElement parent = psiElement.getParent();
|
||||
String toInsert = ";";
|
||||
if (parent instanceof PsiForStatement) {
|
||||
if (((PsiForStatement)parent).getUpdate() == psiElement) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
final Project project = editor.getProject();
|
||||
if (project != null && CodeStyleSettingsManager.getSettings(project).SPACE_AFTER_SEMICOLON) {
|
||||
toInsert += " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
doc.insertString(insertionOffset, toInsert);
|
||||
final TextRange range = condition.getTextRange();
|
||||
final Document document = editor.getDocument();
|
||||
final CharSequence text = document.getCharsSequence();
|
||||
for (int i = range.getEndOffset() - 1, max = forStatement.getTextRange().getEndOffset(); i < max; i++) {
|
||||
if (text.charAt(i) == ';') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
String toInsert = ";";
|
||||
final Project project = editor.getProject();
|
||||
if (project != null && CodeStyleSettingsManager.getSettings(project).SPACE_AFTER_SEMICOLON) {
|
||||
toInsert += " ";
|
||||
}
|
||||
document.insertString(range.getEndOffset(), toInsert);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean fixAfterLastValidElement(@NotNull Editor editor, @Nullable PsiElement psiElement) {
|
||||
if (psiElement == null ||
|
||||
!(psiElement instanceof PsiExpressionStatement) &&
|
||||
!(psiElement instanceof PsiDeclarationStatement) &&
|
||||
!(psiElement instanceof PsiImportStatementBase) &&
|
||||
!(psiElement instanceof PsiDoWhileStatement) &&
|
||||
!(psiElement instanceof PsiReturnStatement) &&
|
||||
!(psiElement instanceof PsiThrowStatement) &&
|
||||
!(psiElement instanceof PsiBreakStatement) &&
|
||||
!(psiElement instanceof PsiContinueStatement) &&
|
||||
!(psiElement instanceof PsiAssertStatement) &&
|
||||
(!(psiElement instanceof PsiField) || psiElement instanceof PsiEnumConstant) &&
|
||||
(!(psiElement instanceof PsiMethod) || (!((PsiMethod)psiElement).getContainingClass().isInterface() &&
|
||||
!((PsiMethod)psiElement).hasModifierProperty(PsiModifier.ABSTRACT)))) {
|
||||
return false;
|
||||
}
|
||||
String text = psiElement.getText();
|
||||
|
||||
int tailLength = 0;
|
||||
ASTNode leaf = TreeUtil.findLastLeaf(psiElement.getNode());
|
||||
while (leaf != null && ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(leaf.getElementType())) {
|
||||
tailLength += leaf.getTextLength();
|
||||
leaf = TreeUtil.prevLeaf(leaf);
|
||||
}
|
||||
|
||||
if (tailLength > 0) {
|
||||
text = text.substring(0, text.length() - tailLength);
|
||||
}
|
||||
|
||||
if (leaf == null) {
|
||||
return false;
|
||||
}
|
||||
int insertionOffset = leaf.getTextRange().getEndOffset();
|
||||
Document doc = editor.getDocument();
|
||||
if (psiElement instanceof PsiField && ((PsiField)psiElement).hasModifierProperty(PsiModifier.ABSTRACT)) {
|
||||
// abstract rarely seem to be field. It is rather incomplete method.
|
||||
doc.insertString(insertionOffset, "()");
|
||||
insertionOffset += "()".length();
|
||||
}
|
||||
|
||||
if (!StringUtil.endsWithChar(text, ';')) {
|
||||
final PsiElement parent = psiElement.getParent();
|
||||
String toInsert = ";";
|
||||
if (parent instanceof PsiForStatement) {
|
||||
if (((PsiForStatement)parent).getUpdate() == psiElement) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
final Project project = editor.getProject();
|
||||
if (project != null && CodeStyleSettingsManager.getSettings(project).SPACE_AFTER_SEMICOLON) {
|
||||
toInsert += " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
doc.insertString(insertionOffset, toInsert);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
public class Foo {
|
||||
void test(int i) {
|
||||
for (int j = 1; j < 10<caret>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
public class Foo {
|
||||
void test(int i) {
|
||||
for (int j = 1; j < 10; <caret>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -214,6 +214,8 @@ public class CompleteStatementTest extends EditorActionTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testForUpdateGeneration() throws Exception { doTest(); }
|
||||
|
||||
private void doTestBracesNextLineStyle() throws Exception {
|
||||
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
|
||||
settings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
|
||||
|
||||
Reference in New Issue
Block a user