Search in sources :

Example 66 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.

the class FileStoreFileBufferFunctions method test17_3.

/*
	 * Test IFileBufferListener#stateChanging for internal changes
	 */
@Test
public void test17_3() throws Exception {
    class Listener extends FileBufferListener {

        public IFileBuffer buffer;

        public int count;

        @Override
        public void stateChanging(IFileBuffer buf) {
            ++count;
            this.buffer = buf;
        }
    }
    Listener listener = new Listener();
    fManager.addFileBufferListener(listener);
    try {
        ITextFileBuffer fileBuffer = fManager.getFileStoreTextFileBuffer(fFileStore);
        assertTrue(listener.count == 0 && listener.buffer == null);
        fManager.connectFileStore(fFileStore, null);
        try {
            fileBuffer = fManager.getFileStoreTextFileBuffer(fFileStore);
            IDocument document = fileBuffer.getDocument();
            document.replace(0, 0, "prefix");
            fileBuffer.commit(null, true);
            assertTrue(listener.count == 1);
            assertTrue(listener.buffer == fileBuffer);
        } finally {
            fManager.disconnectFileStore(fFileStore, null);
        }
    } finally {
        fManager.removeFileBufferListener(listener);
    }
}
Also used : IFileBuffer(org.eclipse.core.filebuffers.IFileBuffer) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 67 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse.platform.text by eclipse.

the class FileStoreFileBuffersForNonExistingWorkspaceFiles method testBug118199_fixed.

