Search in sources :

Example 91 with IDocument

use of org.eclipse.jface.text.IDocument in project eclipse.platform.text by eclipse.

the class FileBufferFunctions method testGetBufferForDocument.

@Test
public void testGetBufferForDocument() throws Exception {
    fManager.connect(fPath, LocationKind.NORMALIZE, null);
    try {
        ITextFileBuffer buffer = fManager.getTextFileBuffer(fPath, LocationKind.NORMALIZE);
        assertNotNull(buffer);
        IDocument document = buffer.getDocument();
        assertNotNull(document);
        assertSame(buffer, fManager.getTextFileBuffer(document));
    } finally {
        fManager.disconnect(fPath, LocationKind.NORMALIZE, null);
    }
}
Also used : ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 92 with IDocument

use of org.eclipse.jface.text.IDocument in project eclipse.platform.text by eclipse.

the class FileBufferFunctions method test6.

/*
	 * Test revert.
	 */
@Test
public void test6() throws Exception {
    fManager.connect(fPath, LocationKind.NORMALIZE, null);
    try {
        ITextFileBuffer fileBuffer = fManager.getTextFileBuffer(fPath, LocationKind.NORMALIZE);
        // set dirty bit
        IDocument document = fileBuffer.getDocument();
        String originalContent = document.get();
        document.replace(document.getLength(), 0, "appendix");
        // invalidate synchronization state
        IFileStore fileStore = FileBuffers.getFileStoreAtLocation(fPath);
        IFileInfo fileInfo = fileStore.fetchInfo();
        fileInfo.setLastModified(1000);
        if (fileInfo.exists())
            fileStore.putInfo(fileInfo, EFS.SET_LAST_MODIFIED, null);
        // revert
        fileBuffer.revert(null);
        // check assertions
        assertEquals(originalContent, document.get());
        assertFalse(fileBuffer.isDirty());
        assertTrue(fileBuffer.isSynchronized());
    } finally {
        fManager.disconnect(fPath, LocationKind.NORMALIZE, null);
    }
}
Also used : IFileInfo(org.eclipse.core.filesystem.IFileInfo) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IFileStore(org.eclipse.core.filesystem.IFileStore) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 93 with IDocument

use of org.eclipse.jface.text.IDocument in project eclipse.platform.text by eclipse.

the class LastSaveReferenceProvider method readDocument.

/**
 * Reads in the saved document into <code>fReference</code>.
 *
 * @param monitor a progress monitor, or <code>null</code>
 * @param force <code>true</code> if the reference document should also
 *        be read if the current document is <code>null</code>,<code>false</code>
 *        if it should only be updated if it already existed.
 */
private void readDocument(IProgressMonitor monitor, boolean force) {
    // protect against concurrent disposal
    IDocumentProvider prov = fDocumentProvider;
    IEditorInput inp = fEditorInput;
    IDocument doc = fReference;
    ITextEditor editor = fEditor;
    if (prov instanceof IStorageDocumentProvider && inp instanceof IStorageEditorInput) {
        IStorageEditorInput input = (IStorageEditorInput) inp;
        IStorageDocumentProvider provider = (IStorageDocumentProvider) prov;
        if (doc == null)
            if (force || fDocumentRead)
                doc = new Document();
            else
                return;
        IJobManager jobMgr = Job.getJobManager();
        try {
            IStorage storage = input.getStorage();
            // check for null for backward compatibility (we used to check before...)
            if (storage == null)
                return;
            fProgressMonitor = monitor;
            ISchedulingRule rule = getSchedulingRule(storage);
            // delay for any other job requiring the lock on file
            try {
                lockDocument(monitor, jobMgr, rule);
                String encoding;
                if (storage instanceof IEncodedStorage)
                    encoding = ((IEncodedStorage) storage).getCharset();
                else
                    encoding = null;
                boolean skipUTF8BOM = isUTF8BOM(encoding, storage);
                setDocumentContent(doc, storage, encoding, monitor, skipUTF8BOM);
            } finally {
                unlockDocument(jobMgr, rule);
                fProgressMonitor = null;
            }
        } catch (CoreException e) {
            return;
        }
        if (monitor != null && monitor.isCanceled())
            return;
        // update state
        synchronized (fLock) {
            if (fDocumentProvider == provider && fEditorInput == input) {
                // only update state if our provider / input pair has not
                // been updated in between (dispose or setActiveEditor)
                fReference = doc;
                fDocumentRead = true;
                addElementStateListener(editor, prov);
            }
        }
    }
}
Also used : IStorageDocumentProvider(org.eclipse.ui.editors.text.IStorageDocumentProvider) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) IJobManager(org.eclipse.core.runtime.jobs.IJobManager) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IStorage(org.eclipse.core.resources.IStorage) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 94 with IDocument

