Introduce generic DocCommentSettings

for use in common algorithms, no direct usage of Java-specific options from
CodeStyleSetting anymore.
This commit is contained in:
Rustam Vishnyakov
2017-07-15 17:40:56 +03:00
parent d79c44ae44
commit 0a17b73142
7 changed files with 153 additions and 25 deletions
@@ -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" +
@@ -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;
}
}
@@ -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;
}
}
}
@@ -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;
}
}
@@ -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<PsiComment> 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<PsiComment> 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,
@@ -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));
}
}
}
@@ -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;
}
}