mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Pass formatting mode via CodeStyleSettings (for Enter handler)
This commit is contained in:
@@ -48,12 +48,14 @@ import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
public class CodeStyleSettings extends CommonCodeStyleSettings implements Cloneable, JDOMExternalizable {
|
||||
public class CodeStyleSettings extends CommonCodeStyleSettings implements Cloneable, JDOMExternalizable, UserDataHolder {
|
||||
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 ADDITIONAL_INDENT_OPTIONS = "ADDITIONAL_INDENT_OPTIONS";
|
||||
|
||||
@@ -96,6 +98,17 @@ 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,6 +15,7 @@
|
||||
*/
|
||||
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;
|
||||
@@ -27,11 +28,10 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* Contains utility methods for core formatter processing.
|
||||
*
|
||||
* @author Denis Zhdanov
|
||||
* @since 4/28/11 4:16 PM
|
||||
*/
|
||||
public class CoreFormatterUtil {
|
||||
|
||||
public final static Key<FormattingMode> MODE = Key.create("formatting.mode");
|
||||
|
||||
private CoreFormatterUtil() {
|
||||
}
|
||||
@@ -261,4 +261,17 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,5 +21,5 @@ package com.intellij.formatting;
|
||||
*/
|
||||
public enum FormattingMode {
|
||||
|
||||
REFORMAT, ADJUST_INDENT
|
||||
REFORMAT, ADJUST_INDENT, ADJUST_INDENT_ON_ENTER
|
||||
}
|
||||
|
||||
+13
-2
@@ -15,14 +15,19 @@
|
||||
*/
|
||||
package com.intellij.psi.impl.source.codeStyle.lineIndent;
|
||||
|
||||
import com.intellij.formatting.FormattingMode;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
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;
|
||||
@@ -63,9 +68,15 @@ public class FormatterBasedIndentAdjuster {
|
||||
int lineStart = myDocument.getLineStartOffset(myLine);
|
||||
CommandProcessor.getInstance().runUndoTransparentAction(() ->
|
||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||
CodeStyleManager.getInstance(myProject).adjustLineIndent(myDocument, lineStart);
|
||||
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);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user