mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-28 11:25:35 +02:00
219923: [Indenter] Provide preferences option to disable auto-indent.
This commit is contained in:
parent
7484954d7f
commit
cf7493d440
7 changed files with 95 additions and 29 deletions
|
@ -24,12 +24,14 @@ import junit.framework.TestSuite;
|
||||||
import org.eclipse.core.runtime.ILogListener;
|
import org.eclipse.core.runtime.ILogListener;
|
||||||
import org.eclipse.core.runtime.IStatus;
|
import org.eclipse.core.runtime.IStatus;
|
||||||
import org.eclipse.core.runtime.Plugin;
|
import org.eclipse.core.runtime.Plugin;
|
||||||
|
import org.eclipse.jface.preference.IPreferenceStore;
|
||||||
import org.eclipse.jface.text.BadLocationException;
|
import org.eclipse.jface.text.BadLocationException;
|
||||||
import org.eclipse.jface.text.Document;
|
import org.eclipse.jface.text.Document;
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.CCorePlugin;
|
import org.eclipse.cdt.core.CCorePlugin;
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
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.ICPartitions;
|
||||||
import org.eclipse.cdt.ui.text.doctools.DefaultMultilineCommentAutoEditStrategy;
|
import org.eclipse.cdt.ui.text.doctools.DefaultMultilineCommentAutoEditStrategy;
|
||||||
|
|
||||||
|
@ -401,6 +403,38 @@ public class CAutoIndentTest extends AbstractAutoEditTest {
|
||||||
assertNoError();
|
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() {
|
private void assertNoError() {
|
||||||
if (!fStatusLog.isEmpty()) {
|
if (!fStatusLog.isEmpty()) {
|
||||||
fail(fStatusLog.get(0).toString());
|
fail(fStatusLog.get(0).toString());
|
||||||
|
|
|
@ -44,6 +44,8 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
import org.eclipse.cdt.ui.text.ICPartitions;
|
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.CEditor;
|
||||||
import org.eclipse.cdt.internal.ui.editor.IndentUtil;
|
import org.eclipse.cdt.internal.ui.editor.IndentUtil;
|
||||||
import org.eclipse.cdt.internal.ui.text.CHeuristicScanner;
|
import org.eclipse.cdt.internal.ui.text.CHeuristicScanner;
|
||||||
|
@ -244,7 +246,7 @@ public class IndentAction extends TextEditorAction {
|
||||||
indent= document.get(offset, wsStart - offset) + computed;
|
indent= document.get(offset, wsStart - offset) + computed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// standard C code indentation
|
// standard C code indentation
|
||||||
if (indent == null) {
|
if (indent == null) {
|
||||||
|
@ -360,7 +362,7 @@ public class IndentAction extends TextEditorAction {
|
||||||
* @return the indent size as defined in the current formatter preferences
|
* @return the indent size as defined in the current formatter preferences
|
||||||
*/
|
*/
|
||||||
private int getIndentSize() {
|
private int getIndentSize() {
|
||||||
return getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, 4);
|
return CodeFormatterUtil.getIndentWidth(getCProject());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -190,6 +190,8 @@ public final class PreferencesMessages extends NLS {
|
||||||
public static String CEditorPreferencePage_WorkspaceDefaultLabel;
|
public static String CEditorPreferencePage_WorkspaceDefaultLabel;
|
||||||
|
|
||||||
public static String SmartTypingConfigurationBlock_autoclose_title;
|
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_title;
|
||||||
public static String SmartTypingConfigurationBlock_tabs_message_tab_text;
|
public static String SmartTypingConfigurationBlock_tabs_message_tab_text;
|
||||||
public static String SmartTypingConfigurationBlock_tabs_message_others_text;
|
public static String SmartTypingConfigurationBlock_tabs_message_others_text;
|
||||||
|
|
|
@ -170,6 +170,8 @@ FoldingConfigurationBlock_error_not_exist= The selected folding provider does no
|
||||||
|
|
||||||
# Smart typing block
|
# Smart typing block
|
||||||
SmartTypingConfigurationBlock_autoclose_title=Automatically close
|
SmartTypingConfigurationBlock_autoclose_title=Automatically close
|
||||||
|
SmartTypingConfigurationBlock_autoindent_newlines=New lines and braces
|
||||||
|
SmartTypingConfigurationBlock_autoindent_title=Automatically indent
|
||||||
SmartTypingConfigurationBlock_tabs_title=Tabulators
|
SmartTypingConfigurationBlock_tabs_title=Tabulators
|
||||||
# The argument will be replaced by the tab display size
|
# 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.
|
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.
|
||||||
|
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -8,6 +8,7 @@
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM Corporation - initial API and implementation
|
* IBM Corporation - initial API and implementation
|
||||||
* Sergey Prigogin, Google
|
* Sergey Prigogin, Google
|
||||||
|
* Anton Leherbauer (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.ui.preferences;
|
package org.eclipse.cdt.internal.ui.preferences;
|
||||||
|
@ -53,6 +54,7 @@ class SmartTypingConfigurationBlock extends AbstractConfigurationBlock {
|
||||||
private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
|
private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
|
||||||
return new OverlayPreferenceStore.OverlayKey[] {
|
return new OverlayPreferenceStore.OverlayKey[] {
|
||||||
new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SMART_PASTE),
|
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_STRINGS),
|
||||||
new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_CLOSE_BRACKETS),
|
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);
|
composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_strings_title);
|
||||||
addStringsSection(composite);
|
addStringsSection(composite);
|
||||||
|
|
||||||
|
composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_autoindent_title);
|
||||||
|
addAutoIndentSection(composite);
|
||||||
|
|
||||||
scrolled.setContent(control);
|
scrolled.setContent(control);
|
||||||
final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
|
final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
|
||||||
scrolled.setMinSize(size.x, size.y);
|
scrolled.setMinSize(size.x, size.y);
|
||||||
return scrolled;
|
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) {
|
private void addStringsSection(Composite composite) {
|
||||||
GridLayout layout= new GridLayout();
|
GridLayout layout= new GridLayout();
|
||||||
composite.setLayout(layout);
|
composite.setLayout(layout);
|
||||||
|
|
|
@ -245,6 +245,10 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void smartIndentAfterNewLine(IDocument d, DocumentCommand c) {
|
private void smartIndentAfterNewLine(IDocument d, DocumentCommand c) {
|
||||||
|
int docLength = d.getLength();
|
||||||
|
if (c.offset == -1 || docLength == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
int addIndent= 0;
|
int addIndent= 0;
|
||||||
CHeuristicScanner scanner= new CHeuristicScanner(d);
|
CHeuristicScanner scanner= new CHeuristicScanner(d);
|
||||||
try {
|
try {
|
||||||
|
@ -253,33 +257,35 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
|
||||||
scanner = new CHeuristicScanner(d, fPartitioning, ICPartitions.C_PREPROCESSOR);
|
scanner = new CHeuristicScanner(d, fPartitioning, ICPartitions.C_PREPROCESSOR);
|
||||||
addIndent= 1;
|
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);
|
int line = d.getLineOfOffset(c.offset);
|
||||||
StringBuffer indent = indenter.computeIndentation(c.offset);
|
IRegion reg = d.getLineInformation(line);
|
||||||
if (indent == null)
|
int start = reg.getOffset();
|
||||||
indent = new StringBuffer();
|
int lineEnd = start + reg.getLength();
|
||||||
if (addIndent > 0 && indent.length() == 0) {
|
|
||||||
indent= indenter.createReusingIndent(indent, addIndent);
|
StringBuffer indent= null;
|
||||||
}
|
CIndenter indenter= new CIndenter(d, scanner, fProject);
|
||||||
try {
|
if (getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_AUTO_INDENT)) {
|
||||||
int line = d.getLineOfOffset(p);
|
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);
|
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);
|
int contentStart = findEndOfWhiteSpace(d, c.offset, lineEnd);
|
||||||
c.length = Math.max(contentStart - c.offset, 0);
|
c.length = Math.max(contentStart - c.offset, 0);
|
||||||
|
|
||||||
int start = reg.getOffset();
|
|
||||||
|
|
||||||
// insert closing brace on new line after an unclosed opening brace
|
// 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)) {
|
if (getBracketCount(d, start, c.offset, true) > 0 && fCloseBrace && !isClosedBrace(d, c.offset, c.length)) {
|
||||||
c.caretOffset = c.offset + buf.length();
|
c.caretOffset = c.offset + buf.length();
|
||||||
|
@ -1121,14 +1127,10 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
|
||||||
return;
|
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);
|
boolean isNewLine= c.length == 0 && c.text != null && isLineDelimiter(d, c.text);
|
||||||
if (isNewLine) {
|
if (isNewLine) {
|
||||||
smartIndentAfterNewLine(d, c);
|
smartIndentAfterNewLine(d, c);
|
||||||
} else if (c.text.length() == 1) {
|
} else if (c.text.length() == 1 && getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_AUTO_INDENT)) {
|
||||||
smartIndentOnKeypress(d, c);
|
smartIndentOnKeypress(d, c);
|
||||||
} else if (c.text.length() > 1 && getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_PASTE)) {
|
} else if (c.text.length() > 1 && getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_PASTE)) {
|
||||||
smartPaste(d, c); // no smart backspace for paste
|
smartPaste(d, c); // no smart backspace for paste
|
||||||
|
|
|
@ -710,6 +710,15 @@ public class PreferenceConstants {
|
||||||
*/
|
*/
|
||||||
public static final String EDITOR_SMART_TAB= "smart_tab"; //$NON-NLS-1$
|
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
|
* The id of the best match hover contributed for extension point
|
||||||
* <code>org.eclipse.cdt.ui.textHovers</code>.
|
* <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_SMART_TAB, true);
|
||||||
store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS, true);
|
store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS, true);
|
||||||
store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS, false);
|
store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS, false);
|
||||||
|
store.setDefault(PreferenceConstants.EDITOR_AUTO_INDENT, true);
|
||||||
|
|
||||||
store.setDefault(PreferenceConstants.ENSURE_NEWLINE_AT_EOF, false);
|
store.setDefault(PreferenceConstants.ENSURE_NEWLINE_AT_EOF, false);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue