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(); } }
edits.apply(document); FileUtils.writeStringToFile(file, document.get());
TextEdit edits = rewriter.rewriteAST(document,null); edits.apply(document); FileUtils.write(javaSRC, document.get());
private static void applyTextFormattings(final IDocument document, final TextEdit edit) { try { edit.apply(document); } catch (final MalformedTreeException e) { throw new IllegalStateException(e); } catch (final BadLocationException e) { throw new IllegalStateException(e); } } }
// get the ast rewriter final ASTRewrite rewriter = ASTRewrite.create(ast); // get the current document source final Document document = new Document(unit.getSource()); // compute the edits you have made to the compilation unit final TextEdit edits = rewriter.rewriteAST(); // apply the edits to the document edits.apply(document); // get the new source String newSource = document.get(); // now write this source to some other file.
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(); }
protected void applyTextEdit(ICompilationUnit cu, TextEdit edits) throws JavaModelException { try { edits.apply(getDocument(cu)); } catch (BadLocationException e) { // content changed under us throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); } } /**
protected void applyTextEdit(ICompilationUnit cu, TextEdit edits) throws JavaModelException { try { edits.apply(getDocument(cu)); } catch (BadLocationException e) { // content changed under us throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); } } /**
protected void applyTextEdit(ICompilationUnit cu, TextEdit edits) throws JavaModelException { try { edits.apply(getDocument(cu)); } catch (BadLocationException e) { // content changed under us throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); } } /**
protected void applyTextEdit(ICompilationUnit cu, TextEdit edits) throws JavaModelException { try { edits.apply(getDocument(cu)); } catch (BadLocationException e) { // content changed under us throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); } } /**
protected void applyTextEdit(ICompilationUnit cu, TextEdit edits) throws JavaModelException { try { edits.apply(getDocument(cu)); } catch (BadLocationException e) { // content changed under us throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); } } /**
protected void applyTextEdit(ICompilationUnit cu, TextEdit edits) throws JavaModelException { try { edits.apply(getDocument(cu)); } catch (BadLocationException e) { // content changed under us throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); } } /**
public void format() { final IDocument document= (IDocument) fDocuments.getFirst(); TextEdit edit= calculateTextEdit(); if (edit == null) return; try { edit.apply(document); } catch (MalformedTreeException x) { JavaPlugin.log(x); } catch (BadLocationException x) { JavaPlugin.log(x); } }
public static String rewriteSource(CompilationUnit cu, File file) { Document document = new Document(getSourceDoc(file.getPath())); TextEdit edits = cu.rewrite(document, formatterOptions); try { edits.apply(document); } catch (Exception e) { throw new RuntimeException(e); } return document.get(); }
private void saveChanges(ICompilationUnit copy, CompilationUnit unit) throws JavaModelException, MalformedTreeException, BadLocationException { Document document= new Document(copy.getBuffer().getContents()); TextEdit edits = unit.rewrite(document, copy.getJavaProject().getOptions(true)); edits.apply(document); copy.getBuffer().setContents(document.get()); copy.save(null, false); }
public void apply(IDocument document) { TextEdit edits = m_rewriter.rewriteAST(document, null); UndoEdit undo = null; try { undo = edits.apply(document); } catch(MalformedTreeException e) { e.printStackTrace(); } catch(BadLocationException e) { e.printStackTrace(); } }
void create(ICompilationUnit cu, boolean toRestore) throws CoreException { ImportRewrite.create(cu, toRestore); TextEdit edit = fImportsStructure.rewriteImports(new NullProgressMonitor()); IDocument document = new Document(cu.getSource()); try { edit.apply(document); } catch (Exception e) { } String imports = document.get(); cu.getBuffer().setContents(imports); cu.commitWorkingCopy(false, new NullProgressMonitor()); }
private void applyTransformation(IDocument document) throws MalformedTreeException { TextEdit newEdit= new MultiTextEdit(0, document.getLength()); ReplaceEdit[] replaces= fModifier.getModifications(document.get()); for (ReplaceEdit replace : replaces) { newEdit.addChild(replace); } try { newEdit.apply(document, TextEdit.NONE); } catch (BadLocationException cannotHappen) { Assert.isTrue(false); } }
private void applyTransformation(IDocument document) throws MalformedTreeException { TextEdit newEdit= new MultiTextEdit(0, document.getLength()); ReplaceEdit[] replaces= fModifier.getModifications(document.get()); for (int i= 0; i < replaces.length; i++) { newEdit.addChild(replaces[i]); } try { newEdit.apply(document, TextEdit.NONE); } catch (BadLocationException cannotHappen) { Assert.isTrue(false); } }
ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setCompilerOptions(....); Document document = new Document(""); parser.setSource(document.get().toCharArray()); CompilationUnit unit = (CompilationUnit)parser.createAST(null); unit.recordModifications(); TextEdit edits = unit.rewrite(document, null); edits.apply(document); // now write document.get() to file