@@ -255,6 +255,81 @@ public int GetOffsetAtPosition(int lineNumber, int columnNumber)
255
255
return offset ;
256
256
}
257
257
258
+ /// <summary>
259
+ /// Calculates the 1-based line and column number position based
260
+ /// on the given buffer offset.
261
+ /// </summary>
262
+ /// <param name="bufferOffset">The buffer offset to convert.</param>
263
+ /// <returns>A new BufferPosition containing the position of the offset.</returns>
264
+ public BufferPosition GetPositionAtOffset ( int bufferOffset )
265
+ {
266
+ BufferRange bufferRange =
267
+ GetRangeBetweenOffsets (
268
+ bufferOffset , bufferOffset ) ;
269
+
270
+ return bufferRange . Start ;
271
+ }
272
+
273
+ /// <summary>
274
+ /// Calculates the 1-based line and column number range based on
275
+ /// the given start and end buffer offsets.
276
+ /// </summary>
277
+ /// <param name="startOffset">The start offset of the range.</param>
278
+ /// <param name="endOffset">The end offset of the range.</param>
279
+ /// <returns>A new BufferRange containing the positions in the offset range.</returns>
280
+ public BufferRange GetRangeBetweenOffsets ( int startOffset , int endOffset )
281
+ {
282
+ bool foundStart = false ;
283
+ int currentOffset = 0 ;
284
+ int searchedOffset = startOffset ;
285
+
286
+ BufferPosition startPosition = new BufferPosition ( ) ;
287
+ BufferPosition endPosition = startPosition ;
288
+
289
+ int line = 0 ;
290
+ while ( line < this . FileLines . Count )
291
+ {
292
+ if ( searchedOffset <= currentOffset + this . FileLines [ line ] . Length )
293
+ {
294
+ int column = searchedOffset - currentOffset ;
295
+
296
+ // Have we already found the start position?
297
+ if ( foundStart )
298
+ {
299
+ // Assign the end position and end the search
300
+ endPosition = new BufferPosition ( line + 1 , column + 1 ) ;
301
+ break ;
302
+ }
303
+ else
304
+ {
305
+ startPosition = new BufferPosition ( line + 1 , column + 1 ) ;
306
+
307
+ // Do we only need to find the start position?
308
+ if ( startOffset == endOffset )
309
+ {
310
+ endPosition = startPosition ;
311
+ break ;
312
+ }
313
+ else
314
+ {
315
+ // Since the end offset can be on the same line,
316
+ // skip the line increment and continue searching
317
+ // for the end position
318
+ foundStart = true ;
319
+ searchedOffset = endOffset ;
320
+ continue ;
321
+ }
322
+ }
323
+ }
324
+
325
+ // Increase the current offset and include newline length
326
+ currentOffset += this . FileLines [ line ] . Length + Environment . NewLine . Length ;
327
+ line ++ ;
328
+ }
329
+
330
+ return new BufferRange ( startPosition , endPosition ) ;
331
+ }
332
+
258
333
#endregion
259
334
260
335
#region Private Methods
0 commit comments