|
| 1 | +const {MarkerIndex} = require('superstring') |
| 2 | +const {Emitter} = require('event-kit') |
| 3 | +const Point = require('../../src/point') |
| 4 | +const Range = require('../../src/range') |
| 5 | +const {MAX_BUILT_IN_SCOPE_ID} = require('../../src/constants') |
| 6 | + |
| 7 | +module.exports = |
| 8 | +class TestDecorationLayer { |
| 9 | + constructor (decorations, buffer, random) { |
| 10 | + this.buffer = buffer |
| 11 | + this.random = random |
| 12 | + this.nextMarkerId = MAX_BUILT_IN_SCOPE_ID + 1 |
| 13 | + this.markerIndex = new MarkerIndex() |
| 14 | + this.classNamesByScopeId = new Map() |
| 15 | + this.emitter = new Emitter() |
| 16 | + |
| 17 | + for (let value of decorations) { |
| 18 | + const className = value[0] |
| 19 | + const [rangeStart, rangeEnd] = Array.from(value[1]) |
| 20 | + const markerId = this.getNextMarkerId() |
| 21 | + this.markerIndex.insert(markerId, Point.fromObject(rangeStart), Point.fromObject(rangeEnd)) |
| 22 | + this.classNamesByScopeId.set(markerId, className) |
| 23 | + } |
| 24 | + |
| 25 | + if (this.buffer) { |
| 26 | + this.buffer.registerTextDecorationLayer(this) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + getNextMarkerId () { |
| 31 | + const nextMarkerId = this.nextMarkerId |
| 32 | + this.nextMarkerId += 2 |
| 33 | + return nextMarkerId |
| 34 | + } |
| 35 | + |
| 36 | + classNameForScopeId (scopeId) { |
| 37 | + return this.classNamesByScopeId.get(scopeId) |
| 38 | + } |
| 39 | + |
| 40 | + buildIterator () { |
| 41 | + return new TestDecorationLayerIterator(this) |
| 42 | + } |
| 43 | + |
| 44 | + getInvalidatedRanges () { return this.invalidatedRanges } |
| 45 | + |
| 46 | + onDidInvalidateRange (fn) { |
| 47 | + return this.emitter.on('did-invalidate-range', fn) |
| 48 | + } |
| 49 | + |
| 50 | + emitInvalidateRangeEvent (range) { |
| 51 | + return this.emitter.emit('did-invalidate-range', range) |
| 52 | + } |
| 53 | + |
| 54 | + bufferDidChange ({oldRange, newRange}) { |
| 55 | + this.invalidatedRanges = [Range.fromObject(newRange)] |
| 56 | + const {inside, overlap} = this.markerIndex.splice(oldRange.start, oldRange.getExtent(), newRange.getExtent()) |
| 57 | + overlap.forEach((id) => this.invalidatedRanges.push(this.markerIndex.getRange(id))) |
| 58 | + inside.forEach((id) => this.invalidatedRanges.push(this.markerIndex.getRange(id))) |
| 59 | + |
| 60 | + this.insertRandomDecorations(oldRange, newRange) |
| 61 | + } |
| 62 | + |
| 63 | + insertRandomDecorations (oldRange, newRange) { |
| 64 | + if (this.invalidatedRanges == null) { this.invalidatedRanges = [] } |
| 65 | + |
| 66 | + const j = this.random(5) |
| 67 | + for (let i = 0; i < j; i++) { |
| 68 | + const markerId = this.getNextMarkerId() |
| 69 | + const className = String.fromCharCode('a'.charCodeAt(0) + this.random(27)) |
| 70 | + this.classNamesByScopeId.set(markerId, className) |
| 71 | + const range = this.getRandomRangeCloseTo(oldRange.union(newRange)) |
| 72 | + this.markerIndex.insert(markerId, range.start, range.end) |
| 73 | + this.invalidatedRanges.push(range) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + getRandomRangeCloseTo (range) { |
| 78 | + let minRow |
| 79 | + if (this.random(10) < 7) { |
| 80 | + minRow = this.constrainRow(range.start.row + this.random.intBetween(-20, 20)) |
| 81 | + } else { |
| 82 | + minRow = 0 |
| 83 | + } |
| 84 | + |
| 85 | + let maxRow |
| 86 | + if (this.random(10) < 7) { |
| 87 | + maxRow = this.constrainRow(range.end.row + this.random.intBetween(-20, 20)) |
| 88 | + } else { |
| 89 | + maxRow = this.buffer.getLastRow() |
| 90 | + } |
| 91 | + |
| 92 | + const startRow = this.random.intBetween(minRow, maxRow) |
| 93 | + const endRow = this.random.intBetween(startRow, maxRow) |
| 94 | + const startColumn = this.random(this.buffer.lineForRow(startRow).length + 1) |
| 95 | + const endColumn = this.random(this.buffer.lineForRow(endRow).length + 1) |
| 96 | + return Range(Point(startRow, startColumn), Point(endRow, endColumn)) |
| 97 | + } |
| 98 | + |
| 99 | + constrainRow (row) { |
| 100 | + return Math.max(0, Math.min(this.buffer.getLastRow(), row)) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +class TestDecorationLayerIterator { |
| 105 | + constructor (layer) { |
| 106 | + this.layer = layer |
| 107 | + } |
| 108 | + |
| 109 | + seek (position) { |
| 110 | + const {containingStart, boundaries} = this.layer.markerIndex.findBoundariesAfter(position, Number.MAX_SAFE_INTEGER) |
| 111 | + this.boundaries = boundaries |
| 112 | + this.boundaryIndex = 0 |
| 113 | + return containingStart |
| 114 | + } |
| 115 | + |
| 116 | + moveToSuccessor () { |
| 117 | + return this.boundaryIndex++ |
| 118 | + } |
| 119 | + |
| 120 | + getPosition () { |
| 121 | + const boundary = this.boundaries[this.boundaryIndex] |
| 122 | + return boundary ? boundary.position : Point.INFINITY |
| 123 | + } |
| 124 | + |
| 125 | + getCloseScopeIds () { |
| 126 | + const result = [] |
| 127 | + const boundary = this.boundaries[this.boundaryIndex] |
| 128 | + if (boundary) { |
| 129 | + boundary.ending.forEach((markerId) => { |
| 130 | + if (!boundary.starting.has(markerId)) { |
| 131 | + result.push(markerId) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | + return result |
| 136 | + } |
| 137 | + |
| 138 | + getOpenScopeIds () { |
| 139 | + const result = [] |
| 140 | + const boundary = this.boundaries[this.boundaryIndex] |
| 141 | + if (boundary) { |
| 142 | + boundary.starting.forEach((markerId) => { |
| 143 | + if (!boundary.ending.has(markerId)) { |
| 144 | + result.push(markerId) |
| 145 | + } |
| 146 | + }) |
| 147 | + } |
| 148 | + return result |
| 149 | + } |
| 150 | +} |
0 commit comments