From 7f015b4ee1e50ef05630bcb15baf60f435aae537 Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Wed, 17 Dec 2014 10:39:05 +0300 Subject: [PATCH] IDEA-87318 Backspace should delete whitespace to chosen indent level, or be configurable --- .../editorActions/BackspaceModeOverride.java | 23 +++++ .../LanguageBackspaceModeOverride.java | 26 ++++++ .../editorActions/SmartBackspaceMode.java | 24 +++++ .../LanguageCodeStyleSettingsProvider.java | 4 - .../editor/EditorSmartKeysConfigurable.form | 87 ++++++++++++------- .../editor/EditorSmartKeysConfigurable.java | 46 +++++++++- .../codeInsight/CodeInsightSettings.java | 8 +- .../AbstractIndentingBackspaceHandler.java | 12 ++- .../SimpleIndentingBackspaceHandler.java | 3 +- .../editorActions/SmartBackspaceDisabler.java | 23 +++++ .../SmartIndentingBackspaceHandler.java | 3 +- .../SimpleIndentingBackspaceHandlerTest.java | 2 +- .../src/messages/ApplicationBundle.properties | 4 + .../src/META-INF/LangExtensionPoints.xml | 3 + python/src/META-INF/python-core.xml | 1 + .../PyLanguageCodeStyleSettingsProvider.java | 5 -- 16 files changed, 212 insertions(+), 62 deletions(-) create mode 100644 platform/lang-api/src/com/intellij/codeInsight/editorActions/BackspaceModeOverride.java create mode 100644 platform/lang-api/src/com/intellij/codeInsight/editorActions/LanguageBackspaceModeOverride.java create mode 100644 platform/lang-api/src/com/intellij/codeInsight/editorActions/SmartBackspaceMode.java create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/editorActions/SmartBackspaceDisabler.java diff --git a/platform/lang-api/src/com/intellij/codeInsight/editorActions/BackspaceModeOverride.java b/platform/lang-api/src/com/intellij/codeInsight/editorActions/BackspaceModeOverride.java new file mode 100644 index 000000000000..a3e676ad8d2d --- /dev/null +++ b/platform/lang-api/src/com/intellij/codeInsight/editorActions/BackspaceModeOverride.java @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2014 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.codeInsight.editorActions; + +public abstract class BackspaceModeOverride { + /** + * @see SmartBackspaceMode + */ + public abstract int getBackspaceMode(int modeFromSettings); +} diff --git a/platform/lang-api/src/com/intellij/codeInsight/editorActions/LanguageBackspaceModeOverride.java b/platform/lang-api/src/com/intellij/codeInsight/editorActions/LanguageBackspaceModeOverride.java new file mode 100644 index 000000000000..604edb0b0687 --- /dev/null +++ b/platform/lang-api/src/com/intellij/codeInsight/editorActions/LanguageBackspaceModeOverride.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2014 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.codeInsight.editorActions; + +import com.intellij.lang.LanguageExtension; + +public class LanguageBackspaceModeOverride extends LanguageExtension { + public static final LanguageBackspaceModeOverride INSTANCE = new LanguageBackspaceModeOverride(); + + private LanguageBackspaceModeOverride() { + super("com.intellij.editor.backspaceModeOverride"); + } +} diff --git a/platform/lang-api/src/com/intellij/codeInsight/editorActions/SmartBackspaceMode.java b/platform/lang-api/src/com/intellij/codeInsight/editorActions/SmartBackspaceMode.java new file mode 100644 index 000000000000..4f15f16318bd --- /dev/null +++ b/platform/lang-api/src/com/intellij/codeInsight/editorActions/SmartBackspaceMode.java @@ -0,0 +1,24 @@ +/* + * Copyright 2000-2014 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.codeInsight.editorActions; + +public class SmartBackspaceMode { + public static final int OFF = 0; + public static final int AUTOINDENT = 1; + public static final int INDENT = 2; + + private SmartBackspaceMode() {} +} 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 3b97016645c2..548a66623415 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java @@ -223,10 +223,6 @@ public abstract class LanguageCodeStyleSettingsProvider { return fieldCollector.getCollectedFields(); } - public boolean isIndentBasedLanguageSemantics() { - return false; - } - private final class SupportedFieldCollector implements CodeStyleSettingsCustomizable { private final Set myCollectedFields = new HashSet(); private SettingsType myCurrSettingsType; diff --git a/platform/lang-impl/src/com/intellij/application/options/editor/EditorSmartKeysConfigurable.form b/platform/lang-impl/src/com/intellij/application/options/editor/EditorSmartKeysConfigurable.form index f9cb5da0f632..96d105a0e2a3 100644 --- a/platform/lang-impl/src/com/intellij/application/options/editor/EditorSmartKeysConfigurable.form +++ b/platform/lang-impl/src/com/intellij/application/options/editor/EditorSmartKeysConfigurable.form @@ -3,7 +3,7 @@ - + @@ -33,7 +33,7 @@ - + @@ -42,7 +42,7 @@ - + @@ -51,7 +51,7 @@ - + @@ -94,7 +94,24 @@ - + + + + + + + + + + + + + + + + + + @@ -103,6 +120,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -135,35 +184,9 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/platform/lang-impl/src/com/intellij/application/options/editor/EditorSmartKeysConfigurable.java b/platform/lang-impl/src/com/intellij/application/options/editor/EditorSmartKeysConfigurable.java index a5551086daaf..92ce93898fea 100644 --- a/platform/lang-impl/src/com/intellij/application/options/editor/EditorSmartKeysConfigurable.java +++ b/platform/lang-impl/src/com/intellij/application/options/editor/EditorSmartKeysConfigurable.java @@ -17,6 +17,7 @@ package com.intellij.application.options.editor; import com.intellij.codeInsight.CodeInsightSettings; +import com.intellij.codeInsight.editorActions.SmartBackspaceMode; import com.intellij.lang.CodeDocumentationAwareCommenter; import com.intellij.lang.Commenter; import com.intellij.lang.Language; @@ -63,7 +64,7 @@ public class EditorSmartKeysConfigurable extends CompositeConfigurable, C public boolean SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO = false; - @MagicConstant(intValues = {OFF, AUTOINDENT, INDENT}) - public int SMART_BACKSPACE = AUTOINDENT; - public static final int OFF = 0; - public static final int AUTOINDENT = 1; - public static final int INDENT = 2; + @MagicConstant(intValues = {SmartBackspaceMode.OFF, SmartBackspaceMode.AUTOINDENT, SmartBackspaceMode.INDENT}) + public int SMART_BACKSPACE = SmartBackspaceMode.AUTOINDENT; public boolean SMART_INDENT_ON_ENTER = true; public boolean INSERT_BRACE_ON_ENTER = true; diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AbstractIndentingBackspaceHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AbstractIndentingBackspaceHandler.java index bbe65098e818..48c11fc0a9c4 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AbstractIndentingBackspaceHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/AbstractIndentingBackspaceHandler.java @@ -20,7 +20,6 @@ import com.intellij.lang.Language; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiFile; -import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider; abstract class AbstractIndentingBackspaceHandler extends BackspaceHandlerDelegate { private final int myMode; @@ -57,12 +56,11 @@ abstract class AbstractIndentingBackspaceHandler extends BackspaceHandlerDelegat protected abstract boolean doCharDeleted(char c, PsiFile file, Editor editor); private static int getBackspaceMode(Language language) { - LanguageCodeStyleSettingsProvider codeStyleSettingsProvider = LanguageCodeStyleSettingsProvider.forLanguage(language); - if (codeStyleSettingsProvider != null && codeStyleSettingsProvider.isIndentBasedLanguageSemantics()) { - return CodeInsightSettings.INDENT; - } - else { - return CodeInsightSettings.getInstance().SMART_BACKSPACE; + int mode = CodeInsightSettings.getInstance().SMART_BACKSPACE; + BackspaceModeOverride override = LanguageBackspaceModeOverride.INSTANCE.forLanguage(language); + if (override != null) { + mode = override.getBackspaceMode(mode); } + return mode; } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandler.java index 17e0e0227bfd..932bb6c2f93b 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandler.java @@ -15,7 +15,6 @@ */ package com.intellij.codeInsight.editorActions; -import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.LogicalPosition; import com.intellij.psi.PsiFile; @@ -24,7 +23,7 @@ public class SimpleIndentingBackspaceHandler extends AbstractIndentingBackspaceH private LogicalPosition myTargetPosition; public SimpleIndentingBackspaceHandler() { - super(CodeInsightSettings.INDENT); + super(SmartBackspaceMode.INDENT); } @Override diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SmartBackspaceDisabler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SmartBackspaceDisabler.java new file mode 100644 index 000000000000..cb1d3fdb18a6 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SmartBackspaceDisabler.java @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2014 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.codeInsight.editorActions; + +public class SmartBackspaceDisabler extends BackspaceModeOverride { + @Override + public int getBackspaceMode(int modeFromSettings) { + return modeFromSettings == SmartBackspaceMode.AUTOINDENT ? SmartBackspaceMode.INDENT : modeFromSettings; + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SmartIndentingBackspaceHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SmartIndentingBackspaceHandler.java index 8ab82c453ba1..94b308c61454 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SmartIndentingBackspaceHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SmartIndentingBackspaceHandler.java @@ -15,7 +15,6 @@ */ package com.intellij.codeInsight.editorActions; -import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.codeStyle.CodeStyleFacade; import com.intellij.formatting.FormatterEx; import com.intellij.formatting.FormattingModel; @@ -42,7 +41,7 @@ public class SmartIndentingBackspaceHandler extends AbstractIndentingBackspaceHa private int myStartOffset; public SmartIndentingBackspaceHandler() { - super(CodeInsightSettings.AUTOINDENT); + super(SmartBackspaceMode.AUTOINDENT); } @Override diff --git a/platform/lang-impl/testSources/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandlerTest.java b/platform/lang-impl/testSources/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandlerTest.java index 514cf7fcdcf1..3dc1145cd308 100644 --- a/platform/lang-impl/testSources/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandlerTest.java +++ b/platform/lang-impl/testSources/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandlerTest.java @@ -33,7 +33,7 @@ public class SimpleIndentingBackspaceHandlerTest extends LightPlatformCodeInsigh private void doTest(String before, String after) { int savedMode = CodeInsightSettings.getInstance().SMART_BACKSPACE; try { - CodeInsightSettings.getInstance().SMART_BACKSPACE = CodeInsightSettings.INDENT; + CodeInsightSettings.getInstance().SMART_BACKSPACE = SmartBackspaceMode.INDENT; configureFromFileText(getTestName(false) + ".txt", before); executeAction(IdeActions.ACTION_EDITOR_BACKSPACE); checkResultByText(after); diff --git a/platform/platform-resources-en/src/messages/ApplicationBundle.properties b/platform/platform-resources-en/src/messages/ApplicationBundle.properties index 953e81956c36..2ebda33e2e4c 100644 --- a/platform/platform-resources-en/src/messages/ApplicationBundle.properties +++ b/platform/platform-resources-en/src/messages/ApplicationBundle.properties @@ -327,6 +327,10 @@ combobox.paste.reformat.none=None combobox.paste.reformat.indent.block=Indent Block combobox.paste.reformat.indent.each.line=Indent Each Line combobox.paste.reformat.reformat.block=Reformat Block +combobox.smart.backspace=Unindent: +combobox.smart.backspace.off=Off +combobox.smart.backspace.simple=Simple +combobox.smart.backspace.smart=Smart combobox.tab.placement.none=None combobox.tab.placement.top=Top combobox.tab.placement.left=Left diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index 12db08cf539e..6b0cd0a5f778 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -74,6 +74,9 @@ interface="com.intellij.codeInsight.editorActions.JoinLinesHandlerDelegate"/> + + + diff --git a/python/src/META-INF/python-core.xml b/python/src/META-INF/python-core.xml index bbe4f8fbe7ba..b00b859243be 100644 --- a/python/src/META-INF/python-core.xml +++ b/python/src/META-INF/python-core.xml @@ -43,6 +43,7 @@ + diff --git a/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java b/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java index 4acfcbd60f74..0c0bc0b59aa9 100644 --- a/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java +++ b/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java @@ -124,11 +124,6 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin return PlatformUtils.isPyCharm() ? DisplayPriority.KEY_LANGUAGE_SETTINGS : DisplayPriority.LANGUAGE_SETTINGS; } - @Override - public boolean isIndentBasedLanguageSemantics() { - return true; - } - @SuppressWarnings("FieldCanBeLocal") private static String SPACING_SETTINGS_PREVIEW = "def settings_preview(argument, key=value):\n" + " dict = {1:'a', 2:'b', 3:'c'}\n" +