forked from ajaxorg/ace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
3078 lines (2713 loc) · 100 KB
/
editor.js
File metadata and controls
3078 lines (2713 loc) · 100 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
"use strict";
/**
* @typedef {import("./virtual_renderer").VirtualRenderer} VirtualRenderer
* @typedef {import("./selection").Selection} Selection
* @typedef {import("../ace-internal").Ace.Point} Point
* @typedef {import("../ace-internal").Ace.SearchOptions} SearchOptions
*/
var oop = require("./lib/oop");
var dom = require("./lib/dom");
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;
var config = require("./config");
var TokenIterator = require("./token_iterator").TokenIterator;
var LineWidgets = require("./line_widgets").LineWidgets;
var GutterKeyboardHandler = require("./keyboard/gutter_handler").GutterKeyboardHandler;
var nls = require("./config").nls;
var clipboard = require("./clipboard");
var keys = require('./lib/keys');
/**
* 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.
**/
class Editor {
/**
* Creates a new `Editor` object.
*
* @param {VirtualRenderer} renderer Associated `VirtualRenderer` that draws everything
* @param {EditSession} [session] The `EditSession` to refer to
* @param {Partial<import("../ace-internal").Ace.EditorOptions>} [options] The default options
**/
constructor(renderer, session, options) {
/**@type{EditSession}*/this.session;
this.$toDestroy = [];
var container = renderer.getContainerElement();
/**@type {HTMLElement & {env?, value?}}*/
this.container = container;
/**@type {VirtualRenderer}*/
this.renderer = renderer;
/**@type {string}*/
this.id = "editor" + (++Editor.$uid);
this.commands = new CommandManager(useragent.isMac ? "mac" : "win", defaultCommands);
if (typeof document == "object") {
this.textInput = new TextInput(renderer.getTextAreaContainer(), this);
this.renderer.textarea = this.textInput.getElement();
// TODO detect touch event support
/**@type {MouseHandler}*/
this.$mouseHandler = new MouseHandler(this);
new FoldHandler(this);
}
/**@type {KeyBinding}*/
this.keyBinding = new KeyBinding(this);
/**@type {Search}*/
this.$search = new Search().set({
wrap: true
});
this.$historyTracker = this.$historyTracker.bind(this);
this.commands.on("exec", this.$historyTracker);
this.$initOperationListeners();
this._$emitInputEvent = lang.delayedCall(function() {
this._signal("input", {});
if (this.session && !this.session.destroyed)
this.session.bgTokenizer.scheduleStart();
}.bind(this));
this.on("change", function(_, _self) {
_self._$emitInputEvent.schedule(31);
});
this.setSession(session || options && options.session || new EditSession(""));
config.resetOptions(this);
if (options)
this.setOptions(options);
config._signal("editor", this);
}
$initOperationListeners() {
this.commands.on("exec", this.startOperation.bind(this), true);
this.commands.on("afterExec", this.endOperation.bind(this), true);
this.$opResetTimer = lang.delayedCall(this.endOperation.bind(this, true));
// todo: add before change events?
this.on("change", function() {
if (!this.curOp) {
this.startOperation();
this.curOp.selectionBefore = this.$lastSel;
}
this.curOp.docChanged = true;
}.bind(this), true);
this.on("changeSelection", function() {
if (!this.curOp) {
this.startOperation();
this.curOp.selectionBefore = this.$lastSel;
}
this.curOp.selectionChanged = true;
}.bind(this), true);
}
startOperation(commandEvent) {
if (this.curOp) {
if (!commandEvent || this.curOp.command)
return;
this.prevOp = this.curOp;
}
if (!commandEvent) {
this.previousCommand = null;
commandEvent = {};
}
this.$opResetTimer.schedule();
/**
* @type {{[key: string]: any;}}
*/
this.curOp = this.session.curOp = {
command: commandEvent.command || {},
args: commandEvent.args,
scrollTop: this.renderer.scrollTop
};
this.curOp.selectionBefore = this.selection.toJSON();
}
/**
* @arg e
*/
endOperation(e) {
if (this.curOp && this.session) {
if (e && e.returnValue === false || !this.session)
return (this.curOp = null);
if (e == true && this.curOp.command && this.curOp.command.name == "mouse")
return;
this._signal("beforeEndOperation");
if (!this.curOp) return;
var command = this.curOp.command;
var scrollIntoView = command && command.scrollIntoView;
if (scrollIntoView) {
switch (scrollIntoView) {
case "center-animate":
scrollIntoView = "animate";
/* fall through */
case "center":
this.renderer.scrollCursorIntoView(null, 0.5);
break;
case "animate":
case "cursor":
this.renderer.scrollCursorIntoView();
break;
case "selectionPart":
var range = this.selection.getRange();
var config = this.renderer.layerConfig;
if (range.start.row >= config.lastRow || range.end.row <= config.firstRow) {
this.renderer.scrollSelectionIntoView(this.selection.anchor, this.selection.lead);
}
break;
default:
break;
}
if (scrollIntoView == "animate")
this.renderer.animateScrolling(this.curOp.scrollTop);
}
var sel = this.selection.toJSON();
this.curOp.selectionAfter = sel;
this.$lastSel = this.selection.toJSON();
// console.log(this.$lastSel+" endOP")
this.session.getUndoManager().addSelection(sel);
this.prevOp = this.curOp;
this.curOp = null;
}
}
/**
* @param e
*/
$historyTracker(e) {
if (!this.$mergeUndoDeltas)
return;
var prev = this.prevOp;
var mergeableCommands = this.$mergeableCommands;
// previous command was the same
var shouldMerge = prev.command && (e.command.name == prev.command.name);
if (e.command.name == "insertstring") {
var text = e.args;
if (this.mergeNextCommand === undefined)
this.mergeNextCommand = true;
shouldMerge = shouldMerge
&& this.mergeNextCommand // previous command allows to coalesce with
&& (!/\s/.test(text) || /\s/.test(prev.args)); // previous insertion was of same type
this.mergeNextCommand = true;
} else {
shouldMerge = shouldMerge
&& mergeableCommands.indexOf(e.command.name) !== -1; // the command is mergeable
}
if (
this.$mergeUndoDeltas != "always"
&& Date.now() - this.sequenceStartTime > 2000
) {
shouldMerge = false; // the sequence is too long
}
if (shouldMerge)
this.session.mergeUndoDeltas = true;
else if (mergeableCommands.indexOf(e.command.name) !== -1)
this.sequenceStartTime = Date.now();
}
/**
* Sets a new key handler, such as "vim" or "windows".
* @param {String | import("../ace-internal").Ace.KeyboardHandler | null} keyboardHandler The new key handler
* @param {() => void} [cb]
**/
setKeyboardHandler(keyboardHandler, cb) {
if (keyboardHandler && typeof keyboardHandler === "string" && keyboardHandler != "ace") {
this.$keybindingId = keyboardHandler;
var _self = this;
config.loadModule(["keybinding", keyboardHandler], function(module) {
if (_self.$keybindingId == keyboardHandler)
_self.keyBinding.setKeyboardHandler(module && module.handler);
cb && cb();
});
} else {
this.$keybindingId = null;
// @ts-ignore
this.keyBinding.setKeyboardHandler(keyboardHandler);
cb && cb();
}
}
/**
* Returns the keyboard handler, such as "vim" or "windows".
* @returns {Object}
**/
getKeyboardHandler() {
return this.keyBinding.getKeyboardHandler();
}
/**
* Sets a new editsession to use. This method also emits the `'changeSession'` event.
* @param {EditSession} [session] The new session to use
**/
setSession(session) {
if (this.session == session)
return;
// make sure operationEnd events are not emitted to wrong session
if (this.curOp) this.endOperation();
this.curOp = {};
var oldSession = this.session;
if (oldSession) {
this.session.off("change", this.$onDocumentChange);
this.session.off("changeMode", this.$onChangeMode);
this.session.off("tokenizerUpdate", this.$onTokenizerUpdate);
this.session.off("changeTabSize", this.$onChangeTabSize);
this.session.off("changeWrapLimit", this.$onChangeWrapLimit);
this.session.off("changeWrapMode", this.$onChangeWrapMode);
this.session.off("changeFold", this.$onChangeFold);
this.session.off("changeFrontMarker", this.$onChangeFrontMarker);
this.session.off("changeBackMarker", this.$onChangeBackMarker);
this.session.off("changeBreakpoint", this.$onChangeBreakpoint);
this.session.off("changeAnnotation", this.$onChangeAnnotation);
this.session.off("changeOverwrite", this.$onCursorChange);
this.session.off("changeScrollTop", this.$onScrollTopChange);
this.session.off("changeScrollLeft", this.$onScrollLeftChange);
var selection = this.session.getSelection();
selection.off("changeCursor", this.$onCursorChange);
selection.off("changeSelection", this.$onSelectionChange);
}
this.session = session;
if (session) {
this.$onDocumentChange = this.onDocumentChange.bind(this);
session.on("change", this.$onDocumentChange);
this.renderer.setSession(session);
this.$onChangeMode = this.onChangeMode.bind(this);
session.on("changeMode", this.$onChangeMode);
this.$onTokenizerUpdate = this.onTokenizerUpdate.bind(this);
session.on("tokenizerUpdate", this.$onTokenizerUpdate);
this.$onChangeTabSize = this.renderer.onChangeTabSize.bind(this.renderer);
session.on("changeTabSize", this.$onChangeTabSize);
this.$onChangeWrapLimit = this.onChangeWrapLimit.bind(this);
session.on("changeWrapLimit", this.$onChangeWrapLimit);
this.$onChangeWrapMode = this.onChangeWrapMode.bind(this);
session.on("changeWrapMode", this.$onChangeWrapMode);
this.$onChangeFold = this.onChangeFold.bind(this);
session.on("changeFold", this.$onChangeFold);
this.$onChangeFrontMarker = this.onChangeFrontMarker.bind(this);
this.session.on("changeFrontMarker", this.$onChangeFrontMarker);
this.$onChangeBackMarker = this.onChangeBackMarker.bind(this);
this.session.on("changeBackMarker", this.$onChangeBackMarker);
this.$onChangeBreakpoint = this.onChangeBreakpoint.bind(this);
this.session.on("changeBreakpoint", this.$onChangeBreakpoint);
this.$onChangeAnnotation = this.onChangeAnnotation.bind(this);
this.session.on("changeAnnotation", this.$onChangeAnnotation);
this.$onCursorChange = this.onCursorChange.bind(this);
this.session.on("changeOverwrite", this.$onCursorChange);
this.$onScrollTopChange = this.onScrollTopChange.bind(this);
this.session.on("changeScrollTop", this.$onScrollTopChange);
this.$onScrollLeftChange = this.onScrollLeftChange.bind(this);
this.session.on("changeScrollLeft", this.$onScrollLeftChange);
this.selection = session.getSelection();
this.selection.on("changeCursor", this.$onCursorChange);
this.$onSelectionChange = this.onSelectionChange.bind(this);
this.selection.on("changeSelection", this.$onSelectionChange);
this.onChangeMode();
this.onCursorChange();
this.onScrollTopChange();
this.onScrollLeftChange();
this.onSelectionChange();
this.onChangeFrontMarker();
this.onChangeBackMarker();
this.onChangeBreakpoint();
this.onChangeAnnotation();
this.session.getUseWrapMode() && this.renderer.adjustWrapLimit();
this.renderer.updateFull();
} else {
this.selection = null;
this.renderer.setSession(session);
}
this._signal("changeSession", {
session: session,
oldSession: oldSession
});
this.curOp = null;
oldSession && oldSession._signal("changeEditor", {oldEditor: this});
session && session._signal("changeEditor", {editor: this});
if (session && !session.destroyed)
session.bgTokenizer.scheduleStart();
}
/**
* Returns the current session being used.
* @returns {EditSession}
**/
getSession() {
return this.session;
}
/**
* Sets the current document to `val`.
* @param {String} val The new value to set for the document
* @param {Number} [cursorPos] Where to set the new value. `undefined` or 0 is selectAll, -1 is at the document start, and 1 is at the end
*
* @returns {String} The current document value
* @related Document.setValue
**/
setValue(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;
}
/**
* Returns the current session's content.
*
* @returns {String}
* @related EditSession.getValue
**/
getValue() {
return this.session.getValue();
}
/**
*
* Returns the currently highlighted selection.
* @returns {Selection} The selection object
**/
getSelection() {
return this.selection;
}
/**
* {:VirtualRenderer.onResize}
* @param {Boolean} [force] If `true`, recomputes the size, even if the height and width haven't changed
* @related VirtualRenderer.onResize
**/
resize(force) {
this.renderer.onResize(force);
}
/**
* {:VirtualRenderer.setTheme}
* @param {string | import("../ace-internal").Ace.Theme} theme The path to a theme
* @param {() => void} [cb] optional callback called when theme is loaded
**/
setTheme(theme, cb) {
this.renderer.setTheme(theme, cb);
}
/**
* {:VirtualRenderer.getTheme}
*
* @returns {String} The set theme
* @related VirtualRenderer.getTheme
**/
getTheme() {
return this.renderer.getTheme();
}
/**
* {:VirtualRenderer.setStyle}
* @param {String} style A class name
* @related VirtualRenderer.setStyle
**/
setStyle(style) {
this.renderer.setStyle(style);
}
/**
* {:VirtualRenderer.unsetStyle}
* @related VirtualRenderer.unsetStyle
* @param {string} style
*/
unsetStyle(style) {
this.renderer.unsetStyle(style);
}
/**
* Gets the current font size of the editor text.
* @return {string}
*/
getFontSize() {
return this.getOption("fontSize") ||
dom.computedStyle(this.container).fontSize;
}
/**
* Set a new font size (in pixels) for the editor text.
* @param {String} size A font size ( _e.g._ "12px")
**/
setFontSize(size) {
this.setOption("fontSize", size);
}
$highlightBrackets() {
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 session = self.session;
if (!session || session.destroyed) return;
if (session.$bracketHighlight) {
session.$bracketHighlight.markerIds.forEach(function(id) {
session.removeMarker(id);
});
session.$bracketHighlight = null;
}
var pos = self.getCursorPosition();
var handler = self.getKeyboardHandler();
var isBackwards = handler && handler.$getDirectionForHighlight && handler.$getDirectionForHighlight(self);
var ranges = session.getMatchingBracketRanges(pos, isBackwards);
if (!ranges) {
var iterator = new TokenIterator(session, pos.row, pos.column);
var token = iterator.getCurrentToken();
if (token && /\b(?:tag-open|tag-name)/.test(token.type)) {
var tagNamesRanges = session.getMatchingTags(pos);
if (tagNamesRanges) ranges = [tagNamesRanges.openTagName, tagNamesRanges.closeTagName];
}
}
if (!ranges && session.$mode.getMatching)
ranges = session.$mode.getMatching(self.session);
if (!ranges) {
if (self.getHighlightIndentGuides()) self.renderer.$textLayer.$highlightIndentGuide();
return;
}
var markerType = "ace_bracket";
if (!Array.isArray(ranges)) {
ranges = [ranges];
} else if (ranges.length == 1) {
markerType = "ace_error_bracket";
}
// show adjacent ranges as one
if (ranges.length == 2) {
if (Range.comparePoints(ranges[0].end, ranges[1].start) == 0)
ranges = [Range.fromPoints(ranges[0].start, ranges[1].end)];
else if (Range.comparePoints(ranges[0].start, ranges[1].end) == 0)
ranges = [Range.fromPoints(ranges[1].start, ranges[0].end)];
}
session.$bracketHighlight = {
ranges: ranges,
markerIds: ranges.map(function(range) {
return session.addMarker(range, markerType, "text");
})
};
if (self.getHighlightIndentGuides()) self.renderer.$textLayer.$highlightIndentGuide();
}, 50);
}
/**
*
* Brings the current `textInput` into focus.
**/
focus() {
this.textInput.focus();
}
/**
* Returns `true` if the current `textInput` is in focus.
* @return {Boolean}
**/
isFocused() {
return this.textInput.isFocused();
}
/**
*
* Blurs the current `textInput`.
**/
blur() {
this.textInput.blur();
}
/**
* Emitted once the editor comes into focus.
**/
onFocus(e) {
if (this.$isFocused)
return;
this.$isFocused = true;
this.renderer.showCursor();
this.renderer.visualizeFocus();
this._emit("focus", e);
}
/**
* Emitted once the editor has been blurred.
**/
onBlur(e) {
if (!this.$isFocused)
return;
this.$isFocused = false;
this.renderer.hideCursor();
this.renderer.visualizeBlur();
this._emit("blur", e);
}
/**
*/
$cursorChange() {
this.renderer.updateCursor();
this.$highlightBrackets();
this.$updateHighlightActiveLine();
}
/**
* Emitted whenever the document is changed.
* @param {import("../ace-internal").Ace.Delta} delta Contains a single property, `data`, which has the delta of changes
**/
onDocumentChange(delta) {
// Rerender and emit "change" event.
var wrap = this.session.$useWrapMode;
var lastRow = (delta.start.row == delta.end.row ? delta.end.row : Infinity);
this.renderer.updateLines(delta.start.row, lastRow, wrap);
this._signal("change", delta);
// Update cursor because tab characters can influence the cursor position.
this.$cursorChange();
}
onTokenizerUpdate(e) {
var rows = e.data;
this.renderer.updateLines(rows.first, rows.last);
}
onScrollTopChange() {
this.renderer.scrollToY(this.session.getScrollTop());
}
onScrollLeftChange() {
this.renderer.scrollToX(this.session.getScrollLeft());
}
/**
* Emitted when the selection changes.
**/
onCursorChange() {
this.$cursorChange();
this._signal("changeSelection");
}
/**
*/
$updateHighlightActiveLine() {
var session = this.getSession();
/**@type {Point|false}*/
var highlight;
if (this.$highlightActiveLine) {
if (this.$selectionStyle != "line" || !this.selection.isMultiLine())
highlight = this.getCursorPosition();
if (this.renderer.theme && this.renderer.theme.$selectionColorConflict && !this.selection.isEmpty())
highlight = false;
if (this.renderer.$maxLines && this.session.getLength() === 1 && !(this.renderer.$minLines > 1))
highlight = false;
}
if (session.$highlightLineMarker && !highlight) {
session.removeMarker(session.$highlightLineMarker.id);
session.$highlightLineMarker = null;
} else if (!session.$highlightLineMarker && highlight) {
var range = new Range(highlight.row, highlight.column, highlight.row, Infinity);
range.id = session.addMarker(range, "ace_active-line", "screenLine");
session.$highlightLineMarker = range;
} else if (highlight) {
session.$highlightLineMarker.start.row = highlight.row;
session.$highlightLineMarker.end.row = highlight.row;
session.$highlightLineMarker.start.column = highlight.column;
session._signal("changeBackMarker");
}
}
/**
*
* @param e
*/
onSelectionChange(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._signal("changeSelection");
}
$getSelectionHighLightRegexp() {
var session = this.session;
var selection = this.getSelectionRange();
if (selection.isEmpty() || selection.isMultiLine())
return;
var startColumn = selection.start.column;
var endColumn = selection.end.column;
var line = session.getLine(selection.start.row);
var needle = line.substring(startColumn, endColumn);
// maximum allowed size for regular expressions in 32000,
// but getting close to it has significant impact on the performance
if (needle.length > 5000 || !/[\w\d]/.test(needle))
return;
var re = this.$search.$assembleRegExp({
wholeWord: true,
caseSensitive: true,
needle: needle
});
var wordWithBoundary = line.substring(startColumn - 1, endColumn + 1);
if (!re.test(wordWithBoundary))
return;
return re;
}
onChangeFrontMarker() {
this.renderer.updateFrontMarkers();
}
onChangeBackMarker() {
this.renderer.updateBackMarkers();
}
onChangeBreakpoint() {
this.renderer.updateBreakpoints();
}
onChangeAnnotation() {
this.renderer.setAnnotations(this.session.getAnnotations());
}
/**
* @param e
*/
onChangeMode (e) {
this.renderer.updateText();
this._emit("changeMode", e);
}
onChangeWrapLimit() {
this.renderer.updateFull();
}
onChangeWrapMode() {
this.renderer.onResize(true);
}
/**
*/
onChangeFold() {
// 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();
}
/**
* Returns the string of text currently highlighted.
* @returns {String}
**/
getSelectedText() {
return this.session.getTextRange(this.getSelectionRange());
}
/**
* Returns the string of text currently highlighted.
* @returns {String}
**/
getCopyText () {
var text = this.getSelectedText();
var nl = this.session.doc.getNewLineCharacter();
var copyLine= false;
if (!text && this.$copyWithEmptySelection) {
copyLine = true;
var ranges = this.selection.getAllRanges();
for (var i = 0; i < ranges.length; i++) {
var range = ranges[i];
if (i && ranges[i - 1].start.row == range.start.row)
continue;
text += this.session.getLine(range.start.row) + nl;
}
}
var e = {text: text};
this._signal("copy", e);
clipboard.lineMode = copyLine ? e.text : false;
return e.text;
}
/**
* Called whenever a text "copy" happens.
**/
onCopy() {
this.commands.exec("copy", this);
}
/**
* Called whenever a text "cut" happens.
**/
onCut() {
this.commands.exec("cut", this);
}
/**
* Called whenever a text "paste" happens.
* @param {String} text The pasted text
* @param {any} event
**/
onPaste(text, event) {
var e = {text: text, event: event};
this.commands.exec("paste", this, e);
}
/**
*
* @param e
* @returns {boolean}
*/
$handlePaste(e) {
if (typeof e == "string")
e = {text: e};
this._signal("paste", e);
var text = e.text;
var lineMode = text === clipboard.lineMode;
var session = this.session;
if (!this.inMultiSelectMode || this.inVirtualSelectionMode) {
if (lineMode)
session.insert({ row: this.selection.lead.row, column: 0 }, text);
else
this.insert(text);
} else if (lineMode) {
this.selection.rangeList.ranges.forEach(function(range) {
session.insert({ row: range.start.row, column: 0 }, text);
});
} else {
var lines = text.split(/\r\n|\r|\n/);
var ranges = this.selection.rangeList.ranges;
var isFullLine = lines.length == 2 && (!lines[0] || !lines[1]);
if (lines.length != ranges.length || isFullLine)
return this.commands.exec("insertstring", this, text);
for (var i = ranges.length; i--;) {
var range = ranges[i];
if (!range.isEmpty())
session.remove(range);
session.insert(range.start, lines[i]);
}
}
}
/**
*
* @param {string | string[]} command
* @param [args]
* @return {boolean}
*/
execCommand(command, args) {
return this.commands.exec(command, this, args);
}
/**
* Inserts `text` into wherever the cursor is pointing.
* @param {String} text The new text to add
* @param {boolean} [pasted]
**/
insert(text, pasted) {
var session = this.session;
var mode = session.getMode();
var cursor = this.getCursorPosition();
if (this.getBehavioursEnabled() && !pasted) {
// Get a transform if the current mode wants one.
var transform = mode.transformAction(session.getState(cursor.row), 'insertion', this, session, text);
if (transform) {
if (text !== transform.text) {
// keep automatic insertion in a separate delta, unless it is in multiselect mode
if (!this.inVirtualSelectionMode) {
this.session.mergeUndoDeltas = false;
this.mergeNextCommand = false;
}
}
text = transform.text;
}
}
if (text == "\t")
text = this.session.getTabString();
// remove selected text
if (!this.selection.isEmpty()) {
var range = this.getSelectionRange();
cursor = this.session.remove(range);
this.clearSelection();
}
else if (this.session.getOverwrite() && text.indexOf("\n") == -1) {
var range = Range.fromPoints(cursor, cursor);
range.end.column += text.length;
this.session.remove(range);
}
if (text == "\n" || text == "\r\n") {
var line = session.getLine(cursor.row);
if (cursor.column > line.search(/\S|$/)) {
var d = line.substr(cursor.column).search(/\S|$/);
session.doc.removeInLine(cursor.row, cursor.column, cursor.column + d);
}
}
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);
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]));
}
}
if (this.$enableAutoIndent) {
if (session.getDocument().isNewLine(text)) {
var lineIndent = mode.getNextLineIndent(lineState, line.slice(0, cursor.column), session.getTabString());
session.insert({row: cursor.row+1, column: 0}, lineIndent);
}
if (shouldOutdent)
mode.autoOutdent(lineState, session, cursor.row);
}
}
autoIndent() {
var session = this.session;
var mode = session.getMode();
var startRow, endRow;
if (this.selection.isEmpty()) {
startRow = 0;
endRow = session.doc.getLength() - 1;
} else {
var selectedRange = this.getSelectionRange();
startRow = selectedRange.start.row;
endRow = selectedRange.end.row;
}
var prevLineState = "";
var prevLine = "";
var lineIndent = "";
var line, currIndent, range;
var tab = session.getTabString();
for (var row = startRow; row <= endRow; row++) {
if (row > 0) {
prevLineState = session.getState(row - 1);
prevLine = session.getLine(row - 1);
lineIndent = mode.getNextLineIndent(prevLineState, prevLine, tab);
}