Reworked formatting mode in CodeStyleManager [IDEA-CR-15969]

This commit is contained in:
Rustam Vishnyakov
2016-11-21 16:21:21 +03:00
parent 12078d486d
commit c7c4b833bb
8 changed files with 60 additions and 58 deletions
@@ -15,6 +15,7 @@
*/
package com.intellij.psi.codeStyle;
import com.intellij.formatting.FormattingMode;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Document;
@@ -180,9 +181,18 @@ public abstract class CodeStyleManager {
*
* @param document the document to reformat.
* @param offset the offset the line at which should be reformatted.
* @param mode the current formatting mode to be used when adjusting line indent.
* @throws IncorrectOperationException if the file is read-only.
* @see FormattingMode
*/
public abstract int adjustLineIndent(@NotNull Document document, int offset);
public abstract int adjustLineIndent(@NotNull Document document, int offset, FormattingMode mode);
/**
* The same as {@link #adjustLineIndent(Document, int, FormattingMode)} but uses {@link FormattingMode#ADJUST_INDENT} as formatting mode.
*/
public final int adjustLineIndent(@NotNull Document document, int offset) {
return adjustLineIndent(document, offset, FormattingMode.ADJUST_INDENT);
}
/**
* @deprecated this method is not intended to be used by plugins.
@@ -264,4 +274,6 @@ public abstract class CodeStyleManager {
public abstract <T extends Throwable> void performActionWithFormatterDisabled(ThrowableRunnable<T> r) throws T;
public abstract <T> T performActionWithFormatterDisabled(Computable<T> r);
public abstract FormattingMode getCurrentFormattingMode();
}
@@ -48,14 +48,12 @@ import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class CodeStyleSettings extends CommonCodeStyleSettings implements Cloneable, JDOMExternalizable, UserDataHolder {
public class CodeStyleSettings extends CommonCodeStyleSettings implements Cloneable, JDOMExternalizable {
public static final int MAX_RIGHT_MARGIN = 1000;
private static final Logger LOG = Logger.getInstance(CodeStyleSettings.class);
private final ClassMap<CustomCodeStyleSettings> myCustomSettings = new ClassMap<>();
private final UserDataHolder myUserDataHolder = new UserDataHolderBase();
@NonNls private static final String REPEAT_ANNOTATIONS = "REPEAT_ANNOTATIONS";
@NonNls private static final String ADDITIONAL_INDENT_OPTIONS = "ADDITIONAL_INDENT_OPTIONS";
@@ -99,17 +97,6 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea
PARAMETER_TYPE_TO_NAME.addPair("*Exception", "e");
}
@Nullable
@Override
public <T> T getUserData(@NotNull Key<T> key) {
return myUserDataHolder.getUserData(key);
}
@Override
public <T> void putUserData(@NotNull Key<T> key, @Nullable T value) {
myUserDataHolder.putUserData(key, value);
}
private static void initGeneralLocalVariable(@NonNls TypeToNameMap map) {
map.addPair("int", "i");
map.addPair("byte", "b");
@@ -15,7 +15,6 @@
*/
package com.intellij.formatting;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiElement;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
@@ -30,8 +29,6 @@ import java.util.Set;
* Contains utility methods for core formatter processing.
*/
public class CoreFormatterUtil {
public final static Key<FormattingMode> MODE = Key.create("formatting.mode");
private CoreFormatterUtil() {
}
@@ -261,17 +258,4 @@ public class CoreFormatterUtil {
return getFirstLeaf(((CompositeBlockWrapper)block).getChildren().get(0));
}
}
/**
* Retrieves the current formatting mode from settings' user data.
*
* @param settings The settings.
* @return The current formatting mode (<code>FormattingMode.REFORMAT</code> by default).
* @see FormattingMode
*/
@NotNull
public static FormattingMode getFormattingMode(@NotNull CodeStyleSettings settings) {
FormattingMode mode = settings.getUserData(MODE);
return mode != null ? mode : FormattingMode.REFORMAT;
}
}
@@ -68,6 +68,8 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
}
};
private final ThreadLocal<FormattingMode> myCurrentFormattingMode = ThreadLocal.withInitial(() -> FormattingMode.REFORMAT);
private final FormatterTagHandler myTagHandler;
private final Project myProject;
@@ -328,7 +330,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
public int adjustLineIndent(@NotNull final PsiFile file, final int offset) throws IncorrectOperationException {
DetectedIndentOptionsNotificationProvider.updateIndentNotification(file, false);
return PostprocessReformattingAspect.getInstance(file.getProject()).disablePostprocessFormattingInside(
() -> doAdjustLineIndentByOffset(file, offset));
() -> doAdjustLineIndentByOffset(file, offset, FormattingMode.ADJUST_INDENT));
}
@Nullable
@@ -347,7 +349,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
}
@Override
public int adjustLineIndent(@NotNull final Document document, final int offset) {
public int adjustLineIndent(@NotNull final Document document, final int offset, FormattingMode mode) {
return PostprocessReformattingAspect.getInstance(getProject()).disablePostprocessFormattingInside(() -> {
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
documentManager.commitDocument(document);
@@ -355,12 +357,12 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
PsiFile file = documentManager.getPsiFile(document);
if (file == null) return offset;
return doAdjustLineIndentByOffset(file, offset);
return doAdjustLineIndentByOffset(file, offset, mode);
});
}
private int doAdjustLineIndentByOffset(@NotNull PsiFile file, int offset) {
final Integer result = new CodeStyleManagerRunnable<Integer>(this, FormattingMode.ADJUST_INDENT) {
private int doAdjustLineIndentByOffset(@NotNull PsiFile file, int offset, FormattingMode mode) {
final Integer result = new CodeStyleManagerRunnable<Integer>(this, mode) {
@Override
protected Integer doPerform(int offset, TextRange range) {
return FormatterEx.getInstanceEx().adjustLineIndent(myModel, mySettings, myIndentOptions, offset, mySignificantRange);
@@ -874,4 +876,13 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
result = result.grown(delta);
return result;
}
@Override
public FormattingMode getCurrentFormattingMode() {
return myCurrentFormattingMode.get();
}
void setCurrentFormattingMode(@NotNull FormattingMode mode) {
myCurrentFormattingMode.set(mode);
}
}
@@ -99,21 +99,32 @@ abstract class CodeStyleManagerRunnable<T> {
mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null;
myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange);
myModel = CoreFormatterUtil.buildModel(builder, file, mySettings, myMode);
if (document != null && useDocumentBaseFormattingModel()) {
myModel = new DocumentBasedFormattingModel(myModel, document, myCodeStyleManager.getProject(), mySettings,
file.getFileType(), file);
FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode();
myCodeStyleManager.setCurrentFormattingMode(myMode);
try {
myModel = buildModel(builder, file, document);
T result = doPerform(offset, range);
if (result != null) {
return result;
}
}
final T result = doPerform(offset, range);
if (result != null) {
return result;
finally {
myCodeStyleManager.setCurrentFormattingMode(currentMode);
}
}
return defaultValue;
}
@NotNull
private FormattingModel buildModel(@NotNull FormattingModelBuilder builder, @NotNull PsiFile file, @Nullable Document document) {
FormattingModel model = CoreFormatterUtil.buildModel(builder, file, mySettings, myMode);
if (document != null && useDocumentBaseFormattingModel()) {
model = new DocumentBasedFormattingModel(model, document, myCodeStyleManager.getProject(), mySettings,
file.getFileType(), file);
}
return model;
}
protected boolean useDocumentBaseFormattingModel() {
return true;
}
@@ -22,12 +22,8 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import org.jetbrains.annotations.NotNull;
import static com.intellij.formatting.CoreFormatterUtil.MODE;
public class FormatterBasedIndentAdjuster {
private final static int MAX_SYNCHRONOUS_ADJUSTMENT_DOC_SIZE = 100000;
@@ -68,15 +64,9 @@ public class FormatterBasedIndentAdjuster {
int lineStart = myDocument.getLineStartOffset(myLine);
CommandProcessor.getInstance().runUndoTransparentAction(() ->
ApplicationManager.getApplication().runWriteAction(() -> {
CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings();
settings.putUserData(MODE, FormattingMode.ADJUST_INDENT_ON_ENTER);
try {
CodeStyleManager.getInstance(myProject).adjustLineIndent(myDocument, lineStart);
}
finally {
settings.putUserData(MODE, null);
}
CodeStyleManager.getInstance(myProject).adjustLineIndent(myDocument, lineStart, FormattingMode.ADJUST_INDENT_ON_ENTER);
}));
}
}
}
@@ -15,6 +15,7 @@
*/
package com.intellij.codeInsight.actions;
import com.intellij.formatting.FormattingMode;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileTypes.FileType;
@@ -123,7 +124,7 @@ public class MockCodeStyleManager extends CodeStyleManager {
}
@Override
public int adjustLineIndent(@NotNull Document document, int offset) {
public int adjustLineIndent(@NotNull Document document, int offset, FormattingMode mode) {
throw new UnsupportedOperationException("com.intellij.codeInsight.actions.MockCodeStyleManager.adjustLineIndent(...)");
}
@@ -187,4 +188,10 @@ public class MockCodeStyleManager extends CodeStyleManager {
throw new UnsupportedOperationException(
"com.intellij.codeInsight.actions.MockCodeStyleManager.performActionWithFormatterDisabled(...)");
}
@Override
public FormattingMode getCurrentFormattingMode() {
throw new UnsupportedOperationException(
"com.intellij.codeInsight.actions.MockCodeStyleManager.getCurrentFormattingMode(...)");
}
}