Clover coverage report - net.sourceforge.astyleclipse Coverage Report
Coverage timestamp: 星期六 九月 23 2006 23:19:47 CST
file stats: LOC: 178   Methods: 5
NCLOC: 106   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AstyleFormatter.java 0% 0% 0% 0%
coverage
 1    /*******************************************************************************
 2    * Copyright (c) 2000, 2004 QNX Software Systems and others.
 3    * All rights reserved. This program and the accompanying materials
 4    * are made available under the terms of the Common Public License v1.0
 5    * which accompanies this distribution, and is available at
 6    * http://www.eclipse.org/legal/cpl-v10.html
 7    *
 8    * Contributors:
 9    * QNX Software Systems - Initial API and implementation
 10    *******************************************************************************/
 11   
 12    package net.sourceforge.astyleclipse;
 13   
 14    import java.io.BufferedReader;
 15    import java.io.StringReader;
 16    import java.util.HashMap;
 17    import java.util.Map;
 18   
 19    import net.sourceforge.astyleclipse.astyle.ASFormatter;
 20    import net.sourceforge.astyleclipse.astyle.ASStreamIterator;
 21    import net.sourceforge.astyleclipse.preferences.PreferenceConstants;
 22    import net.sourceforge.astyleclipse.astyle.ASResource;
 23   
 24    import org.eclipse.cdt.core.formatter.CodeFormatter;
 25    import org.eclipse.cdt.utils.spawner.ProcessFactory;
 26    import org.eclipse.core.runtime.IStatus;
 27    import org.eclipse.core.runtime.Preferences;
 28    import org.eclipse.core.runtime.Status;
 29    import org.eclipse.text.edits.MultiTextEdit;
 30    import org.eclipse.text.edits.ReplaceEdit;
 31    import org.eclipse.text.edits.TextEdit;
 32   
 33    /**
 34    */
 35    public class AstyleFormatter extends CodeFormatter {
 36    Map astyleOptions = new HashMap();
 37    Map fOptions = new HashMap();
 38   
 39    /*
 40    * (non-Javadoc)
 41    *
 42    * @see org.eclipse.cdt.core.org.eclipse.cdt.indent.CodeFormatter#format(int,
 43    * org.eclipse.jface.text.IDocument, int, int, int, java.lang.String)
 44    */
 45  0 public TextEdit format(int kind, String source, int offset, int length,
 46    int indentationLevel, String lineSeparator) {
 47  0 try {
 48  0 AstyleLog.logInfo(source);
 49    // kind,offset,length,indentationLevel,lineSeparator);
 50  0 ASFormatter formatter = new ASFormatter();
 51   
 52    // TODO : check suffix to set C or Java Style
 53    // filename = getSourcefileName();
 54    // if (filename.endsWith(".java")) {
 55    // formatter.setCStyle(false);
 56    // } else {
 57    // formatter.setCStyle(true);
 58    // }
 59    //
 60    // TODO: get all options from preference page, and set
 61  0 setOptions(formatter, astyleOptions);
 62   
 63  0 StringBuffer target = new StringBuffer("");
 64  0 BufferedReader in = null;
 65  0 in = new BufferedReader(new StringReader(source));
 66  0 formatter.init(new ASStreamIterator(in));
 67  0 while (formatter.hasMoreLines()) {
 68  0 String line = formatter.nextLine();
 69    // System.out.println(line);
 70  0 target.append(line);
 71  0 if (formatter.hasMoreLines()) {
 72  0 target.append("\n");
 73    }
 74    }
 75  0 AstyleLog.logInfo("result is " + target.toString());
 76    // TODO: format the who file in iteration 0 ;-)
 77  0 offset = 0;
 78  0 length = source.length();
 79  0 MultiTextEdit textEdit = new MultiTextEdit(offset, length);
 80  0 textEdit
 81    .addChild(new ReplaceEdit(offset, length, target.toString()));
 82    //
 83    // File tempFile = File.createTempFile("indent", null);
 84    // //$NON-NLS-1$
 85    // FileOutputStream ostream = new FileOutputStream(tempFile);
 86    // Writer writer = new OutputStreamWriter(ostream);
 87    // writer.write(source.substring(offset, offset +
 88    // length).toCharArray());
 89    // writer.close(); // close file, otherwise astyle fails to open it
 90    // transform(tempFile.getCanonicalPath());
 91    // FileInputStream istream = new FileInputStream(tempFile);
 92    // Reader reader = new InputStreamReader(istream);
 93    // BufferedReader br = new BufferedReader(reader);
 94    // StringBuffer buffer = new StringBuffer();
 95    // String line;
 96    // while ((line = br.readLine()) != null) {
 97    // buffer.append(line).append(lineSeparator);
 98    // }
 99    // int bLen = buffer.length();
 100    // MultiTextEdit textEdit = new MultiTextEdit(offset, length);
 101    // textEdit.addChild(new ReplaceEdit(offset, length,
 102    // buffer.toString()));
 103    // br.close();
 104  0 return textEdit;
 105    } catch (Throwable e) {
 106  0 Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
 107    IStatus.ERROR, "Error", e); //$NON-NLS-1$
 108  0 Activator.getDefault().getLog().log(status);
 109    }
 110  0 return null;
 111    }
 112    /*
 113    * (non-Javadoc)
 114    *
 115    * @see org.eclipse.cdt.core.org.eclipse.cdt.indent.CodeFormatter#setOptions(java.util.Map)
 116    */
 117  0 public void setOptions(Map options) {
 118  0 fOptions.putAll(options);
 119    }
 120   
 121  0 public void setOptions(ASFormatter formatter, Map options) {
 122  0 Preferences prefs = Activator.getDefault().getPluginPreferences();
 123   
 124  0 String style = prefs.getString(PreferenceConstants.STYLE_CHOICE);
 125  0 AstyleLog.logInfo("style is " + style);
 126  0 formatter.setCStyle(true);
 127  0 if (style.equalsIgnoreCase("ansi")) {
 128  0 formatter.setBracketIndent(false);
 129  0 formatter.setSpaceIndentation(4);
 130  0 formatter.setBracketFormatMode(ASResource.BREAK_MODE);
 131  0 formatter.setClassIndent(false);
 132  0 formatter.setSwitchIndent(false);
 133  0 formatter.setNamespaceIndent(false);
 134    }
 135  0 if (style.equalsIgnoreCase("gnu")) {
 136  0 formatter.setBlockIndent(true);
 137  0 formatter.setSpaceIndentation(2);
 138  0 formatter.setBracketFormatMode(ASResource.BREAK_MODE);
 139  0 formatter.setClassIndent(false);
 140  0 formatter.setSwitchIndent(false);
 141  0 formatter.setNamespaceIndent(false);
 142    }
 143  0 if (style.equalsIgnoreCase("java")) {
 144  0 formatter.setCStyle(false);
 145  0 formatter.setBracketIndent(false);
 146  0 formatter.setSpaceIndentation(4);
 147  0 formatter.setBracketFormatMode(ASResource.ATTACH_MODE);
 148  0 formatter.setSwitchIndent(false);
 149    }
 150  0 if (style.equalsIgnoreCase("kr")) {
 151    // manuallySetCStyle(formatter);
 152  0 formatter.setBracketIndent(false);
 153  0 formatter.setSpaceIndentation(4);
 154  0 formatter.setBracketFormatMode(ASResource.ATTACH_MODE);
 155  0 formatter.setClassIndent(false);
 156  0 formatter.setSwitchIndent(false);
 157  0 formatter.setNamespaceIndent(false);
 158    }
 159  0 if (style.equalsIgnoreCase("linux")) {
 160  0 formatter.setBracketIndent(false);
 161  0 formatter.setSpaceIndentation(8);
 162  0 formatter.setBracketFormatMode(ASResource.BDAC_MODE);
 163  0 formatter.setClassIndent(false);
 164  0 formatter.setSwitchIndent(false);
 165  0 formatter.setNamespaceIndent(false);
 166    }
 167    }
 168   
 169  0 public boolean supportProperty(String propertyID) {
 170    // TODO Auto-generated method stub
 171  0 return false;
 172    }
 173   
 174  0 public String[][] getEnumerationProperty(String propertyID) {
 175    // TODO Auto-generated method stub
 176  0 return null;
 177    }
 178    }