下面列出了怎么用org.eclipse.jface.text.BadLocationException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns the end offset of the line that contains the specified offset or
* if the offset is inside a line delimiter, the end offset of the next
* line.
*
* @param offset
* the offset whose line end offset must be computed
*
* @return the line end offset for the given offset
*
* @exception BadLocationException
* if offset is invalid in the current document
*/
protected int endOfLineOf( int offset ) throws BadLocationException
{
IRegion info = fDocument.getLineInformationOfOffset( offset );
if ( offset <= info.getOffset( ) + info.getLength( ) )
{
return info.getOffset( ) + info.getLength( );
}
int line = fDocument.getLineOfOffset( offset );
try
{
info = fDocument.getLineInformation( line + 1 );
return info.getOffset( ) + info.getLength( );
}
catch ( BadLocationException x )
{
return fDocument.getLength( );
}
}
/**
* Indents the line range specified by <code>lines</code> in
* <code>document</code>. The passed Java project may be
* <code>null</code>, it is used solely to obtain formatter preferences.
*
* @param document the document to be changed
* @param lines the line range to be indented
* @param project the Java project to get the formatter preferences from, or
* <code>null</code> if global preferences should be used
* @param result the result from a previous call to <code>indentLines</code>,
* in order to maintain comment line properties, or <code>null</code>.
* Note that the passed result may be changed by the call.
* @return an indent result that may be queried for changes and can be
* reused in subsequent indentation operations
* @throws BadLocationException if <code>lines</code> is not a valid line
* range on <code>document</code>
*/
public static IndentResult indentLines(IDocument document, ILineRange lines, IJavaProject project, IndentResult result) throws BadLocationException {
int numberOfLines= lines.getNumberOfLines();
if (numberOfLines < 1)
return new IndentResult(null);
result= reuseOrCreateToken(result, numberOfLines);
JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
JavaIndenter indenter= new JavaIndenter(document, scanner, project);
boolean changed= false;
int tabSize= CodeFormatterUtil.getTabWidth(project);
for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++) {
changed |= indentLine(document, line, indenter, scanner, result.commentLinesAtColumnZero, i++, tabSize);
}
result.hasChanged= changed;
return result;
}
protected String doGetAdditionalProposalInfo(IProgressMonitor monitor) {
Document tempDocument = new Document(sourceOpContext.getSource());
doApply(tempDocument, false);
try {
tempDocument.replace(endPositionAfterApply, 0, " ");
} catch(BadLocationException e) {
}
DocumentSourceBuffer tempSourceBuffer = new DocumentSourceBuffer(tempDocument) {
@Override
public Location getLocation_orNull() {
return sourceOpContext.getOptionalFileLocation().orElse(null);
}
};
String doc = new DocDisplayInfoSupplier(tempSourceBuffer, getReplaceOffset())
.doGetDocumentation(EclipseUtils.om(monitor));
if(doc == null) {
return null;
}
return BrowserControlCreator.wrapHTMLBody(doc);
}
/**
* This method overrides the default one (which is suited for Java
* (i.e. result in NOT replacing anything before '.', which causes
* inconvenience, when templates are named like "list.itemize"
*
* @param viewer
* @param offset Document offset
* @return prefix (all character counting back from current cursont
* position, until a space(' '), linefeed('\n'), carriage return('\r'),
* a tab('\t') or the beginning of the file is encountered
*/
protected String extractPrefix(ITextViewer viewer, int offset) {
int i = offset - 1;
if (i == -1) {
return "";
}
StringBuffer sb = new StringBuffer("");
char c;
try {
c = viewer.getDocument().getChar(i);
while (!Character.isWhitespace(c)) {
sb.append(c);
i--;
if (i < 0) {
break;
} else {
c = viewer.getDocument().getChar(i);
}
}
} catch (BadLocationException e) {
TexlipsePlugin.log("BibTemplateCompletion, extractPrefix.", e);
}
return sb.reverse().toString();
}
public void execute(PyEdit pyEdit) {
IAction action = pyEdit.getAction("org.python.pydev.editor.actions.execLineInConsole");
if (action instanceof IExecuteLineAction) {
IExecuteLineAction executeLineAction = (IExecuteLineAction) action;
String commandText = this.interactiveConsoleCommand.commandText;
TextSelectionUtils ts = pyEdit.createTextSelectionUtils();
String selectedText;
try {
selectedText = ts.getSelectedText();
} catch (BadLocationException e) {
selectedText = "";
}
if (selectedText.length() == 0) {
selectedText = ts.getCursorLineContents();
}
executeLineAction.executeText(new FastStringBuffer(commandText, selectedText.length() * 2).replaceAll(
"${text}",
selectedText).toString());
} else {
Log.log("Expected: " + action + " to implement IExecuteLineAction.");
}
}
/**
* Commits the currently added line (last called in update) to the history and keeps it there.
*/
public void commit() {
String lineToAddToHistory = getBufferLine();
try {
historyAsDoc.replace(historyAsDoc.getLength(), 0, lineToAddToHistory + "\n");
} catch (BadLocationException e) {
Log.log(e);
}
if (lineToAddToHistory.length() == 0) {
currLine = lines.size() - 1;
return;
}
lines.set(lines.size() - 1, lineToAddToHistory);
lines.add(""); //$NON-NLS-1$
currLine = lines.size() - 1;
}
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
if(offset < getReplaceOffset())
return false;
String prefix;
try {
prefix = document.get(getReplaceOffset(), offset - getReplaceOffset());
} catch (BadLocationException e) {
return false;
}
boolean validPrefix = isValidPrefix(prefix);
if(validPrefix && event != null) {
// adapt replacement length to document event/
int eventEndOffset = event.fOffset + event.fLength;
// The event should be a common prefix completion (this should be true anyways) :
int replaceEndPos = getReplaceOffset() + getReplaceLength();
if(event.fOffset >= getReplaceOffset() && eventEndOffset <= replaceEndPos) {
int delta = (event.fText == null ? 0 : event.fText.length()) - event.fLength;
this.replaceLength = Math.max(getReplaceLength() + delta, 0);
}
}
return validPrefix;
}
/**
* Applies all given changes to the given document. This method assumes that 'changes' contains only changes
* intended for the given document; the actual URI stored in the changes is ignored.
*/
public void applyAllInSameDocument(Collection<? extends IAtomicChange> changes, IDocument document)
throws BadLocationException {
DocumentRewriteSession rewriteSession = null;
try {
// prepare
if (document instanceof IDocumentExtension4) {
rewriteSession = ((IDocumentExtension4) document).startRewriteSession(
DocumentRewriteSessionType.UNRESTRICTED);
}
// perform replacements
for (IAtomicChange currRepl : changes) {
currRepl.apply(document);
}
} finally {
// cleanup
if (rewriteSession != null)
((IDocumentExtension4) document).stopRewriteSession(rewriteSession);
}
}
/**
* Returns <code>true</code> if the content after the given offset is followed
* by the given <code>value</code> and false otherwise.
*
* @param document the document
* @param offset the offset
* @param value the content value to check
* @return <code>true</code> if the content after the given offset is followed
* by the given <code>value</code> and false otherwise.
*/
private static boolean isFollowedBy(IDocument document, int offset, String value) {
for (int i = 0; i < value.length(); i++) {
if (document.getLength() <= offset) {
return false;
}
try {
if (document.getChar(offset) != value.charAt(i)) {
return false;
}
} catch (BadLocationException e) {
return false;
}
offset++;
}
return true;
}
/**
* Count the whitespace to the left and right of offset, potentially counting over EOLs.
* @param document
* @param offset
* @param dir
* @param ignoreCR - count over EOLs if true
* @return the whitespace count
* @throws BadLocationException
*/
protected int countWS(IDocument document, int offset, int dir, boolean ignoreCR) throws BadLocationException {
String eol = getLineDelimiter();
int lineOff = offset;
int off = offset;
int lastOff = document.getLength(); // -1; // n
char c;
while ((-1 < off && off < lastOff) && (c = document.getChar(off)) <= ' ') {
if (eol.indexOf(c) != -1) {
if (!ignoreCR) {
break;
}
// preserve the position past the last EOL
lineOff = off + dir;
}
off = off + dir;
}
// if ignoreCR == true, then we're interested in complete blank lines only
return Math.abs(offset - (ignoreCR ? lineOff : off));
}
/**
* @see com.mulgasoft.emacsplus.commands.EmacsPlusNoEditHandler#transform(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection, org.eclipse.core.commands.ExecutionEvent)
*/
@Override
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection,
ExecutionEvent event) throws BadLocationException {
int offset = currentSelection.getOffset();
IRegion lineInfo = document.getLineInformationOfOffset(offset);
int lineTotal = document.getNumberOfLines();
int lineNumber = document.getLineOfOffset(offset);
// only include current, if at beginning of line
if (offset != lineInfo.getOffset()) {
++lineNumber;
}
if (document.getLength() == offset) {
lineNumber = lineTotal;
}
setCmdResult(new Integer(lineTotal));
EmacsPlusUtils.showMessage(editor, String.format(COUNT_BUFFER, lineTotal, lineNumber, lineTotal - lineNumber), false);
return super.transform(editor, document, currentSelection, event);
}
/**
* Expects the cursor to be in the same line as the start terminal
* puts any text between start terminal and cursor into a separate newline before the cursor.
* puts any text between cursor and end terminal into a separate newline after the cursor.
* puts the closing terminal into a separate line at the end.
* adds a closing terminal if not existent.
*/
protected CommandInfo handleCursorInFirstLine(IDocument document, DocumentCommand command, IRegion startTerminal,
IRegion stopTerminal) throws BadLocationException {
CommandInfo newC = new CommandInfo();
newC.isChange = true;
newC.offset = command.offset;
newC.text += command.text + indentationString;
newC.cursorOffset = command.offset + newC.text.length();
if (stopTerminal == null && atEndOfLineInput(document, command.offset)) {
newC.text += command.text + getRightTerminal();
}
if (stopTerminal != null && stopTerminal.getOffset() >= command.offset && util.isSameLine(document, stopTerminal.getOffset(), command.offset)) {
String string = document.get(command.offset, stopTerminal.getOffset() - command.offset);
if (string.trim().length() > 0)
newC.text += string.trim();
newC.text += command.text;
newC.length += string.length();
}
return newC;
}
/**
* @return the complete dotted string given the current selection and the strings after
*
* e.g.: if we have a text of
* 'value = aa.bb.cc()' and 'aa' is selected, this method would return the whole dotted string ('aa.bb.cc')
* @throws BadLocationException
*/
public String getFullRepAfterSelection() throws BadLocationException {
int absoluteCursorOffset = getAbsoluteCursorOffset();
int length = doc.getLength();
int end = absoluteCursorOffset;
char ch = doc.getChar(end);
while (Character.isLetterOrDigit(ch) || ch == '.') {
end++;
//check if we can still get some char
if (length - 1 < end) {
break;
}
ch = doc.getChar(end);
}
return doc.get(absoluteCursorOffset, end - absoluteCursorOffset);
}
private String getNamedStyleAtOffset( int offset )
{
// ensure the offset is clean
if ( offset >= fDocument.getLength( ) )
return getNamedStyleAtOffset( fDocument.getLength( ) - 1 );
else if ( offset < 0 )
return getNamedStyleAtOffset( 0 );
try
{
String regionContext = fDocument.getPartition( offset ).getType( );
String namedStyle = (String) fContextToStyleMap.get( regionContext );
if ( namedStyle != null )
{
return namedStyle;
}
}
catch ( BadLocationException e )
{
}
return null;
}
/**
* @throws BadLocationException
*
*/
public void testGeneral() throws BadLocationException {
ps = new PySelection(doc, 0);
assertEquals("TestLine1", ps.getCursorLineContents());
assertEquals("", ps.getLineContentsToCursor());
ps.selectCompleteLine();
assertEquals("TestLine1", ps.getCursorLineContents());
assertEquals("TestLine1", ps.getLine(0));
assertEquals("TestLine2#comm2", ps.getLine(1));
ps.deleteLine(0);
assertEquals("TestLine2#comm2", ps.getLine(0));
ps.addLine("TestLine1", 0);
}
/**
* Semantic quickfix removing the override flag for a rule.
*
* @param issue
* the issue
* @param acceptor
* the acceptor
*/
@Fix(FormatJavaValidator.OVERRIDE_ILLEGAL_CODE)
public void removeOverride(final Issue issue, final IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Remove override", "Remove override.", null, new IModification() {
@Override
public void apply(final IModificationContext context) throws BadLocationException {
context.getXtextDocument().modify(new IUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(final XtextResource state) {
Rule rule = (Rule) state.getEObject(issue.getUriToProblem().fragment());
rule.setOverride(false);
return null;
}
});
}
});
}
private void performEdit(IDocument document, long oldFileValue, LinkedList<UndoEdit> editCollector, long[] oldDocValue, boolean[] setContentStampSuccess) throws MalformedTreeException, BadLocationException, CoreException {
if (document instanceof IDocumentExtension4) {
oldDocValue[0]= ((IDocumentExtension4)document).getModificationStamp();
} else {
oldDocValue[0]= oldFileValue;
}
// perform the changes
for (int index= 0; index < fUndos.length; index++) {
UndoEdit edit= fUndos[index];
UndoEdit redo= edit.apply(document, TextEdit.CREATE_UNDO);
editCollector.addFirst(redo);
}
if (document instanceof IDocumentExtension4 && fDocumentStamp != IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP) {
try {
((IDocumentExtension4)document).replace(0, 0, "", fDocumentStamp); //$NON-NLS-1$
setContentStampSuccess[0]= true;
} catch (BadLocationException e) {
throw wrapBadLocationException(e);
}
}
}
public PythonSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
super(parent, ruler, null, false, styles, new PyAbstractIndentGuidePreferencesProvider() {
@Override
public int getTabWidth() {
return DefaultIndentPrefs.get(null).getTabWidth();
}
});
StyledText text = this.getTextWidget();
text.addBidiSegmentListener(new BidiSegmentListener() {
@Override
public void lineGetSegments(BidiSegmentEvent event) {
try {
event.segments = getBidiLineSegments(event.lineOffset);
} catch (BadLocationException x) {
// ignore
}
}
});
updateViewerFont();
updateViewerColors();
getPreferenceStore().addPropertyChangeListener(propertyChangeListener);
}
@Override
public void documentChanged(DocumentEvent event) {
IDocument document = event.getDocument();
try {
int startLine = DocumentHelper.getStartLine(event);
if (!DocumentHelper.isRemove(event)) {
int endLine = DocumentHelper.getEndLine(event, false);
// Insert new lines
for (int i = startLine; i < endLine; i++) {
DocumentLineList.this.addLine(i + 1);
}
if (startLine == endLine) {
DocumentLineList.this.updateLine(startLine);
}
} else {
// Update line
DocumentLineList.this.updateLine(startLine);
}
invalidateLine(startLine);
} catch (BadLocationException e) {
TMUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, e.getMessage(), e));
}
}
protected boolean isReplacedAreaEmpty(TemplateContext context) {
// don't trim the buffer if the replacement area is empty
// case: surrounding empty lines with block
if (context instanceof DocumentTemplateContext) {
DocumentTemplateContext dtc= (DocumentTemplateContext) context;
if (dtc.getStart() == dtc.getCompletionOffset())
try {
IDocument document= dtc.getDocument();
int lineOffset= document.getLineInformationOfOffset(dtc.getStart()).getOffset();
//only if we are at the beginning of the line
if (lineOffset != dtc.getStart())
return false;
//Does the selection only contain whitespace characters?
if (document.get(dtc.getStart(), dtc.getEnd() - dtc.getStart()).trim().length() == 0)
return true;
} catch (BadLocationException x) {
// ignore - this may happen when the document was modified after the initial invocation, and the
// context does not track the changes properly - don't trim in that case
return true;
}
}
return false;
}
private String evaluateChanges(String uri, List<TextEdit> edits) throws BadLocationException, JavaModelException {
assertFalse("No edits generated: " + edits, edits == null || edits.isEmpty());
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
assertNotNull("CU not found: " + uri, cu);
Document doc = new Document();
if (cu.exists()) {
doc.set(cu.getSource());
}
return TextEditUtil.apply(doc, edits);
}
/**
* Utility: Converts document's offset to Location
* @return Location
*/
static public Location offsetToLocation(IDocument document, int offset) {
try {
int line = document.getLineOfOffset(offset);
int line_start = document.getLineOffset(line);
return new Location(line, offset - line_start);
} catch (BadLocationException e) {
return new Location();
}
}
/**
* From <code>position</code> to the left, eats any whitespace and the first identifier, returning
* the position of the first identifier character (in normal read order).
* <p>When called on a document with content <code>" some string "</code> and positionition 13, the
* return value will be 6 (the first letter in <code>string</code>).
* </p>
*
* @param document the document being modified
* @param position the first character position in <code>document</code> to be considered
* @param partitioning the document partitioning
* @return the smallest character position of an identifier or -1 if none can be found; always <= <code>position</code>
*/
private static int eatIdentToLeft(IDocument document, int position, String partitioning) {
if (position < 0)
return -1;
Assert.isTrue(position < document.getLength());
int p= firstNonWhitespaceBackward(document, position, partitioning, -1);
if (p == -1)
return -1;
try {
while (p >= 0) {
char ch= document.getChar(p);
if (Character.isJavaIdentifierPart(ch)) {
p--;
continue;
}
// length must be > 0
if (Character.isWhitespace(ch) && p != position)
return p + 1;
else
return -1;
}
// start of document reached
return 0;
} catch (BadLocationException e) {
}
return -1;
}
private static Position getPosition(ASTNode node, IDocument document) {
int offset = node.getStartPosition();
try {
IRegion region = document.getLineInformationOfOffset(offset);
return new Position(region.getOffset() + region.getLength(), 1);
} catch (BadLocationException e) {
return new Position(offset, 1);
}
}
/**
* Contrary to addIncrement(String), this method does not verify that the
* input terminates with the newline separator. It is up to the caller to
* only provide valid input.
*/
public void addLine(String text) throws BadLocationException
{
// don't waste time, skip empty or new lines
if (text == null || text.length() == 0 || text.equals("\n")) {
return;
}
document.replace(document.getLength(), 0, text);
}
@Override
public boolean rangesEqual(int thisIndex, IRangeComparator other, int otherIndex) {
try {
return getHash(thisIndex).equals(((LineComparator) other).getHash(otherIndex));
} catch (BadLocationException e) {
Log.log(e);
return false;
}
}
private String getLineText(ITextViewer viewer, int lineNumber) {
IDocument doc = viewer.getDocument();
String result;
try {
IRegion lineRegion = doc.getLineInformation(lineNumber);
result = doc.get(lineRegion.getOffset(), lineRegion.getLength());
} catch (BadLocationException e) {
result = EMPTY_STRING;
}
return result;
}
public String[] getArguments() {
String prefix= ""; //$NON-NLS-1$
String postfix= ""; //$NON-NLS-1$
String word;
try {
word= fDocument.get(getOffset(), getLength());
} catch (BadLocationException e) {
return null;
}
try {
IRegion line= fDocument.getLineInformationOfOffset(getOffset());
prefix= fDocument.get(line.getOffset(), getOffset() - line.getOffset());
int postfixStart= getOffset() + getLength();
postfix= fDocument.get(postfixStart, line.getOffset() + line.getLength() - postfixStart);
} catch (BadLocationException exception) {
// Do nothing
}
return new String[] {
word,
prefix,
postfix,
isSentenceStart() ? Boolean.toString(true) : Boolean
.toString(false),
isDictionaryMatch() ? Boolean.toString(true) : Boolean
.toString(false) };
}
@Test
public void testNoNewline() throws IOException {
TagBasedTLCOutputIncrementalParser parser = new TagBasedTLCOutputIncrementalParser(new DummyModel(), 0, false);
// read in test input and feed it to the parser
try {
parser.addIncrement("@[email protected][email protected] 2262:0 @[email protected][email protected]");
} catch (BadLocationException e) {
return;
}
Assert.fail("Parser is expected to throw BLE on input without newline");
}
/**
* Restores any decorated regions and updates the buffer's variable offsets.
*
* @return the buffer.
* @throws MalformedTreeException
* @throws BadLocationException
*/
public TemplateBuffer updateBuffer() throws MalformedTreeException, BadLocationException {
checkState();
TemplateVariable[] variables= fBuffer.getVariables();
try {
removeRangeMarkers(fPositions, fDocument, variables);
} catch (BadPositionCategoryException x) {
Assert.isTrue(false);
}
fBuffer.setContent(fDocument.get(), variables);
fDocument= null;
return fBuffer;
}