Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CodeMirror
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Moritz Aurel Pascal Schubotz
CodeMirror
Commits
7dad9d59
Commit
7dad9d59
authored
8 years ago
by
Marijn Haverbeke
Browse files
Options
Downloads
Patches
Plain Diff
Use ES6 classes for BranchChunk and LeafChunk
parent
cdc7b716
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/model/chunk.js
+49
-40
49 additions, 40 deletions
src/model/chunk.js
with
49 additions
and
40 deletions
src/model/chunk.js
+
49
−
40
View file @
7dad9d59
...
@@ -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
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment