diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContext.java index f87fae0d5af..9166cba0246 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContext.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContext.java @@ -126,7 +126,8 @@ public class CContext extends TranslationUnitContext { boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER); ICProject project= getCProject(); - CFormatter formatter= new CFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentationLevel(), useCodeFormatter, project); + int indentationLevel = isReadOnly() ? 0 : getIndentationLevel(); + CFormatter formatter= new CFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), indentationLevel, useCodeFormatter, project); formatter.format(buffer, this); return buffer; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CFormatter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CFormatter.java index 1177cc5bc86..73c68fc3296 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CFormatter.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CFormatter.java @@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.corext.template.c; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -41,6 +42,7 @@ import org.eclipse.text.edits.TextEdit; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.formatter.CodeFormatter; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.ui.text.ICPartitions; @@ -95,6 +97,7 @@ public class CFormatter { internalFormat(document, context); convertLineDelimiters(document); + convertTabs(document); if (isReplacedAreaEmpty(context)) trimStart(document); @@ -134,6 +137,50 @@ public class CFormatter { } } + /** + * Converts tabs into spaces if such conversion is required according + * to the current code style. + * + * @param document the document to process. + * @throws BadLocationException + */ + private void convertTabs(IDocument document) throws BadLocationException { + int lines= document.getNumberOfLines(); + if (lines == 0) + return; + + String option; + if (fProject == null) + option= CCorePlugin.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR); + else + option= fProject.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, true); + + if (!CCorePlugin.SPACE.equals(option)) + return; + + int tabWidth= CodeFormatterUtil.getTabWidth(fProject); + char[] spaces= null; + for (int line= 0; line < lines; line++) { + IRegion region= document.getLineInformation(line); + int offset= region.getOffset(); + int length = region.getLength(); + for (int i= 0; i < length; i++) { + char c= document.getChar(offset + i); + if (c == '\t') { + int numSpaces= tabWidth - i % tabWidth; + if (spaces == null) { + spaces= new char[tabWidth]; + Arrays.fill(spaces, ' '); + } + document.replace(offset + i, 1, String.valueOf(spaces, 0, numSpaces)); + numSpaces--; + i += numSpaces; + length += numSpaces; + } + } + } + } + private void trimStart(IDocument document) throws BadLocationException { int i= 0; while ((i != document.getLength()) && Character.isWhitespace(document.getChar(i))) @@ -175,13 +222,9 @@ public class CFormatter { } private void indent(IDocument document) throws BadLocationException, MalformedTreeException { - // first line - int offset= document.getLineOffset(0); - document.replace(offset, 0, CodeFormatterUtil.createIndentString(fInitialIndentLevel, fProject)); - - // following lines + String indent = CodeFormatterUtil.createIndentString(fInitialIndentLevel, fProject); int lineCount= document.getNumberOfLines(); - IndentUtil.indentLines(document, new LineRange(1, lineCount - 1), fProject, null); + IndentUtil.indentLines(document, new LineRange(0, lineCount), indent); } /** diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java index 96539e48278..d57404f180a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005 IBM Corporation and others. + * Copyright (c) 2005, 2007 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 @@ -17,6 +17,7 @@ import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITypedRegion; import org.eclipse.jface.text.TextUtilities; import org.eclipse.jface.text.source.ILineRange; +import org.eclipse.jface.text.source.LineRange; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; @@ -102,6 +103,22 @@ public final class IndentUtil { return result; } + /** + * Inserts indent string at the beginning of each line in lines. + * @param document the document to be changed. + * @param lines the line range to be indented. + * @param indent the indent string to be inserted. + * @throws BadLocationException if lines is not a valid line + * range on document + */ + public static void indentLines(IDocument document, LineRange lines, String indent) throws BadLocationException { + int numberOfLines= lines.getNumberOfLines(); + for (int line= lines.getStartLine(), last= line + numberOfLines; line < last; line++) { + int offset= document.getLineOffset(line); + document.replace(offset, 0, indent); + } + } + /** * Returns true if line comments at column 0 should be indented inside, false otherwise. * @@ -580,5 +597,4 @@ public final class IndentUtil { computed= new StringBuffer(previousIndent); return computed.toString(); } - }