@Test
public void testBug118199_fixed() throws Exception {
    IPath location = getPath();
    IFile file = getProject().getWorkspace().getRoot().getFileForLocation(location);
    if (file == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new IOException("File '" + location + "' can not be found.");
    }
    IPath path = file.getFullPath();
    assertFalse(file.exists());
    fManager.connect(path, LocationKind.IFILE, null);
    try {
        ITextFileBuffer buffer = fManager.getTextFileBuffer(path, LocationKind.IFILE);
        buffer.getDocument().set("test");
        buffer.commit(null, false);
    } finally {
        fManager.disconnect(path, LocationKind.IFILE, null);
    }
    assertTrue(file.exists());
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IOException(java.io.IOException) Test(org.junit.Test)

Example 68 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project linuxtools by eclipse.

the class GNUHyperlinkDetector method detectHyperlinks.

/**
 * Detector using RuleBasedScanner.
 */
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (documentLocation == null) {
        ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
        ITextFileBuffer buffer = bufferManager.getTextFileBuffer(textViewer.getDocument());
        if (buffer == null) {
            return null;
        }
        documentLocation = buffer.getLocation().removeLastSegments(1);
    }
    IDocument thisDoc = textViewer.getDocument();
    GNUHyperlinkScanner scanner = new GNUHyperlinkScanner();
    ITypedRegion partitionInfo = null;
    try {
        partitionInfo = thisDoc.getPartition(region.getOffset());
    } catch (org.eclipse.jface.text.BadLocationException e1) {
        e1.printStackTrace();
        return null;
    }
    scanner.setRange(thisDoc, partitionInfo.getOffset(), partitionInfo.getLength());
    Token tmpToken = (Token) scanner.nextToken();
    String tokenStr = (String) tmpToken.getData();
    if (tokenStr == null) {
        return null;
    }
    // null.
    while (region.getOffset() < scanner.getTokenOffset() || region.getOffset() > scanner.getOffset() || tokenStr.equals("_other")) {
        tmpToken = (Token) scanner.nextToken();
        tokenStr = (String) tmpToken.getData();
        if (tokenStr == null)
            return null;
    }
    Region tokenRegion = new Region(scanner.getTokenOffset(), scanner.getTokenLength());
    String line = "";
    try {
        line = thisDoc.get(tokenRegion.getOffset(), tokenRegion.getLength());
    } catch (org.eclipse.jface.text.BadLocationException e1) {
        e1.printStackTrace();
        return null;
    }
    // process file link
    if (tokenStr.equals(GNUHyperlinkScanner.FILE_NAME)) {
        Region pathRegion = null;
        int lineOffset = 0;
        // cut "* " if necessary
        if (line.startsWith("* ")) {
            lineOffset = 2;
            line = line.substring(2);
        }
        pathRegion = new Region(tokenRegion.getOffset() + lineOffset, line.length());
        if (documentLocation == null)
            return null;
        // Replace any escape characters added to name
        line = line.replaceAll("\\\\(.)", "$1");
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IFile fileLoc = (IFile) root.findMember(documentLocation.append(line));
        if (fileLoc != null && fileLoc.exists()) {
            return new IHyperlink[] { new FileHyperlink(pathRegion, fileLoc) };
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) Token(org.eclipse.jface.text.rules.Token) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ITypedRegion(org.eclipse.jface.text.ITypedRegion) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument)

Example 69 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project eclipse-pmd by acanda.

the class JavaQuickFix method fixMarkersInFile.

/**
 * Fixes all provided markers in a file.
 *
 * @param markers The markers to fix. There is at least one marker in this collection and all markers can be fixed
 *            by this quick fix.
 */
protected void fixMarkersInFile(final IFile file, final List<IMarker> markers, final IProgressMonitor monitor) {
    monitor.subTask(file.getFullPath().toOSString());
    final Optional<ICompilationUnit> optionalCompilationUnit = getCompilationUnit(file);
    if (!optionalCompilationUnit.isPresent()) {
        return;
    }
    final ICompilationUnit compilationUnit = optionalCompilationUnit.get();
    ITextFileBufferManager bufferManager = null;
    final IPath path = compilationUnit.getPath();
    try {
        bufferManager = FileBuffers.getTextFileBufferManager();
        bufferManager.connect(path, LocationKind.IFILE, null);
        final ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
        final IDocument document = textFileBuffer.getDocument();
        final IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
        final ASTParser astParser = ASTParser.newParser(AST.JLS4);
        astParser.setKind(ASTParser.K_COMPILATION_UNIT);
        astParser.setResolveBindings(needsTypeResolution());
        astParser.setSource(compilationUnit);
        final SubProgressMonitor parserMonitor = new SubProgressMonitor(monitor, 100);
        final CompilationUnit ast = (CompilationUnit) astParser.createAST(parserMonitor);
        parserMonitor.done();
        startFixingMarkers(ast);
        final Map<?, ?> options = compilationUnit.getJavaProject().getOptions(true);
        for (final IMarker marker : markers) {
            try {
                final MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
                // if the annotation is null it means that is was deleted by a previous quick fix
                if (annotation != null) {
                    final Optional<T> node = getNodeFinder(annotationModel.getPosition(annotation)).findNode(ast);
                    if (node.isPresent()) {
                        final boolean isSuccessful = fixMarker(node.get(), document, options);
                        if (isSuccessful) {
                            marker.delete();
                        }
                    }
                }
            } finally {
                monitor.worked(100);
            }
        }
        finishFixingMarkers(ast, document, options);
        // commit changes to underlying file if it is not opened in an editor
        if (!isEditorOpen(file)) {
            final SubProgressMonitor commitMonitor = new SubProgressMonitor(monitor, 100);
            textFileBuffer.commit(commitMonitor, false);
            commitMonitor.done();
        } else {
            monitor.worked(100);
        }
    } catch (CoreException | MalformedTreeException | BadLocationException e) {
    // TODO: log error
    // PMDPlugin.getDefault().error("Error processing quickfix", e);
    } finally {
        if (bufferManager != null) {
            try {
                bufferManager.disconnect(path, LocationKind.IFILE, null);
            } catch (final CoreException e) {
            // TODO: log error
            // PMDPlugin.getDefault().error("Error processing quickfix", e);
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) AST(org.eclipse.jdt.core.dom.AST) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IMarker(org.eclipse.core.resources.IMarker) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 70 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class CodeRefactoringUtil method getIndentationLevel.

public static int getIndentationLevel(ASTNode node, ICompilationUnit unit) throws CoreException {
    IPath fullPath = unit.getCorrespondingResource().getFullPath();
    try {
        FileBuffers.getTextFileBufferManager().connect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
        ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(fullPath, LocationKind.IFILE);
        try {
            IRegion region = buffer.getDocument().getLineInformationOfOffset(node.getStartPosition());
            return Strings.computeIndentUnits(buffer.getDocument().get(region.getOffset(), region.getLength()), unit.getJavaProject());
        } catch (BadLocationException exception) {
            JavaPlugin.log(exception);
        }
        return 0;
    } finally {
        FileBuffers.getTextFileBufferManager().disconnect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)133 Test (org.junit.Test)85 IDocument (org.eclipse.jface.text.IDocument)54 IFileBuffer (org.eclipse.core.filebuffers.IFileBuffer)37 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)35 IPath (org.eclipse.core.runtime.IPath)31 IFile (org.eclipse.core.resources.IFile)18 BadLocationException (org.eclipse.jface.text.BadLocationException)15 IFileStore (org.eclipse.core.filesystem.IFileStore)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 CoreException (org.eclipse.core.runtime.CoreException)8 Position (org.eclipse.jface.text.Position)8 IFileInfo (org.eclipse.core.filesystem.IFileInfo)7 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)7 Match (org.eclipse.search.ui.text.Match)7 IFolder (org.eclipse.core.resources.IFolder)5 IStatus (org.eclipse.core.runtime.IStatus)5 Path (org.eclipse.core.runtime.Path)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 Status (org.eclipse.core.runtime.Status)4