This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.js
executable file
·188 lines (173 loc) · 6.19 KB
/
init.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See http://opensource.org/licenses/MIT for more information.
* This information must remain intact.
*/
(function(global, $){
var codiad = global.codiad,
scripts = document.getElementsByTagName('script'),
path = scripts[scripts.length-1].src.split('?')[0],
curpath = path.split('/').slice(0, -1).join('/')+'/';
$(function() {
codiad.Bookmark.init();
});
codiad.Bookmark = {
path: curpath,
init: function() {
var _this = this;
amplify.subscribe('active.onOpen', function(path){
var manager = codiad.editor.getActive().commands;
manager.addCommand({
name: 'SetBookmark',
bindKey: {win: 'Ctrl-B', mac: 'Command-B'},
exec: function(e) {
_this.setBookmark();
}
});
manager.addCommand({
name: 'NextBookmark',
bindKey: {win: 'Alt-B', mac: 'Alt-B'},
exec: function(e) {
_this.jumpDown();
}
});
manager.addCommand({
name: 'PrevBookmark',
bindKey: {win: 'Alt-Shift-B', mac: 'Alt-Shift-B'},
exec: function(e) {
_this.jumpUp();
}
});
});
},
//////////////////////////////////////////////////////////
//
// Set or remove bookmark/s
//
// Parameters:
//
// line - {number} - (optional) - Line of new or existing bookmark
//
//////////////////////////////////////////////////////////
setBookmark: function(line) {
var editor = codiad.editor.getActive();
var multi = false;
if ( (typeof(line) == 'undefined') || (line < 0) || (line >= editor.getSession().getLength()) ) {
line = editor.getCursorPosition().row;
multi = editor.inMultiSelectMode;
}
if (multi) {
//Multiselection
var multiRanges = editor.multiSelect.getAllRanges();
for (var i = 0; i < multiRanges.length; i++) {
this.new(multiRanges[i].cursor.row);
}
} else {
//Singleselection
this.new(line);
}
},
//////////////////////////////////////////////////////////
//
// Set or remove breakpoint
//
// Parameters:
//
// line - {number} - (optional) - Line of new or existing breakpoint
//
//////////////////////////////////////////////////////////
new: function(line) {
var session = codiad.editor.getActive().getSession();
if (this.getPoints().indexOf(line) !== -1) {
//Breakpoint already exits, remove it
session.clearBreakpoint(line);
} else {
session.setBreakpoint(line, line + ' bookmark');
}
},
//////////////////////////////////////////////////////////
//
// Jump to next breakpoint
//
//////////////////////////////////////////////////////////
jumpDown: function() {
var points = this.getPoints();
if (points.length === 0) {
return false;
}
var pos = codiad.editor.getActive().getCursorPosition();
if (points.indexOf(pos.row) == -1) {
points.push(pos.row);
}
var index = points.indexOf(pos.row);
if (index == (points.length-1)) {
this.jumpTo(points[0]);
} else {
this.jumpTo(points[index+1]);
}
},
//////////////////////////////////////////////////////////
//
// Jump to previous breakpoint
//
//////////////////////////////////////////////////////////
jumpUp: function() {
var points = this.getPoints();
if (points.length === 0) {
return false;
}
var pos = codiad.editor.getActive().getCursorPosition();
if (points.indexOf(pos.row) == -1) {
points.push(pos.row);
}
var index = points.indexOf(pos.row);
if (index === 0) {
this.jumpTo(points[points.length-1]);
} else {
this.jumpTo(points[index-1]);
}
},
//////////////////////////////////////////////////////////
//
// Jump to a certain line
//
// Parameters
//
// line - {number} - Line to jump to
//
//////////////////////////////////////////////////////////
jumpTo: function(line) {
codiad.editor.gotoLine(line+1);
},
//////////////////////////////////////////////////////////
//
// Get existing bookmark
//
//////////////////////////////////////////////////////////
getPoints: function() {
var session = codiad.editor.getActive().getSession();
var points = session.getBreakpoints();
var buf = [];
$.each(points, function(i, item){
if (typeof(item) != 'undefined') {
item = parseInt(item.replace(' bookmark', ''));
buf.push(item);
}
});
return buf;
},
//////////////////////////////////////////////////////////
//
// Sort bookmarks by line number
//
// Parameters
//
// points - {Array} - Bookmarks to sort
//
//////////////////////////////////////////////////////////
sortPoints: function(points) {
return points.sort(function(a,b){return a-b});
}
};
})(this, jQuery);