Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Leherbauer2008-08-08 11:31:33 +0000
committerAnton Leherbauer2008-08-08 11:31:33 +0000
commita116ec2b177d83594c1909a6d3f4f929dfe9587f (patch)
tree8f750c150f64bd467b3e1c6827d71680674f1a00
parent8b5b59d03576dfcb37861c2fa8a74b115ac01475 (diff)
downloadorg.eclipse.cdt-a116ec2b177d83594c1909a6d3f4f929dfe9587f.tar.gz
org.eclipse.cdt-a116ec2b177d83594c1909a6d3f4f929dfe9587f.tar.xz
org.eclipse.cdt-a116ec2b177d83594c1909a6d3f4f929dfe9587f.zip
Fix for 242707 - Tab is not indenting line in C/C++ editor
Patch by Andrew Gvozdev
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractAutoEditTest.java13
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java77
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java5
3 files changed, 86 insertions, 9 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractAutoEditTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractAutoEditTest.java
index 10cb5cb0430..c01483576de 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractAutoEditTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/AbstractAutoEditTest.java
@@ -9,6 +9,7 @@
* Anton Leherbauer (Wind River Systems) - initial API and implementation
* Sergey Prigogin, Google
* Andrew Ferguson (Symbian)
+ * Andrew Gvozdev
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text;
@@ -42,7 +43,8 @@ public class AbstractAutoEditTest extends BaseTestCase {
*/
protected static class AutoEditTester {
- private Map fStrategyMap = new HashMap();
+ private IAutoEditStrategy tabsToSpacesConverter=null;
+ private Map<String, IAutoEditStrategy> fStrategyMap = new HashMap<String, IAutoEditStrategy>();
IDocument fDoc;
private String fPartitioning;
private int fCaretOffset;
@@ -53,12 +55,16 @@ public class AbstractAutoEditTest extends BaseTestCase {
fPartitioning = partitioning;
}
+ public void setTabsToSpacesConverter(IAutoEditStrategy converter) {
+ tabsToSpacesConverter = converter;
+ }
+
public void setAutoEditStrategy(String contentType, IAutoEditStrategy aes) {
fStrategyMap.put(contentType, aes);
}
public IAutoEditStrategy getAutoEditStrategy(String contentType) {
- return (IAutoEditStrategy)fStrategyMap.get(contentType);
+ return fStrategyMap.get(contentType);
}
/**
@@ -88,6 +94,9 @@ public class AbstractAutoEditTest extends BaseTestCase {
}
private void customizeDocumentCommand(TestDocumentCommand command) throws BadLocationException {
+ if (tabsToSpacesConverter != null) {
+ tabsToSpacesConverter.customizeDocumentCommand(fDoc, command);
+ }
IAutoEditStrategy aes = getAutoEditStrategy(getContentType(command.offset));
if (aes != null) {
aes.customizeDocumentCommand(fDoc, command);
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 e4622569755..dc31dbc1b14 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
@@ -9,6 +9,7 @@
* Anton Leherbauer (Wind River Systems) - initial API and implementation
* Sergey Prigogin, Google
* Andrew Ferguson (Symbian)
+ * Andrew Gvozdev
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text;
@@ -26,10 +27,13 @@ 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.DefaultLineTracker;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.TabsToSpacesConverter;
import org.eclipse.cdt.core.CCorePlugin;
+import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.text.ICPartitions;
@@ -45,8 +49,8 @@ import org.eclipse.cdt.internal.ui.text.CTextTools;
*/
public class CAutoIndentTest extends AbstractAutoEditTest {
- private HashMap fOptions;
- private List fStatusLog;
+ private HashMap<String, String> fOptions;
+ private List<IStatus> fStatusLog;
private ILogListener fLogListener;
@@ -68,7 +72,7 @@ public class CAutoIndentTest extends AbstractAutoEditTest {
// shell.forceFocus();
fOptions= CCorePlugin.getOptions();
- fStatusLog= Collections.synchronizedList(new ArrayList());
+ fStatusLog= Collections.synchronizedList(new ArrayList<IStatus>());
fLogListener= new ILogListener() {
public void logging(IStatus status, String plugin) {
if(!status.isOK()) {
@@ -348,7 +352,7 @@ public class CAutoIndentTest extends AbstractAutoEditTest {
public void testBracketIndentForConstructorDefinition_Bug183814() throws BadLocationException {
DefaultCodeFormatterOptions whitesmiths= DefaultCodeFormatterOptions.getWhitesmithsSettings();
- CCorePlugin.setOptions(new HashMap(whitesmiths.getMap()));
+ CCorePlugin.setOptions(new HashMap<String, String>(whitesmiths.getMap()));
AutoEditTester tester = createAutoEditTester(); //$NON-NLS-1$
tester.type("Foo::Foo()\n{");
@@ -357,7 +361,7 @@ public class CAutoIndentTest extends AbstractAutoEditTest {
public void testSmartPasteWhitesmiths_Bug180531() throws Exception {
DefaultCodeFormatterOptions whitesmiths= DefaultCodeFormatterOptions.getWhitesmithsSettings();
- CCorePlugin.setOptions(new HashMap(whitesmiths.getMap()));
+ CCorePlugin.setOptions(new HashMap<String, String>(whitesmiths.getMap()));
AutoEditTester tester = createAutoEditTester(); //$NON-NLS-1$
tester.type("A::~A()\n{");
@@ -381,7 +385,7 @@ public class CAutoIndentTest extends AbstractAutoEditTest {
DefaultCodeFormatterOptions defaultOptions= DefaultCodeFormatterOptions.getDefaultSettings();
defaultOptions.indent_body_declarations_compare_to_namespace_header= true;
- CCorePlugin.setOptions(new HashMap(defaultOptions.getMap()));
+ CCorePlugin.setOptions(new HashMap<String, String>(defaultOptions.getMap()));
tester = createAutoEditTester();
tester.type("namespace ns {\n");
@@ -435,6 +439,67 @@ public class CAutoIndentTest extends AbstractAutoEditTest {
}
}
+ public void testTabsAsSpaces_SmartIndentDisabled_Bug242707() throws Exception {
+ HashMap<String, String> options = new HashMap<String, String>();
+ options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
+ options.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, "3");
+ options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "3");
+ DefaultCodeFormatterOptions defaultOptions= DefaultCodeFormatterOptions.getDefaultSettings();
+ defaultOptions.set(options);
+ CCorePlugin.setOptions(new HashMap<String, String>(defaultOptions.getMap()));
+
+ IPreferenceStore store= PreferenceConstants.getPreferenceStore();
+ store.setValue(PreferenceConstants.EDITOR_SMART_TAB, false);
+
+ AutoEditTester tester = createAutoEditTester();
+
+ TabsToSpacesConverter tabToSpacesConverter = new TabsToSpacesConverter();
+ tabToSpacesConverter.setNumberOfSpacesPerTab(3);
+ tabToSpacesConverter.setLineTracker(new DefaultLineTracker());
+ tester.setTabsToSpacesConverter(tabToSpacesConverter);
+
+
+ try {
+ tester.type("void main() {\n"); //$NON-NLS-1$
+ assertEquals(1, tester.getCaretLine());
+ // Nested statement is indented
+ assertEquals(3, tester.getCaretColumn());
+ assertEquals(" ", tester.getLine(0)); //$NON-NLS-1$
+ // The brace was closed automatically.
+ assertEquals("}", tester.getLine(1)); //$NON-NLS-1$
+ tester.type('\t');
+ // Indent from previous line + expanded tab
+ assertEquals(" ", tester.getLine(0)); //$NON-NLS-1$
+ // Return normal indentation
+ tester.backspace(3);
+ assertEquals(" ", tester.getLine(0)); //$NON-NLS-1$
+ tester.type("for (;;)\n");
+ // Check indentation under "for" operator
+ assertEquals(" ", tester.getLine(0)); //$NON-NLS-1$
+ // Remove all symbols on the line
+ tester.backspace(6);
+ assertEquals("", tester.getLine(0)); //$NON-NLS-1$
+ // Tabulation should not trigger autoindent, just 1 tab filled with spaces
+ tester.type("\t");
+ assertEquals(" ", tester.getLine(0)); //$NON-NLS-1$
+ tester.type("\t");
+ // Check one more tab
+ assertEquals(" ", tester.getLine(0)); //$NON-NLS-1$
+ // Clean the line to repeat 2 last entries but with spaces
+ tester.backspace(6);
+ assertEquals("", tester.getLine(0)); //$NON-NLS-1$
+ // 1-st sequence of spaces
+ tester.type(" ");
+ assertEquals(" ", tester.getLine(0)); //$NON-NLS-1$
+ // 2-nd sequence of spaces
+ tester.type(" ");
+ assertEquals(" ", tester.getLine(0)); //$NON-NLS-1$
+
+ } finally {
+ store.setToDefault(PreferenceConstants.EDITOR_SMART_TAB);
+ }
+ }
+
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/text/CAutoIndentStrategy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java
index 57172cff4af..c4126fefe5f 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
@@ -11,6 +11,7 @@
* Anton Leherbauer (Wind River Systems)
* Sergey Prigogin (Google)
* Andrew Ferguson (Symbian)
+ * Andrew Gvozdev
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text;
@@ -1130,7 +1131,9 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
smartIndentAfterNewLine(d, c);
} 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)) {
+ } else if (c.text.length() > 1
+ && getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_PASTE)
+ && c.text.trim().length() != 0) {
smartPaste(d, c); // no smart backspace for paste
}
}

Back to the top