Source:
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(); } int startOffset = startLineOffset + startPosition.getInLineOffset(); int endOffset = endLineOffset + endPosition.getInLineOffset(); int length = endOffset - startOffset; return new org.eclipse.jface.text.TextSelection(startOffset, length); }

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