Search in sources :

Example 51 with ITextFileBufferManager

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

the class UndoDocumentChange method performEdits.

private UndoEdit performEdits() throws BadLocationException, MalformedTreeException {
    ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
    ITextFileBuffer fileBuffer = fileBufferManager.getTextFileBuffer(fDocument);
    if (fileBuffer == null || !fileBuffer.isSynchronizationContextRequested()) {
        return fUndo.apply(fDocument, TextEdit.CREATE_UNDO);
    }
    /** The lock for waiting for computation in the UI thread to complete. */
    final Lock completionLock = new Lock();
    final UndoEdit[] result = new UndoEdit[1];
    final BadLocationException[] exception = new BadLocationException[1];
    Runnable runnable = new Runnable() {

        public void run() {
            synchronized (completionLock) {
                try {
                    result[0] = fUndo.apply(fDocument, TextEdit.CREATE_UNDO);
                } catch (BadLocationException e) {
                    exception[0] = e;
                } finally {
                    completionLock.fDone = true;
                    completionLock.notifyAll();
                }
            }
        }
    };
    synchronized (completionLock) {
        fileBufferManager.execute(runnable);
        while (!completionLock.fDone) {
            try {
                completionLock.wait(500);
            } catch (InterruptedException x) {
            }
        }
    }
    if (exception[0] != null) {
        throw exception[0];
    }
    return result[0];
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) UndoEdit(org.eclipse.text.edits.UndoEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 52 with ITextFileBufferManager

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

the class JavadocContentAccess2 method getIFileContent.

/**
     * Reads the content of the IFile.
     *
     * @param file the file whose content has to be read
     * @return the content of the file
     * @throws CoreException if the file could not be successfully connected or disconnected
     */
private static String getIFileContent(IFile file) throws CoreException {
    String content = null;
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    IPath fullPath = file.getFullPath();
    manager.connect(fullPath, LocationKind.IFILE, null);
    try {
        ITextFileBuffer buffer = manager.getTextFileBuffer(fullPath, LocationKind.IFILE);
        if (buffer != null) {
            content = buffer.getDocument().get();
        }
    } finally {
        manager.disconnect(fullPath, LocationKind.IFILE, null);
    }
    return content;
}
Also used : IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Example 53 with ITextFileBufferManager

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

the class CodeAssist method computeAssistProposals.

@SuppressWarnings("unchecked")
public Proposals computeAssistProposals(IJavaProject project, String fqn, int offset, List<Problem> problems) throws CoreException {
    ICompilationUnit compilationUnit;
    IType type = project.findType(fqn);
    if (type == null) {
        return null;
    }
    if (type.isBinary()) {
        throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CORE_EXCEPTION, "Can't calculate Quick Assist on binary file"));
    } else {
        compilationUnit = type.getCompilationUnit();
    }
    IBuffer buffer = compilationUnit.getBuffer();
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    bufferManager.connect(compilationUnit.getPath(), LocationKind.IFILE, new NullProgressMonitor());
    ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(compilationUnit.getPath(), LocationKind.IFILE);
    IDocument document = textFileBuffer.getDocument();
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    AssistContext context = new AssistContext(compilationUnit, offset, 0);
    ArrayList proposals = new ArrayList<>();
    JavaCorrectionProcessor.collectProposals(context, problems, true, true, proposals);
    return convertProposals(offset, compilationUnit, viewer, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) JavaModelStatus(org.eclipse.jdt.internal.core.JavaModelStatus) Point(org.eclipse.swt.graphics.Point) IBuffer(org.eclipse.jdt.core.IBuffer) IType(org.eclipse.jdt.core.IType) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument)

Example 54 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project AutoRefactor by JnRouvignac.

the class ApplyRefactoringsJob method applyRefactoring.

private void applyRefactoring(ICompilationUnit compilationUnit, AggregateASTVisitor refactoringToApply, JavaProjectOptions options, SubMonitor monitor) throws Exception {
    final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    final IPath path = compilationUnit.getPath();
    final LocationKind locationKind = LocationKind.NORMALIZE;
    try {
        bufferManager.connect(path, locationKind, null);
        final ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, locationKind);
        if (!textFileBuffer.isSynchronized()) {
            /*
                 * Cannot read the source when a file is not synchronized,
                 * Let's ignore this file to avoid problems when:
                 * - doing string manipulation with the source text
                 * - applying automated refactorings to such files
                 */
            environment.getLogger().error("File \"" + compilationUnit.getPath() + "\" is not synchronized with the file system." + " Automated refactorings will not be applied to it.");
            return;
        }
        final IDocument document = textFileBuffer.getDocument();
        applyRefactoring(document, compilationUnit, refactoringToApply, options, monitor);
    } finally {
        bufferManager.disconnect(path, locationKind, null);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) LocationKind(org.eclipse.core.filebuffers.LocationKind) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument)

Example 55 with ITextFileBufferManager

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

the class FileBufferCreation method test6.

/*
	 * Tests that a workspace file linked to an external file and the external file result
	 * in two different, independent file buffers.
	 */
@Test
public void test6() throws Exception {
    IPath path1 = createLinkedFile("file1", "testResources/ExternalFile");
    assertNotNull(path1);
    File externalFile = FileTool.getFileInPlugin(FileBuffersTestPlugin.getDefault(), new Path("testResources/ExternalFile"));
    assertNotNull(externalFile);
    IPath path2 = new Path(externalFile.getAbsolutePath());
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    manager.connect(path1, LocationKind.NORMALIZE, null);
    ITextFileBuffer buffer1 = manager.getTextFileBuffer(path1, LocationKind.NORMALIZE);
    assertNotNull(buffer1);
    manager.connect(path2, LocationKind.NORMALIZE, null);
    ITextFileBuffer buffer2 = manager.getTextFileBuffer(path2, LocationKind.NORMALIZE);
    assertNotNull(buffer2);
    IDocument document1 = buffer1.getDocument();
    assertNotNull(document1);
    assertSame(buffer1, manager.getTextFileBuffer(document1));
    IDocument document2 = buffer2.getDocument();
    assertNotNull(document2);
    assertSame(buffer2, manager.getTextFileBuffer(document2));
    assertEquals(document1.get(), document2.get());
    assertEquals(CONTENT3, document1.get());
    try {
        document1.replace(0, document1.getLength(), CONTENT1);
    } catch (BadLocationException x) {
        Assert.assertFalse(false);
    }
    assertFalse(document1.get().equals(document2.get()));
    manager.disconnect(path1, LocationKind.NORMALIZE, null);
    assertNull(manager.getTextFileBuffer(path1, LocationKind.NORMALIZE));
    manager.disconnect(path2, LocationKind.NORMALIZE, null);
    assertNull(manager.getTextFileBuffer(path2, LocationKind.NORMALIZE));
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Aggregations

ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)87 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)66 IDocument (org.eclipse.jface.text.IDocument)54 IPath (org.eclipse.core.runtime.IPath)52 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 TextEdit (org.eclipse.text.edits.TextEdit)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 UndoEdit (org.eclipse.text.edits.UndoEdit)6 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)5