List of usage examples for org.eclipse.jdt.core.formatter CodeFormatter K_COMPILATION_UNIT
int K_COMPILATION_UNIT
To view the source code for org.eclipse.jdt.core.formatter CodeFormatter K_COMPILATION_UNIT.
Click Source Link
Note: since 3.14, if the formatted compilation unit is a module description (i.e.
From source file:PTCodeFormatterTask.java
License:Open Source License
public void execute() throws BuildException { if (filesets.size() == 0) { throw new BuildException("Specify at least one source - a file or " + "a fileset."); }//from w w w . j a va 2 s . c om for (int i = 0; i < filesets.size(); i++) { FileSet fs = filesets.elementAt(i); DirectoryScanner ds = fs.getDirectoryScanner(getProject()); File fromDir = fs.getDir(getProject()); String[] srcFiles = ds.getIncludedFiles(); if (srcFiles.length == 0) { throw new BuildException("Cannot find any files in the fileset."); } for (int j = 0; j < srcFiles.length; j++) { try { File file = new File(fromDir, srcFiles[j]); FileInputStream fis = new FileInputStream(file); int count = (int) file.length(); byte data[] = new byte[count]; String formatted_result = ""; String result = ""; try { fis.read(data); fis.close(); result = new String(data); TextEdit te = cf.format(CodeFormatter.K_COMPILATION_UNIT, result, 0, result.length(), 0, null); if (te == null) { // couldn't format input formatted_result = result; } else { Document d = new Document(); d.set(result); try { te.apply(d, TextEdit.UPDATE_REGIONS); } catch (Exception e) { BuildException be = new BuildException(); be.fillInStackTrace(); throw be; } formatted_result = d.get(); } System.out.println("Formatting: " + srcFiles[j]); if (!result.equals(formatted_result)) { FileOutputStream os = new FileOutputStream(new File(fromDir, srcFiles[j])); OutputStreamWriter writer = new OutputStreamWriter(os); writer.write(formatted_result); writer.close(); } else { System.out.println(srcFiles[j] + " unchanged"); } } catch (IOException e) { System.err.println("Internal Error " + e); } } catch (FileNotFoundException e) { System.err.println("Internal Error " + e); } } } }
From source file:at.bestsolution.efxclipse.tooling.converter.ConvertFXMLHandler.java
License:Open Source License
@Override protected String format(String contents, CodeFormatter codeFormatter) { if (codeFormatter instanceof CodeFormatter) { IDocument doc = new Document(contents); // FIXME always returns null TextEdit edit = ((CodeFormatter) codeFormatter).format(CodeFormatter.K_COMPILATION_UNIT, doc.get(), 0, doc.get().length(), 0, null); if (edit != null) { try { edit.apply(doc);/*from w w w . j a v a 2 s. c o m*/ contents = doc.get(); } catch (Exception e) { // TODO e.printStackTrace(); } } } return contents; }
From source file:com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.java
License:Open Source License
private static String format(IProject project, String contents, IPath to) { String name = to.lastSegment(); if (name.endsWith(DOT_XML)) { XmlFormatStyle formatStyle = EclipseXmlPrettyPrinter.getForFile(to); EclipseXmlFormatPreferences prefs = EclipseXmlFormatPreferences.create(); return EclipseXmlPrettyPrinter.prettyPrint(contents, prefs, formatStyle, null); } else if (name.endsWith(DOT_JAVA)) { Map<?, ?> options = null; if (project != null && project.isAccessible()) { try { IJavaProject javaProject = BaseProjectHelper.getJavaProject(project); if (javaProject != null) { options = javaProject.getOptions(true); }//from w ww. ja v a 2s . c om } catch (CoreException e) { AdtPlugin.log(e, null); } } if (options == null) { options = JavaCore.getOptions(); } CodeFormatter formatter = ToolFactory.createCodeFormatter(options); try { IDocument doc = new org.eclipse.jface.text.Document(); // format the file (the meat and potatoes) doc.set(contents); TextEdit edit = formatter.format( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0, contents.length(), 0, null); if (edit != null) { edit.apply(doc); } return doc.get(); } catch (Exception e) { AdtPlugin.log(e, null); } } return contents; }
From source file:com.arcbees.gwtp.plugin.core.util.CodeFormattingUtil.java
License:Apache License
public String formatCodeJavaClass(IDocument document) throws JavaModelException { TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, document.get(), 0, document.get().length(), 0, null); try {//from w w w . ja v a 2 s.co m edit.apply(document); } catch (Exception e) { // TODO e.printStackTrace(); } return document.get(); }
From source file:com.arcbees.gwtp.plugin.core.util.CodeFormattingUtil.java
License:Apache License
public String formatCodeJavaClass(String contents) throws JavaModelException { IDocument document = new Document(contents); TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, document.get(), 0, document.get().length(), 0, null); try {/*from ww w. ja va2s . co m*/ edit.apply(document); } catch (Exception e) { // TODO e.printStackTrace(); } return document.get(); }
From source file:com.arcbees.gwtp.plugin.core.util.CodeFormattingUtil.java
License:Apache License
public void formatCodeJavaClassAndSaveIt(ICompilationUnit unit, boolean forceWriting) throws JavaModelException { IDocument document = new Document(unit.getSource()); TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, document.get(), 0, document.get().length(), 0, null); try {//from w w w .j a va 2s. co m edit.apply(document); } catch (Exception e) { // TODO e.printStackTrace(); } String newSource = document.get(); // update of the compilation unit and save it IBuffer buffer = unit.getBuffer(); buffer.setContents(newSource); buffer.save(progressMonitor, forceWriting); }
From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java
License:Open Source License
private void preserveExistingLineBreaks() { // normally n empty lines = n+1 line breaks, but not at the file start and end Token first = this.tm.get(0); int startingBreaks = first.getLineBreaksBefore(); first.clearLineBreaksBefore();//from ww w .j a v a 2 s . com first.putLineBreaksBefore(startingBreaks - 1); this.tm.traverse(0, new TokenTraverser() { DefaultCodeFormatterOptions options2 = WrapPreparator.this.options; @Override protected boolean token(Token token, int index) { boolean isBetweenImports = index > WrapPreparator.this.importsStart && index < WrapPreparator.this.importsEnd; int lineBreaks = getLineBreaksToPreserve(getPrevious(), token, isBetweenImports); if (lineBreaks <= getLineBreaksBefore()) return true; if (lineBreaks == 1) { if ((!this.options2.join_wrapped_lines && token.isWrappable()) || index == 0) token.breakBefore(); } else if (lineBreaks > 1) { token.putLineBreaksBefore(lineBreaks); } return true; } }); Token last = this.tm.get(this.tm.size() - 1); last.clearLineBreaksAfter(); int endingBreaks = getLineBreaksToPreserve(last, null, false); if (endingBreaks > 0) { last.putLineBreaksAfter(endingBreaks); } else if ((this.kind & CodeFormatter.K_COMPILATION_UNIT) != 0 && this.options.insert_new_line_at_end_of_file_if_missing) { last.breakAfter(); } }
From source file:com.diffplug.gradle.spotless.java.eclipse.EclipseFormatterStepImpl.java
License:Apache License
public String format(String raw) throws Exception { TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, raw, 0, raw.length(), 0, UNIX); if (edit == null) { throw new IllegalArgumentException("Invalid java syntax for formatting."); } else {// w w w . j a v a2 s . co m IDocument doc = new Document(raw); edit.apply(doc); return doc.get(); } }
From source file:com.diffplug.gradle.spotless.java.EclipseFormatterStep.java
License:Apache License
public String format(String raw) throws Exception { TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, raw, 0, raw.length(), 0, LineEnding.UNIX.string);//from www . j ava 2s . c om if (edit == null) { throw new IllegalArgumentException("Invalid java syntax for formatting."); } else { IDocument doc = new Document(raw); edit.apply(doc); return doc.get(); } }
From source file:com.diffplug.spotless.extra.eclipse.java.EclipseJdtFormatterStepImpl.java
License:Apache License
public String format(String raw) throws Exception { TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, raw, 0, raw.length(), 0, SpotlessEclipseFramework.LINE_DELIMITER); if (edit == null) { throw new IllegalArgumentException("Invalid java syntax for formatting."); } else {//w w w. j a v a 2s . co m IDocument doc = new Document(raw); edit.apply(doc); return doc.get(); } }