select-highlighted-or-nearest-token.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { expect } from 'chai'
  2. import { EditorState, EditorSelection } from '@codemirror/state'
  3. import { selectHighlightedOrNearestToken } from '../../../../../frontend/js/features/source-editor/utils/select-highlighted-or-nearest-token'
  4. function cursorState(doc: string, cursorPos: number) {
  5. return EditorState.create({
  6. doc,
  7. selection: EditorSelection.cursor(cursorPos),
  8. })
  9. }
  10. function selectionState(doc: string, from: number, to: number) {
  11. return EditorState.create({
  12. doc,
  13. selection: EditorSelection.range(from, to),
  14. })
  15. }
  16. function sliceRange(state: EditorState, range: { from: number; to: number }) {
  17. return state.doc.sliceString(range.from, range.to)
  18. }
  19. describe('selectHighlightedOrNearestToken', function () {
  20. it('returns current highlighted selection when it exists', function () {
  21. const state = selectionState('hello world', 0, 5)
  22. const result = selectHighlightedOrNearestToken(state)
  23. expect(result).to.not.be.null
  24. expect(sliceRange(state, result!)).to.equal('hello')
  25. })
  26. it('selects the token containing the cursor', function () {
  27. const state = cursorState('hello world', 8)
  28. const result = selectHighlightedOrNearestToken(state)
  29. expect(result).to.not.be.null
  30. expect(sliceRange(state, result!)).to.equal('world')
  31. })
  32. it('selects nearest token when cursor is in leading whitespace', function () {
  33. const state = cursorState(' hello world', 0)
  34. const result = selectHighlightedOrNearestToken(state)
  35. expect(result).to.not.be.null
  36. expect(sliceRange(state, result!)).to.equal('hello')
  37. })
  38. it('selects token when cursor is at its boundary', function () {
  39. // pos 5 is immediately after "hello" (from=0, to=5) — pos equals to, so dist is 0
  40. const state = cursorState('hello world', 5)
  41. const result = selectHighlightedOrNearestToken(state)
  42. expect(result).to.not.be.null
  43. expect(sliceRange(state, result!)).to.equal('hello')
  44. })
  45. it('selects nearer token when cursor is between two tokens', function () {
  46. // "hello world" — pos 6, "hello" ends at 5 (dist 1), "world" starts at 8 (dist 2)
  47. const state = cursorState('hello world', 6)
  48. const result = selectHighlightedOrNearestToken(state)
  49. expect(result).to.not.be.null
  50. expect(sliceRange(state, result!)).to.equal('hello')
  51. })
  52. it('selects earlier token when equidistant between two tokens', function () {
  53. // "hello world" — pos 6 is equidistant from "hello" (ends at 5, dist 1) and "world" (starts at 7, dist 1)
  54. const state = cursorState('hello world', 6)
  55. const result = selectHighlightedOrNearestToken(state)
  56. expect(result).to.not.be.null
  57. expect(sliceRange(state, result!)).to.equal('hello')
  58. })
  59. it('returns null when line has no tokens', function () {
  60. const state = cursorState(' \n\t ', 2)
  61. const result = selectHighlightedOrNearestToken(state)
  62. expect(result).to.be.null
  63. })
  64. })