diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TemplateFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TemplateFormatterTest.java new file mode 100644 index 00000000000..02edaa2ae0e --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TemplateFormatterTest.java @@ -0,0 +1,145 @@ +/******************************************************************************* + * Copyright (c) 2008 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: + * Anton Leherbauer (Wind River Systems) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text; + +import java.util.HashMap; + +import junit.framework.TestSuite; + +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.templates.DocumentTemplateContext; +import org.eclipse.jface.text.templates.GlobalTemplateVariables; +import org.eclipse.jface.text.templates.Template; +import org.eclipse.jface.text.templates.TemplateBuffer; +import org.eclipse.jface.text.templates.TemplateContext; +import org.eclipse.jface.text.templates.TemplateContextType; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.ui.tests.BaseUITestCase; + +import org.eclipse.cdt.internal.corext.template.c.CFormatter; + +/** + * Tests for the template formatter (CFormatter). + * + * @since 5.0 + */ +public class TemplateFormatterTest extends BaseUITestCase { + + private static class TestTemplateContextType extends TemplateContextType { + TestTemplateContextType() { + super("test"); + addResolver(new GlobalTemplateVariables.Cursor()); + addResolver(new GlobalTemplateVariables.WordSelection()); + addResolver(new GlobalTemplateVariables.LineSelection()); + addResolver(new GlobalTemplateVariables.Dollar()); + addResolver(new GlobalTemplateVariables.Date()); + addResolver(new GlobalTemplateVariables.Year()); + addResolver(new GlobalTemplateVariables.Time()); + addResolver(new GlobalTemplateVariables.User()); + } + } + + public static TestSuite suite() { + return suite(TemplateFormatterTest.class, "_"); + } + + private TemplateContextType fTemplateContextType; + private String fSelection; + private HashMap fDefaultOptions; + + protected void setUp() throws Exception { + super.setUp(); + fTemplateContextType= new TestTemplateContextType(); + fSelection= "while (true) {\n\tdoSomething();\n}"; + fDefaultOptions= CCorePlugin.getDefaultOptions(); + } + + protected void tearDown() throws Exception { + CCorePlugin.setOptions(fDefaultOptions); + super.tearDown(); + } + + private void setOption(String key, String value) { + HashMap options= new HashMap(1); + options.put(key, value); + CCorePlugin.setOptions(options); + } + + protected void assertFormatterResult() throws Exception { + StringBuffer[] contents= getContentsForTest(2); + String before= contents[0].toString().replaceAll("\\r\\n", "\n"); + String expected= contents[1].toString(); + final Document document = new Document(before); + TemplateContext context= new DocumentTemplateContext(fTemplateContextType, document, 0, document.getLength()); + context.setVariable(GlobalTemplateVariables.SELECTION, fSelection); + Template template= new Template("test", "descr", fTemplateContextType.getId(), before, false); + TemplateBuffer buffer= context.evaluate(template); + CFormatter formatter= new CFormatter("\n", 0, false, null); + formatter.format(buffer, context); + assertEquals(expected, buffer.getString()); + } + + + //for(int var=0; var 0 && startLine < endLine) { + if (startLine < endLine) { int shiftWidth= IndentUtil.computeVisualLength(shift, tabWidth); for (int line= startLine + 1; line <= endLine; ++line) { - delta -= IndentUtil.cutIndent(document, line, shiftWidth, tabWidth); + String currentIndent= IndentUtil.getCurrentIndent(document, line, false); + int indentWidth= IndentUtil.computeVisualLength(currentIndent, tabWidth); + int newIndentWidth= Math.max(0, indentWidth - shiftWidth); + // replace current indent with prefix + spaces + String newIndent= IndentUtil.changePrefix(prefix, prefix.length() + newIndentWidth, 1, true); int lineOffset= document.getLineOffset(line); - document.replace(lineOffset, 0, prefix); - delta += prefix.length(); + document.replace(lineOffset, currentIndent.length(), newIndent); + delta -= currentIndent.length(); + delta += newIndent.length(); } } } catch (BadLocationException exc) { @@ -286,7 +279,7 @@ public class CFormatter { private static final String CATEGORY= "__template_variables"; //$NON-NLS-1$ private Document fDocument; private final TemplateBuffer fBuffer; - private List fPositions; + private List fPositions; /** * Creates a new tracker. @@ -361,11 +354,11 @@ public class CFormatter { return fBuffer; } - private List createRangeMarkers(TemplateVariable[] variables, IDocument document) throws MalformedTreeException, BadLocationException { - Map markerToOriginal= new HashMap(); + private List createRangeMarkers(TemplateVariable[] variables, IDocument document) throws MalformedTreeException, BadLocationException { + Map markerToOriginal= new HashMap(); MultiTextEdit root= new MultiTextEdit(0, document.getLength()); - List edits= new ArrayList(); + List edits= new ArrayList(); boolean hasModifications= false; for (int i= 0; i != variables.length; i++) { final TemplateVariable variable= variables[i]; @@ -396,12 +389,12 @@ public class CFormatter { root.apply(document, TextEdit.UPDATE_REGIONS); } - List positions= new ArrayList(); - for (Iterator it= edits.iterator(); it.hasNext();) { - TextEdit edit= (TextEdit) it.next(); + List positions= new ArrayList(); + for (Iterator it= edits.iterator(); it.hasNext();) { + TextEdit edit= it.next(); try { // abuse TypedPosition to piggy back the original contents of the position - final TypedPosition pos= new TypedPosition(edit.getOffset(), edit.getLength(), (String) markerToOriginal.get(edit)); + final TypedPosition pos= new TypedPosition(edit.getOffset(), edit.getLength(), markerToOriginal.get(edit)); document.addPosition(CATEGORY, pos); positions.add(pos); } catch (BadPositionCategoryException x) { @@ -417,11 +410,11 @@ public class CFormatter { return length == 0 || value.trim().length() == 0; } - private void removeRangeMarkers(List positions, IDocument document, TemplateVariable[] variables) throws MalformedTreeException, BadLocationException, BadPositionCategoryException { + private void removeRangeMarkers(List positions, IDocument document, TemplateVariable[] variables) throws MalformedTreeException, BadLocationException, BadPositionCategoryException { // revert previous changes - for (Iterator it= positions.iterator(); it.hasNext();) { - TypedPosition position= (TypedPosition) it.next(); + for (Iterator it= positions.iterator(); it.hasNext();) { + TypedPosition position= it.next(); // remove and re-add in order to not confuse ExclusivePositionUpdater document.removePosition(CATEGORY, position); final String original= position.getType(); @@ -432,13 +425,13 @@ public class CFormatter { document.addPosition(position); } - Iterator it= positions.iterator(); + Iterator it= positions.iterator(); for (int i= 0; i != variables.length; i++) { TemplateVariable variable= variables[i]; int[] offsets= new int[variable.getOffsets().length]; for (int j= 0; j != offsets.length; j++) - offsets[j]= ((Position) it.next()).getOffset(); + offsets[j]= it.next().getOffset(); variable.setOffsets(offsets); }