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

Move LeafChunk/BranchChunk back to non-class constructors

So that Doc can do its auto-new trick before calling the
superclass constructor.

Closes #4768
parent 0426274f
No related branches found
No related tags found
No related merge requests found
...@@ -15,19 +15,19 @@ import { signalLater } from "../util/operation_group" ...@@ -15,19 +15,19 @@ 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 class LeafChunk { export function LeafChunk(lines) {
constructor(lines) { this.lines = lines
this.lines = lines this.parent = null
this.parent = null let height = 0
let height = 0 for (let i = 0; i < lines.length; ++i) {
for (let i = 0; i < lines.length; ++i) { lines[i].parent = this
lines[i].parent = this height += lines[i].height
height += lines[i].height
}
this.height = height
} }
this.height = height
}
chunkSize() { return this.lines.length } LeafChunk.prototype = {
chunkSize() { return this.lines.length },
// Remove the n lines at offset 'at'. // Remove the n lines at offset 'at'.
removeInner(at, n) { removeInner(at, n) {
...@@ -38,12 +38,12 @@ export class LeafChunk { ...@@ -38,12 +38,12 @@ export class LeafChunk {
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(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.
...@@ -51,7 +51,7 @@ export class LeafChunk { ...@@ -51,7 +51,7 @@ export class LeafChunk {
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(at, n, op) { iterN(at, n, op) {
...@@ -60,21 +60,21 @@ export class LeafChunk { ...@@ -60,21 +60,21 @@ export class LeafChunk {
} }
} }
export class BranchChunk { export function BranchChunk(children) {
constructor(children) { this.children = children
this.children = children let size = 0, height = 0
let size = 0, height = 0 for (let i = 0; i < children.length; ++i) {
for (let i = 0; i < children.length; ++i) { let ch = children[i]
let ch = children[i] size += ch.chunkSize(); height += ch.height
size += ch.chunkSize(); height += ch.height ch.parent = this
ch.parent = this
}
this.size = size
this.height = height
this.parent = null
} }
this.size = size
this.height = height
this.parent = null
}
chunkSize() { return this.size } BranchChunk.prototype = {
chunkSize() { return this.size },
removeInner(at, n) { removeInner(at, n) {
this.size -= n this.size -= n
...@@ -98,11 +98,11 @@ export class BranchChunk { ...@@ -98,11 +98,11 @@ export class BranchChunk {
this.children = [new LeafChunk(lines)] this.children = [new LeafChunk(lines)]
this.children[0].parent = this this.children[0].parent = this
} }
} },
collapse(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(at, lines, height) { insertInner(at, lines, height) {
this.size += lines.length this.size += lines.length
...@@ -128,7 +128,7 @@ export class BranchChunk { ...@@ -128,7 +128,7 @@ export class BranchChunk {
} }
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() { maybeSpill() {
...@@ -151,7 +151,7 @@ export class BranchChunk { ...@@ -151,7 +151,7 @@ export class BranchChunk {
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(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) {
......
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