Source:
<code>public static ITextSelection calculateOffsets(IEditorPart editorPart, TextSelection selection) { Objects.requireNonNull(editorPart, "The given editor part must not be null"); Objects.requireNonNull(selection, "The given selection must not be null"); if (selection.isEmpty()) { throw new IllegalArgumentException("The given text selection must not be empty"); } if (!(editorPart instanceof ITextEditor)) { return org.eclipse.jface.text.TextSelection.emptySelection(); } ITextEditor textEditor = (ITextEditor) editorPart; IDocumentProvider documentProvider = textEditor.getDocumentProvider(); if (documentProvider == null) { return org.eclipse.jface.text.TextSelection.emptySelection(); } IDocument document = textEditor.getDocumentProvider().getDocument(editorPart.getEditorInput()); if (document == null) { return org.eclipse.jface.text.TextSelection.emptySelection(); } TextPosition startPosition = selection.getStartPosition(); TextPosition endPosition = selection.getEndPosition(); int startLineOffset; int endLineOffset; try { startLineOffset = document.getLineOffset(startPosition.getLineNumber()); if (endPosition.getLineNumber() == startPosition.getLineNumber()) { endLineOffset = startLineOffset; } else { endLineOffset = document.getLineOffset(endPosition.getLineNumber()); } } catch (BadLocationException e) { LOG.warn( "Could not calculate the line offset for the given selection " + selection + " in the document " + document, e); return org.eclipse.jface.text.TextSelection.emptySelection(); <START> } <END> int startOffset = startLineOffset + startPosition.getInLineOffset(); int endOffset = endLineOffset + endPosition.getInLineOffset(); int length = endOffset - startOffset; return new org.eclipse.jface.text.TextSelection(startOffset, length); }</code><technical_language>add a sanity check is compare TextLocations. Eg. a TextLocation(0, 20) TextLocation(1, 5). TextLocation is invalid line altered in some contains 4 characters now. In end end a negative length. This happen if transmit faulty values</technical_language>

Target:
public static ITextSelection calculateOffsets(IEditorPart editorPart, TextSelection selection) { Objects.requireNonNull(editorPart, "The given editor part must not be null"); Objects.requireNonNull(selection, "The given selection must not be null"); if (selection.isEmpty()) { throw new IllegalArgumentException("The given text selection must not be empty"); } if (!(editorPart instanceof ITextEditor)) { return org.eclipse.jface.text.TextSelection.emptySelection(); } ITextEditor textEditor = (ITextEditor) editorPart; IDocumentProvider documentProvider = textEditor.getDocumentProvider(); if (documentProvider == null) { return org.eclipse.jface.text.TextSelection.emptySelection(); } IDocument document = documentProvider.getDocument(textEditor.getEditorInput()); if (document == null) { return org.eclipse.jface.text.TextSelection.emptySelection(); } TextPosition startPosition = selection.getStartPosition(); TextPosition endPosition = selection.getEndPosition(); int startLineOffset; int endLineOffset; try { startLineOffset = document.getLineOffset(startPosition.getLineNumber()); if (endPosition.getLineNumber() == startPosition.getLineNumber()) { endLineOffset = startLineOffset; } else { endLineOffset = document.getLineOffset(endPosition.getLineNumber()); } } catch (BadLocationException e) { log.warn( "Could not calculate the line offset for the given selection " + selection + " in the document " + document, e); return org.eclipse.jface.text.TextSelection.emptySelection(); } int startOffset = startLineOffset + startPosition.getInLineOffset(); int endOffset = endLineOffset + endPosition.getInLineOffset(); int length = endOffset - startOffset; return new org.eclipse.jface.text.TextSelection(startOffset, length); }