Search in sources :

Example 16 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class MultiStateUndoChange method perform.

/**
	 * {@inheritDoc}
	 */
public Change perform(IProgressMonitor pm) throws CoreException {
    if (fValidationState == null || fValidationState.isValid(needsSaving(), false).hasFatalError())
        return new NullChange();
    if (pm == null)
        pm = new NullProgressMonitor();
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    //$NON-NLS-1$
    pm.beginTask("", 2);
    ITextFileBuffer buffer = null;
    try {
        manager.connect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
        buffer = manager.getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
        IDocument document = buffer.getDocument();
        ContentStamp currentStamp = ContentStamps.get(fFile, document);
        // perform the changes
        LinkedList list = new LinkedList();
        for (int index = 0; index < fUndos.length; index++) {
            UndoEdit edit = fUndos[index];
            UndoEdit redo = edit.apply(document, TextEdit.CREATE_UNDO);
            list.addFirst(redo);
        }
        // try to restore the document content stamp
        boolean success = ContentStamps.set(document, fContentStampToRestore);
        if (needsSaving()) {
            buffer.commit(pm, false);
            if (!success) {
                // We weren't able to restore document stamp.
                // Since we save restore the file stamp instead
                ContentStamps.set(fFile, fContentStampToRestore);
            }
        }
        return createUndoChange((UndoEdit[]) list.toArray(new UndoEdit[list.size()]), currentStamp);
    } catch (BadLocationException e) {
        throw Changes.asCoreException(e);
    } finally {
        if (buffer != null)
            manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) NullChange(org.eclipse.ltk.core.refactoring.NullChange) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ContentStamp(org.eclipse.ltk.core.refactoring.ContentStamp) UndoEdit(org.eclipse.text.edits.UndoEdit) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IDocument(org.eclipse.jface.text.IDocument) LinkedList(java.util.LinkedList) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 17 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class TextEditBasedChange method getContent.

String getContent(IDocument document, IRegion region, boolean expandRegionToFullLine, int surroundingLines) throws CoreException {
    try {
        if (expandRegionToFullLine) {
            int startLine = Math.max(document.getLineOfOffset(region.getOffset()) - surroundingLines, 0);
            int endLine;
            if (region.getLength() == 0) {
                // or else spurious changes show up that look like deletes from the source
                if (surroundingLines == 0) {
                    //$NON-NLS-1$
                    return "";
                }
                endLine = Math.min(document.getLineOfOffset(region.getOffset()) + surroundingLines - 1, document.getNumberOfLines() - 1);
            } else {
                endLine = Math.min(document.getLineOfOffset(region.getOffset() + region.getLength() - 1) + surroundingLines, document.getNumberOfLines() - 1);
            }
            int offset = document.getLineInformation(startLine).getOffset();
            IRegion endLineRegion = document.getLineInformation(endLine);
            int length = endLineRegion.getOffset() + endLineRegion.getLength() - offset;
            return document.get(offset, length);
        } else {
            return document.get(region.getOffset(), region.getLength());
        }
    } catch (BadLocationException e) {
        throw Changes.asCoreException(e);
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 18 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class TextFileChange method performEdits.

/*
	 * @see org.eclipse.ltk.core.refactoring.TextChange#performEdits(org.eclipse.jface.text.IDocument)
	 * @since 3.5
	 */
protected UndoEdit performEdits(final IDocument document) throws BadLocationException, MalformedTreeException {
    if (!fBuffer.isSynchronizationContextRequested()) {
        return super.performEdits(document);
    }
    ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
    /** 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] = TextFileChange.super.performEdits(document);
                } 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) UndoEdit(org.eclipse.text.edits.UndoEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) Lock(org.eclipse.ltk.internal.core.refactoring.Lock)

Example 19 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class UndoTextFileChange method performEdits.

private UndoEdit performEdits(ITextFileBuffer buffer, final IDocument document, final boolean[] setContentStampSuccess) throws MalformedTreeException, BadLocationException, CoreException {
    if (!buffer.isSynchronizationContextRequested()) {
        return doPerformEdits(document, setContentStampSuccess);
    }
    ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
    /** 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[] badLocationException = new BadLocationException[1];
    final MalformedTreeException[] malformedTreeException = new MalformedTreeException[1];
    final CoreException[] coreException = new CoreException[1];
    Runnable runnable = new Runnable() {

        public void run() {
            synchronized (completionLock) {
                try {
                    result[0] = doPerformEdits(document, setContentStampSuccess);
                } catch (BadLocationException e) {
                    badLocationException[0] = e;
                } catch (MalformedTreeException e) {
                    malformedTreeException[0] = e;
                } catch (CoreException e) {
                    coreException[0] = e;
                } finally {
                    completionLock.fDone = true;
                    completionLock.notifyAll();
                }
            }
        }
    };
    synchronized (completionLock) {
        fileBufferManager.execute(runnable);
        while (!completionLock.fDone) {
            try {
                completionLock.wait(500);
            } catch (InterruptedException x) {
            }
        }
    }
    if (badLocationException[0] != null) {
        throw badLocationException[0];
    } else if (malformedTreeException[0] != null) {
        throw malformedTreeException[0];
    } else if (coreException[0] != null) {
        throw coreException[0];
    }
    return result[0];
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) UndoEdit(org.eclipse.text.edits.UndoEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) Lock(org.eclipse.ltk.internal.core.refactoring.Lock)

Example 20 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class JavaWordFinder method findWord.

public static IRegion findWord(IDocument document, int offset) {
    int start = -2;
    int end = -1;
    try {
        int pos = offset;
        char c;
        while (pos >= 0) {
            c = document.getChar(pos);
            if (!Character.isJavaIdentifierPart(c)) {
                // Check for surrogates
                if (isSurrogate(c)) {
                /*
						 * XXX: Here we should create the code point and test whether
						 * it is a Java identifier part. Currently this is not possible
						 * because java.lang.Character in 1.4 does not support surrogates
						 * and because com.ibm.icu.lang.UCharacter.isJavaIdentifierPart(int)
						 * is not correctly implemented.
						 */
                } else {
                    break;
                }
            }
            --pos;
        }
        start = pos;
        pos = offset;
        int length = document.getLength();
        while (pos < length) {
            c = document.getChar(pos);
            if (!Character.isJavaIdentifierPart(c)) {
                if (isSurrogate(c)) {
                /*
						 * XXX: Here we should create the code point and test whether
						 * it is a Java identifier part. Currently this is not possible
						 * because java.lang.Character in 1.4 does not support surrogates
						 * and because com.ibm.icu.lang.UCharacter.isJavaIdentifierPart(int)
						 * is not correctly implemented.
						 */
                } else {
                    break;
                }
            }
            ++pos;
        }
        end = pos;
    } catch (BadLocationException x) {
    }
    if (start >= -1 && end > -1) {
        if (start == offset && end == offset) {
            try {
                char c = document.getChar(offset);
                switch(c) {
                    case '-':
                        if (document.getChar(offset + 1) == '>') {
                            return new Region(offset, 2);
                        }
                        break;
                    case '>':
                        if (document.getChar(offset - 1) == '-') {
                            return new Region(offset - 1, 2);
                        }
                        break;
                    case ':':
                        if (document.getChar(offset + 1) == ':') {
                            return new Region(offset, 2);
                        } else if (document.getChar(offset - 1) == ':') {
                            return new Region(offset - 1, 2);
                        }
                        break;
                }
            } catch (BadLocationException e) {
            }
            return new Region(offset, 0);
        } else if (start == offset) {
            //XXX: probably unused...
            return new Region(start, end - start);
        } else {
            return new Region(start + 1, end - start - 1);
        }
    }
    return null;
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)133 IDocument (org.eclipse.jface.text.IDocument)58 IRegion (org.eclipse.jface.text.IRegion)43 Document (org.eclipse.jface.text.Document)26 Point (org.eclipse.swt.graphics.Point)17 CoreException (org.eclipse.core.runtime.CoreException)16 Position (org.eclipse.jface.text.Position)13 StyledString (org.eclipse.jface.viewers.StyledString)13 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)13 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)12 TemplateException (org.eclipse.jface.text.templates.TemplateException)12 TextEdit (org.eclipse.text.edits.TextEdit)12 UndoEdit (org.eclipse.text.edits.UndoEdit)10 Region (org.eclipse.jface.text.Region)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)8 ArrayList (java.util.ArrayList)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)6 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)6 DefaultLineTracker (org.eclipse.jface.text.DefaultLineTracker)6