forked from github/game-off-2012
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.js
More file actions
2200 lines (1906 loc) · 68.3 KB
/
editor.js
File metadata and controls
2200 lines (1906 loc) · 68.3 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
require("./lib/fixoldbrowsers");
var oop = require("./lib/oop");
var lang = require("./lib/lang");
var useragent = require("./lib/useragent");
var TextInput = require("./keyboard/textinput").TextInput;
var MouseHandler = require("./mouse/mouse_handler").MouseHandler;
var FoldHandler = require("./mouse/fold_handler").FoldHandler;
var KeyBinding = require("./keyboard/keybinding").KeyBinding;
var EditSession = require("./edit_session").EditSession;
var Search = require("./search").Search;
var Range = require("./range").Range;
var EventEmitter = require("./lib/event_emitter").EventEmitter;
var CommandManager = require("./commands/command_manager").CommandManager;
var defaultCommands = require("./commands/default_commands").commands;
/**
* class Editor
*
* The main entry point into the Ace functionality. The `Editor` manages the `EditSession` (which manages `Document`s), as well as the `VirtualRenderer`, which draws everything to the screen. Event sessions dealing with the mouse and keyboard are bubbled up from `Document` to the `Editor`, which decides what to do with them.
*
**/
/**
* new Editor(renderer, session)
* - renderer (VirtualRenderer): Associated `VirtualRenderer` that draws everything
* - session (EditSession): The `EditSession` to refer to
*
* Creates a new `Editor` object.
*
**/
var Editor = function(renderer, session) {
var container = renderer.getContainerElement();
this.container = container;
this.renderer = renderer;
this.commands = new CommandManager(useragent.isMac ? "mac" : "win", defaultCommands);
this.textInput = new TextInput(renderer.getTextAreaContainer(), this);
this.renderer.textarea = this.textInput.getElement();
this.keyBinding = new KeyBinding(this);
// TODO detect touch event support
this.$mouseHandler = new MouseHandler(this);
new FoldHandler(this);
this.$blockScrolling = 0;
this.$search = new Search().set({
wrap: true
});
this.setSession(session || new EditSession(""));
};
(function(){
oop.implement(this, EventEmitter);
/**
* Editor.setKeyboardHandler(keyboardHandler)
*
* Sets a new keyboard handler.
**/
this.setKeyboardHandler = function(keyboardHandler) {
this.keyBinding.setKeyboardHandler(keyboardHandler);
};
/** related to: KeyBinding
* Editor.getKeyboardHandler() -> String
*
* Returns the keyboard handler.
**/
this.getKeyboardHandler = function() {
return this.keyBinding.getKeyboardHandler();
};
/**
* Editor.setSession(session)
* - session (EditSession): The new session to use
*
* Sets a new editsession to use. This method also emits the `'changeSession'` event.
**/
/**
* Editor@changeSession(e)
* - e (Object): An object with two properties, `oldSession` and `session`, that represent the old and new [[EditSession]]s.
*
* Emitted whenever the [[EditSession]] changes.
**/
this.setSession = function(session) {
if (this.session == session)
return;
if (this.session) {
var oldSession = this.session;
this.session.removeEventListener("change", this.$onDocumentChange);
this.session.removeEventListener("changeMode", this.$onChangeMode);
this.session.removeEventListener("tokenizerUpdate", this.$onTokenizerUpdate);
this.session.removeEventListener("changeTabSize", this.$onChangeTabSize);
this.session.removeEventListener("changeWrapLimit", this.$onChangeWrapLimit);
this.session.removeEventListener("changeWrapMode", this.$onChangeWrapMode);
this.session.removeEventListener("onChangeFold", this.$onChangeFold);
this.session.removeEventListener("changeFrontMarker", this.$onChangeFrontMarker);
this.session.removeEventListener("changeBackMarker", this.$onChangeBackMarker);
this.session.removeEventListener("changeBreakpoint", this.$onChangeBreakpoint);
this.session.removeEventListener("changeAnnotation", this.$onChangeAnnotation);
this.session.removeEventListener("changeOverwrite", this.$onCursorChange);
this.session.removeEventListener("changeScrollTop", this.$onScrollTopChange);
this.session.removeEventListener("changeLeftTop", this.$onScrollLeftChange);
var selection = this.session.getSelection();
selection.removeEventListener("changeCursor", this.$onCursorChange);
selection.removeEventListener("changeSelection", this.$onSelectionChange);
}
this.session = session;
this.$onDocumentChange = this.onDocumentChange.bind(this);
session.addEventListener("change", this.$onDocumentChange);
this.renderer.setSession(session);
this.$onChangeMode = this.onChangeMode.bind(this);
session.addEventListener("changeMode", this.$onChangeMode);
this.$onTokenizerUpdate = this.onTokenizerUpdate.bind(this);
session.addEventListener("tokenizerUpdate", this.$onTokenizerUpdate);
this.$onChangeTabSize = this.renderer.onChangeTabSize.bind(this.renderer);
session.addEventListener("changeTabSize", this.$onChangeTabSize);
this.$onChangeWrapLimit = this.onChangeWrapLimit.bind(this);
session.addEventListener("changeWrapLimit", this.$onChangeWrapLimit);
this.$onChangeWrapMode = this.onChangeWrapMode.bind(this);
session.addEventListener("changeWrapMode", this.$onChangeWrapMode);
this.$onChangeFold = this.onChangeFold.bind(this);
session.addEventListener("changeFold", this.$onChangeFold);
this.$onChangeFrontMarker = this.onChangeFrontMarker.bind(this);
this.session.addEventListener("changeFrontMarker", this.$onChangeFrontMarker);
this.$onChangeBackMarker = this.onChangeBackMarker.bind(this);
this.session.addEventListener("changeBackMarker", this.$onChangeBackMarker);
this.$onChangeBreakpoint = this.onChangeBreakpoint.bind(this);
this.session.addEventListener("changeBreakpoint", this.$onChangeBreakpoint);
this.$onChangeAnnotation = this.onChangeAnnotation.bind(this);
this.session.addEventListener("changeAnnotation", this.$onChangeAnnotation);
this.$onCursorChange = this.onCursorChange.bind(this);
this.session.addEventListener("changeOverwrite", this.$onCursorChange);
this.$onScrollTopChange = this.onScrollTopChange.bind(this);
this.session.addEventListener("changeScrollTop", this.$onScrollTopChange);
this.$onScrollLeftChange = this.onScrollLeftChange.bind(this);
this.session.addEventListener("changeScrollLeft", this.$onScrollLeftChange);
this.selection = session.getSelection();
this.selection.addEventListener("changeCursor", this.$onCursorChange);
this.$onSelectionChange = this.onSelectionChange.bind(this);
this.selection.addEventListener("changeSelection", this.$onSelectionChange);
this.onChangeMode();
this.$blockScrolling += 1;
this.onCursorChange();
this.$blockScrolling -= 1;
this.onScrollTopChange();
this.onScrollLeftChange();
this.onSelectionChange();
this.onChangeFrontMarker();
this.onChangeBackMarker();
this.onChangeBreakpoint();
this.onChangeAnnotation();
this.session.getUseWrapMode() && this.renderer.adjustWrapLimit();
this.renderer.updateFull();
this._emit("changeSession", {
session: session,
oldSession: oldSession
});
};
/**
* Editor.getSession() -> EditSession
*
* Returns the current session being used.
**/
this.getSession = function() {
return this.session;
};
/** related to: Document.setValue
* Editor.setValue(val [,cursorPos]) -> String
* - val (String): The new value to set for the document
* - cursorPos (Number): Where to set the new value. `undefined` or 0 is selectAll, -1 is at the document start, and 1 is at the end
*
* Sets the current document to `val`.
**/
this.setValue = function(val, cursorPos) {
this.session.doc.setValue(val);
if (!cursorPos)
this.selectAll();
else if (cursorPos == 1)
this.navigateFileEnd();
else if (cursorPos == -1)
this.navigateFileStart();
return val;
};
/** related to: EditSession.getValue
* Editor.getValue() -> String
*
* Returns the current session's content.
**/
this.getValue = function() {
return this.session.getValue();
};
/**
* Editor.getSelection() -> String
*
* Returns the currently highlighted selection.
**/
this.getSelection = function() {
return this.selection;
};
/** related to: VirtualRenderer.onResize
* Editor.resize(force)
* - force (Boolean): If `true`, recomputes the size, even if the height and width haven't changed
*
* {:VirtualRenderer.onResize}
**/
this.resize = function(force) {
this.renderer.onResize(force);
};
/**
* Editor.setTheme(theme)
* - theme (String): The path to a theme
*
* {:VirtualRenderer.setTheme}
**/
this.setTheme = function(theme) {
this.renderer.setTheme(theme);
};
/** related to: VirtualRenderer.getTheme
* Editor.getTheme() -> String
*
* {:VirtualRenderer.getTheme}
**/
this.getTheme = function() {
return this.renderer.getTheme();
};
/** related to: VirtualRenderer.setStyle
* Editor.setStyle(style)
* - style (String): A class name
*
* {:VirtualRenderer.setStyle}
**/
this.setStyle = function(style) {
this.renderer.setStyle(style);
};
/** related to: VirtualRenderer.unsetStyle
* Editor.unsetStyle(style)
*
* {:VirtualRenderer.unsetStyle}
**/
this.unsetStyle = function(style) {
this.renderer.unsetStyle(style);
};
/**
* Editor.setFontSize(size)
* - size (Number): A font size
*
* Set a new font size (in pixels) for the editor text.
**/
this.setFontSize = function(size) {
this.container.style.fontSize = size;
this.renderer.updateFontSize();
};
/** internal, hide
* Editor.$highlightBrackets()
*
**/
this.$highlightBrackets = function() {
if (this.session.$bracketHighlight) {
this.session.removeMarker(this.session.$bracketHighlight);
this.session.$bracketHighlight = null;
}
if (this.$highlightPending) {
return;
}
// perform highlight async to not block the browser during navigation
var self = this;
this.$highlightPending = true;
setTimeout(function() {
self.$highlightPending = false;
var pos = self.session.findMatchingBracket(self.getCursorPosition());
if (pos) {
var range = new Range(pos.row, pos.column, pos.row, pos.column+1);
self.session.$bracketHighlight = self.session.addMarker(range, "ace_bracket", "text");
}
}, 50);
};
/**
* Editor.focus()
*
* Brings the current `textInput` into focus.
**/
this.focus = function() {
// Safari needs the timeout
// iOS and Firefox need it called immediately
// to be on the save side we do both
var _self = this;
setTimeout(function() {
_self.textInput.focus();
});
this.textInput.focus();
};
/**
* Editor.isFocused() -> Boolean
*
* Returns true if the current `textInput` is in focus.
**/
this.isFocused = function() {
return this.textInput.isFocused();
};
/**
* Editor.blur()
*
* Blurs the current `textInput`.
**/
this.blur = function() {
this.textInput.blur();
};
/**
* Editor@focus()
*
* Emitted once the editor comes into focus.
**/
this.onFocus = function() {
if (this.$isFocused)
return;
this.$isFocused = true;
this.renderer.showCursor();
this.renderer.visualizeFocus();
this._emit("focus");
};
/**
* Editor@blur()
*
* Emitted once the editor has been blurred.
**/
this.onBlur = function() {
if (!this.$isFocused)
return;
this.$isFocused = false;
this.renderer.hideCursor();
this.renderer.visualizeBlur();
this._emit("blur");
};
this.$cursorChange = function() {
this.renderer.updateCursor();
};
/**
* Editor@change(e)
* - e (Object): Contains a single property, `data`, which has the delta of changes
*
* Emitted whenever the document is changed.
*
**/
this.onDocumentChange = function(e) {
var delta = e.data;
var range = delta.range;
var lastRow;
if (range.start.row == range.end.row && delta.action != "insertLines" && delta.action != "removeLines")
lastRow = range.end.row;
else
lastRow = Infinity;
this.renderer.updateLines(range.start.row, lastRow);
this._emit("change", e);
// update cursor because tab characters can influence the cursor position
this.$cursorChange();
};
this.onTokenizerUpdate = function(e) {
var rows = e.data;
this.renderer.updateLines(rows.first, rows.last);
};
this.onScrollTopChange = function() {
this.renderer.scrollToY(this.session.getScrollTop());
};
this.onScrollLeftChange = function() {
this.renderer.scrollToX(this.session.getScrollLeft());
};
/**
* Editor@changeSelection()
*
* Emitted when the selection changes.
*
**/
this.onCursorChange = function() {
this.$cursorChange();
if (!this.$blockScrolling) {
this.renderer.scrollCursorIntoView();
}
this.$highlightBrackets();
this.$updateHighlightActiveLine();
this._emit("changeSelection");
};
/** internal, hide
* Editor.$updateHighlightActiveLine()
*
*
**/
this.$updateHighlightActiveLine = function() {
var session = this.getSession();
var highlight;
if (this.$highlightActiveLine) {
if ((this.$selectionStyle != "line" || !this.selection.isMultiLine()))
highlight = this.getCursorPosition();
}
if (session.$highlightLineMarker && !highlight) {
session.removeMarker(session.$highlightLineMarker.id);
session.$highlightLineMarker = null;
} else if (!session.$highlightLineMarker && highlight) {
session.$highlightLineMarker = session.highlightLines(highlight.row, highlight.row, "ace_active-line");
} else if (highlight) {
session.$highlightLineMarker.start.row = highlight.row;
session.$highlightLineMarker.end.row = highlight.row;
session._emit("changeBackMarker");
}
};
this.onSelectionChange = function(e) {
var session = this.session;
if (session.$selectionMarker) {
session.removeMarker(session.$selectionMarker);
}
session.$selectionMarker = null;
if (!this.selection.isEmpty()) {
var range = this.selection.getRange();
var style = this.getSelectionStyle();
session.$selectionMarker = session.addMarker(range, "ace_selection", style);
} else {
this.$updateHighlightActiveLine();
}
var re = this.$highlightSelectedWord && this.$getSelectionHighLightRegexp()
this.session.highlight(re);
this._emit("changeSelection");
};
this.$getSelectionHighLightRegexp = function() {
var session = this.session;
var selection = this.getSelectionRange();
if (selection.isEmpty() || selection.isMultiLine())
return;
var startOuter = selection.start.column - 1;
var endOuter = selection.end.column + 1;
var line = session.getLine(selection.start.row);
var lineCols = line.length;
var needle = line.substring(Math.max(startOuter, 0),
Math.min(endOuter, lineCols));
// Make sure the outer characters are not part of the word.
if ((startOuter >= 0 && /^[\w\d]/.test(needle)) ||
(endOuter <= lineCols && /[\w\d]$/.test(needle)))
return;
needle = line.substring(selection.start.column, selection.end.column);
if (!/^[\w\d]+$/.test(needle))
return;
var re = this.$search.$assembleRegExp({
wholeWord: true,
caseSensitive: true,
needle: needle
});
return re;
};
this.onChangeFrontMarker = function() {
this.renderer.updateFrontMarkers();
};
this.onChangeBackMarker = function() {
this.renderer.updateBackMarkers();
};
this.onChangeBreakpoint = function() {
this.renderer.updateBreakpoints();
};
this.onChangeAnnotation = function() {
this.renderer.setAnnotations(this.session.getAnnotations());
};
this.onChangeMode = function() {
this.renderer.updateText();
};
this.onChangeWrapLimit = function() {
this.renderer.updateFull();
};
this.onChangeWrapMode = function() {
this.renderer.onResize(true);
};
this.onChangeFold = function() {
// Update the active line marker as due to folding changes the current
// line range on the screen might have changed.
this.$updateHighlightActiveLine();
// TODO: This might be too much updating. Okay for now.
this.renderer.updateFull();
};
/**
* Editor.getCopyText() -> String
*
* Returns the string of text currently highlighted.
**/
/**
* Editor@copy(text)
* - text (String): The copied text
*
* Emitted when text is copied.
**/
this.getCopyText = function() {
var text = "";
if (!this.selection.isEmpty())
text = this.session.getTextRange(this.getSelectionRange());
this._emit("copy", text);
return text;
};
/**
* Editor.onCopy()
*
* Called whenever a text "copy" happens.
**/
this.onCopy = function() {
this.commands.exec("copy", this);
};
/**
* Editor.onCut()
*
* called whenever a text "cut" happens.
**/
this.onCut = function() {
this.commands.exec("cut", this);
};
/**
* Editor.onPaste(text)
* - text (String): The pasted text
*
* Called whenever a text "paste" happens.
**/
/**
* Editor@paste(text)
* - text (String): The pasted text
*
* Emitted when text is pasted.
**/
this.onPaste = function(text) {
// todo this should change when paste becomes a command
if (this.$readOnly)
return;
this._emit("paste", text);
this.insert(text);
};
this.execCommand = function(command, args) {
this.commands.exec(command, this, args);
};
/**
* Editor.insert(text)
* - text (String): The new text to add
*
* Inserts `text` into wherever the cursor is pointing.
**/
this.insert = function(text) {
var session = this.session;
var mode = session.getMode();
var cursor = this.getCursorPosition();
if (this.getBehavioursEnabled()) {
// Get a transform if the current mode wants one.
var transform = mode.transformAction(session.getState(cursor.row), 'insertion', this, session, text);
if (transform)
text = transform.text;
}
text = text.replace("\t", this.session.getTabString());
// remove selected text
if (!this.selection.isEmpty()) {
cursor = this.session.remove(this.getSelectionRange());
this.clearSelection();
}
else if (this.session.getOverwrite()) {
var range = new Range.fromPoints(cursor, cursor);
range.end.column += text.length;
this.session.remove(range);
}
this.clearSelection();
var start = cursor.column;
var lineState = session.getState(cursor.row);
var line = session.getLine(cursor.row);
var shouldOutdent = mode.checkOutdent(lineState, line, text);
var end = session.insert(cursor, text);
if (transform && transform.selection) {
if (transform.selection.length == 2) { // Transform relative to the current column
this.selection.setSelectionRange(
new Range(cursor.row, start + transform.selection[0],
cursor.row, start + transform.selection[1]));
} else { // Transform relative to the current row.
this.selection.setSelectionRange(
new Range(cursor.row + transform.selection[0],
transform.selection[1],
cursor.row + transform.selection[2],
transform.selection[3]));
}
}
// TODO disabled multiline auto indent
// possibly doing the indent before inserting the text
// if (cursor.row !== end.row) {
if (session.getDocument().isNewLine(text)) {
var lineIndent = mode.getNextLineIndent(lineState, line.slice(0, cursor.column), session.getTabString());
this.moveCursorTo(cursor.row+1, 0);
var size = session.getTabSize();
var minIndent = Number.MAX_VALUE;
for (var row = cursor.row + 1; row <= end.row; ++row) {
var indent = 0;
line = session.getLine(row);
for (var i = 0; i < line.length; ++i)
if (line.charAt(i) == '\t')
indent += size;
else if (line.charAt(i) == ' ')
indent += 1;
else
break;
if (/[^\s]/.test(line))
minIndent = Math.min(indent, minIndent);
}
for (var row = cursor.row + 1; row <= end.row; ++row) {
var outdent = minIndent;
line = session.getLine(row);
for (var i = 0; i < line.length && outdent > 0; ++i)
if (line.charAt(i) == '\t')
outdent -= size;
else if (line.charAt(i) == ' ')
outdent -= 1;
session.remove(new Range(row, 0, row, i));
}
session.indentRows(cursor.row + 1, end.row, lineIndent);
}
if (shouldOutdent)
mode.autoOutdent(lineState, session, cursor.row);
};
this.onTextInput = function(text) {
this.keyBinding.onTextInput(text);
};
this.onCommandKey = function(e, hashId, keyCode) {
this.keyBinding.onCommandKey(e, hashId, keyCode);
};
/** related to: EditSession.setOverwrite
* Editor.setOverwrite(overwrite)
* - overwrite (Boolean): Defines wheter or not to set overwrites
*
* Pass in `true` to enable overwrites in your session, or `false` to disable. If overwrites is enabled, any text you enter will type over any text after it. If the value of `overwrite` changes, this function also emites the `changeOverwrite` event.
*
**/
this.setOverwrite = function(overwrite) {
this.session.setOverwrite(overwrite);
};
/** related to: EditSession.getOverwrite
* Editor.getOverwrite() -> Boolean
*
* Returns `true` if overwrites are enabled; `false` otherwise.
**/
this.getOverwrite = function() {
return this.session.getOverwrite();
};
/** related to: EditSession.toggleOverwrite
* Editor.toggleOverwrite()
*
* Sets the value of overwrite to the opposite of whatever it currently is.
**/
this.toggleOverwrite = function() {
this.session.toggleOverwrite();
};
/**
* Editor.setScrollSpeed(speed)
* - speed (Number): A value indicating the new speed
*
* Sets how fast the mouse scrolling should do.
*
**/
this.setScrollSpeed = function(speed) {
this.$mouseHandler.setScrollSpeed(speed);
};
/**
* Editor.getScrollSpeed() -> Number
*
* Returns the value indicating how fast the mouse scroll speed is.
**/
this.getScrollSpeed = function() {
return this.$mouseHandler.getScrollSpeed();
};
/**
* Editor.setDragDelay(dragDelay)
* - dragDelay (Number): A value indicating the new delay
*
* Sets the delay (in milliseconds) of the mouse drag.
*
**/
this.setDragDelay = function(dragDelay) {
this.$mouseHandler.setDragDelay(dragDelay);
};
/**
* Editor.getDragDelay() -> Number
*
* Returns the current mouse drag delay.
**/
this.getDragDelay = function() {
return this.$mouseHandler.getDragDelay();
};
this.$selectionStyle = "line";
/**
* Editor.setSelectionStyle(style)
* - style (String): The new selection style
*
* Indicates how selections should occur. By default, selections are set to "line". This function also emits the `'changeSelectionStyle'` event.
*
**/
/**
* Editor@changeSelectionStyle(data)
* - data (Object): Contains one property, `data`, which indicates the new selection style
*
* Emitted when the selection style changes, via [[Editor.setSelectionStyle]].
*
**/
this.setSelectionStyle = function(style) {
if (this.$selectionStyle == style) return;
this.$selectionStyle = style;
this.onSelectionChange();
this._emit("changeSelectionStyle", {data: style});
};
/**
* Editor.getSelectionStyle() -> String
*
* Returns the current selection style.
**/
this.getSelectionStyle = function() {
return this.$selectionStyle;
};
this.$highlightActiveLine = true;
/**
* Editor.setHighlightActiveLine(shouldHighlight)
* - shouldHighlight (Boolean): Set to `true` to highlight the current line
*
* Determines whether or not the current line should be highlighted.
*
**/
this.setHighlightActiveLine = function(shouldHighlight) {
if (this.$highlightActiveLine == shouldHighlight)
return;
this.$highlightActiveLine = shouldHighlight;
this.$updateHighlightActiveLine();
};
/**
* Editor.getHighlightActiveLine() -> Boolean
*
* Returns `true` if current lines are always highlighted.
**/
this.getHighlightActiveLine = function() {
return this.$highlightActiveLine;
};
this.$highlightGutterLine = true;
this.setHighlightGutterLine = function(shouldHighlight) {
if (this.$highlightGutterLine == shouldHighlight)
return;
this.renderer.setHighlightGutterLine(shouldHighlight);
this.$highlightGutterLine = shouldHighlight;
};
this.getHighlightGutterLine = function() {
return this.$highlightGutterLine;
};
this.$highlightSelectedWord = true;
/**
* Editor.setHighlightSelectedWord(shouldHighlight)
* - shouldHighlight (Boolean): Set to `true` to highlight the currently selected word
*
* Determines if the currently selected word should be highlighted.
**/
this.setHighlightSelectedWord = function(shouldHighlight) {
if (this.$highlightSelectedWord == shouldHighlight)
return;
this.$highlightSelectedWord = shouldHighlight;
this.$onSelectionChange();
};
/**
* Editor.getHighlightSelectedWord() -> Boolean
*
* Returns `true` if currently highlighted words are to be highlighted.
**/
this.getHighlightSelectedWord = function() {
return this.$highlightSelectedWord;
};
this.setAnimatedScroll = function(shouldAnimate){
this.renderer.setAnimatedScroll(shouldAnimate);
};
this.getAnimatedScroll = function(){
return this.renderer.getAnimatedScroll();
};
/**
* Editor.setShowInvisibles(showInvisibles)
* - showInvisibles (Boolean): Specifies whether or not to show invisible characters
*
* If `showInvisibiles` is set to `true`, invisible characters—like spaces or new lines—are show in the editor.
**/
this.setShowInvisibles = function(showInvisibles) {
this.renderer.setShowInvisibles(showInvisibles);
};
/**
* Editor.getShowInvisibles() -> Boolean
*
* Returns `true` if invisible characters are being shown.
**/
this.getShowInvisibles = function() {
return this.renderer.getShowInvisibles();
};
this.setDisplayIndentGuides = function(display) {
this.renderer.setDisplayIndentGuides(display);
};
this.getDisplayIndentGuides = function() {
return this.renderer.getDisplayIndentGuides();
};
/**
* Editor.setShowPrintMargin(showPrintMargin)
* - showPrintMargin (Boolean): Specifies whether or not to show the print margin
*
* If `showPrintMargin` is set to `true`, the print margin is shown in the editor.
**/
this.setShowPrintMargin = function(showPrintMargin) {
this.renderer.setShowPrintMargin(showPrintMargin);
};
/**
* Editor.getShowPrintMargin() -> Boolean
*
* Returns `true` if the print margin is being shown.
**/
this.getShowPrintMargin = function() {
return this.renderer.getShowPrintMargin();
};
/**
* Editor.setPrintMarginColumn(showPrintMargin)
* - showPrintMargin (Number): Specifies the new print margin
*
* Sets the column defining where the print margin should be.
*
**/
this.setPrintMarginColumn = function(showPrintMargin) {
this.renderer.setPrintMarginColumn(showPrintMargin);
};
/**
* Editor.getPrintMarginColumn() -> Number
*
* Returns the column number of where the print margin is.
**/
this.getPrintMarginColumn = function() {
return this.renderer.getPrintMarginColumn();
};