diff --git a/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java b/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java index 7e67d189bf20..b6ef41f05fe2 100644 --- a/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java +++ b/java/java-impl/src/com/intellij/ide/JavaLanguageCodeStyleSettingsProvider.java @@ -214,6 +214,33 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett return new JavaIndentOptionsEditor(); } + + @Override + @NotNull + public DocCommentSettings getDocCommentSettings(@NotNull PsiFile file) { + if (file.isValid()) { + return new DocCommentSettings() { + private final CodeStyleSettings mySettings = CodeStyleSettingsManager.getSettings(file.getProject()); + + @Override + public boolean isDocFormattingEnabled() { + return mySettings.ENABLE_JAVADOC_FORMATTING; + } + + @Override + public void setDocFormattingEnabled(boolean formattingEnabled) { + mySettings.ENABLE_JAVADOC_FORMATTING = formattingEnabled; + } + + @Override + public boolean isLeadingAsteriskEnabled() { + return mySettings.JD_LEADING_ASTERISKS_ARE_ENABLED; + } + }; + } + return super.getDocCommentSettings(file); + } + private static final String GENERAL_CODE_SAMPLE = "public class Foo {\n" + " public int[] X = new int[]{1, 3, 5, 7, 9, 11};\n" + diff --git a/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java b/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java index 007f4359cfed..730a40052f5e 100644 --- a/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java +++ b/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java @@ -303,4 +303,18 @@ public abstract class CodeStyleManager { } return FormattingMode.REFORMAT; } + + /** + * Run the given runnable disabling doc comment formatting. + * @param file The file for which doc comment formatting should be temporarily disabled. + * @param runnable The runnable to run. + */ + public void runWithDocCommentFormattingDisabled(@NotNull PsiFile file, @NotNull Runnable runnable) { + runnable.run(); + } + + @NotNull + public DocCommentSettings getDocCommentSettings(@NotNull PsiFile file) { + return DocCommentSettings.DEFAULTS; + } } diff --git a/platform/core-api/src/com/intellij/psi/codeStyle/DocCommentSettings.java b/platform/core-api/src/com/intellij/psi/codeStyle/DocCommentSettings.java new file mode 100644 index 000000000000..3c86b15b58bd --- /dev/null +++ b/platform/core-api/src/com/intellij/psi/codeStyle/DocCommentSettings.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2017 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.codeStyle; + +/** + * A set of code documentation comment settings if supported by a language used in common doc comment handling algorithms. + */ +public interface DocCommentSettings { + /** + * Default doc comment settings if not provided by {@code LanguageCodeStyleSettingsProvider} + */ + DocCommentSettings DEFAULTS = new Defaults(); + + /** + * @return True if doc comment formatting enabled. + */ + boolean isDocFormattingEnabled(); + + /** + * Enable or disable doc comment formatting. + * @param formattingEnabled The enable/disable flag. + */ + void setDocFormattingEnabled(boolean formattingEnabled); + + /** + * @return True if a leading asterisk '*' should be inserted on a new comment line. + */ + boolean isLeadingAsteriskEnabled(); + + final class Defaults implements DocCommentSettings { + + @Override + public boolean isDocFormattingEnabled() { + return true; + } + + @Override + public void setDocFormattingEnabled(boolean formattingEnabled) { + } + + @Override + public boolean isLeadingAsteriskEnabled() { + return true; + } + } +} diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java b/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java index 06e52c69a1dc..b2ded58919f7 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java @@ -276,4 +276,14 @@ public abstract class LanguageCodeStyleSettingsProvider { myCollectedFields.add(fieldName); } } + + /** + * Returns code documentation comment settings for the PSI file. + * @param file The file to return current document settings for. + * @return Documentation comment settings. + */ + @NotNull + public DocCommentSettings getDocCommentSettings(@NotNull PsiFile file) { + return DocCommentSettings.DEFAULTS; + } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java index ff3f922c2c4c..5eeb995df704 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java @@ -43,7 +43,6 @@ import com.intellij.openapi.util.UserDataHolder; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.formatter.FormatterUtil; import com.intellij.psi.impl.source.PostprocessReformattingAspect; @@ -372,7 +371,8 @@ public class EnterHandler extends BaseEnterHandler { commentContext.docStart = false; } else { - commentContext.docAsterisk = CodeStyleSettingsManager.getSettings(getProject()).JD_LEADING_ASTERISKS_ARE_ENABLED; + commentContext.docAsterisk = + CodeStyleManager.getInstance(getProject()).getDocCommentSettings(myFile).isLeadingAsteriskEnabled(); commentContext.docStart = false; } } @@ -550,7 +550,7 @@ public class EnterHandler extends BaseEnterHandler { myOffset = CharArrayUtil.shiftForwardUntil(text, myOffset, docCommentLinePrefix) + 1; removeTrailingSpaces(myDocument, myOffset); - if (!CodeStyleSettingsManager.getSettings(getProject()).JD_LEADING_ASTERISKS_ARE_ENABLED) { + if (!CodeStyleManager.getInstance(getProject()).getDocCommentSettings(myFile).isLeadingAsteriskEnabled()) { LOG.assertTrue(CharArrayUtil.regionMatches(myDocument.getCharsSequence(),myOffset - docCommentLinePrefix.length(), docCommentLinePrefix)); myDocument.deleteString(myOffset - docCommentLinePrefix.length(), myOffset); myOffset--; @@ -578,20 +578,10 @@ public class EnterHandler extends BaseEnterHandler { } CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject()); - CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(getProject()); - boolean old = codeStyleSettings.ENABLE_JAVADOC_FORMATTING; - codeStyleSettings.ENABLE_JAVADOC_FORMATTING = false; + final Ref commentRef = Ref.create(comment); + codeStyleManager.runWithDocCommentFormattingDisabled(myFile, () -> formatComment(commentRef, codeStyleManager)); + comment = commentRef.get(); - try { - RangeMarker commentMarker = myDocument.createRangeMarker(comment.getTextRange().getStartOffset(), - comment.getTextRange().getEndOffset()); - codeStyleManager.reformatNewlyAddedElement(comment.getNode().getTreeParent(), comment.getNode()); - comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(commentMarker.getStartOffset()), PsiComment.class); - commentMarker.dispose(); - } - finally { - codeStyleSettings.ENABLE_JAVADOC_FORMATTING = old; - } PsiElement next = comment.getNextSibling(); if (next == null && comment.getParent().getClass() == comment.getClass()) { next = comment.getParent().getNextSibling(); // expanding chameleon comment produces comment under comment @@ -609,6 +599,15 @@ public class EnterHandler extends BaseEnterHandler { return comment; } + private void formatComment(Ref commentRef, CodeStyleManager codeStyleManager) { + PsiComment comment = commentRef.get(); + RangeMarker commentMarker = myDocument.createRangeMarker(comment.getTextRange().getStartOffset(), + comment.getTextRange().getEndOffset()); + codeStyleManager.reformatNewlyAddedElement(comment.getNode().getTreeParent(), comment.getNode()); + commentRef.set(PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(commentMarker.getStartOffset()), PsiComment.class)); + commentMarker.dispose(); + } + @Nullable private PsiComment createJavaDocStub(final CodeInsightSettings settings, final PsiComment comment, diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/PostprocessReformattingAspect.java b/platform/lang-impl/src/com/intellij/psi/impl/source/PostprocessReformattingAspect.java index e88e4c29e95d..939f75432ecf 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/PostprocessReformattingAspect.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/PostprocessReformattingAspect.java @@ -40,6 +40,7 @@ import com.intellij.pom.tree.events.ChangeInfo; import com.intellij.pom.tree.events.TreeChange; import com.intellij.pom.tree.events.TreeChangeEvent; import com.intellij.psi.*; +import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.impl.PsiManagerEx; @@ -344,16 +345,11 @@ public class PostprocessReformattingAspect implements PomModelAspect { toDispose.addAll(normalizedActions); // only in following loop real changes in document are made + final FileViewProvider viewProvider = key; for (final PostponedAction normalizedAction : normalizedActions) { - CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myPsiManager.getProject()); - boolean old = settings.ENABLE_JAVADOC_FORMATTING; - settings.ENABLE_JAVADOC_FORMATTING = false; - try { - normalizedAction.execute(key); - } - finally { - settings.ENABLE_JAVADOC_FORMATTING = old; - } + CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myPsiManager.getProject()); + codeStyleManager.runWithDocCommentFormattingDisabled( + viewProvider.getPsi(viewProvider.getBaseLanguage()), () -> normalizedAction.execute(viewProvider)); } } } diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java index 79e537934960..0222af396ef5 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java @@ -905,4 +905,27 @@ public class CodeStyleManagerImpl extends CodeStyleManager implements Formatting CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(file.getProject()); return builder.createModel(file, settings); } + + @Override + public void runWithDocCommentFormattingDisabled(@NotNull PsiFile file, @NotNull Runnable runnable) { + DocCommentSettings docSettings = getDocCommentSettings(file); + boolean currDocFormattingEnabled = docSettings.isDocFormattingEnabled(); + docSettings.setDocFormattingEnabled(false); + try { + runnable.run(); + } + finally { + docSettings.setDocFormattingEnabled(currDocFormattingEnabled); + } + } + + @NotNull + public DocCommentSettings getDocCommentSettings(@NotNull PsiFile file) { + Language language = file.getLanguage(); + LanguageCodeStyleSettingsProvider settingsProvider = LanguageCodeStyleSettingsProvider.forLanguage(language); + if (settingsProvider != null) { + return settingsProvider.getDocCommentSettings(file); + } + return DocCommentSettings.DEFAULTS; + } }