diff options
author | Anton Leherbauer | 2010-03-18 11:07:50 +0000 |
---|---|---|
committer | Anton Leherbauer | 2010-03-18 11:07:50 +0000 |
commit | a1bcbb5cfaf363705bec6cc135372fa47857e1e2 (patch) | |
tree | 1842923d695c42e9483f0b9c7150732a44ccbb9b | |
parent | 0cf744b4f4175e569c4b34fe25c7dd233369eab8 (diff) | |
download | org.eclipse.cdt-a1bcbb5cfaf363705bec6cc135372fa47857e1e2.tar.gz org.eclipse.cdt-a1bcbb5cfaf363705bec6cc135372fa47857e1e2.tar.xz org.eclipse.cdt-a1bcbb5cfaf363705bec6cc135372fa47857e1e2.zip |
Bug 280596 - Wrong indentation with spaces following a tab
3 files changed, 146 insertions, 5 deletions
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 7ec192e9040..999aefc0bfa 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2010 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 @@ -275,7 +275,10 @@ public class IndentAction extends TextEditorAction { // then just shift to the right if (fIsTabAction && caret == end && whiteSpaceLength(currentIndent) >= whiteSpaceLength(indent)) { int indentWidth= whiteSpaceLength(currentIndent) + getIndentSize(); - String replacement= IndentUtil.changePrefix(currentIndent.trim(), indentWidth, getTabSize(), useSpaces()); + if (useTabsAndSpaces()) { + currentIndent = trimSpacesRight(currentIndent); + } + String replacement= IndentUtil.changePrefix(currentIndent, indentWidth, getTabSize(), useSpaces()); document.replace(offset, length, replacement); fCaretOffset= offset + replacement.length(); return true; @@ -296,6 +299,20 @@ public class IndentAction extends TextEditorAction { } /** + * Strip trailing space characters. + * + * @param indent + * @return string with trailing spaces removed + */ + private String trimSpacesRight(String indent) { + int i = indent.length() - 1; + while (i >= 0 && indent.charAt(i) == ' ') { + --i; + } + return indent.substring(0, i+1); + } + + /** * Computes and returns the indentation for a block comment line. * * @param document the document @@ -346,6 +363,16 @@ public class IndentAction extends TextEditorAction { } /** + * Returns whether mixed tabs/spaces should be used for indentation, depending on the editor and + * formatter preferences. + * + * @return <code>true</code> if tabs and spaces should be used + */ + private boolean useTabsAndSpaces() { + return DefaultCodeFormatterConstants.MIXED.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR)); + } + + /** * Returns the tab size used by the editor, which is deduced from the * formatter preferences. * diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java index b19f0957a36..e93b299b8cd 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2009 IBM Corporation and others. + * Copyright (c) 2005, 2010 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 @@ -75,7 +75,6 @@ import org.eclipse.jface.text.ITypedRegion; import org.eclipse.jface.text.IWidgetTokenKeeper; import org.eclipse.jface.text.Position; import org.eclipse.jface.text.Region; -import org.eclipse.jface.text.TabsToSpacesConverter; import org.eclipse.jface.text.TextUtilities; import org.eclipse.jface.text.contentassist.ContentAssistant; import org.eclipse.jface.text.contentassist.IContentAssistant; @@ -212,6 +211,7 @@ import org.eclipse.cdt.internal.ui.text.DocumentCharacterIterator; import org.eclipse.cdt.internal.ui.text.ICReconcilingListener; import org.eclipse.cdt.internal.ui.text.PreferencesAdapter; import org.eclipse.cdt.internal.ui.text.Symbols; +import org.eclipse.cdt.internal.ui.text.TabsToSpacesConverter; import org.eclipse.cdt.internal.ui.text.c.hover.SourceViewerInformationControl; import org.eclipse.cdt.internal.ui.text.contentassist.ContentAssistPreference; import org.eclipse.cdt.internal.ui.util.CUIHelp; @@ -1619,8 +1619,8 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC int tabWidth= getSourceViewerConfiguration().getTabWidth(asv); if (textWidget.getTabs() != tabWidth) textWidget.setTabs(tabWidth); + uninstallTabsToSpacesConverter(); if (isTabsToSpacesConversionEnabled()) { - uninstallTabsToSpacesConverter(); installTabsToSpacesConverter(); } else { updateIndentationMode(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/TabsToSpacesConverter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/TabsToSpacesConverter.java new file mode 100644 index 00000000000..b516056eda2 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/TabsToSpacesConverter.java @@ -0,0 +1,114 @@ +/******************************************************************************* + * Copyright (c) 2010 Wind River Systems, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Wind River Systems - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.text; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.DocumentCommand; +import org.eclipse.jface.text.IAutoEditStrategy; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ILineTracker; +import org.eclipse.jface.text.IRegion; + +/** + * Auto edit strategy that converts tabs into spaces. + * <p> + * This class is derived from the platform version adding a fix for bug 306333. + * Can be removed when the bug is fixed. + * </p> + * @see org.eclipse.jface.text.TabsToSpacesConverter + */ +public class TabsToSpacesConverter implements IAutoEditStrategy { + + private int fTabRatio; + private ILineTracker fLineTracker; + + + public void setNumberOfSpacesPerTab(int ratio) { + fTabRatio= ratio; + } + + public void setLineTracker(ILineTracker lineTracker) { + fLineTracker= lineTracker; + } + + private int insertTabString(StringBuffer buffer, int offsetInLine) { + + if (fTabRatio == 0) + return 0; + + int remainder= offsetInLine % fTabRatio; + remainder= fTabRatio - remainder; + for (int i= 0; i < remainder; i++) + buffer.append(' '); + return remainder; + } + + public void customizeDocumentCommand(IDocument document, DocumentCommand command) { + String text= command.text; + if (text == null) + return; + + int index= text.indexOf('\t'); + if (index > -1) { + + StringBuffer buffer= new StringBuffer(); + + fLineTracker.set(command.text); + int lines= fLineTracker.getNumberOfLines(); + + try { + + for (int i= 0; i < lines; i++) { + + int offset= fLineTracker.getLineOffset(i); + int endOffset= offset + fLineTracker.getLineLength(i); + String line= text.substring(offset, endOffset); + + int position= 0; + if (i == 0) { + IRegion firstLine= document.getLineInformationOfOffset(command.offset); + position= computeVisualLength(document.get(firstLine.getOffset(), command.offset - firstLine.getOffset())); + } + + int length= line.length(); + for (int j= 0; j < length; j++) { + char c= line.charAt(j); + if (c == '\t') { + position += insertTabString(buffer, position); + } else { + buffer.append(c); + ++ position; + } + } + + } + + command.text= buffer.toString(); + + } catch (BadLocationException x) { + } + } + } + + private int computeVisualLength(String text) { + int length = text.length(); + int offset = 0; + for (int i=0; i < length; ++i) { + if (text.charAt(i) == '\t') { + if (fTabRatio != 0) + offset += fTabRatio - offset % fTabRatio; + } else { + ++offset; + } + } + return offset; + } +} |