Search in sources :

Example 31 with ITextFileBufferManager

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");
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) Position(org.eclipse.jface.text.Position) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IRegion(org.eclipse.jface.text.IRegion) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 32 with ITextFileBufferManager

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);
    }
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager)

Example 33 with ITextFileBufferManager

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();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) AST(org.eclipse.jdt.core.dom.AST) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 34 with ITextFileBufferManager

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;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument)

Example 35 with ITextFileBufferManager

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);
                }
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) IFileEditorInput(org.eclipse.ui.IFileEditorInput) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)86 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)65 IDocument (org.eclipse.jface.text.IDocument)53 IPath (org.eclipse.core.runtime.IPath)51 CoreException (org.eclipse.core.runtime.CoreException)36 BadLocationException (org.eclipse.jface.text.BadLocationException)26 IFile (org.eclipse.core.resources.IFile)22 Test (org.junit.Test)17 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 IOException (java.io.IOException)10 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)10 IResource (org.eclipse.core.resources.IResource)8 Path (org.eclipse.core.runtime.Path)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 TextEdit (org.eclipse.text.edits.TextEdit)6 UndoEdit (org.eclipse.text.edits.UndoEdit)6 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)5