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;
}
}
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();
}
}
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;
}
}
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;
}
}
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();
}
}
Aggregations