use of org.eclipse.jface.text.IDocument in project eclipse.platform.text by eclipse.

the class GotoLineTest method goToLine.

private void goToLine(int line, int expectedResult) {
    IWorkbench workbench = PlatformUI.getWorkbench();
    IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
    try {
        IEditorPart part = IDE.openEditor(page, fFile);
        if (part instanceof ITextEditor) {
            ITextEditor editor = (ITextEditor) part;
            IAction action = editor.getAction(ITextEditorActionConstants.GOTO_LINE);
            Accessor accessor = new Accessor(action, GotoLineAction.class);
            accessor.invoke("gotoLine", new Class[] { int.class }, new Integer[] { Integer.valueOf(line) });
            Control control = part.getAdapter(Control.class);
            if (control instanceof StyledText) {
                int caretLine = -1;
                StyledText styledText = (StyledText) control;
                int caret = styledText.getCaretOffset();
                try {
                    IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
                    caretLine = document.getLineOfOffset(caret);
                } catch (BadLocationException e1) {
                    fail();
                }
                assertEquals(expectedResult, caretLine);
            } else
                fail();
        } else
            fail();
    } catch (PartInitException e) {
        fail();
    }
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) Control(org.eclipse.swt.widgets.Control) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) StyledText(org.eclipse.swt.custom.StyledText) IAction(org.eclipse.jface.action.IAction) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) Accessor(org.eclipse.text.tests.Accessor) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 95 with IDocument

use of org.eclipse.jface.text.IDocument in project eclipse.platform.text by eclipse.

the class MarkerAnnotationOrderTest method testDirectDependency.

@Test
public void testDirectDependency() {
    final ArrayList<IStatus> list = new ArrayList<>(2);
    Bundle bundle = Platform.getBundle(EditorsUI.PLUGIN_ID);
    ILog log = Platform.getLog(bundle);
    log.addLogListener(new ILogListener() {

        @Override
        public void logging(IStatus status, String plugin) {
            list.add(status);
        }
    });
    TestMarkerAnnotationModel t1 = new TestMarkerAnnotationModel();
    Position position = new Position(0);
    position.delete();
    IDocument d = null;
    try {
        t1.updateMarker(d, null, position);
    } catch (CoreException e) {
        fail("update marker failed to execute");
        log(e);
    }
    assertEquals("Wrong number of messages", 2, list.size());
    assertEquals("Wrong Message for first status", "Marker Updater 'org.eclipse.ui.texteditor.BasicMarkerUpdaterTest2' and 'org.eclipse.ui.texteditor.BasicMarkerUpdaterTest1' depend on each other, 'org.eclipse.ui.texteditor.BasicMarkerUpdaterTest2' will run before 'org.eclipse.ui.texteditor.BasicMarkerUpdaterTest1'", ((Status) list.get(0)).getMessage());
    assertEquals("Wrong Message for second status", "Marker Updater 'org.eclipse.ui.texteditor.BasicMarkerUpdaterTest4' and 'org.eclipse.ui.texteditor.BasicMarkerUpdaterTest1' depend on each other, 'org.eclipse.ui.texteditor.BasicMarkerUpdaterTest4' will run before 'org.eclipse.ui.texteditor.BasicMarkerUpdaterTest1'", ((Status) list.get(1)).getMessage());
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) Position(org.eclipse.jface.text.Position) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ILog(org.eclipse.core.runtime.ILog) ILogListener(org.eclipse.core.runtime.ILogListener) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)488 BadLocationException (org.eclipse.jface.text.BadLocationException)195 Document (org.eclipse.jface.text.Document)118 Test (org.junit.Test)93 IRegion (org.eclipse.jface.text.IRegion)72 Point (org.eclipse.swt.graphics.Point)63 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)54 Position (org.eclipse.jface.text.Position)51 ArrayList (java.util.ArrayList)44 CoreException (org.eclipse.core.runtime.CoreException)39 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)30 IPath (org.eclipse.core.runtime.IPath)26 IFile (org.eclipse.core.resources.IFile)25 IStatus (org.eclipse.core.runtime.IStatus)25 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)24 TextEdit (org.eclipse.text.edits.TextEdit)23 Region (org.eclipse.jface.text.Region)22 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)22 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)21 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)20