1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 14:15:23 +02:00

219923: [Indenter] Provide preferences option to disable auto-indent.

This commit is contained in:
Anton Leherbauer 2008-03-03 13:53:57 +00:00
parent 7484954d7f
commit cf7493d440
7 changed files with 95 additions and 29 deletions

View file

@ -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());

View file

@ -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());
}
/**

View file

@ -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;

View file

@ -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 <a>code style preference page</a>. The current indentation mode uses tabs.

View file

@ -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);

View file

@ -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

View file

@ -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.
* <p>
* Value is of type <code>Boolean</code>.
* </p>
*/
public final static String EDITOR_AUTO_INDENT= "autoIndent"; //$NON-NLS-1$
/**
* The id of the best match hover contributed for extension point
* <code>org.eclipse.cdt.ui.textHovers</code>.
@ -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);