range.js 963 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * ...something resembling a binary search, to find the lowest line within the range.
  3. * And then you could break as soon as the line is longer than the range...
  4. */
  5. module.exports.sliceRange = (lines, startCol, endCol, inclusive = false) => {
  6. let start = 0
  7. let end = lines.length - 1
  8. /**
  9. * I consider this a temporary solution until I find an alternaive way to fix the "off by one issue"
  10. */
  11. const extStartCol = inclusive ? startCol - 1 : startCol
  12. while (start < end) {
  13. const mid = (start + end) >> 1
  14. if (lines[mid].startCol <= startCol && lines[mid].endCol > extStartCol) {
  15. start = mid
  16. end = start
  17. } else if (lines[mid].startCol > startCol) {
  18. end = mid - 1
  19. } else {
  20. start = mid + 1
  21. }
  22. }
  23. if (start === end) {
  24. while (end < lines.length && extStartCol < lines[end].endCol && endCol >= lines[end].startCol) {
  25. ++end
  26. }
  27. return lines.slice(start, end)
  28. } else {
  29. return []
  30. }
  31. }