public String apply(String contents) { final TextEdit edit = codeFormatter.format( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0, contents.length(), 0, Constants.LINE_SEPARATOR); if (edit == null) { // TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost return contents; } IDocument document = new Document(contents); try { edit.apply(document); } catch (Exception e) { throw new RuntimeException( "Failed to format the generated source code.", e); } return document.get(); } }
/** * Creates edits that describe how to format the given string. Returns <code>null</code> if the code could not be formatted for the given kind. * @throws IllegalArgumentException If the offset and length are not inside the string, a * IllegalArgumentException is thrown. */ public static TextEdit format2(int kind, String string, int offset, int length, int indentationLevel, String lineSeparator, Map options) { if (offset < 0 || length < 0 || offset + length > string.length()) { throw new IllegalArgumentException("offset or length outside of string. offset: " + offset + ", length: " + length + ", string size: " + string.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ } return ToolFactory.createCodeFormatter(options).format(kind, string, offset, length, indentationLevel, lineSeparator); }
public static TextEdit reformat(int kind, String string, int offset, int length, int indentationLevel, String lineSeparator, Map options) { if (offset < 0 || length < 0 || offset + length > string.length()) { throw new IllegalArgumentException("offset or length outside of string. offset: " + offset + ", length: " + length + ", string size: " + string.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ } return ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_EXISTING).format(kind, string, offset, length, indentationLevel, lineSeparator); }
public TextEdit formatString(int kind, String string, int offset, int length, int indentationLevel) { return ToolFactory.createCodeFormatter(this.options).format(kind, string, offset, length, indentationLevel, this.lineDelimiter); }
public TextEdit formatString(int kind, String string, int offset, int length, int indentationLevel) { return ToolFactory.createCodeFormatter(this.options).format(kind, string, offset, length, indentationLevel, this.lineDelimiter); }
public TextEdit formatString(int kind, String string, int offset, int length, int indentationLevel) { return ToolFactory.createCodeFormatter(this.options).format(kind, string, offset, length, indentationLevel, this.lineDelimiter); }
public TextEdit formatString(int kind, String string, int offset, int length, int indentationLevel) { return ToolFactory.createCodeFormatter(this.options).format(kind, string, offset, length, indentationLevel, this.lineDelimiter); }
public TextEdit formatString(int kind, String string, int offset, int length, int indentationLevel) { return ToolFactory.createCodeFormatter(this.options).format(kind, string, offset, length, indentationLevel, this.lineDelimiter); }
public TextEdit formatString(int kind, String string, int offset, int length, int indentationLevel) { return ToolFactory.createCodeFormatter(this.options).format(kind, string, offset, length, indentationLevel, this.lineDelimiter); }
/** * Creates edits that describe how to format the given string. Returns * <code>null</code> if the code could not be formatted for the given * kind. * * @throws IllegalArgumentException if the offset and length are not * inside the string */ public static TextEdit format2(int kind, String string, int indentationLevel, String lineSeparator, Map options) { int length= string.length(); if (0 < 0 || length < 0 || 0 + length > string.length()) { throw new IllegalArgumentException("offset or length outside of string. offset: " + 0 + ", length: " + length + ", string size: " + string.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ } return ToolFactory.createCodeFormatter(options).format(kind, string, 0, length, indentationLevel, lineSeparator); }
public String doFormat(String code) { TextEdit te = formatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, lineEnding); if (te == null) throw new IllegalArgumentException( "Code cannot be formatted. Possible cause " + "is unmatched source/target/compliance version."); IDocument doc = new Document(code); try { te.apply(doc); } catch (MalformedTreeException | BadLocationException e) { throw new IllegalStateException("Code cannot be formatted. original code:\n" + code); } return doc.get(); }
public static void main(String[] args) { String code = "public class TestFormatter{public static void main(String[] args){System.out.println(\"Hello World\");}}"; CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null); TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null); IDocument doc = new Document(code); try { textEdit.apply(doc); System.out.println(doc.get()); } catch (MalformedTreeException e) { e.printStackTrace(); } catch (BadLocationException e) { e.printStackTrace(); } }
String code = "public class geo{public static void main(String[] args){System.out.println(\"geo\");}}"; CodeFormatter cf = new DefaultCodeFormatter(); TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null); IDocument dc = new Document(code); try { te.apply(dc); System.out.println(dc.get()); } catch (MalformedTreeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); }
private static void format(final IDocument document) { final String sourceCode = document.get(); final int length = document.getLength(); final TextEdit edit = formatter.format(CodeFormatter.K_STATEMENTS, sourceCode, 0, length, 0, LINE_SEPARATOR); if (edit != null) { applyTextFormattings(document, edit); } }
public static void formatUnitSourceCode(ICompilationUnit unit, IProgressMonitor monitor) throws JavaModelException { CodeFormatter formatter = ToolFactory.createCodeFormatter(null); ISourceRange range = unit.getSourceRange(); TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, unit.getSource(), range.getOffset(), range.getLength(), 0, null); if (formatEdit != null && formatEdit.hasChildren()) { unit.applyTextEdit(formatEdit, monitor); } else { monitor.done(); } }
import org.eclipse.jdt.core.ToolFactory; import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jdt.core.ISourceRange; import org.eclipse.text.edits.TextEdit; import org.eclipse.jdt.core.ICompilationUnit; ... ICompilationUnit cu = ... ... CodeFormatter formatter = ToolFactory.createCodeFormatter(null); ISourceRange range = cu.getSourceRange(); TextEdit indent_edit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, cu.getSource(), range.getOffset(), range.getLength(), 0, null); cu.applyTextEdit(indent_edit, null); cu.reconcile();
public static String format(String code, Map<String, String> options) { DefaultCodeFormatterOptions cfOptions = DefaultCodeFormatterOptions.getJavaConventionsSettings(); cfOptions.tab_char = DefaultCodeFormatterOptions.TAB; CodeFormatter cf = new DefaultCodeFormatter(cfOptions, options); TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0, code.length(), 0, null); IDocument dc = new Document(code); try { te.apply(dc); } catch (Exception e) { throw new MinnalGeneratorException("Failed while formatting the code", e); } return dc.get(); } }
protected static String formatCode(String contents, CodeFormatter codeFormatter) { IDocument doc = new Document(contents); TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, doc.get(), 0, doc.get().length(), 0, null); try { edit.apply(doc); contents = doc.get(); } catch (Exception exception) { CodeGenEcorePlugin.INSTANCE.log(exception); } return contents; }
private List<org.eclipse.lsp4j.TextEdit> format(ICompilationUnit cu, IDocument document, IRegion region, FormattingOptions options, boolean includeComments, IProgressMonitor monitor) { if (cu == null || document == null || region == null || monitor.isCanceled()) { return Collections.emptyList(); } CodeFormatter formatter = ToolFactory.createCodeFormatter(getOptions(options, cu)); String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document); String sourceToFormat = document.get(); int kind = getFormattingKind(cu, includeComments); TextEdit format = formatter.format(kind, sourceToFormat, region.getOffset(), region.getLength(), 0, lineDelimiter); if (format == null || format.getChildren().length == 0 || monitor.isCanceled()) { // nothing to return return Collections.<org.eclipse.lsp4j.TextEdit>emptyList(); } MultiTextEdit flatEdit = TextEditUtil.flatten(format); return convertEdits(flatEdit.getChildren(), document); }
protected static String formatCode(String contents, CodeFormatter codeFormatter) { IDocument doc = new Document(contents); TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, doc.get(), 0, doc.get().length(), 0, null); try { edit.apply(doc); contents = doc.get(); } catch (Exception exception) { CodeGenEcorePlugin.INSTANCE.log(exception); } return contents; }