IDEA-69648 Complete Current Statement: Correct 'for' completion

Provided completion for standalone 'for' keyword
This commit is contained in:
Denis Zhdanov
2011-05-13 18:17:23 +04:00
parent 9a3bb4c453
commit f4469717d7
4 changed files with 91 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.editorActions.smartEnter;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
/**
* {@link Fixer} that handles use-cases like below:
* <b>before:</b>
* <pre>
* void foo() {
* for[caret]
* }
* </pre>
*
* <b>after:</b>
* <pre>
* void foo() {
* for ([caret]) {
*
* }
* }
* </pre>
*
* @author Denis Zhdanov
* @since 5/13/11 4:24 PM
*/
public class ForStatementFixer implements Fixer {
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
if (!(psiElement instanceof PsiForStatement)) {
return;
}
PsiForStatement forStatement = (PsiForStatement)psiElement;
final PsiJavaToken lParenth = forStatement.getLParenth();
final PsiJavaToken rParenth = forStatement.getRParenth();
if (lParenth == null || rParenth == null) {
final TextRange textRange = forStatement.getTextRange();
editor.getDocument().replaceString(textRange.getStartOffset(), textRange.getEndOffset(), "for () {\n}");
processor.registerUnresolvedError(textRange.getStartOffset() + "for (".length());
return;
}
final PsiStatement initialization = forStatement.getInitialization();
if (initialization == null) {
processor.registerUnresolvedError(lParenth.getTextRange().getEndOffset());
return;
}
final PsiExpression condition = forStatement.getCondition();
if (condition == null) {
processor.registerUnresolvedError(initialization.getTextRange().getEndOffset());
return;
}
if (forStatement.getUpdate() == null) {
processor.registerUnresolvedError(condition.getTextRange().getEndOffset());
}
}
}

View File

@@ -0,0 +1,5 @@
public class Foo {
void test(int i) {
for<caret>
}
}

View File

@@ -0,0 +1,6 @@
public class Foo {
void test(int i) {
for (<caret>) {
}
}
}

View File

@@ -201,6 +201,8 @@ public class CompleteStatementTest extends EditorActionTestCase {
} }
public void testNoUnnecessaryEmptyLineAtCodeBlock() throws Exception { doTest(); } public void testNoUnnecessaryEmptyLineAtCodeBlock() throws Exception { doTest(); }
public void testForStatementGeneration() throws Exception { doTest(); }
private void doTestBracesNextLineStyle() throws Exception { private void doTestBracesNextLineStyle() throws Exception {
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());