mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-08 18:26:01 +02:00
Fix for 185712: Invalid indenting of code template
Patch by Sergey Prigogin <eclipse.sprigogin@gmail.com>
This commit is contained in:
parent
7fb1be766c
commit
f2b9070846
3 changed files with 69 additions and 9 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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 <code>indent</code> string at the beginning of each line in <code>lines</code>.
|
||||
* @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 <code>lines</code> is not a valid line
|
||||
* range on <code>document</code>
|
||||
*/
|
||||
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 <code>true</code> if line comments at column 0 should be indented inside, <code>false</code> otherwise.
|
||||
*
|
||||
|
@ -580,5 +597,4 @@ public final class IndentUtil {
|
|||
computed= new StringBuffer(previousIndent);
|
||||
return computed.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue