range.js 953 B

1234567891011121314151617181920212223242526272829303132333435
  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
  8. if (inclusive) {
  9. // I consider this a temporary solution until I find an alternaive way to fix the "off by one issue"
  10. --startCol
  11. }
  12. while (start < end) {
  13. let mid = (start + end) >> 1
  14. if (startCol >= lines[mid].endCol) {
  15. start = mid + 1
  16. } else if (endCol < lines[mid].startCol) {
  17. end = mid - 1
  18. } else {
  19. end = mid
  20. while (mid >= 0 && startCol < lines[mid].endCol && endCol >= lines[mid].startCol) {
  21. --mid
  22. }
  23. start = mid + 1
  24. break
  25. }
  26. }
  27. while (end < lines.length && startCol < lines[end].endCol && endCol >= lines[end].startCol) {
  28. ++end
  29. }
  30. return lines.slice(start, end)
  31. }