String getSource(ASTNode node) { try { return compilationUnit.getSource(); } catch (JavaModelException e) { throw new UnhandledException(node, e); } }
public SourceFormatOperation(ICompilationUnit icu) throws JavaModelException { this(icu.getJavaProject(), new Document(icu.getSource()), icu.getSourceRange()); }
public ASTRewrite getASTRewrite() { if (fRewrite == null) { fRewrite= ASTRewrite.create(getRoot().getAST()); if (fRememberContent != null) { // wain until ast rewrite is accessed first try { fRememberContent.set(fCu.getSource()); } catch (JavaModelException e) { fRememberContent= null; } } } return fRewrite; }
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 ASTRewrite getASTRewrite() { if (fRewrite == null) { fRewrite= ASTRewrite.create(getRoot().getAST()); if (fRememberContent != null) { // wain until ast rewrite is accessed first try { fRememberContent.set(fCu.getSource()); } catch (JavaModelException e) { fRememberContent= null; } } } return fRewrite; }
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(); } }
public ASTRewrite getASTRewrite() { if (fRewrite == null) { fRewrite= ASTRewrite.create(getRoot().getAST()); if (fRememberContent != null) { // wain until ast rewrite is accessed first try { fRememberContent.set(fCu.getSource()); } catch (JavaModelException e) { fRememberContent= null; } } } return fRewrite; }
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()); }
/** * Returns the new line number of the from the source of the specified member's compilation unit * @param member the member to query * @param offset the offset * @return the new line number * @throws JavaModelException */ private int getNewLineNumber(IMember member, int offset) throws JavaModelException { int lineNumber = getLineNumber(); Document document = new Document(member.getCompilationUnit().getSource()); try { lineNumber = document.getLineOfOffset(offset); } catch (BadLocationException e) { } return lineNumber; }
private IDocument getDocument(ICompilationUnit cu) throws JavaModelException { IFile file= (IFile) cu.getResource(); IDocument document= JavaUI.getDocumentProvider().getDocument(new FileEditorInput(file)); if (document == null) { return new Document(cu.getSource()); // only used by test cases } return document; }
private IDocument getDocument(ICompilationUnit cu) throws JavaModelException { IFile file= (IFile) cu.getResource(); IDocument document= JavaUI.getDocumentProvider().getDocument(new FileEditorInput(file)); if (document == null) { return new Document(cu.getSource()); // only used by test cases } return document; }
public JavaConverter.ConversionResult toXtend(final ICompilationUnit cu) { try { final ASTParserFactory.ASTParserWrapper parser = this.astParserFactory.createJavaParser(cu); final ASTNode root = parser.createAST(); return this.executeAstFlattener(cu.getSource(), root, parser.getTargetLevel(), false); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
private IDocument getDocument(ICompilationUnit cu) throws JavaModelException { IFile file= (IFile) cu.getResource(); IDocument document= JavaUI.getDocumentProvider().getDocument(new FileEditorInput(file)); if (document == null) { return new Document(cu.getSource()); // only used by test cases } return document; }
protected static void formatProject(IProgressMonitor monitor, IWorkingCopyManager workingCopyManager, IJavaProject p) throws CoreException { for (IPackageFragment pck : p.getPackageFragments()) { for (ICompilationUnit u : pck.getCompilationUnits()) { // the cu write operation also formats the unit. just overwrite with itself. CompilationUnitWriteOperation w = new CompilationUnitWriteOperation(u, u.getSource()); w.validate(); w.run(monitor, workingCopyManager); } } }
@Override public void updateNode() throws JavaModelException { int start= fNode.getStartPosition(); int length= fNode.getLength(); String msg= "Cannot update found node: nodeType=" + fNode.getNodeType() + "; " //$NON-NLS-1$//$NON-NLS-2$ + fNode.toString() + "[" + start + ", " + length + "] in " + fCuRewrite.getCu(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ JavaPlugin.log(new Exception(msg + ":\n" + fCuRewrite.getCu().getSource().substring(start, start + length))); //$NON-NLS-1$ fResult.addError(msg, JavaStatusContext.create(fCuRewrite.getCu(), fNode)); } @Override
@Override public void updateNode() throws JavaModelException { int start= fNode.getStartPosition(); int length= fNode.getLength(); String msg= "Cannot update found node: nodeType=" + fNode.getNodeType() + "; " //$NON-NLS-1$//$NON-NLS-2$ + fNode.toString() + "[" + start + ", " + length + "] in " + fCuRewrite.getCu(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ JavaPlugin.log(new Exception(msg + ":\n" + fCuRewrite.getCu().getSource().substring(start, start + length))); //$NON-NLS-1$ fResult.addError(msg, JavaStatusContext.create(fCuRewrite.getCu(), fNode)); } @Override
private ICompilationUnit createCompilationUnit(ICompilationUnit unit) { try { unit.getResource().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor()); if (unit.getResource().exists()) { IJavaElement parent = unit.getParent(); if (parent instanceof IPackageFragment) { IPackageFragment pkg = (IPackageFragment) parent; unit = pkg.createCompilationUnit(unit.getElementName(), unit.getSource(), true, new NullProgressMonitor()); } } } catch (CoreException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } return unit; }
public void updateNode() throws JavaModelException { int start= fNode.getStartPosition(); int length= fNode.getLength(); String msg= "Cannot update found node: nodeType=" + fNode.getNodeType() + "; " //$NON-NLS-1$//$NON-NLS-2$ + fNode.toString() + "[" + start + ", " + length + "] in " + fCuRewrite.getCu(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ JavaPlugin.log(new Exception(msg + ":\n" + fCuRewrite.getCu().getSource().substring(start, start + length))); //$NON-NLS-1$ fResult.addError(msg, JavaStatusContext.create(fCuRewrite.getCu(), fNode)); } protected ListRewrite getParamgumentsRewrite() {
private static IDocument aquireDocument(ICompilationUnit cu, IProgressMonitor monitor) throws CoreException { if (JavaModelUtil.isPrimary(cu)) { IFile file= (IFile) cu.getResource(); if (file.exists()) { ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager(); IPath path= cu.getPath(); bufferManager.connect(path, LocationKind.IFILE, monitor); return bufferManager.getTextFileBuffer(path, LocationKind.IFILE).getDocument(); } } monitor.done(); return new Document(cu.getSource()); }
@Override public boolean visit(MultiTextEdit edit) { try { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength())); Document doc = new Document(compilationUnit.getSource()); edit.apply(doc, TextEdit.UPDATE_REGIONS); String content = doc.get(edit.getOffset(), edit.getLength()); te.setNewText(content); converted.add(te); return false; } catch (JavaModelException | MalformedTreeException | BadLocationException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return false; }