Search in sources :

Example 6 with BadLocationException

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

the class CodeTemplateContext method changeLineDelimiter.

private static String changeLineDelimiter(String code, String lineDelim) {
    try {
        ILineTracker tracker = new DefaultLineTracker();
        tracker.set(code);
        int nLines = tracker.getNumberOfLines();
        if (nLines == 1) {
            return code;
        }
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < nLines; i++) {
            if (i != 0) {
                buf.append(lineDelim);
            }
            IRegion region = tracker.getLineInformation(i);
            String line = code.substring(region.getOffset(), region.getOffset() + region.getLength());
            buf.append(line);
        }
        return buf.toString();
    } catch (BadLocationException e) {
        // can not happen
        return code;
    }
}
Also used : DefaultLineTracker(org.eclipse.jface.text.DefaultLineTracker) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) ILineTracker(org.eclipse.jface.text.ILineTracker)

Example 7 with BadLocationException

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

the class JavaContext method getKey.

/*
	 * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getKey()
	 */
@Override
public String getKey() {
    if (getCompletionLength() == 0)
        return super.getKey();
    try {
        IDocument document = getDocument();
        int start = getStart();
        int end = getCompletionOffset();
        return start <= end ? document.get(start, end - start) : //$NON-NLS-1$
        "";
    } catch (BadLocationException e) {
        return super.getKey();
    }
}
Also used : IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 8 with BadLocationException

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

the class JavaContext method getIndentation.

/**
	 * Returns the indentation level at the position of code completion.
	 *
	 * @return the indentation level at the position of the code completion
	 */
private int getIndentation() {
    int start = getStart();
    IDocument document = getDocument();
    try {
        IRegion region = document.getLineInformationOfOffset(start);
        String lineContent = document.get(region.getOffset(), region.getLength());
        IJavaProject project = getJavaProject();
        return Strings.computeIndentUnits(lineContent, project);
    } catch (BadLocationException e) {
        return 0;
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 with BadLocationException

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

the class JavaIndenter method getLeadingWhitespace.

/**
	 * Returns the indentation of the line at <code>offset</code> as a
	 * <code>StringBuffer</code>. If the offset is not valid, the empty string
	 * is returned.
	 *
	 * @param offset the offset in the document
	 * @return the indentation (leading whitespace) of the line in which
	 * 		   <code>offset</code> is located
	 */
private StringBuffer getLeadingWhitespace(int offset) {
    StringBuffer indent = new StringBuffer();
    try {
        IRegion line = fDocument.getLineInformationOfOffset(offset);
        int lineOffset = line.getOffset();
        int nonWS = fScanner.findNonWhitespaceForwardInAnyPartition(lineOffset, lineOffset + line.getLength());
        indent.append(fDocument.get(lineOffset, nonWS - lineOffset));
        return indent;
    } catch (BadLocationException e) {
        return indent;
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 10 with BadLocationException

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

the class JavaContext method getEnd.

/*
	 * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getEnd()
	 */
@Override
public int getEnd() {
    if (fIsManaged || getCompletionLength() == 0)
        return super.getEnd();
    try {
        IDocument document = getDocument();
        int start = getCompletionOffset();
        int end = getCompletionOffset() + getCompletionLength();
        while (start != end && Character.isWhitespace(document.getChar(end - 1))) end--;
        return end;
    } catch (BadLocationException e) {
        return super.getEnd();
    }
}
Also used : IDocument(org.eclipse.jface.text.IDocument) 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