From cf7493d440b7113c315c4025a4064ad3feaf0e49 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Mon, 3 Mar 2008 13:53:57 +0000 Subject: [PATCH] 219923: [Indenter] Provide preferences option to disable auto-indent. --- .../cdt/ui/tests/text/CAutoIndentTest.java | 34 ++++++++++++ .../cdt/internal/ui/actions/IndentAction.java | 6 ++- .../ui/preferences/PreferencesMessages.java | 2 + .../PreferencesMessages.properties | 2 + .../SmartTypingConfigurationBlock.java | 16 +++++- .../internal/ui/text/CAutoIndentStrategy.java | 54 ++++++++++--------- .../eclipse/cdt/ui/PreferenceConstants.java | 10 ++++ 7 files changed, 95 insertions(+), 29 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java index cf9a40b6af6..e4622569755 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java @@ -24,12 +24,14 @@ import junit.framework.TestSuite; import org.eclipse.core.runtime.ILogListener; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Plugin; +import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.PreferenceConstants; import org.eclipse.cdt.ui.text.ICPartitions; import org.eclipse.cdt.ui.text.doctools.DefaultMultilineCommentAutoEditStrategy; @@ -401,6 +403,38 @@ public class CAutoIndentTest extends AbstractAutoEditTest { assertNoError(); } + public void testAutoIndentDisabled_Bug219923() throws Exception { + AutoEditTester tester = createAutoEditTester(); //$NON-NLS-1$ + IPreferenceStore store= PreferenceConstants.getPreferenceStore(); + try { + store.setValue(PreferenceConstants.EDITOR_AUTO_INDENT, false); + tester.type("void main() {\n"); //$NON-NLS-1$ + assertEquals(1, tester.getCaretLine()); + // Nested statement is not indented + assertEquals(0, tester.getCaretColumn()); + // The brace was closed automatically. + assertEquals("}", tester.getLine(1)); //$NON-NLS-1$ + tester.type('\t'); + tester.type('\n'); + // indent from previous line + assertEquals(1, tester.getCaretColumn()); + tester.type('{'); + tester.type('\n'); + // indent from previous line + assertEquals(1, tester.getCaretColumn()); + tester.type('}'); + tester.type('\n'); + // indent from previous line + assertEquals(1, tester.getCaretColumn()); + tester.backspace(); + tester.type('\n'); + // indent from previous line + assertEquals(0, tester.getCaretColumn()); + } finally { + store.setToDefault(PreferenceConstants.EDITOR_AUTO_INDENT); + } + } + private void assertNoError() { if (!fStatusLog.isEmpty()) { fail(fStatusLog.get(0).toString()); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/IndentAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/IndentAction.java index 547bb9437ac..cebe096b276 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/IndentAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/IndentAction.java @@ -44,6 +44,8 @@ import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.text.ICPartitions; +import org.eclipse.cdt.internal.corext.util.CodeFormatterUtil; + import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.IndentUtil; import org.eclipse.cdt.internal.ui.text.CHeuristicScanner; @@ -244,7 +246,7 @@ public class IndentAction extends TextEditorAction { indent= document.get(offset, wsStart - offset) + computed; } } - } + } // standard C code indentation if (indent == null) { @@ -360,7 +362,7 @@ public class IndentAction extends TextEditorAction { * @return the indent size as defined in the current formatter preferences */ private int getIndentSize() { - return getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, 4); + return CodeFormatterUtil.getIndentWidth(getCProject()); } /** diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java index 06c4939b1aa..8d253edec0d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java @@ -190,6 +190,8 @@ public final class PreferencesMessages extends NLS { public static String CEditorPreferencePage_WorkspaceDefaultLabel; public static String SmartTypingConfigurationBlock_autoclose_title; + public static String SmartTypingConfigurationBlock_autoindent_newlines; + public static String SmartTypingConfigurationBlock_autoindent_title; public static String SmartTypingConfigurationBlock_tabs_title; public static String SmartTypingConfigurationBlock_tabs_message_tab_text; public static String SmartTypingConfigurationBlock_tabs_message_others_text; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties index 2550523bc69..c83819e0feb 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties @@ -170,6 +170,8 @@ FoldingConfigurationBlock_error_not_exist= The selected folding provider does no # Smart typing block SmartTypingConfigurationBlock_autoclose_title=Automatically close +SmartTypingConfigurationBlock_autoindent_newlines=New lines and braces +SmartTypingConfigurationBlock_autoindent_title=Automatically indent SmartTypingConfigurationBlock_tabs_title=Tabulators # The argument will be replaced by the tab display size SmartTypingConfigurationBlock_tabs_message_tab_text=The tab display value (currently {0}) and whether spaces are used to indent lines are configured on the code style preference page. The current indentation mode uses tabs. diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/SmartTypingConfigurationBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/SmartTypingConfigurationBlock.java index 63434e98bf8..2ca63eac5e9 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/SmartTypingConfigurationBlock.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/SmartTypingConfigurationBlock.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 IBM Corporation and others. + * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Sergey Prigogin, Google + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.preferences; @@ -53,6 +54,7 @@ class SmartTypingConfigurationBlock extends AbstractConfigurationBlock { private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() { return new OverlayPreferenceStore.OverlayKey[] { new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SMART_PASTE), + new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_AUTO_INDENT), new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_CLOSE_STRINGS), new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_CLOSE_BRACKETS), @@ -99,12 +101,24 @@ class SmartTypingConfigurationBlock extends AbstractConfigurationBlock { composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_strings_title); addStringsSection(composite); + composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_autoindent_title); + addAutoIndentSection(composite); + scrolled.setContent(control); final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT); scrolled.setMinSize(size.x, size.y); return scrolled; } + private void addAutoIndentSection(Composite composite) { + GridLayout layout= new GridLayout(); + composite.setLayout(layout); + + String label; + label= PreferencesMessages.SmartTypingConfigurationBlock_autoindent_newlines; + addCheckBox(composite, label, PreferenceConstants.EDITOR_AUTO_INDENT, 0); + } + private void addStringsSection(Composite composite) { GridLayout layout= new GridLayout(); composite.setLayout(layout); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java index 208825fb6d4..9d2e543687c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java @@ -245,6 +245,10 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { } private void smartIndentAfterNewLine(IDocument d, DocumentCommand c) { + int docLength = d.getLength(); + if (c.offset == -1 || docLength == 0) + return; + int addIndent= 0; CHeuristicScanner scanner= new CHeuristicScanner(d); try { @@ -253,33 +257,35 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { scanner = new CHeuristicScanner(d, fPartitioning, ICPartitions.C_PREPROCESSOR); addIndent= 1; } - } catch (BadLocationException exc) { - } - int docLength = d.getLength(); - if (c.offset == -1 || docLength == 0) - return; - int p = (c.offset == docLength ? c.offset - 1 : c.offset); - CIndenter indenter = new CIndenter(d, scanner, fProject); - StringBuffer indent = indenter.computeIndentation(c.offset); - if (indent == null) - indent = new StringBuffer(); - if (addIndent > 0 && indent.length() == 0) { - indent= indenter.createReusingIndent(indent, addIndent); - } - try { - int line = d.getLineOfOffset(p); + int line = d.getLineOfOffset(c.offset); + IRegion reg = d.getLineInformation(line); + int start = reg.getOffset(); + int lineEnd = start + reg.getLength(); + + StringBuffer indent= null; + CIndenter indenter= new CIndenter(d, scanner, fProject); + if (getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_AUTO_INDENT)) { + indent= indenter.computeIndentation(c.offset); + } else { + // reuse existing indent + int wsEnd= findEndOfWhiteSpace(d, start, c.offset); + if (wsEnd > start) { + indent= new StringBuffer(d.get(start, wsEnd - start)); + addIndent= 0; + } + } + if (indent == null) { + indent= new StringBuffer(); + } + if (addIndent > 0 && indent.length() == 0) { + indent= indenter.createReusingIndent(indent, addIndent); + } StringBuffer buf = new StringBuffer(c.text + indent); - - IRegion reg = d.getLineInformation(line); - int lineEnd = reg.getOffset() + reg.getLength(); - int contentStart = findEndOfWhiteSpace(d, c.offset, lineEnd); c.length = Math.max(contentStart - c.offset, 0); - int start = reg.getOffset(); - // insert closing brace on new line after an unclosed opening brace if (getBracketCount(d, start, c.offset, true) > 0 && fCloseBrace && !isClosedBrace(d, c.offset, c.length)) { c.caretOffset = c.offset + buf.length(); @@ -1121,14 +1127,10 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { return; } - /* - * I removed the workaround for 48339 as I believe the recent changes to - * FastCPartitioner are enough to fix this. - */ boolean isNewLine= c.length == 0 && c.text != null && isLineDelimiter(d, c.text); if (isNewLine) { smartIndentAfterNewLine(d, c); - } else if (c.text.length() == 1) { + } else if (c.text.length() == 1 && getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_AUTO_INDENT)) { smartIndentOnKeypress(d, c); } else if (c.text.length() > 1 && getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_PASTE)) { smartPaste(d, c); // no smart backspace for paste diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java index 65fd11d86c2..a70b63744f4 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java @@ -710,6 +710,15 @@ public class PreferenceConstants { */ public static final String EDITOR_SMART_TAB= "smart_tab"; //$NON-NLS-1$ + /** + * A named preference that controls whether the 'auto indent' feature is + * enabled. + *

+ * Value is of type Boolean. + *

+ */ + public final static String EDITOR_AUTO_INDENT= "autoIndent"; //$NON-NLS-1$ + /** * The id of the best match hover contributed for extension point * org.eclipse.cdt.ui.textHovers. @@ -1397,6 +1406,7 @@ public class PreferenceConstants { store.setDefault(PreferenceConstants.EDITOR_SMART_TAB, true); store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS, true); store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS, false); + store.setDefault(PreferenceConstants.EDITOR_AUTO_INDENT, true); store.setDefault(PreferenceConstants.ENSURE_NEWLINE_AT_EOF, false);