Skip to content
Snippets Groups Projects
Commit 7dad9d59 authored by Marijn Haverbeke's avatar Marijn Haverbeke
Browse files

Use ES6 classes for BranchChunk and LeafChunk

parent cdc7b716
No related branches found
No related tags found
No related merge requests found
...@@ -15,21 +15,22 @@ import { signalLater } from "../util/operation_group" ...@@ -15,21 +15,22 @@ import { signalLater } from "../util/operation_group"
// //
// See also http://marijnhaverbeke.nl/blog/codemirror-line-tree.html // See also http://marijnhaverbeke.nl/blog/codemirror-line-tree.html
export function LeafChunk(lines) { export class LeafChunk {
this.lines = lines constructor(lines) {
this.parent = null this.lines = lines
let height = 0 this.parent = null
for (let i = 0; i < lines.length; ++i) { let height = 0
lines[i].parent = this for (let i = 0; i < lines.length; ++i) {
height += lines[i].height lines[i].parent = this
height += lines[i].height
}
this.height = height
} }
this.height = height
}
LeafChunk.prototype = { chunkSize() { return this.lines.length }
chunkSize: function() { return this.lines.length },
// Remove the n lines at offset 'at'. // Remove the n lines at offset 'at'.
removeInner: function(at, n) { removeInner(at, n) {
for (let i = at, e = at + n; i < e; ++i) { for (let i = at, e = at + n; i < e; ++i) {
let line = this.lines[i] let line = this.lines[i]
this.height -= line.height this.height -= line.height
...@@ -37,41 +38,45 @@ LeafChunk.prototype = { ...@@ -37,41 +38,45 @@ LeafChunk.prototype = {
signalLater(line, "delete") signalLater(line, "delete")
} }
this.lines.splice(at, n) this.lines.splice(at, n)
}, }
// Helper used to collapse a small branch into a single leaf. // Helper used to collapse a small branch into a single leaf.
collapse: function(lines) { collapse(lines) {
lines.push.apply(lines, this.lines) lines.push.apply(lines, this.lines)
}, }
// Insert the given array of lines at offset 'at', count them as // Insert the given array of lines at offset 'at', count them as
// having the given height. // having the given height.
insertInner: function(at, lines, height) { insertInner(at, lines, height) {
this.height += height this.height += height
this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at)) this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at))
for (let i = 0; i < lines.length; ++i) lines[i].parent = this for (let i = 0; i < lines.length; ++i) lines[i].parent = this
}, }
// Used to iterate over a part of the tree. // Used to iterate over a part of the tree.
iterN: function(at, n, op) { iterN(at, n, op) {
for (let e = at + n; at < e; ++at) for (let e = at + n; at < e; ++at)
if (op(this.lines[at])) return true if (op(this.lines[at])) return true
} }
} }
export function BranchChunk(children) { export class BranchChunk {
this.children = children constructor(children) {
let size = 0, height = 0 this.children = children
for (let i = 0; i < children.length; ++i) { let size = 0, height = 0
let ch = children[i] for (let i = 0; i < children.length; ++i) {
size += ch.chunkSize(); height += ch.height let ch = children[i]
ch.parent = this size += ch.chunkSize(); height += ch.height
ch.parent = this
}
this.size = size
this.height = height
this.parent = null
} }
this.size = size
this.height = height
this.parent = null
}
BranchChunk.prototype = { chunkSize() { return this.size }
chunkSize: function() { return this.size },
removeInner: function(at, n) { removeInner(at, n) {
this.size -= n this.size -= n
for (let i = 0; i < this.children.length; ++i) { for (let i = 0; i < this.children.length; ++i) {
let child = this.children[i], sz = child.chunkSize() let child = this.children[i], sz = child.chunkSize()
...@@ -93,11 +98,13 @@ BranchChunk.prototype = { ...@@ -93,11 +98,13 @@ BranchChunk.prototype = {
this.children = [new LeafChunk(lines)] this.children = [new LeafChunk(lines)]
this.children[0].parent = this this.children[0].parent = this
} }
}, }
collapse: function(lines) {
collapse(lines) {
for (let i = 0; i < this.children.length; ++i) this.children[i].collapse(lines) for (let i = 0; i < this.children.length; ++i) this.children[i].collapse(lines)
}, }
insertInner: function(at, lines, height) {
insertInner(at, lines, height) {
this.size += lines.length this.size += lines.length
this.height += height this.height += height
for (let i = 0; i < this.children.length; ++i) { for (let i = 0; i < this.children.length; ++i) {
...@@ -121,9 +128,10 @@ BranchChunk.prototype = { ...@@ -121,9 +128,10 @@ BranchChunk.prototype = {
} }
at -= sz at -= sz
} }
}, }
// When a node has grown, check whether it should be split. // When a node has grown, check whether it should be split.
maybeSpill: function() { maybeSpill() {
if (this.children.length <= 10) return if (this.children.length <= 10) return
let me = this let me = this
do { do {
...@@ -143,8 +151,9 @@ BranchChunk.prototype = { ...@@ -143,8 +151,9 @@ BranchChunk.prototype = {
sibling.parent = me.parent sibling.parent = me.parent
} while (me.children.length > 10) } while (me.children.length > 10)
me.parent.maybeSpill() me.parent.maybeSpill()
}, }
iterN: function(at, n, op) {
iterN(at, n, op) {
for (let i = 0; i < this.children.length; ++i) { for (let i = 0; i < this.children.length; ++i) {
let child = this.children[i], sz = child.chunkSize() let child = this.children[i], sz = child.chunkSize()
if (at < sz) { if (at < sz) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment