mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Merge pull request #12262 from atom/as-delete-obsolete-code
Delete Obsolete Code
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
{times, random} = require 'underscore-plus'
|
||||
randomWords = require 'random-words'
|
||||
TextBuffer = require 'text-buffer'
|
||||
TextEditor = require '../src/text-editor'
|
||||
|
||||
describe "TextEditor", ->
|
||||
[editor, tokenizedBuffer, buffer, steps] = []
|
||||
|
||||
softWrapColumn = 80
|
||||
|
||||
beforeEach ->
|
||||
atom.config.set('editor.softWrapAtPreferredLineLength', true)
|
||||
atom.config.set('editor.preferredLineLength', softWrapColumn)
|
||||
|
||||
it "properly renders soft-wrapped lines when randomly mutated", ->
|
||||
times 10, (i) ->
|
||||
buffer = new TextBuffer
|
||||
editor = atom.workspace.buildTextEditor({buffer})
|
||||
editor.setEditorWidthInChars(80)
|
||||
tokenizedBuffer = editor.tokenizedBuffer
|
||||
steps = []
|
||||
|
||||
times 30, ->
|
||||
randomlyMutateEditor()
|
||||
verifyLines()
|
||||
|
||||
verifyLines = ->
|
||||
{bufferRows, screenLines} = getReferenceScreenLines()
|
||||
for referenceBufferRow, screenRow in bufferRows
|
||||
referenceScreenLine = screenLines[screenRow]
|
||||
actualBufferRow = editor.bufferRowForScreenRow(screenRow)
|
||||
unless actualBufferRow is referenceBufferRow
|
||||
logLines()
|
||||
throw new Error("Invalid buffer row #{actualBufferRow} for screen row #{screenRow}", )
|
||||
|
||||
actualScreenLine = editor.lineTextForScreenRow(screenRow)
|
||||
unless actualScreenLine is referenceScreenLine
|
||||
logLines()
|
||||
throw new Error("Invalid line text at screen row #{screenRow}")
|
||||
|
||||
logLines = ->
|
||||
console.log "==== screen lines ===="
|
||||
editor.logScreenLines()
|
||||
console.log "==== reference lines ===="
|
||||
{bufferRows, screenLines} = getReferenceScreenLines()
|
||||
for bufferRow, screenRow in bufferRows
|
||||
console.log screenRow, bufferRow, screenLines[screenRow].text
|
||||
console.log "==== steps to reproduce this failure: ==="
|
||||
for step in steps
|
||||
console.log 'editor.' + step[0] + '('+ step[1..].map((a) -> JSON.stringify(a)).join(', ') + ')'
|
||||
|
||||
randomlyMutateEditor = ->
|
||||
if Math.random() < .2
|
||||
softWrapped = not editor.isSoftWrapped()
|
||||
steps.push(['setSoftWrapped', softWrapped])
|
||||
editor.setSoftWrapped(softWrapped)
|
||||
else
|
||||
range = getRandomRange()
|
||||
text = getRandomText()
|
||||
steps.push(['setTextInBufferRange', range, text])
|
||||
editor.setTextInBufferRange(range, text)
|
||||
|
||||
getRandomRange = ->
|
||||
startRow = random(0, buffer.getLastRow())
|
||||
startColumn = random(0, buffer.lineForRow(startRow).length)
|
||||
endRow = random(startRow, buffer.getLastRow())
|
||||
endColumn = random(0, buffer.lineForRow(endRow).length)
|
||||
[[startRow, startColumn], [endRow, endColumn]]
|
||||
|
||||
getRandomText = ->
|
||||
text = []
|
||||
max = buffer.getText().split(/\s/).length * 0.75
|
||||
|
||||
times random(5, max), ->
|
||||
if Math.random() < .1
|
||||
text += '\n'
|
||||
else
|
||||
text += " " if /\w$/.test(text)
|
||||
text += randomWords(exactly: 1)
|
||||
text
|
||||
|
||||
getReferenceScreenLines = ->
|
||||
referenceEditor = atom.workspace.buildTextEditor()
|
||||
referenceEditor.setEditorWidthInChars(80)
|
||||
referenceEditor.setText(editor.getText())
|
||||
referenceEditor.setSoftWrapped(editor.isSoftWrapped())
|
||||
|
||||
screenLines = [0..referenceEditor.getLastScreenRow()].map (row) -> referenceEditor.lineTextForScreenRow(row)
|
||||
bufferRows = referenceEditor.bufferRowsForScreenRows(0, referenceEditor.getLastScreenRow())
|
||||
|
||||
{screenLines, bufferRows}
|
||||
@@ -1,104 +0,0 @@
|
||||
RowMap = require '../src/row-map'
|
||||
|
||||
describe "RowMap", ->
|
||||
map = null
|
||||
|
||||
beforeEach ->
|
||||
map = new RowMap
|
||||
|
||||
describe "::screenRowRangeForBufferRow(bufferRow)", ->
|
||||
it "returns the range of screen rows corresponding to the given buffer row", ->
|
||||
map.spliceRegions(0, 0, [
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 1, screenRows: 5}
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 5, screenRows: 1}
|
||||
])
|
||||
|
||||
expect(map.screenRowRangeForBufferRow(0)).toEqual [0, 1]
|
||||
expect(map.screenRowRangeForBufferRow(5)).toEqual [5, 10]
|
||||
expect(map.screenRowRangeForBufferRow(6)).toEqual [10, 11]
|
||||
expect(map.screenRowRangeForBufferRow(11)).toEqual [15, 16]
|
||||
expect(map.screenRowRangeForBufferRow(12)).toEqual [15, 16]
|
||||
expect(map.screenRowRangeForBufferRow(16)).toEqual [16, 17]
|
||||
|
||||
describe "::bufferRowRangeForScreenRow(screenRow)", ->
|
||||
it "returns the range of buffer rows corresponding to the given screen row", ->
|
||||
map.spliceRegions(0, 0, [
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 1, screenRows: 5}
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 5, screenRows: 1}
|
||||
])
|
||||
|
||||
expect(map.bufferRowRangeForScreenRow(0)).toEqual [0, 1]
|
||||
expect(map.bufferRowRangeForScreenRow(5)).toEqual [5, 6]
|
||||
expect(map.bufferRowRangeForScreenRow(6)).toEqual [5, 6]
|
||||
expect(map.bufferRowRangeForScreenRow(10)).toEqual [6, 7]
|
||||
expect(map.bufferRowRangeForScreenRow(14)).toEqual [10, 11]
|
||||
expect(map.bufferRowRangeForScreenRow(15)).toEqual [11, 16]
|
||||
expect(map.bufferRowRangeForScreenRow(16)).toEqual [16, 17]
|
||||
|
||||
describe "::spliceRegions(startBufferRow, bufferRowCount, regions)", ->
|
||||
it "can insert regions when empty", ->
|
||||
regions = [
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 1, screenRows: 5}
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 5, screenRows: 1}
|
||||
]
|
||||
map.spliceRegions(0, 0, regions)
|
||||
expect(map.getRegions()).toEqual regions
|
||||
|
||||
it "can insert wrapped lines into rectangular regions", ->
|
||||
map.spliceRegions(0, 0, [{bufferRows: 10, screenRows: 10}])
|
||||
map.spliceRegions(5, 0, [{bufferRows: 1, screenRows: 3}])
|
||||
expect(map.getRegions()).toEqual [
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 1, screenRows: 3}
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
]
|
||||
|
||||
it "can splice wrapped lines into rectangular regions", ->
|
||||
map.spliceRegions(0, 0, [{bufferRows: 10, screenRows: 10}])
|
||||
map.spliceRegions(5, 1, [{bufferRows: 1, screenRows: 3}])
|
||||
expect(map.getRegions()).toEqual [
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 1, screenRows: 3}
|
||||
{bufferRows: 4, screenRows: 4}
|
||||
]
|
||||
|
||||
it "can splice folded lines into rectangular regions", ->
|
||||
map.spliceRegions(0, 0, [{bufferRows: 10, screenRows: 10}])
|
||||
map.spliceRegions(5, 3, [{bufferRows: 3, screenRows: 1}])
|
||||
expect(map.getRegions()).toEqual [
|
||||
{bufferRows: 5, screenRows: 5}
|
||||
{bufferRows: 3, screenRows: 1}
|
||||
{bufferRows: 2, screenRows: 2}
|
||||
]
|
||||
|
||||
it "can replace folded regions with a folded region that surrounds them", ->
|
||||
map.spliceRegions(0, 0, [
|
||||
{bufferRows: 3, screenRows: 3}
|
||||
{bufferRows: 3, screenRows: 1}
|
||||
{bufferRows: 1, screenRows: 1}
|
||||
{bufferRows: 3, screenRows: 1}
|
||||
{bufferRows: 3, screenRows: 3}
|
||||
])
|
||||
map.spliceRegions(2, 8, [{bufferRows: 8, screenRows: 1}])
|
||||
expect(map.getRegions()).toEqual [
|
||||
{bufferRows: 2, screenRows: 2}
|
||||
{bufferRows: 8, screenRows: 1}
|
||||
{bufferRows: 3, screenRows: 3}
|
||||
]
|
||||
|
||||
it "merges adjacent rectangular regions", ->
|
||||
map.spliceRegions(0, 0, [
|
||||
{bufferRows: 3, screenRows: 3}
|
||||
{bufferRows: 3, screenRows: 1}
|
||||
{bufferRows: 1, screenRows: 1}
|
||||
{bufferRows: 3, screenRows: 1}
|
||||
{bufferRows: 3, screenRows: 3}
|
||||
])
|
||||
|
||||
map.spliceRegions(3, 7, [{bufferRows: 5, screenRows: 5}])
|
||||
@@ -1,120 +0,0 @@
|
||||
{spliceWithArray} = require 'underscore-plus'
|
||||
|
||||
# Used by the display buffer to map screen rows to buffer rows and vice-versa.
|
||||
# This mapping may not be 1:1 due to folds and soft-wraps. This object maintains
|
||||
# an array of regions, which contain `bufferRows` and `screenRows` fields.
|
||||
#
|
||||
# Rectangular Regions:
|
||||
# If a region has the same number of buffer rows and screen rows, it is referred
|
||||
# to as "rectangular", and represents one or more non-soft-wrapped, non-folded
|
||||
# lines.
|
||||
#
|
||||
# Trapezoidal Regions:
|
||||
# If a region has one buffer row and more than one screen row, it represents a
|
||||
# soft-wrapped line. If a region has one screen row and more than one buffer
|
||||
# row, it represents folded lines
|
||||
module.exports =
|
||||
class RowMap
|
||||
constructor: ->
|
||||
@regions = []
|
||||
|
||||
# Public: Returns a copy of all the regions in the map
|
||||
getRegions: ->
|
||||
@regions.slice()
|
||||
|
||||
# Public: Returns an end-row-exclusive range of screen rows corresponding to
|
||||
# the given buffer row. If the buffer row is soft-wrapped, the range may span
|
||||
# multiple screen rows. Otherwise it will span a single screen row.
|
||||
screenRowRangeForBufferRow: (targetBufferRow) ->
|
||||
{region, bufferRows, screenRows} = @traverseToBufferRow(targetBufferRow)
|
||||
|
||||
if region? and region.bufferRows isnt region.screenRows
|
||||
[screenRows, screenRows + region.screenRows]
|
||||
else
|
||||
screenRows += targetBufferRow - bufferRows
|
||||
[screenRows, screenRows + 1]
|
||||
|
||||
# Public: Returns an end-row-exclusive range of buffer rows corresponding to
|
||||
# the given screen row. If the screen row is the first line of a folded range
|
||||
# of buffer rows, the range may span multiple buffer rows. Otherwise it will
|
||||
# span a single buffer row.
|
||||
bufferRowRangeForScreenRow: (targetScreenRow) ->
|
||||
{region, screenRows, bufferRows} = @traverseToScreenRow(targetScreenRow)
|
||||
if region? and region.bufferRows isnt region.screenRows
|
||||
[bufferRows, bufferRows + region.bufferRows]
|
||||
else
|
||||
bufferRows += targetScreenRow - screenRows
|
||||
[bufferRows, bufferRows + 1]
|
||||
|
||||
# Public: If the given buffer row is part of a folded row range, returns that
|
||||
# row range. Otherwise returns a range spanning only the given buffer row.
|
||||
bufferRowRangeForBufferRow: (targetBufferRow) ->
|
||||
{region, bufferRows} = @traverseToBufferRow(targetBufferRow)
|
||||
if region? and region.bufferRows isnt region.screenRows
|
||||
[bufferRows, bufferRows + region.bufferRows]
|
||||
else
|
||||
[targetBufferRow, targetBufferRow + 1]
|
||||
|
||||
# Public: Given a starting buffer row, the number of buffer rows to replace,
|
||||
# and an array of regions of shape {bufferRows: n, screenRows: m}, splices
|
||||
# the regions at the appropriate location in the map. This method is used by
|
||||
# display buffer to keep the map updated when the underlying buffer changes.
|
||||
spliceRegions: (startBufferRow, bufferRowCount, regions) ->
|
||||
endBufferRow = startBufferRow + bufferRowCount
|
||||
{index, bufferRows} = @traverseToBufferRow(startBufferRow)
|
||||
precedingRows = startBufferRow - bufferRows
|
||||
|
||||
count = 0
|
||||
while region = @regions[index + count]
|
||||
count++
|
||||
bufferRows += region.bufferRows
|
||||
if bufferRows >= endBufferRow
|
||||
followingRows = bufferRows - endBufferRow
|
||||
break
|
||||
|
||||
if precedingRows > 0
|
||||
regions.unshift({bufferRows: precedingRows, screenRows: precedingRows})
|
||||
|
||||
if followingRows > 0
|
||||
regions.push({bufferRows: followingRows, screenRows: followingRows})
|
||||
|
||||
spliceWithArray(@regions, index, count, regions)
|
||||
@mergeAdjacentRectangularRegions(index - 1, index + regions.length)
|
||||
|
||||
traverseToBufferRow: (targetBufferRow) ->
|
||||
bufferRows = 0
|
||||
screenRows = 0
|
||||
for region, index in @regions
|
||||
if (bufferRows + region.bufferRows) > targetBufferRow
|
||||
return {region, index, screenRows, bufferRows}
|
||||
bufferRows += region.bufferRows
|
||||
screenRows += region.screenRows
|
||||
{index, screenRows, bufferRows}
|
||||
|
||||
traverseToScreenRow: (targetScreenRow) ->
|
||||
bufferRows = 0
|
||||
screenRows = 0
|
||||
for region, index in @regions
|
||||
if (screenRows + region.screenRows) > targetScreenRow
|
||||
return {region, index, screenRows, bufferRows}
|
||||
bufferRows += region.bufferRows
|
||||
screenRows += region.screenRows
|
||||
{index, screenRows, bufferRows}
|
||||
|
||||
mergeAdjacentRectangularRegions: (startIndex, endIndex) ->
|
||||
for index in [endIndex..startIndex]
|
||||
if 0 < index < @regions.length
|
||||
leftRegion = @regions[index - 1]
|
||||
rightRegion = @regions[index]
|
||||
leftIsRectangular = leftRegion.bufferRows is leftRegion.screenRows
|
||||
rightIsRectangular = rightRegion.bufferRows is rightRegion.screenRows
|
||||
if leftIsRectangular and rightIsRectangular
|
||||
@regions.splice index - 1, 2,
|
||||
bufferRows: leftRegion.bufferRows + rightRegion.bufferRows
|
||||
screenRows: leftRegion.screenRows + rightRegion.screenRows
|
||||
return
|
||||
|
||||
# Public: Returns an array of strings describing the map's regions.
|
||||
inspect: ->
|
||||
for {bufferRows, screenRows} in @regions
|
||||
"#{bufferRows}:#{screenRows}"
|
||||
Reference in New Issue
Block a user