search.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * search
  3. * @author oldj
  4. * @blog https://oldj.net
  5. */
  6. 'use strict'
  7. export function getNextPos (pos, cursor) {
  8. let {ch, line} = cursor
  9. let mm = 1e10
  10. let target_ch = mm
  11. let target_line = mm
  12. let target_pos = null
  13. pos.map(p => {
  14. let p_ch = p[0].ch
  15. let p_line = p[0].line
  16. if (p_line < line) return
  17. if (p_line === line && p_ch < ch) return
  18. if ((p_line < target_line) || (p_line === target_line && p_ch < target_ch)) {
  19. target_line = p_line
  20. target_ch = p_ch
  21. target_pos = p
  22. }
  23. })
  24. return target_pos
  25. }
  26. export function getPreviousPos (pos, cursor) {
  27. let {ch, line} = cursor
  28. let target_ch = 0
  29. let target_line = 0
  30. let target_pos = null
  31. pos.map(p => {
  32. let p_ch = p[1].ch
  33. let p_line = p[1].line
  34. if (p_line > line) return
  35. if (p_line === line && p_ch >= ch) return
  36. if ((p_line > target_line) || (p_line === target_line && p_ch > target_ch)) {
  37. target_line = p_line
  38. target_ch = p_ch
  39. target_pos = p
  40. }
  41. })
  42. return target_pos
  43. }