import { assert } from './debug' ; import { Position , Range , TextRange } from './textRange' ; import { TextRangeCollection } from './textRangeCollection' ; export function convertOffsetToPosition ( offset , lines ) { if ( lines . end === 0 ) { return { line : 0 , character : 0 , } ; } let offsetAdjustment = 0 ; if ( offset >= lines . end ) { offset = lines . end - 1 ; offsetAdjustment = 1 ; } const itemIndex = lines . getItemContaining ( offset ) ; assert ( itemIndex >= 0 && itemIndex <= lines . length ) ; const lineRange = lines . getItemAt ( itemIndex ) ; assert ( lineRange !== undefined ) ; return { line : itemIndex , character : offset - lineRange . start + offsetAdjustment , } ; } export function convertOffsetsToRange ( startOffset , endOffset , lines ) { const start = convertOffsetToPosition ( startOffset , lines ) ; const end = convertOffsetToPosition ( endOffset , lines ) ; return { start , end } ; } export function convertPositionToOffset ( position , lines ) { if ( position . line >= lines . count ) { return undefined ; } return lines . getItemAt ( position . line ) . start + position . character ; } export function convertRangeToTextRange ( range , lines ) { const start = convertPositionToOffset ( range . start , lines ) ; if ( start === undefined ) { return undefined ; } const end = convertPositionToOffset ( range . end , lines ) ; if ( end === undefined ) { return undefined ; } return TextRange . fromBounds ( start , end ) ; } export function convertTextRangeToRange ( range , lines ) { return convertOffsetsToRange ( range . start , TextRange . getEnd ( range ) , lines ) ; }