mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-59662: Provide ability to specify right margin (columns) for each file type separately (enabled for PHP)
This commit is contained in:
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.folding.impl;
|
||||
|
||||
import com.intellij.codeInsight.ExpectedTypeInfo;
|
||||
import com.intellij.codeInsight.ExpectedTypesProvider;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
@@ -27,7 +28,7 @@ public class JavaFoldingBuilder extends JavaFoldingBuilderBase {
|
||||
@Override
|
||||
protected boolean isBelowRightMargin(Project project, int lineLength) {
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
|
||||
return lineLength <= settings.RIGHT_MARGIN;
|
||||
return lineLength <= settings.getRightMargin(JavaLanguage.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.psi.impl.source.codeStyle.javadoc;
|
||||
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
@@ -469,20 +470,21 @@ public class JDParser {
|
||||
boolean firstLineShorter,
|
||||
int firstLinePrefixLength)
|
||||
{
|
||||
int rightMargin = mySettings.getRightMargin(JavaLanguage.INSTANCE);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
List<String> list;
|
||||
|
||||
//If wrap comments selected, comments should be wrapped by the right margin
|
||||
if (mySettings.WRAP_COMMENTS) {
|
||||
list = toArrayWrapping(str, mySettings.RIGHT_MARGIN - prefix.length());
|
||||
list = toArrayWrapping(str, rightMargin - prefix.length());
|
||||
|
||||
if (firstLineShorter
|
||||
&& list != null && !list.isEmpty()
|
||||
&& list.get(0).length() > mySettings.RIGHT_MARGIN - firstLinePrefixLength)
|
||||
&& list.get(0).length() > rightMargin - firstLinePrefixLength)
|
||||
{
|
||||
list = new ArrayList<String>();
|
||||
//want the first line to be shorter, according to it's prefix
|
||||
String firstLine = toArrayWrapping(str, mySettings.RIGHT_MARGIN - firstLinePrefixLength).get(0);
|
||||
String firstLine = toArrayWrapping(str, rightMargin - firstLinePrefixLength).get(0);
|
||||
//so now first line is exactly same width we need
|
||||
list.add(firstLine);
|
||||
str = str.substring(firstLine.length());
|
||||
@@ -493,7 +495,7 @@ public class JDParser {
|
||||
}
|
||||
|
||||
//getting all another lines according to their prefix
|
||||
List<String> subList = toArrayWrapping(str, mySettings.RIGHT_MARGIN - prefix.length());
|
||||
List<String> subList = toArrayWrapping(str, rightMargin - prefix.length());
|
||||
|
||||
//removing pre tag
|
||||
if (unclosedPreTag && subList != null && !subList.isEmpty()) {
|
||||
|
||||
@@ -56,6 +56,7 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea
|
||||
|
||||
public CodeStyleSettings(boolean loadExtensions) {
|
||||
super(null);
|
||||
RIGHT_MARGIN = DEFAULT_RIGHT_MARGIN;
|
||||
initTypeToName();
|
||||
initImportsByDefault();
|
||||
|
||||
@@ -243,7 +244,6 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea
|
||||
public int INNER_CLASSES_ORDER_WEIGHT = 7;
|
||||
|
||||
//----------------- WRAPPING ---------------------------
|
||||
public int RIGHT_MARGIN = 120;
|
||||
public boolean WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN = false;
|
||||
|
||||
|
||||
@@ -875,4 +875,38 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea
|
||||
return myCommonSettingsManager.getCommonSettings(langName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves right margin for the given language. The language may overwrite default RIGHT_MARGIN value with its own RIGHT_MARGIN
|
||||
* in language's CommonCodeStyleSettings instance.
|
||||
*
|
||||
* @param language The language to get right margin for or null if root (default) right margin is requested.
|
||||
* @return The right margin for the language if it is defined (not null) and its settings contain non-negative margin. Root (default)
|
||||
* margin otherwise (CodeStyleSettings.RIGHT_MARGIN).
|
||||
*/
|
||||
public int getRightMargin(@Nullable Language language) {
|
||||
if (language != null) {
|
||||
CommonCodeStyleSettings langSettings = getCommonSettings(language);
|
||||
if (langSettings != null) {
|
||||
if (langSettings.RIGHT_MARGIN >= 0) return langSettings.RIGHT_MARGIN;
|
||||
}
|
||||
}
|
||||
return RIGHT_MARGIN;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns another right margin for the language or (if it is null) to root (default) margin.
|
||||
*
|
||||
* @param language The language to assign the right margin to or null if root (default) right margin is to be changed.
|
||||
* @param rightMargin New right margin.
|
||||
*/
|
||||
public void setRightMargin(@Nullable Language language, int rightMargin) {
|
||||
if (language != null) {
|
||||
CommonCodeStyleSettings langSettings = getCommonSettings(language);
|
||||
if (langSettings != null) {
|
||||
langSettings.RIGHT_MARGIN = rightMargin;
|
||||
return;
|
||||
}
|
||||
}
|
||||
RIGHT_MARGIN = rightMargin;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +111,7 @@ public interface CodeStyleSettingsCustomizable {
|
||||
}
|
||||
|
||||
enum WrappingOrBraceOption {
|
||||
RIGHT_MARGIN,
|
||||
KEEP_CONTROL_STATEMENT_IN_ONE_LINE,
|
||||
LINE_COMMENT_AT_FIRST_COLUMN,
|
||||
BLOCK_COMMENT_AT_FIRST_COLUMN,
|
||||
|
||||
@@ -161,7 +161,13 @@ public class CommonCodeStyleSettings {
|
||||
void copyNonDefaultValuesFrom(CommonCodeStyleSettings from) {
|
||||
CommonCodeStyleSettings defaultSettings = new CommonCodeStyleSettings(null);
|
||||
PARENT_SETTINGS_INSTALLED =
|
||||
copyFields(getClass().getFields(), from, this, new SupportedFieldsDiffFilter(from, getSupportedFields(), defaultSettings));
|
||||
copyFields(getClass().getFields(), from, this, new SupportedFieldsDiffFilter(from, getSupportedFields(), defaultSettings) {
|
||||
@Override
|
||||
public boolean isAccept(@NotNull Field field) {
|
||||
if ("RIGHT_MARGIN".equals(field.getName())) return false; // Never copy RIGHT_MARGIN, it is inherited automatically if -1
|
||||
return super.isAccept(field);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void copyFields(Field[] fields, Object from, Object to) {
|
||||
@@ -280,6 +286,8 @@ public class CommonCodeStyleSettings {
|
||||
}
|
||||
|
||||
//----------------- GENERAL --------------------
|
||||
public int RIGHT_MARGIN = -1;
|
||||
public final static int DEFAULT_RIGHT_MARGIN = 120;
|
||||
|
||||
public boolean LINE_COMMENT_AT_FIRST_COLUMN = true;
|
||||
public boolean BLOCK_COMMENT_AT_FIRST_COLUMN = true;
|
||||
|
||||
@@ -201,7 +201,7 @@ public abstract class CodeStyleAbstractPanel implements Disposable {
|
||||
|
||||
private int getAdjustedRightMargin() {
|
||||
int result = getRightMargin();
|
||||
return result > 0 ? result : CodeStyleFacade.getInstance(ProjectUtil.guessCurrentProject(getPanel())).getRightMargin();
|
||||
return result > 0 ? result : CodeStyleFacade.getInstance(ProjectUtil.guessCurrentProject(getPanel())).getRightMargin(getDefaultLanguage());
|
||||
}
|
||||
|
||||
protected abstract int getRightMargin();
|
||||
@@ -226,7 +226,7 @@ public abstract class CodeStyleAbstractPanel implements Disposable {
|
||||
catch (ConfigurationException ignore) {
|
||||
}
|
||||
CodeStyleSettings clone = mySettings.clone();
|
||||
clone.RIGHT_MARGIN = getAdjustedRightMargin();
|
||||
clone.setRightMargin(getDefaultLanguage(), getAdjustedRightMargin());
|
||||
CodeStyleSettingsManager.getInstance(project).setTemporarySettings(clone);
|
||||
PsiFile formatted;
|
||||
try {
|
||||
@@ -267,7 +267,7 @@ public abstract class CodeStyleAbstractPanel implements Disposable {
|
||||
Document document = documentManager.getDocument(psiFile);
|
||||
if (document != null) {
|
||||
CodeStyleSettings clone = mySettings.clone();
|
||||
clone.RIGHT_MARGIN = getAdjustedRightMargin();
|
||||
clone.setRightMargin(getDefaultLanguage(), getAdjustedRightMargin());
|
||||
CodeStyleSettingsManager.getInstance(project).setTemporarySettings(clone);
|
||||
try {
|
||||
CodeStyleManager.getInstance(project).reformat(psiFile);
|
||||
|
||||
+142
@@ -340,6 +340,16 @@ public abstract class OptionTableWithPreviewPanel extends MultilanguageCodeStyle
|
||||
addOption(fieldName, title, null, options, values);
|
||||
}
|
||||
|
||||
protected void addOption(@NotNull String fieldName,
|
||||
@NotNull String title,
|
||||
@Nullable String groupName,
|
||||
int minValue,
|
||||
int maxValue,
|
||||
int defaultValue,
|
||||
String defaultValueText) {
|
||||
myOptions.add(new IntOption(null, fieldName, title, groupName, null, null, minValue, maxValue, defaultValue, defaultValueText));
|
||||
}
|
||||
|
||||
protected void addOption(@NotNull String fieldName, @NotNull String title, @Nullable String groupName) {
|
||||
myOptions.add(new BooleanOption(null, fieldName, title, groupName, null, null));
|
||||
}
|
||||
@@ -469,6 +479,78 @@ public abstract class OptionTableWithPreviewPanel extends MultilanguageCodeStyle
|
||||
}
|
||||
}
|
||||
|
||||
private class IntOption extends Option {
|
||||
|
||||
private final int myMinValue;
|
||||
private final int myMaxValue;
|
||||
private final int myDefaultValue;
|
||||
@Nullable private String myDefaultValueText;
|
||||
|
||||
public IntOption(Class<? extends CustomCodeStyleSettings> clazz,
|
||||
@NotNull String fieldName,
|
||||
@NotNull String title,
|
||||
@Nullable String groupName,
|
||||
@Nullable OptionAnchor anchor,
|
||||
@Nullable String anchorFiledName,
|
||||
int minValue,
|
||||
int maxValue,
|
||||
int defaultValue,
|
||||
@Nullable String defaultValueText) {
|
||||
super(clazz, fieldName, title, groupName, anchor, anchorFiledName);
|
||||
myMinValue = minValue;
|
||||
myMaxValue = maxValue;
|
||||
myDefaultValue = defaultValue;
|
||||
myDefaultValueText = defaultValueText;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue(CodeStyleSettings settings) {
|
||||
try {
|
||||
int value = field.getInt(getSettings(settings));
|
||||
return value == myDefaultValue && myDefaultValueText != null ? myDefaultValueText : value;
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Object value, CodeStyleSettings settings) {
|
||||
//noinspection EmptyCatchBlock
|
||||
try {
|
||||
if (myDefaultValueText != null && !myDefaultValueText.equals(value)) {
|
||||
field.setInt(getSettings(settings), ((Integer)value).intValue());
|
||||
}
|
||||
else {
|
||||
field.setInt(getSettings(settings), -1);
|
||||
}
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public int getMinValue() {
|
||||
return myMinValue;
|
||||
}
|
||||
|
||||
public int getMaxValue() {
|
||||
return myMaxValue;
|
||||
}
|
||||
|
||||
public int getDefaultValue() {
|
||||
return myDefaultValue;
|
||||
}
|
||||
|
||||
public boolean isDefaultText(Object value) {
|
||||
return myDefaultValueText != null && myDefaultValueText.equals(value);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getDefaultValueText() {
|
||||
return myDefaultValueText;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({"HardCodedStringLiteral"})
|
||||
public final ColumnInfo TITLE = new ColumnInfo("TITLE") {
|
||||
@Override
|
||||
@@ -604,6 +686,7 @@ public abstract class OptionTableWithPreviewPanel extends MultilanguageCodeStyle
|
||||
private final JLabel myComboBox = new JLabel();
|
||||
private final JCheckBox myCheckBox = new JCheckBox();
|
||||
private final JPanel myEmptyLabel = new JPanel();
|
||||
private final JLabel myIntLabel = new JLabel();
|
||||
|
||||
@Override
|
||||
public Component getTableCellRendererComponent(JTable table,
|
||||
@@ -636,6 +719,10 @@ public abstract class OptionTableWithPreviewPanel extends MultilanguageCodeStyle
|
||||
myComboBox.setEnabled(isEnabled);
|
||||
return myComboBox;
|
||||
}
|
||||
else if (value instanceof Integer) {
|
||||
myIntLabel.setText(value.toString());
|
||||
return myIntLabel;
|
||||
}
|
||||
|
||||
myCheckBox.putClientProperty("JComponent.sizeVariant", "small");
|
||||
myComboBox.putClientProperty("JComponent.sizeVariant", "small");
|
||||
@@ -645,12 +732,55 @@ public abstract class OptionTableWithPreviewPanel extends MultilanguageCodeStyle
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyIntOptionEditor extends JTextField {
|
||||
private int myMinValue;
|
||||
private int myMaxValue;
|
||||
private int myDefaultValue;
|
||||
private String myDefaultValueText;
|
||||
|
||||
private MyIntOptionEditor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Object getPresentableValue() {
|
||||
int value = validateAndGetIntOption();
|
||||
return value == myDefaultValue && myDefaultValueText != null ? myDefaultValueText : value;
|
||||
}
|
||||
|
||||
private int validateAndGetIntOption() {
|
||||
try {
|
||||
int value = Integer.parseInt(getText());
|
||||
return value >= myMinValue && value <= myMaxValue ? value : myDefaultValue;
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
return myDefaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public void setMinValue(int minValue) {
|
||||
myMinValue = minValue;
|
||||
}
|
||||
|
||||
public void setMaxValue(int maxValue) {
|
||||
myMaxValue = maxValue;
|
||||
}
|
||||
|
||||
public void setDefaultValue(int defaultValue) {
|
||||
myDefaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public void setDefaultValueText(String defaultValueText) {
|
||||
myDefaultValueText = defaultValueText;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
private class MyValueEditor extends AbstractTableCellEditor {
|
||||
private final JCheckBox myBooleanEditor = new JCheckBox();
|
||||
private JBComboBoxTableCellEditorComponent myOptionsEditor = new JBComboBoxTableCellEditorComponent();
|
||||
private MyIntOptionEditor myIntOptionsEditor = new MyIntOptionEditor();
|
||||
private Component myCurrentEditor = null;
|
||||
private MyTreeNode myCurrentNode = null;
|
||||
|
||||
@@ -684,6 +814,9 @@ public abstract class OptionTableWithPreviewPanel extends MultilanguageCodeStyle
|
||||
else if (myCurrentEditor == myBooleanEditor) {
|
||||
return myBooleanEditor.isSelected() ? Boolean.TRUE : Boolean.FALSE;
|
||||
}
|
||||
else if (myCurrentEditor == myIntOptionsEditor) {
|
||||
return myIntOptionsEditor.getPresentableValue();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -702,6 +835,15 @@ public abstract class OptionTableWithPreviewPanel extends MultilanguageCodeStyle
|
||||
myBooleanEditor.setSelected(node.getValue() == Boolean.TRUE);
|
||||
myBooleanEditor.setEnabled(node.isEnabled());
|
||||
}
|
||||
else if (node.getKey() instanceof IntOption) {
|
||||
IntOption intOption = (IntOption)node.getKey();
|
||||
myCurrentEditor = myIntOptionsEditor;
|
||||
myIntOptionsEditor.setText(intOption.isDefaultText(node.getValue()) ? "" : node.getValue().toString());
|
||||
myIntOptionsEditor.setMinValue(intOption.getMinValue());
|
||||
myIntOptionsEditor.setMaxValue(intOption.getMaxValue());
|
||||
myIntOptionsEditor.setDefaultValue(intOption.getDefaultValue());
|
||||
myIntOptionsEditor.setDefaultValueText(intOption.getDefaultValueText());
|
||||
}
|
||||
else {
|
||||
myCurrentEditor = myOptionsEditor;
|
||||
myOptionsEditor.setCell(table, row, column);
|
||||
|
||||
+2
@@ -32,6 +32,8 @@ public class WrappingAndBracesPanel extends OptionTableWithPreviewPanel {
|
||||
|
||||
@Override
|
||||
protected void initTables() {
|
||||
addOption("RIGHT_MARGIN", ApplicationBundle.message("editbox.right.margin.columns"), null, 0, 999, -1, ApplicationBundle.message("settings.code.style.default.general"));
|
||||
|
||||
addOption("KEEP_LINE_BREAKS", ApplicationBundle.message("wrapping.keep.line.breaks"), WRAPPING_KEEP);
|
||||
addOption("KEEP_FIRST_COLUMN_COMMENT", ApplicationBundle.message("wrapping.keep.comment.at.first.column"), WRAPPING_KEEP);
|
||||
addOption("KEEP_CONTROL_STATEMENT_IN_ONE_LINE", ApplicationBundle.message("checkbox.keep.when.reformatting.control.statement.in.one.line"), WRAPPING_KEEP);
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ public class ParagraphFillHandler {
|
||||
document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(),
|
||||
replacementText);
|
||||
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(
|
||||
CodeStyleSettingsManager.getSettings(element.getProject()));
|
||||
CodeStyleSettingsManager.getSettings(element.getProject()), element.getLanguage());
|
||||
codeFormatter.doWrapLongLinesIfNecessary(editor, element.getProject(), document,
|
||||
textRange.getStartOffset(),
|
||||
textRange.getStartOffset() + replacementText.length() + 1);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.intellij.formatting;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
@@ -25,6 +26,8 @@ import com.intellij.openapi.editor.impl.BulkChangesMerger;
|
||||
import com.intellij.openapi.editor.impl.TextChangeImpl;
|
||||
import com.intellij.openapi.fileTypes.StdFileTypes;
|
||||
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.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
@@ -145,6 +148,7 @@ class FormatProcessor {
|
||||
private WhiteSpace myLastWhiteSpace;
|
||||
private boolean myDisposed;
|
||||
private CommonCodeStyleSettings.IndentOptions myJavaIndentOptions;
|
||||
private final int myRightMargin;
|
||||
|
||||
@NotNull
|
||||
private State myCurrentState;
|
||||
@@ -172,6 +176,23 @@ class FormatProcessor {
|
||||
mySettings = settings;
|
||||
myDocument = docModel.getDocument();
|
||||
myCurrentState = new WrapBlocksState(rootBlock, docModel, affectedRanges, interestingOffset);
|
||||
myRightMargin = getRightMargin(rootBlock);
|
||||
}
|
||||
|
||||
private int getRightMargin(Block rootBlock) {
|
||||
if (rootBlock instanceof ASTBlock) {
|
||||
ASTNode node = ((ASTBlock)rootBlock).getNode();
|
||||
if (node != null) {
|
||||
PsiElement psiElement = node.getPsi();
|
||||
if (psiElement.isValid()) {
|
||||
PsiFile psiFile = psiElement.getContainingFile();
|
||||
if (psiFile != null) {
|
||||
return mySettings.getRightMargin(psiFile.getViewProvider().getBaseLanguage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return mySettings.RIGHT_MARGIN;
|
||||
}
|
||||
|
||||
private LeafBlockWrapper getLastBlock() {
|
||||
@@ -813,7 +834,7 @@ class FormatProcessor {
|
||||
*/
|
||||
private boolean lineOver() {
|
||||
return !myCurrentBlock.containsLineFeeds() &&
|
||||
CoreFormatterUtil.getStartColumn(myCurrentBlock) + myCurrentBlock.getLength() > mySettings.RIGHT_MARGIN;
|
||||
CoreFormatterUtil.getStartColumn(myCurrentBlock) + myCurrentBlock.getLength() > myRightMargin;
|
||||
}
|
||||
|
||||
private void defineAlignOffset(final LeafBlockWrapper block) {
|
||||
|
||||
+1
-1
@@ -636,7 +636,7 @@ public class PostprocessReformattingAspect implements PomModelAspect {
|
||||
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myPsiManager.getProject());
|
||||
final Document document = viewProvider.getDocument();
|
||||
assert document != null;
|
||||
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(styleSettings);
|
||||
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(styleSettings, viewProvider.getBaseLanguage());
|
||||
|
||||
documentManager.commitDocument(document);
|
||||
return codeFormatter;
|
||||
|
||||
+9
-7
@@ -78,10 +78,12 @@ public class CodeFormatterFacade {
|
||||
|
||||
private final CodeStyleSettings mySettings;
|
||||
private final FormatterTagHandler myTagHandler;
|
||||
private final int myRightMargin;
|
||||
|
||||
public CodeFormatterFacade(CodeStyleSettings settings) {
|
||||
public CodeFormatterFacade(CodeStyleSettings settings, @Nullable Language language) {
|
||||
mySettings = settings;
|
||||
myTagHandler = new FormatterTagHandler(settings);
|
||||
myRightMargin = mySettings.getRightMargin(language);
|
||||
}
|
||||
|
||||
public ASTNode processElement(ASTNode element) {
|
||||
@@ -637,8 +639,8 @@ public class CodeFormatterFacade {
|
||||
}
|
||||
|
||||
private int wrapPositionForTextWithoutTabs(int startLineOffset, int endLineOffset, int targetRangeEndOffset) {
|
||||
if (Math.min(endLineOffset, targetRangeEndOffset) - startLineOffset > mySettings.RIGHT_MARGIN) {
|
||||
return startLineOffset + mySettings.RIGHT_MARGIN - FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS;
|
||||
if (Math.min(endLineOffset, targetRangeEndOffset) - startLineOffset > myRightMargin) {
|
||||
return startLineOffset + myRightMargin - FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -659,13 +661,13 @@ public class CodeFormatterFacade {
|
||||
case '\t': symbolWidth = tabSize - (width % tabSize); break;
|
||||
default: symbolWidth = 1;
|
||||
}
|
||||
if (width + symbolWidth + FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS >= mySettings.RIGHT_MARGIN
|
||||
if (width + symbolWidth + FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS >= myRightMargin
|
||||
&& (Math.min(endLineOffset, targetRangeEndOffset) - i) >= FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS)
|
||||
{
|
||||
// Remember preferred position.
|
||||
result = i - 1;
|
||||
}
|
||||
if (width + symbolWidth >= mySettings.RIGHT_MARGIN) {
|
||||
if (width + symbolWidth >= myRightMargin) {
|
||||
wrapLine = true;
|
||||
break;
|
||||
}
|
||||
@@ -700,12 +702,12 @@ public class CodeFormatterFacade {
|
||||
break;
|
||||
default: newX = x + EditorUtil.charWidth(c, Font.PLAIN, editor); symbolWidth = 1;
|
||||
}
|
||||
if (width + symbolWidth + FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS >= mySettings.RIGHT_MARGIN
|
||||
if (width + symbolWidth + FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS >= myRightMargin
|
||||
&& (Math.min(endLineOffset, targetRangeEndOffset) - i) >= FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS)
|
||||
{
|
||||
result = i - 1;
|
||||
}
|
||||
if (width + symbolWidth >= mySettings.RIGHT_MARGIN) {
|
||||
if (width + symbolWidth >= myRightMargin) {
|
||||
wrapLine = true;
|
||||
break;
|
||||
}
|
||||
|
||||
+3
-2
@@ -20,6 +20,7 @@
|
||||
package com.intellij.psi.impl.source.codeStyle;
|
||||
|
||||
import com.intellij.codeStyle.CodeStyleFacade;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.options.Configurable;
|
||||
@@ -68,8 +69,8 @@ public class CodeStyleFacadeImpl extends CodeStyleFacade {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRightMargin() {
|
||||
return CodeStyleSettingsManager.getSettings(myProject).RIGHT_MARGIN;
|
||||
public int getRightMargin(Language language) {
|
||||
return CodeStyleSettingsManager.getSettings(myProject).getRightMargin(language);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+3
-3
@@ -101,7 +101,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
|
||||
}
|
||||
|
||||
ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(element);
|
||||
final PsiElement formatted = SourceTreeToPsiMap.treeElementToPsi(new CodeFormatterFacade(getSettings()).processElement(treeElement));
|
||||
final PsiElement formatted = SourceTreeToPsiMap.treeElementToPsi(new CodeFormatterFacade(getSettings(), element.getLanguage()).processElement(treeElement));
|
||||
if (!canChangeWhiteSpacesOnly) {
|
||||
return postProcessElement(formatted);
|
||||
}
|
||||
@@ -180,7 +180,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
|
||||
ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(file);
|
||||
transformAllChildren(treeElement);
|
||||
|
||||
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings());
|
||||
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(), file.getLanguage());
|
||||
LOG.assertTrue(file.isValid());
|
||||
|
||||
if (editor == null) {
|
||||
@@ -274,7 +274,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
|
||||
}
|
||||
|
||||
ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(element);
|
||||
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings());
|
||||
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(), element.getLanguage());
|
||||
final PsiElement formatted = SourceTreeToPsiMap.treeElementToPsi(codeFormatter.processRange(treeElement, startOffset, endOffset));
|
||||
|
||||
return canChangeWhiteSpacesOnly ? formatted : postProcessElement(formatted);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
package com.intellij.codeStyle;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
@@ -53,7 +54,7 @@ public abstract class CodeStyleFacade {
|
||||
|
||||
public abstract boolean isSmartTabs(final FileType fileType);
|
||||
|
||||
public abstract int getRightMargin();
|
||||
public abstract int getRightMargin(Language language);
|
||||
|
||||
public abstract boolean isWrapWhenTypingReachesRightMargin();
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
package com.intellij.codeStyle;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.options.Configurable;
|
||||
@@ -39,7 +40,7 @@ public class DefaultCodeStyleFacade extends CodeStyleFacade {
|
||||
return "\n";
|
||||
}
|
||||
|
||||
public int getRightMargin() {
|
||||
public int getRightMargin(Language language) {
|
||||
return 80;
|
||||
}
|
||||
|
||||
|
||||
@@ -322,7 +322,7 @@ public final class EditorImpl extends UserDataHolderBase implements EditorEx, Hi
|
||||
myScheme = createBoundColorSchemeDelegate(null);
|
||||
initTabPainter();
|
||||
myIsViewer = viewer;
|
||||
mySettings = new SettingsImpl(this);
|
||||
mySettings = new SettingsImpl(this, project);
|
||||
|
||||
mySelectionModel = new SelectionModelImpl(this);
|
||||
myMarkupModel = new EditorMarkupModelImpl(this);
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
package com.intellij.openapi.editor.impl;
|
||||
|
||||
import com.intellij.codeStyle.CodeStyleFacade;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.EditorSettings;
|
||||
import com.intellij.openapi.editor.ex.EditorEx;
|
||||
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
|
||||
@@ -32,10 +34,14 @@ import com.intellij.openapi.editor.impl.softwrap.SoftWrapAppliancePlaces;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SettingsImpl implements EditorSettings {
|
||||
@Nullable private final EditorEx myEditor;
|
||||
@Nullable private final Language myLanguage;
|
||||
private Boolean myIsCamelWords;
|
||||
|
||||
// This group of settings does not have UI
|
||||
@@ -78,9 +84,14 @@ public class SettingsImpl implements EditorSettings {
|
||||
private Boolean myRenamePreselect = null;
|
||||
private Boolean myWrapWhenTypingReachesRightMargin = null;
|
||||
private Boolean myShowIntentionBulb = null;
|
||||
|
||||
public SettingsImpl() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
public SettingsImpl(@Nullable EditorEx editor) {
|
||||
public SettingsImpl(@Nullable EditorEx editor, @Nullable Project project) {
|
||||
myEditor = editor;
|
||||
myLanguage = editor != null && project != null ? getDocumentLanguage(project, editor.getDocument()) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -144,7 +155,17 @@ public class SettingsImpl implements EditorSettings {
|
||||
@Override
|
||||
public int getRightMargin(Project project) {
|
||||
return myRightMargin != null ? myRightMargin.intValue() :
|
||||
CodeStyleFacade.getInstance(project).getRightMargin();
|
||||
CodeStyleFacade.getInstance(project).getRightMargin(myLanguage);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Language getDocumentLanguage(@Nullable Project project, @NotNull Document document) {
|
||||
if (project != null) {
|
||||
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
|
||||
PsiFile file = documentManager.getPsiFile(document);
|
||||
if (file != null) return file.getLanguage();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -136,7 +136,7 @@ public class TextComponentEditor extends UserDataHolderBase implements Editor {
|
||||
@NotNull
|
||||
public EditorSettings getSettings() {
|
||||
if (mySettings == null) {
|
||||
mySettings = new SettingsImpl(null);
|
||||
mySettings = new SettingsImpl();
|
||||
}
|
||||
return mySettings;
|
||||
}
|
||||
|
||||
@@ -641,4 +641,6 @@ checkbox.reformat.on.typing.rbrace=Reformat block on typing '}'
|
||||
|
||||
group.richcopy=Rich-text copy
|
||||
combobox.richcopy.color.scheme=Color scheme
|
||||
combobox.richcopy.color.scheme.active=Active scheme
|
||||
combobox.richcopy.color.scheme.active=Active scheme
|
||||
|
||||
settings.code.style.default.general=Default (General)
|
||||
@@ -145,7 +145,7 @@ public class Pep8ExternalAnnotator extends ExternalAnnotator<Pep8ExternalAnnotat
|
||||
}
|
||||
final PyPep8Inspection inspection = (PyPep8Inspection)profile.getUnwrappedTool(PyPep8Inspection.KEY.toString(), file);
|
||||
final List<String> ignoredErrors = inspection.ignoredErrors;
|
||||
final int margin = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings().RIGHT_MARGIN;
|
||||
final int margin = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings().getRightMargin(file.getLanguage());
|
||||
return new State(homePath, file.getText(), profile.getErrorLevel(key, file), ignoredErrors, margin);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user