forked from atom/atom
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgutter-container.js
More file actions
117 lines (104 loc) · 3.04 KB
/
gutter-container.js
File metadata and controls
117 lines (104 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { Emitter } = require('event-kit');
const Gutter = require('./gutter');
module.exports = class GutterContainer {
constructor(textEditor) {
this.gutters = [];
this.textEditor = textEditor;
this.emitter = new Emitter();
}
scheduleComponentUpdate() {
this.textEditor.scheduleComponentUpdate();
}
destroy() {
// Create a copy, because `Gutter::destroy` removes the gutter from
// GutterContainer's @gutters.
const guttersToDestroy = this.gutters.slice(0);
for (let gutter of guttersToDestroy) {
if (gutter.name !== 'line-number') {
gutter.destroy();
}
}
this.gutters = [];
this.emitter.dispose();
}
addGutter(options) {
options = options || {};
const gutterName = options.name;
if (gutterName === null) {
throw new Error('A name is required to create a gutter.');
}
if (this.gutterWithName(gutterName)) {
throw new Error(
'Tried to create a gutter with a name that is already in use.'
);
}
const newGutter = new Gutter(this, options);
let inserted = false;
// Insert the gutter into the gutters array, sorted in ascending order by 'priority'.
// This could be optimized, but there are unlikely to be many gutters.
for (let i = 0; i < this.gutters.length; i++) {
if (this.gutters[i].priority >= newGutter.priority) {
this.gutters.splice(i, 0, newGutter);
inserted = true;
break;
}
}
if (!inserted) {
this.gutters.push(newGutter);
}
this.scheduleComponentUpdate();
this.emitter.emit('did-add-gutter', newGutter);
return newGutter;
}
getGutters() {
return this.gutters.slice();
}
gutterWithName(name) {
for (let gutter of this.gutters) {
if (gutter.name === name) {
return gutter;
}
}
return null;
}
observeGutters(callback) {
for (let gutter of this.getGutters()) {
callback(gutter);
}
return this.onDidAddGutter(callback);
}
onDidAddGutter(callback) {
return this.emitter.on('did-add-gutter', callback);
}
onDidRemoveGutter(callback) {
return this.emitter.on('did-remove-gutter', callback);
}
/*
Section: Private Methods
*/
// Processes the destruction of the gutter. Throws an error if this gutter is
// not within this gutterContainer.
removeGutter(gutter) {
const index = this.gutters.indexOf(gutter);
if (index > -1) {
this.gutters.splice(index, 1);
this.scheduleComponentUpdate();
this.emitter.emit('did-remove-gutter', gutter.name);
} else {
throw new Error(
'The given gutter cannot be removed because it is not ' +
'within this GutterContainer.'
);
}
}
// The public interface is Gutter::decorateMarker or TextEditor::decorateMarker.
addGutterDecoration(gutter, marker, options) {
if (gutter.type === 'line-number') {
options.type = 'line-number';
} else {
options.type = 'gutter';
}
options.gutterName = gutter.name;
return this.textEditor.decorateMarker(marker, options);
}
};