use of org.eclipse.core.filebuffers.ITextFileBufferManager in project eclipse-cs by checkstyle.
the class AbstractASTResolution method run.
/**
* {@inheritDoc}
*/
@Override
public void run(IMarker marker) {
IResource resource = marker.getResource();
if (!(resource instanceof IFile)) {
return;
}
ICompilationUnit compilationUnit = getCompilationUnit(marker);
if (compilationUnit == null) {
return;
}
ITextFileBufferManager bufferManager = null;
IPath path = compilationUnit.getPath();
try {
final IProgressMonitor monitor = new NullProgressMonitor();
// open the file the editor
JavaUI.openInEditor(compilationUnit);
// reimplemented according to this article
// http://www.eclipse.org/articles/Article-JavaCodeManipulation_AST/index.html
bufferManager = FileBuffers.getTextFileBufferManager();
bufferManager.connect(path, null);
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
IDocument document = textFileBuffer.getDocument();
IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
// by a previous quickfix
if (annotation == null) {
return;
}
Position pos = annotationModel.getPosition(annotation);
final IRegion lineInfo = document.getLineInformationOfOffset(pos.getOffset());
final int markerStart = pos.getOffset();
ASTParser astParser = ASTParser.newParser(AST.JLS3);
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
astParser.setSource(compilationUnit);
CompilationUnit ast = (CompilationUnit) astParser.createAST(monitor);
ast.recordModifications();
ast.accept(handleGetCorrectingASTVisitor(lineInfo, markerStart));
// rewrite all recorded changes to the document
TextEdit edit = ast.rewrite(document, compilationUnit.getJavaProject().getOptions(true));
edit.apply(document);
// commit changes to underlying file
if (mAutoCommit) {
textFileBuffer.commit(monitor, false);
}
} catch (CoreException e) {
CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
} catch (MalformedTreeException e) {
CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
} catch (BadLocationException e) {
CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
} finally {
if (bufferManager != null) {
try {
bufferManager.disconnect(path, null);
} catch (CoreException e) {
// $NON-NLS-1$
CheckstyleLog.log(e, "Error processing quickfix");
}
}
}
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project egit by eclipse.
the class IndexDiffCache method registerBufferListener.
private void registerBufferListener() {
bufferListener = new ExternalFileBufferListener();
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
if (bufferManager != null) {
bufferManager.addFileBufferListener(bufferListener);
}
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project evosuite by EvoSuite.
the class ResolutionMarkerThrowsException method run.
@Override
public void run(IMarker marker) {
// TODO Auto-generated method stub
IResource res = marker.getResource();
try {
String markerMessage = (String) marker.getAttribute(IMarker.MESSAGE);
String exception = markerMessage.split(" ")[0];
String[] exceptionPackageArray = exception.split("\\.");
String exceptionPackage = "";
for (int i = 0; i < exceptionPackageArray.length - 1; i++) {
exceptionPackage += exceptionPackageArray[i] + ".";
}
exception = exception.substring(exceptionPackage.length());
System.out.println("Package: " + exceptionPackage + ", Exception: " + exception);
ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res);
CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);
int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
if (position == 1) {
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
position = compunit.getPosition(line, 0);
}
AST ast = compunit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
IJavaElement element = icomp.getElementAt(position);
IJavaElement method = getMethod(element);
// TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
MethodDeclaration md = td.getMethods()[0];
int counter = 1;
while (!md.getName().getFullyQualifiedName().equals(method.getElementName()) && counter < td.getMethods().length) {
md = td.getMethods()[counter];
System.out.println(md.getName().getFullyQualifiedName() + " " + method.getElementName());
counter++;
}
ListRewrite lr = rewriter.getListRewrite(md, MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY);
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(exceptionPackage + exception));
ListRewrite lrClass = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
lrClass.insertAt(id, 0, null);
Statement s = (Statement) rewriter.createStringPlaceholder(exception, ASTNode.EMPTY_STATEMENT);
lr.insertAt(s, 0, null);
System.out.println("MD: " + md.getName() + "\nList: " + lr.getOriginalList());
ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
IPath path = compunit.getJavaElement().getPath();
try {
bm.connect(path, null, null);
ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
IDocument document = textFileBuffer.getDocument();
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
textFileBuffer.commit(null, false);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bm.disconnect(path, null, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// (4)
}
System.out.println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project eclipse.platform.releng by eclipse.
the class PomVersionErrorReporter method createDocument.
/**
* Creates a new {@link IDocument} for the given {@link IFile}. <code>null</code>
* is returned if the {@link IFile} does not exist or the {@link ITextFileBufferManager}
* cannot be acquired or there was an exception trying to create the {@link IDocument}.
*
* @param file
* @return a new {@link IDocument} or <code>null</code>
*/
protected IDocument createDocument(IFile file) {
if (!file.exists()) {
return null;
}
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
if (manager == null) {
return null;
}
try {
manager.connect(file.getFullPath(), LocationKind.NORMALIZE, null);
ITextFileBuffer textBuf = manager.getTextFileBuffer(file.getFullPath(), LocationKind.NORMALIZE);
IDocument document = textBuf.getDocument();
manager.disconnect(file.getFullPath(), LocationKind.NORMALIZE, null);
return document;
} catch (CoreException e) {
RelEngPlugin.log(e);
}
return null;
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project eclipse-integration-commons by spring-projects.
the class DocumentFetcher method evaluateTextEditor.
private void evaluateTextEditor(Map<IFile, IDocument> result, IEditorPart ep) {
IEditorInput input = ep.getEditorInput();
if (input instanceof IFileEditorInput) {
IFile file = ((IFileEditorInput) input).getFile();
if (!result.containsKey(file)) {
// take the first editor found
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
if (textFileBuffer != null) {
// file buffer has precedence
result.put(file, textFileBuffer.getDocument());
} else {
// use document provider
IDocument document = ((ITextEditor) ep).getDocumentProvider().getDocument(input);
if (document != null) {
result.put(file, document);
}
}
}
}
}
Aggregations