mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
cidr-formatter: braces
This commit is contained in:
@@ -32,10 +32,10 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class BraceEnforcer extends JavaJspRecursiveElementVisitor {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.codeStyle.BraceEnforcer");
|
||||
|
||||
private final AbstractPostFormatProcessor myPostProcessor;
|
||||
private final PostFormatProcessorHelper myPostProcessor;
|
||||
|
||||
public BraceEnforcer(CodeStyleSettings settings) {
|
||||
myPostProcessor = new AbstractPostFormatProcessor(settings);
|
||||
myPostProcessor = new PostFormatProcessorHelper(settings);
|
||||
}
|
||||
|
||||
@Override public void visitReferenceExpression(PsiReferenceExpression expression) {
|
||||
@@ -96,7 +96,7 @@ public class BraceEnforcer extends JavaJspRecursiveElementVisitor {
|
||||
private void processStatement(PsiStatement statement, PsiStatement blockCandidate, int options) {
|
||||
if (blockCandidate instanceof PsiBlockStatement || blockCandidate == null) return;
|
||||
if (options == CodeStyleSettings.FORCE_BRACES_ALWAYS ||
|
||||
options == CodeStyleSettings.FORCE_BRACES_IF_MULTILINE && AbstractPostFormatProcessor.isMultiline(statement)) {
|
||||
options == CodeStyleSettings.FORCE_BRACES_IF_MULTILINE && PostFormatProcessorHelper.isMultiline(statement)) {
|
||||
replaceWithBlock(statement, blockCandidate);
|
||||
}
|
||||
}
|
||||
@@ -146,11 +146,11 @@ public class BraceEnforcer extends JavaJspRecursiveElementVisitor {
|
||||
}
|
||||
|
||||
protected boolean checkElementContainsRange(final PsiElement element) {
|
||||
return myPostProcessor.checkElementContainsRange(element);
|
||||
return myPostProcessor.isElementPartlyInRange(element);
|
||||
}
|
||||
|
||||
protected boolean checkRangeContainsElement(final PsiElement element) {
|
||||
return myPostProcessor.checkRangeContainsElement(element);
|
||||
return myPostProcessor.isElementFullyInRange(element);
|
||||
}
|
||||
|
||||
public PsiElement process(PsiElement formatted) {
|
||||
|
||||
@@ -40,10 +40,10 @@ public class ImportsFormatter extends XmlRecursiveElementVisitor {
|
||||
private static final @NonNls String PAGE_DIRECTIVE = "page";
|
||||
private static final @NonNls String IMPORT_ATT = "import";
|
||||
|
||||
private final AbstractPostFormatProcessor myPostProcessor;
|
||||
private final PostFormatProcessorHelper myPostProcessor;
|
||||
|
||||
public ImportsFormatter(final CodeStyleSettings settings, PsiFile file) {
|
||||
myPostProcessor = new AbstractPostFormatProcessor(settings);
|
||||
myPostProcessor = new PostFormatProcessorHelper(settings);
|
||||
myDocumentModel = FormattingDocumentModelImpl.createOn(file);
|
||||
myIndentOptions = settings.getIndentOptions(file.getFileType());
|
||||
}
|
||||
@@ -65,7 +65,7 @@ public class ImportsFormatter extends XmlRecursiveElementVisitor {
|
||||
@Override public void visitXmlAttribute(XmlAttribute attribute) {
|
||||
if (isPageDirectiveTag(attribute.getParent())) {
|
||||
final XmlAttributeValue valueElement = attribute.getValueElement();
|
||||
if (valueElement != null && checkRangeContainsElement(attribute) && isImportAttribute(attribute) && AbstractPostFormatProcessor
|
||||
if (valueElement != null && checkRangeContainsElement(attribute) && isImportAttribute(attribute) && PostFormatProcessorHelper
|
||||
.isMultiline(valueElement)) {
|
||||
final int oldLength = attribute.getTextLength();
|
||||
ASTNode valueToken = findValueToken(valueElement.getNode());
|
||||
@@ -147,11 +147,11 @@ public class ImportsFormatter extends XmlRecursiveElementVisitor {
|
||||
}
|
||||
|
||||
protected boolean checkElementContainsRange(final PsiElement element) {
|
||||
return myPostProcessor.checkElementContainsRange(element);
|
||||
return myPostProcessor.isElementPartlyInRange(element);
|
||||
}
|
||||
|
||||
protected boolean checkRangeContainsElement(final PsiElement element) {
|
||||
return myPostProcessor.checkRangeContainsElement(element);
|
||||
return myPostProcessor.isElementFullyInRange(element);
|
||||
}
|
||||
|
||||
public PsiElement process(PsiElement formatted) {
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class PsiTreeUtil {
|
||||
@@ -194,27 +195,19 @@ public class PsiTreeUtil {
|
||||
return parents;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T findChildOfType(@NotNull final PsiElement element, @NotNull final Class<T> aClass) {
|
||||
@Nullable public static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element, @NotNull final Class<T> aClass) {
|
||||
return findChildOfType(element, aClass, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T findChildOfType(@NotNull final PsiElement element,
|
||||
@NotNull final Class<T> aClass,
|
||||
final boolean strict) {
|
||||
@Nullable public static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element, @NotNull final Class<T> aClass, final boolean strict) {
|
||||
return findChildOfAnyType(element, strict, aClass);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T findChildOfAnyType(@NotNull final PsiElement element, @NotNull final Class<T>... classes) {
|
||||
@Nullable public static <T extends PsiElement> T findChildOfAnyType(@Nullable final PsiElement element, @NotNull final Class<T>... classes) {
|
||||
return findChildOfAnyType(element, true, classes);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T findChildOfAnyType(@NotNull final PsiElement element,
|
||||
final boolean strict,
|
||||
@NotNull final Class<T>... classes) {
|
||||
@Nullable public static <T extends PsiElement> T findChildOfAnyType(@Nullable final PsiElement element, final boolean strict, @NotNull final Class<T>... classes) {
|
||||
PsiElementProcessor.FindElement<PsiElement> processor = new PsiElementProcessor.FindElement<PsiElement>() {
|
||||
@Override
|
||||
public boolean execute(PsiElement each) {
|
||||
@@ -232,9 +225,9 @@ public class PsiTreeUtil {
|
||||
return (T)processor.getFoundElement();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getChildOfType(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
@Nullable public static <T extends PsiElement> T getChildOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
|
||||
if (element == null) return null;
|
||||
for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
}
|
||||
return null;
|
||||
@@ -247,8 +240,9 @@ public class PsiTreeUtil {
|
||||
return child;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T[] getChildrenOfType(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
@Nullable public static <T extends PsiElement> T[] getChildrenOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
|
||||
if (element == null) return null;
|
||||
|
||||
List<T> result = null;
|
||||
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
if (instanceOf(aClass, child)) {
|
||||
@@ -259,8 +253,9 @@ public class PsiTreeUtil {
|
||||
return result == null ? null : ArrayUtil.toObjectArray(result, aClass);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static <T extends PsiElement> List<T> getChildrenOfTypeAsList(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
@NotNull public static <T extends PsiElement> List<T> getChildrenOfTypeAsList(@Nullable PsiElement element, @NotNull Class<T> aClass) {
|
||||
if (element == null) return Collections.emptyList();
|
||||
|
||||
List<T> result = new SmartList<T>();
|
||||
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
if (instanceOf(aClass, child)) {
|
||||
@@ -296,28 +291,28 @@ public class PsiTreeUtil {
|
||||
* @return the element, or null if none was found.
|
||||
* @since 5.1
|
||||
*/
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getChildOfAnyType(@NotNull PsiElement element, @NotNull Class<? extends T>... classes) {
|
||||
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
for (Class<? extends T> aClass : classes) {
|
||||
@Nullable public static <T extends PsiElement> T getChildOfAnyType(@Nullable PsiElement element, @NotNull Class<? extends T>... classes) {
|
||||
if (element == null) return null;
|
||||
for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){
|
||||
for(Class<? extends T> aClass : classes) {
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getNextSiblingOfType(@NotNull PsiElement sibling, @NotNull Class<T> aClass) {
|
||||
for (PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()) {
|
||||
@Nullable public static <T extends PsiElement> T getNextSiblingOfType(@Nullable PsiElement sibling, @NotNull Class<T> aClass) {
|
||||
if (sibling == null) return null;
|
||||
for(PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()){
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getPrevSiblingOfType(@NotNull PsiElement sibling, @NotNull Class<T> aClass) {
|
||||
for (PsiElement child = sibling.getPrevSibling(); child != null; child = child.getPrevSibling()) {
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
@Nullable public static <T extends PsiElement> T getPrevSiblingOfType(@Nullable PsiElement sibling, @NotNull Class<T> aClass) {
|
||||
if (sibling == null) return null;
|
||||
for(PsiElement child = sibling.getPrevSibling(); child != null; child = child.getPrevSibling()){
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+4
-4
@@ -23,11 +23,11 @@ import org.jetbrains.annotations.Nullable;
|
||||
/**
|
||||
* @author lesya
|
||||
*/
|
||||
public class AbstractPostFormatProcessor {
|
||||
public class PostFormatProcessorHelper {
|
||||
public final CodeStyleSettings mySettings;
|
||||
private TextRange myResultTextRange;
|
||||
|
||||
public AbstractPostFormatProcessor(final CodeStyleSettings settings) {
|
||||
public PostFormatProcessorHelper(final CodeStyleSettings settings) {
|
||||
mySettings = settings;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public class AbstractPostFormatProcessor {
|
||||
myResultTextRange.getEndOffset() - oldTextLength + newTextLength);
|
||||
}
|
||||
|
||||
public boolean checkElementContainsRange(final PsiElement element) {
|
||||
public boolean isElementPartlyInRange(final PsiElement element) {
|
||||
if (myResultTextRange == null) return true;
|
||||
|
||||
final TextRange elementRange = element.getTextRange();
|
||||
@@ -47,7 +47,7 @@ public class AbstractPostFormatProcessor {
|
||||
|
||||
}
|
||||
|
||||
public boolean checkRangeContainsElement(final PsiElement element) {
|
||||
public boolean isElementFullyInRange(final PsiElement element) {
|
||||
if (myResultTextRange == null) return true;
|
||||
|
||||
final TextRange elementRange = element.getTextRange();
|
||||
Reference in New Issue
Block a user