IDEA-85813 Field/variable declaration/assignment alignment lost with Refactor>Rename or Reformat Code

Expand field range to the field group range during the formatting if necessary
This commit is contained in:
Denis.Zhdanov
2012-05-10 07:38:28 +04:00
parent 2b4248f6ce
commit 2bcb01f172
7 changed files with 200 additions and 4 deletions
@@ -0,0 +1,120 @@
/*
* Copyright 2000-2012 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.psi.impl.source.codeStyle;
import com.intellij.lang.ASTNode;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.JavaJspElementType;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
/**
* There is a possible case that the project is configured to keep fields in columns:
* <pre>
* class Test {
* int i = 1;
* int fieldWithLongName = 2;
* }
* </pre>
* Suppose that one of the fields is renamed. We want to reformat the whole fields group then in order to keep that 'field columns'.
* <p/>
* Current extension checks if given range intersects with a field from a field group and expands its boundaries to contain
* the whole group.
* <p/>
* Thread-safe.
*
* @author Denis Zhdanov
* @since 5/9/12 4:54 PM
*/
public class FieldInColumnsPreFormatProcessor implements PreFormatProcessor {
@NotNull
@Override
public TextRange process(@NotNull ASTNode element, @NotNull TextRange range) {
//region Checking that everything is ready to expand the range for the 'fields in columns'.
final PsiElement psi = element.getPsi();
if (psi == null) {
return range;
}
final PsiFile file = psi.getContainingFile();
if (file == null) {
return range;
}
final Project project = psi.getProject();
final CommonCodeStyleSettings settings
= CodeStyleSettingsManager.getInstance(project).getCurrentSettings().getCommonSettings(JavaLanguage.INSTANCE);
if (!settings.ALIGN_GROUP_FIELD_DECLARATIONS) {
return range;
}
final PsiElement startElement = file.findElementAt(range.getStartOffset());
if (startElement == null) {
return range;
}
final PsiField parent = PsiTreeUtil.getParentOfType(startElement, PsiField.class);
if (parent == null) {
return range;
}
//endregion
//region Calculating start offset to use by the start offset of the first sibling white space or field to the left of the current field.
int startToUse = range.getStartOffset();
for (PsiElement f = parent; f != null; f = f.getPrevSibling()) {
final ASTNode node = f.getNode();
if (node == null) {
break;
}
if (JavaJspElementType.WHITE_SPACE_BIT_SET.contains(node.getElementType()) || f instanceof PsiField) {
startToUse = f.getTextRange().getStartOffset();
}
else if (!ElementType.JAVA_COMMENT_BIT_SET.contains(node.getElementType())) {
break;
}
}
//endregion
//region Calculating end offset to use by the end offset of the last field in a group located to the right of the current field.
int endToUse = range.getEndOffset();
for (PsiElement f = parent; f != null; f = f.getPrevSibling()) {
final ASTNode node = f.getNode();
if (node == null) {
break;
}
if (f instanceof PsiField) {
endToUse = f.getTextRange().getEndOffset();
}
else if (!JavaJspElementType.WHITE_SPACE_BIT_SET.contains(node.getElementType()) &&
!ElementType.JAVA_COMMENT_BIT_SET.contains(node.getElementType()))
{
break;
}
}
//endregion
return TextRange.from(startToUse, endToUse);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -25,10 +25,12 @@ import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.codeStyle.javadoc.CommentFormatter;
import com.intellij.psi.javadoc.PsiDocComment;
import org.jetbrains.annotations.NotNull;
public class FormatCommentsProcessor implements PreFormatProcessor {
@NotNull
@Override
public TextRange process(final ASTNode element, final TextRange range) {
public TextRange process(@NotNull final ASTNode element, @NotNull final TextRange range) {
final Project project = SourceTreeToPsiMap.treeElementToPsi(element).getProject();
if (!CodeStyleSettingsManager.getSettings(project).ENABLE_JAVADOC_FORMATTING ||
element.getPsi().getContainingFile().getLanguage() != StdLanguages.JAVA) {
@@ -0,0 +1,23 @@
/*
* Copyright 2000-2012 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.
*/
class Test {
int jj = 1;
int fieldWithLongName = 2;
void test() {
jj = 3;
}
}
@@ -0,0 +1,23 @@
/*
* Copyright 2000-2012 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.
*/
class Test {
int i = 1;
int fieldWithLongName = 2;
void test() {
<caret>i = 3;
}
}
@@ -1,3 +1,19 @@
/*
* Copyright 2000-2012 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.
*/
/*
* Created by IntelliJ IDEA.
* User: dsl
@@ -10,7 +26,9 @@ package com.intellij.refactoring;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.psi.PsiElement;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.refactoring.rename.RenameWrongRefHandler;
import org.jetbrains.annotations.NonNls;
@@ -66,6 +84,13 @@ public class RenameFieldTest extends LightRefactoringTestCase {
assertFalse(RenameWrongRefHandler.isAvailable(getProject(), getEditor(), getFile()));
}
public void testFieldInColumns() throws Exception {
// Assuming that test infrastructure setups temp settings (CodeStyleSettingsManager.setTemporarySettings()) and we don't
// need to perform explicit clean-up at the test level.
CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE).ALIGN_GROUP_FIELD_DECLARATIONS = true;
doTest("jj", "java");
}
protected static void perform(String newName) {
PsiElement element = TargetElementUtilBase.findTargetElement(myEditor, TargetElementUtilBase
.ELEMENT_NAME_ACCEPTED | TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED);
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -19,6 +19,7 @@ package com.intellij.psi.impl.source.codeStyle;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
/**
* @author yole
@@ -26,5 +27,6 @@ import com.intellij.openapi.util.TextRange;
public interface PreFormatProcessor {
ExtensionPointName<PreFormatProcessor> EP_NAME = ExtensionPointName.create("com.intellij.preFormatProcessor");
TextRange process(ASTNode element, TextRange range);
@NotNull
TextRange process(@NotNull ASTNode element, @NotNull TextRange range);
}
+1
View File
@@ -888,6 +888,7 @@
<elementSignatureProvider implementation="com.intellij.codeInsight.folding.impl.JavaElementSignatureProvider"/>
<preFormatProcessor implementation="com.intellij.psi.impl.source.codeStyle.FormatCommentsProcessor"/>
<preFormatProcessor implementation="com.intellij.psi.impl.source.codeStyle.FieldInColumnsPreFormatProcessor"/>
<postFormatProcessor implementation="com.intellij.psi.impl.source.codeStyle.BracePostFormatProcessor"/>
<postFormatProcessor implementation="com.intellij.psi.impl.source.codeStyle.ImportPostFormatProcessor"/>
<codeInspection.InspectionExtension implementation="com.intellij.codeInspection.ex.JavaInspectionExtensionsFactory"/>