diff --git a/java/java-impl/src/com/intellij/psi/formatter/JavadocFormatterUtilHelper.java b/java/java-impl/src/com/intellij/psi/formatter/JavadocFormatterUtilHelper.java deleted file mode 100644 index a7ee0d5733ff..000000000000 --- a/java/java-impl/src/com/intellij/psi/formatter/JavadocFormatterUtilHelper.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2000-2009 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.formatter; - -import com.intellij.lang.ASTNode; -import com.intellij.psi.impl.source.tree.ElementType; -import com.intellij.psi.impl.source.tree.LeafElement; - -/** - * @author max - */ -public class JavadocFormatterUtilHelper implements FormatterUtilHelper { - @Override - public boolean addWhitespace(final ASTNode treePrev, final LeafElement whiteSpaceElement) { - return false; - } - - @Override - public boolean containsWhitespacesOnly(final ASTNode node) { - return node.getElementType() == ElementType.DOC_COMMENT_DATA && node.textContains('\n') && node.getText().trim().length() == 0; - } -} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/psi/formatter/FormatterUtilHelper.java b/java/java-impl/src/com/intellij/psi/formatter/JavadocWhiteSpaceFormattingStrategy.java similarity index 59% rename from platform/lang-impl/src/com/intellij/psi/formatter/FormatterUtilHelper.java rename to java/java-impl/src/com/intellij/psi/formatter/JavadocWhiteSpaceFormattingStrategy.java index 4a50ea5682a6..4f20b9225206 100644 --- a/platform/lang-impl/src/com/intellij/psi/formatter/FormatterUtilHelper.java +++ b/java/java-impl/src/com/intellij/psi/formatter/JavadocWhiteSpaceFormattingStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -13,16 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * @author max - */ package com.intellij.psi.formatter; import com.intellij.lang.ASTNode; -import com.intellij.psi.impl.source.tree.LeafElement; +import com.intellij.psi.JavaDocTokenType; +import org.jetbrains.annotations.NotNull; -public interface FormatterUtilHelper { - boolean containsWhitespacesOnly(ASTNode node); - boolean addWhitespace(final ASTNode treePrev, final LeafElement whiteSpaceElement); -} \ No newline at end of file +/** + * @author max + */ +public class JavadocWhiteSpaceFormattingStrategy extends WhiteSpaceFormattingStrategyAdapter { + @Override + public boolean containsWhitespacesOnly(@NotNull final ASTNode node) { + return node.getElementType() == JavaDocTokenType.DOC_COMMENT_DATA && node.textContains('\n') && node.getText().trim().length() == 0; + } +} diff --git a/java/java-impl/src/com/intellij/psi/impl/source/javadoc/JavadocManagerImpl.java b/java/java-impl/src/com/intellij/psi/impl/source/javadoc/JavadocManagerImpl.java index 4c8adf977de9..72f9fa1ac5b7 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/javadoc/JavadocManagerImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/javadoc/JavadocManagerImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -22,8 +22,6 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiField; import com.intellij.psi.PsiMethod; -import com.intellij.psi.formatter.FormatterUtil; -import com.intellij.psi.formatter.JavadocFormatterUtilHelper; import com.intellij.psi.javadoc.JavadocManager; import com.intellij.psi.javadoc.JavadocTagInfo; import org.jetbrains.annotations.NotNull; @@ -39,10 +37,6 @@ import java.util.List; public class JavadocManagerImpl implements JavadocManager { private final List myInfos; - static { - FormatterUtil.addHelper(new JavadocFormatterUtilHelper()); - } - public JavadocManagerImpl(Project project) { myInfos = new ArrayList(); diff --git a/platform/lang-api/src/com/intellij/openapi/editor/LanguageLineWrapPositionStrategy.java b/platform/lang-api/src/com/intellij/openapi/editor/LanguageLineWrapPositionStrategy.java index ee7a4a28b794..fdbed6855d52 100644 --- a/platform/lang-api/src/com/intellij/openapi/editor/LanguageLineWrapPositionStrategy.java +++ b/platform/lang-api/src/com/intellij/openapi/editor/LanguageLineWrapPositionStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * 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. @@ -42,6 +42,7 @@ public class LanguageLineWrapPositionStrategy extends LanguageExtension ourHelpers = ContainerUtil.createEmptyCOWList(); - public static final Collection FORMATTER_ACTION_NAMES = Collections.unmodifiableCollection(ContainerUtil.addAll( new HashSet(), ReformatAndOptimizeImportsProcessor.COMMAND_NAME, ReformatCodeProcessor.COMMAND_NAME )); @@ -47,10 +44,6 @@ public class FormatterUtil { private FormatterUtil() { } - public static void addHelper(FormatterUtilHelper helper) { - ourHelpers.add(helper); - } - public static boolean isWhitespaceOrEmpty(@Nullable ASTNode node) { if (node == null) return false; IElementType type = node.getElementType(); @@ -209,10 +202,11 @@ public class FormatterUtil { if (node == null) return false; if (isWhitespaceOrEmpty(node)) return true; - for (FormatterUtilHelper helper : ourHelpers) { - if (helper.containsWhitespacesOnly(node)) return true; + for (WhiteSpaceFormattingStrategy strategy : WhiteSpaceFormattingStrategyFactory.getAllStrategies()) { + if (strategy.containsWhitespacesOnly(node)) { + return true; + } } - return false; } @@ -350,8 +344,10 @@ public class FormatterUtil { } private static void addWhiteSpace(final ASTNode treePrev, final LeafElement whiteSpaceElement) { - for (FormatterUtilHelper helper : ourHelpers) { - if (helper.addWhitespace(treePrev, whiteSpaceElement)) return; + for (WhiteSpaceFormattingStrategy strategy : WhiteSpaceFormattingStrategyFactory.getAllStrategies()) { + if (strategy.addWhitespace(treePrev, whiteSpaceElement)) { + return; + } } final ASTNode treeParent = treePrev.getTreeParent(); diff --git a/platform/lang-impl/src/com/intellij/psi/formatter/FormattingDocumentModelImpl.java b/platform/lang-impl/src/com/intellij/psi/formatter/FormattingDocumentModelImpl.java index b62885ad6dcf..6777eef9f01c 100644 --- a/platform/lang-impl/src/com/intellij/psi/formatter/FormattingDocumentModelImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/FormattingDocumentModelImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -17,7 +17,6 @@ package com.intellij.psi.formatter; import com.intellij.formatting.FormattingDocumentModel; -import com.intellij.formatting.WhiteSpaceFormattingStrategy; import com.intellij.lang.Language; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; diff --git a/platform/lang-api/src/com/intellij/formatting/LanguageWhiteSpaceFormattingStrategy.java b/platform/lang-impl/src/com/intellij/psi/formatter/LanguageWhiteSpaceFormattingStrategy.java similarity index 91% rename from platform/lang-api/src/com/intellij/formatting/LanguageWhiteSpaceFormattingStrategy.java rename to platform/lang-impl/src/com/intellij/psi/formatter/LanguageWhiteSpaceFormattingStrategy.java index 03151f76faa2..bc9ee2f729b7 100644 --- a/platform/lang-api/src/com/intellij/formatting/LanguageWhiteSpaceFormattingStrategy.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/LanguageWhiteSpaceFormattingStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * 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. @@ -13,12 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.formatting; +package com.intellij.psi.formatter; import com.intellij.lang.LanguageExtension; -import java.util.Collection; - /** * Exposes pre-configured {@link WhiteSpaceFormattingStrategy} objects to use in a per-language manner. * diff --git a/platform/lang-api/src/com/intellij/psi/formatter/StaticSymbolWhiteSpaceDefinitionStrategy.java b/platform/lang-impl/src/com/intellij/psi/formatter/StaticSymbolWhiteSpaceDefinitionStrategy.java similarity index 95% rename from platform/lang-api/src/com/intellij/psi/formatter/StaticSymbolWhiteSpaceDefinitionStrategy.java rename to platform/lang-impl/src/com/intellij/psi/formatter/StaticSymbolWhiteSpaceDefinitionStrategy.java index 8b475d5f3f33..208304a481ea 100644 --- a/platform/lang-api/src/com/intellij/psi/formatter/StaticSymbolWhiteSpaceDefinitionStrategy.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/StaticSymbolWhiteSpaceDefinitionStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * 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. @@ -15,7 +15,6 @@ */ package com.intellij.psi.formatter; -import com.intellij.formatting.WhiteSpaceFormattingStrategy; import gnu.trove.TIntHashSet; import org.jetbrains.annotations.NotNull; diff --git a/platform/lang-api/src/com/intellij/psi/formatter/StaticTextWhiteSpaceDefinitionStrategy.java b/platform/lang-impl/src/com/intellij/psi/formatter/StaticTextWhiteSpaceDefinitionStrategy.java similarity index 97% rename from platform/lang-api/src/com/intellij/psi/formatter/StaticTextWhiteSpaceDefinitionStrategy.java rename to platform/lang-impl/src/com/intellij/psi/formatter/StaticTextWhiteSpaceDefinitionStrategy.java index a0b74f2ff73c..ecf51aacde47 100644 --- a/platform/lang-api/src/com/intellij/psi/formatter/StaticTextWhiteSpaceDefinitionStrategy.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/StaticTextWhiteSpaceDefinitionStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * 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. diff --git a/platform/lang-api/src/com/intellij/formatting/WhiteSpaceFormattingStrategy.java b/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategy.java similarity index 77% rename from platform/lang-api/src/com/intellij/formatting/WhiteSpaceFormattingStrategy.java rename to platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategy.java index 7248f3bd6497..200738ab2bab 100644 --- a/platform/lang-api/src/com/intellij/formatting/WhiteSpaceFormattingStrategy.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * 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. @@ -13,9 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.formatting; +package com.intellij.psi.formatter; +import com.intellij.lang.ASTNode; import com.intellij.psi.PsiElement; +import com.intellij.psi.impl.source.tree.LeafElement; import org.jetbrains.annotations.NotNull; /** @@ -43,6 +45,14 @@ public interface WhiteSpaceFormattingStrategy { */ int check(@NotNull CharSequence text, int start, int end); + /** + * Allows to answer if given node should be treated as white space node. + * + * @param node node to check + * @return true if given node should be treated as white space; false otherwise + */ + boolean containsWhitespacesOnly(@NotNull ASTNode node); + /** * @return true if default white space strategy used by formatter should be replaced by the current one; * false to indicate that current strategy should be used in composition with default strategy @@ -52,7 +62,7 @@ public interface WhiteSpaceFormattingStrategy { boolean replaceDefaultStrategy(); /** - * Main formatter duty is to tweak white space symbols (add/remove/modify them). However, it may be necessary + * Main formatter's duty is to tweak white space symbols (add/remove/modify them). However, it may be necessary * to pay special attention to that. For example it may be necessary to ensure that '\' symbol is * used inside multiline expression in case of Python etc. *

@@ -86,4 +96,16 @@ public interface WhiteSpaceFormattingStrategy { */ CharSequence adjustWhiteSpaceIfNecessary(@NotNull CharSequence whiteSpaceText, @NotNull PsiElement startElement, int startOffset, int endOffset); + + /** + * Allows to customize addition of the given white space element to the AST referenced by the given node. + * + * @param treePrev target node to use as an anchor for inserting given white space element + * @param whiteSpaceElement target white space element to insert + * @return true if given white space element was added in a custom way during the current method call + * processing; + * false as an indicator that given white space element has not been inserted during the + * current method call + */ + boolean addWhitespace(@NotNull ASTNode treePrev, @NotNull LeafElement whiteSpaceElement); } diff --git a/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategyAdapter.java b/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategyAdapter.java new file mode 100644 index 000000000000..e6778bb4bdf9 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategyAdapter.java @@ -0,0 +1,67 @@ +/* + * 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.psi.formatter; + +import com.intellij.lang.ASTNode; +import com.intellij.psi.PsiElement; +import com.intellij.psi.impl.source.tree.LeafElement; +import org.jetbrains.annotations.NotNull; + +/** + * @author Denis Zhdanov + * @since 12/6/11 3:58 PM + */ +public class WhiteSpaceFormattingStrategyAdapter implements WhiteSpaceFormattingStrategy { + + private final WhiteSpaceFormattingStrategy DELEGATE = new StaticSymbolWhiteSpaceDefinitionStrategy(' ', '\t', '\n'); + + @Override + public int check(@NotNull CharSequence text, int start, int end) { + return DELEGATE.check(text, start, end); + } + + @Override + public boolean containsWhitespacesOnly(@NotNull ASTNode node) { + return false; + } + + @Override + public boolean replaceDefaultStrategy() { + return false; + } + + @NotNull + @Override + public CharSequence adjustWhiteSpaceIfNecessary(@NotNull CharSequence whiteSpaceText, + @NotNull CharSequence text, + int startOffset, + int endOffset) { + return whiteSpaceText; + } + + @Override + public CharSequence adjustWhiteSpaceIfNecessary(@NotNull CharSequence whiteSpaceText, + @NotNull PsiElement startElement, + int startOffset, + int endOffset) { + return whiteSpaceText; + } + + @Override + public boolean addWhitespace(@NotNull ASTNode treePrev, @NotNull LeafElement whiteSpaceElement) { + return false; + } +} diff --git a/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategyFactory.java b/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategyFactory.java index cfc9df603f61..0f9cbdfeaba0 100644 --- a/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategyFactory.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/WhiteSpaceFormattingStrategyFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * 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. @@ -15,17 +15,17 @@ */ package com.intellij.psi.formatter; -import com.intellij.formatting.LanguageWhiteSpaceFormattingStrategy; -import com.intellij.formatting.WhiteSpaceFormattingStrategy; import com.intellij.lang.Language; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; +import com.intellij.util.PatchedWeakReference; import org.jetbrains.annotations.NotNull; -import java.util.Arrays; -import java.util.List; +import java.lang.ref.WeakReference; +import java.util.*; +import java.util.concurrent.atomic.AtomicReference; /** * Contains utility methods for working with {@link WhiteSpaceFormattingStrategy}. @@ -39,6 +39,9 @@ public class WhiteSpaceFormattingStrategyFactory { new StaticSymbolWhiteSpaceDefinitionStrategy(' ', '\t', '\n'), new CdataWhiteSpaceDefinitionStrategy() ); + private static final AtomicReference>> myCachedStrategies + = new AtomicReference>>(); + private WhiteSpaceFormattingStrategyFactory() { } @@ -65,6 +68,37 @@ public class WhiteSpaceFormattingStrategyFactory { return result; } + /** + * @return collection of all registered white space strategies + */ + @NotNull + public static Collection getAllStrategies() { + final WeakReference> reference = myCachedStrategies.get(); + if (reference != null) { + final Collection strategies = reference.get(); + if (strategies != null) { + return strategies; + } + } + final Collection languages = Language.getRegisteredLanguages(); + if (languages == null) { + final List result = Collections.emptyList(); + myCachedStrategies.set(new PatchedWeakReference>(result)); + return result; + } + + Set result = new HashSet(SHARED_STRATEGIES); + final LanguageWhiteSpaceFormattingStrategy languageStrategy = LanguageWhiteSpaceFormattingStrategy.INSTANCE; + for (Language language : languages) { + final WhiteSpaceFormattingStrategy strategy = languageStrategy.forLanguage(language); + if (strategy != null) { + result.add(strategy); + } + } + myCachedStrategies.set(new PatchedWeakReference>(result)); + return result; + } + /** * Returns white space strategy to use for the document managed by the given editor. * diff --git a/platform/platform-resources/src/META-INF/XmlPlugin.xml b/platform/platform-resources/src/META-INF/XmlPlugin.xml index c34a9f2622b0..35e5c4d04628 100644 --- a/platform/platform-resources/src/META-INF/XmlPlugin.xml +++ b/platform/platform-resources/src/META-INF/XmlPlugin.xml @@ -152,6 +152,8 @@ + diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 417f7e3b014a..5f56a92b7bc2 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -690,6 +690,8 @@ + diff --git a/xml/impl/src/com/intellij/lang/xml/XmlFormattingModelBuilder.java b/xml/impl/src/com/intellij/lang/xml/XmlFormattingModelBuilder.java index 5fa15242b867..d87bdf460328 100644 --- a/xml/impl/src/com/intellij/lang/xml/XmlFormattingModelBuilder.java +++ b/xml/impl/src/com/intellij/lang/xml/XmlFormattingModelBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -26,21 +26,15 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleSettings; -import com.intellij.psi.formatter.FormatterUtil; import com.intellij.psi.formatter.FormattingDocumentModelImpl; -import com.intellij.psi.formatter.XmlFormatterUtilHelper; import com.intellij.psi.formatter.xml.XmlBlock; import com.intellij.psi.formatter.xml.XmlPolicy; import com.intellij.psi.impl.source.SourceTreeToPsiMap; -import com.intellij.lang.xml.XmlFormattingModel; import com.intellij.psi.impl.source.tree.TreeElement; import com.intellij.psi.impl.source.tree.TreeUtil; import org.jetbrains.annotations.NotNull; public class XmlFormattingModelBuilder implements FormattingModelBuilder { - static { - FormatterUtil.addHelper(new XmlFormatterUtilHelper()); - } @NotNull public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) { @@ -54,4 +48,4 @@ public class XmlFormattingModelBuilder implements FormattingModelBuilder { public TextRange getRangeAffectingIndent(PsiFile file, int offset, ASTNode elementAtOffset) { return null; } -} \ No newline at end of file +} diff --git a/xml/impl/src/com/intellij/psi/formatter/XmlFormatterUtilHelper.java b/xml/impl/src/com/intellij/lang/xml/XmlWhiteSpaceFormattingStrategy.java similarity index 85% rename from xml/impl/src/com/intellij/psi/formatter/XmlFormatterUtilHelper.java rename to xml/impl/src/com/intellij/lang/xml/XmlWhiteSpaceFormattingStrategy.java index fb39592bd81e..10cab622cd93 100644 --- a/xml/impl/src/com/intellij/psi/formatter/XmlFormatterUtilHelper.java +++ b/xml/impl/src/com/intellij/lang/xml/XmlWhiteSpaceFormattingStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -13,14 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * @author max - */ -package com.intellij.psi.formatter; +package com.intellij.lang.xml; import com.intellij.lang.ASTFactory; import com.intellij.lang.ASTNode; +import com.intellij.psi.formatter.WhiteSpaceFormattingStrategyAdapter; import com.intellij.psi.impl.source.codeStyle.CodeEditUtil; import com.intellij.psi.impl.source.tree.Factory; import com.intellij.psi.impl.source.tree.LeafElement; @@ -30,8 +27,14 @@ import com.intellij.psi.xml.XmlElementType; import com.intellij.psi.xml.XmlText; import com.intellij.psi.xml.XmlTokenType; import com.intellij.util.CharTable; +import org.jetbrains.annotations.NotNull; + +/** + * @author Denis Zhdanov + * @since 12/6/11 4:51 PM + */ +public class XmlWhiteSpaceFormattingStrategy extends WhiteSpaceFormattingStrategyAdapter { -public class XmlFormatterUtilHelper implements FormatterUtilHelper { private static void addWhitespaceToTagBody(final ASTNode treePrev, final LeafElement whiteSpaceElement) { final CharTable charTable = SharedImplUtil.findCharTableByTree(treePrev); final ASTNode treeParent = treePrev.getTreeParent(); @@ -64,7 +67,7 @@ public class XmlFormatterUtilHelper implements FormatterUtilHelper { } } - protected boolean isInsideTagBody(ASTNode place) { + protected boolean isInsideTagBody(@NotNull ASTNode place) { final ASTNode treeParent = place.getTreeParent(); if(treeParent.getElementType() != XmlElementType.XML_TAG && treeParent.getElementType() != XmlElementType.HTML_TAG) return false; @@ -75,7 +78,7 @@ public class XmlFormatterUtilHelper implements FormatterUtilHelper { return false; } - public boolean addWhitespace(final ASTNode treePrev, final LeafElement whiteSpaceElement) { + public boolean addWhitespace(@NotNull final ASTNode treePrev, @NotNull final LeafElement whiteSpaceElement) { if (isInsideTagBody(treePrev)) { addWhitespaceToTagBody(treePrev, whiteSpaceElement); return true; @@ -84,7 +87,7 @@ public class XmlFormatterUtilHelper implements FormatterUtilHelper { return false; } - public boolean containsWhitespacesOnly(final ASTNode node) { + public boolean containsWhitespacesOnly(@NotNull final ASTNode node) { return (node.getElementType() == XmlTokenType.XML_DATA_CHARACTERS) && node.getText().trim().length() == 0; }