-
Notifications
You must be signed in to change notification settings - Fork 25
/
editor.js
3107 lines (2937 loc) · 143 KB
/
editor.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
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
/**Created by Fry on 10/29/15.*/
/* this has bugs. Conclusion: codemirror not really deignd for npm, too many plug-ins, etc.
so load the old fashion way in index_obsolete.html
window.CodeMirror = require("codemirror") //now in index_obsolete.html because this didn't work
require("codemirror/mode/javascript/javascript")
//require("eslint")
//require("codemirror/addon/lint/lint.js")
require("codemirror-lint-eslint")
require("codemirror/addon/dialog/dialog.js") //maybe used by find ???
require("codemirror/addon/search/searchcursor.js")
require("codemirror/addon/search/search.js")
require("codemirror/addon/edit/matchbrackets.js")
require("codemirror/addon/fold/foldcode.js")
require("codemirror/addon/fold/foldgutter.js")
require("codemirror/addon/fold/brace-fold.js")
require("codemirror/addon/fold/comment-fold.js")
*/
var myCodeMirror
function Editor(){} //just a namespace of *some* internal fns
Editor.current_file_path = null //could be "new buffer" or "/Users/.../foo.fs"
Editor.view = "JS"
Editor.init_editor = function(){
myCodeMirror = CodeMirror.fromTextArea(js_textarea_id,
{lineNumbers: true,
//lineWrapping: true,
mode: "javascript",
matchBrackets: true,
foldGutter: true,
//extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }}, works ony when on line that can be folded
gutters: ["CodeMirror-linenumbers", "CodeMirror-lint-markers", "CodeMirror-foldgutter"],
lint: true,
smartIndent: false, //default is true but that screws up a lot. false is suppose to
//indent each line to the line above it when you hit Return
indentUnit: 4, //default is 2. Using 4 makes it same size as tab, then cmd(mac or ctrl(PC) open square will unindent by this amount.
extraKeys: //undo z and select_all (a) work automaticaly with proper ctrl and cmd bindings for win and mac
((operating_system === "mac") ? //the "Alt" key on a Mac is labeled "option" on mac keyboard.
{"Alt-Left": Series.ts_or_replace_sel_left,
"Alt-Right": Series.ts_or_replace_sel_right,
"Shift-Alt-Right": Series.ts_sel_shift_right, //no non ts semantics see above for why cuttong this
"Alt-Up": Series.ts_or_replace_sel_up,
"Alt-Down": Series.ts_or_replace_sel_down,
"Cmd-E": eval_button_action, //the correct Cmd-e doesn't work
"Cmd-J": Editor.insert_new_job,
"Cmd-N": Editor.edit_new_file,
"Cmd-O": Editor.open_on_dde_computer,
"Cmd-R": Editor.move_to_instruction,
"Cmd-S": Editor.save, //mac
"Shift-Cmd-S": Editor.save_as
}: //"win" and "linux"
{"Alt-Left": Series.ts_or_replace_sel_left,
"Alt-Right": Series.ts_or_replace_sel_right,
"Shift-Alt-Right": Series.ts_sel_shift_right, //no non ts semantics decided to cut this as is uncommonly used and shift right is "continue selection" in normal test editor and conde mirror AND alt_shift_right too hairy to remember.
"Alt-Up": Series.ts_or_replace_sel_up,
"Alt-Down": Series.ts_or_replace_sel_down,
"Ctrl-E": eval_button_action, //the correct Cmd-e doesn't work
"Ctrl-J": Editor.insert_new_job,
"Ctrl-N": Editor.edit_new_file,
"Ctrl-O": Editor.open_on_dde_computer,
"Ctrl-R": Editor.move_to_instruction,
"Ctrl-S": Editor.save, //windows
"Shift-Ctrl-S": Editor.save_as
}
)
});
undo_id.onclick = Editor.undo
set_menu_string(undo_id, "Undo", "z")
redo_id.onclick = function(){myCodeMirror.getDoc().redo()}
find_id.onclick = function(){CodeMirror.commands.findPersistent(myCodeMirror)}
set_menu_string(find_id, "Find", "f")
find_next_id.onclick = function(){CodeMirror.commands.findNext(myCodeMirror)}
set_menu_string(find_next_id, "Find Next", "g")
find_prev_id.onclick = function(){CodeMirror.commands.findPrev(myCodeMirror)}
set_menu_string(find_prev_id, "Find Prev shift", "g")
set_menu_string(save_as_id, "Save As... shift", "s")
replace_id.onclick = function(){CodeMirror.commands.replace(myCodeMirror)} //allows user to also replace all.
fold_all_id.onclick = function(){CodeMirror.commands.foldAll(myCodeMirror)}
unfold_all_id.onclick = function(){CodeMirror.commands.unfoldAll(myCodeMirror)}
select_expr_id.onclick = function(){Editor.select_expr()}
select_all_id.onclick = function(){
CodeMirror.commands.selectAll(myCodeMirror)
myCodeMirror.focus()
}
indent_selection_id.onclick = function(){CodeMirror.commands.indentAuto(myCodeMirror)}
pretty_print_id.onclick = function(){
if(Editor.view === "JS") {
let js = Editor.get_javascript("auto")
js = Editor.pretty_print(js)
if(Editor.is_selection()){
Editor.replace_selection(js)
}
else {
Editor.set_javascript(js)
}
}
else if(Editor.view === "HCA"){
HCA.pretty_print()
}
else{
warning("DDE can't pretty print the editor with view type: " + Editor.view)
}
}
set_menu_string(select_all_id, "Select All", "a")
myCodeMirror.on("mousedown", //"mousedown",
function(cm, mouse_event){
if(mouse_event.altKey){
var line_char = myCodeMirror.coordsChar({left: mouse_event.x, top: mouse_event.y})
myCodeMirror.getDoc().setCursor(line_char)
if (Editor.select_expr()){
setTimeout(function(){ //without setTimeout, the sel isn't really selected by the tme we call MakeInstruction.show
let sel = Editor.get_any_selection()
if((typeof(sel) === "string") &&
(sel !== "") &&
!sel.startsWith("new TestSuite")) { //exclude new TestSuite because usually when you're
//selecting a testsuite you're doing it to run it, and running it should
//remain with Simulator showing, not switch to Make Instruction.
//MakeInstruction.show(sel)
show_in_misc_pane("Make Instruction", sel)
}
}, 200)
mouse_event.preventDefault()
}
}
//I didn't need this setTimeout in ChromeApps,
//I could just call Editor.show_identifier_info directly
//but in Electron when I do that, you have to click twice
//to get the right click help to show up.
//Wrapping the setTimeout fixes that bug.
setTimeout(function() {Editor.show_identifier_info()}, 1)
myCodeMirror.focus()
cmd_input_clicked_on_last = false
})
myCodeMirror.getDoc().on("change", Editor.mark_as_changed)
//document.querySelector(".CodeMirror").addEventListener("mouseup", function(){
// out("got mouseup")
//})
}
Editor.pretty_print = function(js){
var beautify = require("js-beautify")
js = beautify.js(js)
return js
}
Editor.current_buffer_needs_saving = false
Editor.mark_as_changed = function(){
editor_needs_saving_id.innerHTML = "<span title='editor needs saving'>*</span>"
Editor.current_buffer_needs_saving = true
}
Editor.unmark_as_changed = function(){
editor_needs_saving_id.innerHTML = " "
Editor.current_buffer_needs_saving = false
}
//var myCodeMirrorDoc = myCodeMirror.getDoc()
//myCodeMirrorDoc.on("change", Editor.mark_as_changed)
//used both from JS pane Edit menu undo item AND by App builder (called from sandbox))
Editor.undo = function(){ myCodeMirror.getDoc().undo() }
//fold examples. How do I implement menu items for fold all and unfold all?
//editor_html.foldCode(CodeMirror.Pos(0, 0));
//editor_html.foldCode(CodeMirror.Pos(21, 0));
//returns null if path is not in menu. path expected to be a full path,
//even the menu has partial paths.
Editor.index_of_path_in_file_menu = function(path){
let files_menu_path = Editor.path_to_files_menu_path(path)
for(let i = 0; i < file_name_id.children.length; i++){
let a_path = file_name_id.children[i].innerHTML
if (a_path == files_menu_path) { return i }
}
return null
}
//returns false if path is not in menu, true if it is. path expected to be a full path,
Editor.set_files_menu_to_path = function(path) {
if(!path) {
if(Editor.current_file_path){
path = Editor.current_file_path
}
else { return false }
}
let i = Editor.index_of_path_in_file_menu(path)
if (i === null) { return false }
else {
file_name_id.selectedIndex = i
return true
}
}
/* not called. use Editor.set_files_menu_to_path
Editor.select_file_in_file_menu = function(path){
let inner_path = Editor.path_to_files_menu_path(path)
var the_opt_elts = file_name_id.children
for (var index = 0; index < the_opt_elts.length; index++){
var elt = the_opt_elts[index]
if (elt.innerText == inner_path){
file_name_id.selectedIndex = index
return true
}
}
return false //no such path in file menu
} */
Editor.files_menu_path_separator = " --- "//this fails due to auto converstion ot < somewhere" <> " //" " //works but confusing with program dots. " . . . " //sticking in HTML shows html src, not rendered "<span style='margin-right:20px;'/>" //" " //attempt to stick in non-breaking space fails. String.fromCharCode(160) + String.fromCharCode(160) + String.fromCharCode(160) + String.fromCharCode(160) + String.fromCharCode(160) //" "
//below a folder always ends with a slash or a colon (as in "dexter0:")
Editor.files_menu_path_to_folder_and_name = function(path){
let [name, fold] = path.split(Editor.files_menu_path_separator)
if(fold == "dde_apps/") { fold = dde_apps_folder + "/" }
return [fold, name]
}
Editor.make_files_menu_path = function(folder, name) {
if (folder.startsWith(dde_apps_folder)){
folder = "dde_apps" + folder.substring(dde_apps_folder.length)
}
return name + Editor.files_menu_path_separator + folder
}
//returns an array of folder and file name.
// The returned folder always ends with slash or colon.
Editor.path_to_folder_and_name = function(path){
let file_name_start_index = path.lastIndexOf("/")
if(file_name_start_index == -1) { file_name_start_index = path.lastIndexOf(":") } //happens with dexter0:foo.js and C:foo.js
if(file_name_start_index == -1) { //happens with "foo.js"
return[dde_apps_folder + "/", path]
}
else {
return [path.substring(0, file_name_start_index + 1), path.substring(file_name_start_index + 1)]
}
}
//menu path will look like: "foo.js /Users/Joe/Documents/dde_apps/"
//note the space between the name and the folder.
/*Editor.path_to_files_menu_path = function(path){
if (path.startsWith(dde_apps_folder)){
path = "dde_apps" + path.substring(dde_apps_folder.length)
}
let file_name_start_index = path.lastIndexOf("/") + 1
let file_name = path.substring(file_name_start_index)
let fold = path.substring(0, file_name_start_index)
let menu_path = file_name + Editor.files_menu_path_separator + fold
return menu_path
}*/
Editor.path_to_files_menu_path = function(path){
if(path == "new buffer") { return path }
else if (path.startsWith(dde_apps_folder)){
path = "dde_apps" + path.substring(dde_apps_folder.length)
}
let [fold, name] = Editor.path_to_folder_and_name(path)
return Editor.make_files_menu_path(fold, name)
}
Editor.files_menu_path_to_path = function(menu_path){
if(menu_path == "new buffer") { return menu_path }
else {
let [fold, name] = Editor.files_menu_path_to_folder_and_name(menu_path)
return fold + name
}
}
//adds to files menu AND updates persistent files_menu_paths
Editor.add_path_to_files_menu = function(path){
let existing_index = Editor.index_of_path_in_file_menu(path)
if (existing_index === null) {
var opt = document.createElement("OPTION")
let inner_path = Editor.path_to_files_menu_path(path)
var textelt = document.createTextNode(inner_path); // Create a text node
opt.appendChild(textelt);
if (file_name_id.hasChildNodes()){
file_name_id.insertBefore(opt, file_name_id.firstChild)
}
else{
file_name_id.add(opt)
}
file_name_id.selectedIndex = 0
if(path != "new buffer"){
let paths = persistent_get("files_menu_paths")
paths.unshift(path) //push on to top of menu
if (paths.length > 20) { paths = paths.slice(0, 20) }
persistent_set("files_menu_paths", paths)
}
}
else {
file_name_id.selectedIndex = existing_index
//so that on reboot, dde will come up with this file on top of menu and in editor buffer
let paths = persistent_get("files_menu_paths")
let path_index = paths.indexOf(path)
paths.splice(path_index, 1)
paths.unshift(path) //push on to top of menu
persistent_set("files_menu_paths", paths)
}
}
Editor.remove_new_buffer_from_files_menu = function(){
let index = Editor.index_of_path_in_file_menu("new buffer") //returns null if none
if(typeof(index) == "number") {
file_name_id.removeChild(file_name_id.childNodes[index])
}
}
//returns true or false.
// Called by Editor.restore_files_menu_paths_and_last_file and
//storage.js dde_init_dot_js_initialize
// This implies that user is a first time user, OR at least
// hasn't saved any files so they haven't used DDE much at all.
Editor.files_menu_paths_empty_or_contains_only_dde_init = function(){
const paths = persistent_get("files_menu_paths")
if(paths.length == 0) { return true }
else if ((paths.length == 1) &&
((paths[0] == "dde_init.js") ||
paths[0].endsWith("dde_apps/dde_init.js"))) {
return true
}
else { return false }
}
Editor.restore_files_menu_paths_and_last_file = function(){ //called by on ready
if(file_exists("") && file_exists("dde_persistent.json")){ //Documents/dde_apps
const paths = persistent_get("files_menu_paths")
let existing_paths = []
var html = ""
for(let path of paths){
if(file_exists(path)) {
existing_paths.push(path) //don't put non-existent files on the menu
let inner_path = Editor.path_to_files_menu_path(path)
html += '<option>' + inner_path + "</option>"
}
}
if (existing_paths.length !== paths.length) {
persistent_set("files_menu_paths", existing_paths)
}
file_name_id.innerHTML = html
if(Editor.files_menu_paths_empty_or_contains_only_dde_init()){
Editor.edit_new_file()
}
else {
let latest_file = existing_paths[0]
if(latest_file.endsWith("/dde_init.js")){ //don't edit this file as usually users won't want to
if(paths.length > 1) { latest_file = paths[1]}
else {
Editor.edit_new_file()
return
}
}
try {
Editor.edit_file(latest_file) //sometimes paths[0] will be 'new buffer' and that's fine
}
catch(err) {
warning("Could not find the last edited file:<br/><code title='unEVALable'>" + latest_file +
"</code><br/> to insert into the editor.")
Editor.edit_new_file()
}
}
}
else {
warning("could not find the file: dde_apps/dde_perisistent.json")
}
}
Editor.get_any_selection = function(){
//do in this order because clicking in editor window makes other selections go away.
//so if there are other selections, it means you made them AFTER you made
//the editor window sel, and therefore your attention is on that non-editor selection.
var sel_text = ""
//this clause catches html selection inside code and samp tags.
if (!window.getSelection().isCollapsed) { //got sel in doc or output pane
let sel_text = window.getSelection().getRangeAt(0).toString()
return sel_text
}
//this clause catches cmd_input as well as the codemirror text area.
//if both are selected, codemirror wins.
else if (document.activeElement &&
["INPUT", "TEXTAREA"].includes(document.activeElement.tagName) &&
document.activeElement.selectionStart != document.activeElement.selectionEnd) {
let full_src = document.activeElement.value
return full_src.substring(document.activeElement.selectionStart, document.activeElement.selectionEnd)
}
//sel_text = Editor.get_cmd_selection() //this is caught by the above clause
//if(sel_text.length > 0 ) { return sel_text }
if (Editor.view == "JS") {
sel_text = myCodeMirror.doc.getValue().substring(Editor.selection_start(), Editor.selection_end())
}
else if (Editor.view == "DefEng") {
sel_text = myCodeMirror.doc.getValue().substring(Editor.selection_start(), Editor.selection_end())
}
else if (Editor.view == "Blocks"){ //Blocks view
sel_text = Workspace.inst.get_javascript(true)
}
else if (Editor.view == "HCA"){
sel_text = HCA.get_javascript(true) //gets JSON string
}
else { sel_text = "" }
return sel_text //will be "" if no selection
}
//return editor sel or if none, cmd input, or if none, ""
/*Not used. Use Editor.get_any_selection instead
Editor.get_selection_or_cmd_input = function(){
var sel_text = myCodeMirror.doc.getValue().substring(Editor.selection_start(), Editor.selection_end())
if (sel_text.length == 0) { sel_text = cmd_input_id.value }
return sel_text
}*/
//if there is no selecion in the cmd input ,return "", else return the selected text.
Editor.get_cmd_selection = function(){
const sel_start = cmd_input_id.selectionStart
const sel_end = cmd_input_id.selectionEnd
if (sel_start != sel_end) { //got sel in cmd_input
const full_src = cmd_input_id.value
return full_src.substring(sel_start, sel_end)
}
else { return "" }
}
Editor.get_javascript = function(use_selection=false){
//if use_selection is true, return it.
// if false, return whole buffer.
// if "auto", then if sel, return it, else return whole buffer.
if((Editor.view == "JS") || (Editor.view == "DefEng")) {
let full_src = myCodeMirror.doc.getValue() //$("#js_textarea_id").val() //careful: js_textarea_id.value returns a string with an extra space on the end! A crhome bug that jquery fixes
if (use_selection){ //true or "auto"
let sel_text = full_src.substring(Editor.selection_start(), Editor.selection_end())
if (use_selection === true) { return sel_text}
else if (use_selection == "auto") {
if (sel_text == "") { return full_src}
else { return sel_text }
}
}
else { return full_src }
}
else if (Editor.view == "Blocks"){
return Workspace.inst.get_javascript(use_selection)
}
else if (Editor.view == "HCA"){
return HCA.get_javascript(use_selection)
}
else {
return ""
//shouldnt("Editor.get_javascript found invalid Editor.view of: " + Editor.view)
}
}
Editor.set_javascript = function(text) {
//$("#js_textarea_id").val(text)
if (typeof (text) === "string") {
try {
myCodeMirror.doc.setValue(text)
} catch (err) {
}
//probably an error thrown by the linter which we don't want
//to actually throw an error, so catch it.
//started happening after 3.8.11
}
else {
shouldnt("Editor.set_javscript passed non-string: " + text)
}
}
Editor.selection_start = function(){
return myCodeMirror.indexFromPos(myCodeMirror.getCursor("start"))
}
Editor.selection_column_number = function(){
return myCodeMirror.getCursor("start").ch
}
Editor.selection_line_number = function(){
return myCodeMirror.getCursor("start").line
}
Editor.line_number = function (src, pos) { //all numbers zero based. assume src linefeeds are just 1 char ie \n
//and make that newline char be the last char on the line, not the first char of the next line.
var cur_line = -1
var cur_col = 0
var line_number = 0
for (var i = pos; i > 0; i--) {
var char = src.charAt(i)
if (char == '\n')
line_number++
}
return line_number
}
Editor.selection_end = function(){
return myCodeMirror.indexFromPos(myCodeMirror.getCursor("end"))
}
Editor.is_selection = function(){
return Editor.selection_start() !== Editor.selection_end()
}
Editor.select_javascript = function(start, end=start){
//js_textarea_id.setSelectionRange(start, end)
//$('#js_textarea_id').focus() //scroll to make the selection visible .. Wierdly bad var refs get a squiggly red underline voer the whole var name, but not sure how this happens. But its good.
//$('#js_textarea_id').scrollTop(start); //doesnt' scroll all the way
var doc = myCodeMirror.getDoc()
var cm_start_pos = doc.posFromIndex(start)
var cm_end_pos = doc.posFromIndex(end)
doc.setSelection(cm_start_pos, cm_end_pos, {scroll:true}) //documented to force the initial char of the selection to be in view, but doesn't work
//important for inserting the TestSuite example for example.
//Codemirror doc claims the default is true, but apparently not
if(cm_start_pos.ch < 21){ //column less than 21, just make it move to 0
myCodeMirror.scrollIntoView({line: cm_start_pos.line, ch:0 })
}
else { //otherwise, give me 8 chars of "context" to the left.
myCodeMirror.scrollIntoView({line: cm_start_pos.line, ch: cm_start_pos.ch - 8 })
}
myCodeMirror.focus()
}
Editor.replace_at_positions = function(new_text, start_pos, end_pos, select_new_text=false){
Editor.select_javascript(start_pos, end_pos)
Editor.replace_selection(new_text, select_new_text)
}
//2nd arg can be boolean or a number which is a position relative to the
//actual editor start pos of the new_text to be inserted.
//3rd arg (if any) is also relative to the editor start pos.
//it defaults to the end of the new_text inserted.
//If 3rd arg is neg, it means relative to the designated start of the selection + length of the new_text
//so 3rd arg of -1 means one char in from the end of the new inserted text.
//to select all of the inserted text, just make 2nd arg true.
//to select in from the end give a 2nd arg of some int, (could be 0) and a neg number for 3rd arg.
Editor.replace_selection = function(new_text, select_new_text=false, end_pos_of_selection_relative=null){
var doc = myCodeMirror.getDoc()
var select_arg
if (select_new_text === false) { doc.replaceSelection(new_text, "start")}
else if (select_new_text === true) { doc.replaceSelection(new_text, "around")}
else if (typeof(select_new_text) == "number"){
var editor_start_pos = Editor.selection_start()
var start_sel
if (select_new_text < 0){ //means fron the end of the new_text
start_sel = editor_start_pos + new_text.length + select_new_text
}
else {
start_sel = editor_start_pos + select_new_text
}
var end_sel = editor_start_pos + new_text.length
if (typeof(end_pos_of_selection_relative) == "number"){
if (end_pos_of_selection_relative < 0){
end_sel = editor_start_pos + new_text.length + end_pos_of_selection_relative
}
else {
end_sel = editor_start_pos + end_pos_of_selection_relative
}
}
doc.replaceSelection(new_text)
Editor.select_javascript(start_sel, end_sel)
}
setTimeout(function(){myCodeMirror.focus()})
}
Editor.path_to_sel_map = {}
Editor.store_selection_in_map = function (){
Editor.path_to_sel_map[Editor.current_file_path] = [Editor.selection_start(), Editor.selection_end()]
}
//doc.setSelection(anchor: {line, ch}, ?head: {line, ch}, ?options: object)
Editor.restore_selection_from_map = function(){
const start_end = Editor.path_to_sel_map[Editor.current_file_path]
if (start_end){
Editor.select_javascript(start_end[0], start_end[1])
}
}
/*Editor.open = function(){
let cont = '<input type="submit" value="DDE computer"/>\n'
for(let dex_name of Dexter.all_names){
if(Dexter[dex_name]) {//should hit every time, but just a check
cont +=
`<br/><input style="margin-top:5px" type="submit" value="` + dex_name + `"/>\n`
}
}
show_window({title: "Choose computer<br/>to open file from",
content: cont,
width: 220,
x: 50,
y: 50,
callback: function(vals) {
if(vals.clicked_button_value == "DDE computer") {
setTimeout(Editor.open_on_dde_computer, 10)
}
else {
let dex_name = vals.clicked_button_value
setTimeout(function() {Editor.open_on_dexter_computer(dex_name)}, 10)
}
}
}
)
}*/
function open_from_dexter_computer_cb(vals) {
let dex_name = vals.clicked_button_value
setTimeout(function() {Editor.open_on_dexter_computer(dex_name)}, 10)
}
Editor.open_from_dexter_computer = function(){
if(Dexter.all_names.length == 1){ //no need for a dialog to choose which dexter
Editor.open_on_dexter_computer(Dexter.all_names[0])
}
else {
let cont = "" //'<input type="submit" value="DDE computer"/>\n'
for(let dex_name of Dexter.all_names){
if(Dexter[dex_name]) {//should hit every time, but just a check
cont += `<input style="margin-top:5px" type="submit" value="` + dex_name + `"/><br/>\n`
}
}
show_window({title: "Choose computer<br/>to open file from",
content: cont,
width: 220,
x: 50,
y: 50,
callback: open_from_dexter_computer_cb
}
)
}
}
Editor.open_on_dde_computer = function(){
choose_file({title: "Choose a file to edit", properties: ['openFile']},
function(err, path) {
if (err) {
warning("Editor.open_on_dde_computer canceled")
} else {
Editor.edit_file(path)
}
})
}
//can't be a closure, can't be in a class'es namespace. yuck.
function open_on_dexter_computer_show_window_cb(vals) {
let file_path = vals.open_on_dexter_computer_file_path_id
persistent_set("last_open_dexter_file_path", file_path) //does not include "dexter:" in it.
file_path = vals.dexter_name + ":" + file_path //we cannot close over dexter_name because show_window can't take a closure for a callback
Editor.edit_file(file_path)
}
Editor.open_on_dexter_computer = function(dex_name){
show_window({title: "Enter file on <i>" + dex_name + "</i> to open",
content: '<i>Opening Dexter files considers simulation state<br/>' +
'when determining where to get the file content.<br/>' +
'If you want content from Dexter, select<br/>' +
'the <b>real</b> button in the Misc pane header.</i><br/>' +
'<input id="open_on_dexter_computer_file_path_id" value="' + persistent_get("last_open_dexter_file_path") + '" style="width:350px;font-size:16px;margin-top:10px;"/>\n' +
'<p></p><center><input type="submit" value="Open"/></center>\n' +
'<input name="dexter_name" style="display:none;" value="' + dex_name + '"/>',
width: 390,
height: 200,
x: 50,
y: 50,
callback: open_on_dexter_computer_show_window_cb
})
setTimeout(function() {open_on_dexter_computer_file_path_id.focus()}, 100)
}
handle_open_system_file = function(vals){
if(vals.clicked_button_value == "edit dde_init.js"){
Editor.edit_file("dde_init.js")
}
else if (vals.clicked_button_value == "show dde_persistent.json"){
let content = read_file("dde_persistent.json")
content = replace_substrings(content, "\n", "<br/>")
}
else if (vals.clicked_button_value.endsWith("Defaults.make_ins")){
let path = vals.clicked_button_value.split(" ")[1]
let rob_name = path.split(":")[0]
let rob = Dexter[rob_name]
const sim_actual = Robot.get_simulate_actual(rob.simulate)
if(sim_actual === true) {
warning("You are getting the content of " + path +
"<br/>from the DDE computer because the simulate radio button " +
"<br/>in the Misc pane is selected." +
"<br/>To get the file content from Dexter," +
"<br/>select the 'real' radio button.")
}
Editor.edit_file(path)
}
else if (vals.clicked_button_value.endsWith("errors.log")){
let path = vals.clicked_button_value.split(" ")[1]
let rob_name = path.split(":")[0]
read_file_async(path,
undefined,
function(err, content){
if(err) {
warning("Could not get " + path + "<br/>Error: " + err)
}
else {
content = replace_substrings(content, "\n", "<br/>")
out("<b>" + rob_name + ":/srv/samba/share/errors.log</b> content:<br/>" + content)
}
})
}
}
Editor.open_system_file = function(){
let cont = "<fieldset><legend>on DDE Computer</legend>\n" +
"<input type='submit' value='edit dde_init.js'/><br/>" +
"<input type='submit' value='show dde_persistent.json'/>" +
"</fieldset>"
for(let dex_name of Dexter.all_names){
cont += "<fieldset><legend>on " + dex_name + "</legend>\n" +
"<input type='submit' value='edit " + dex_name + ":../Defaults.make_ins'/><br/>" +
"<input type='submit' value='show " + dex_name + ":../errors.log'/>" +
"</fieldset>"
}
show_window({title: "Open System File",
content: cont,
width: 275,
height: 450,
callback: handle_open_system_file
})
}
Editor.remove = function(path_to_remove=Editor.current_file_path){
if(path_to_remove == "new buffer") {
let index = Editor.index_of_path_in_file_menu("new buffer") //returns null if none
if(typeof(index) == "number") {
file_name_id.removeChild(file_name_id.childNodes[index])
if (Editor.current_file_path == path_to_remove){
let files_menu_path = file_name_id.childNodes[0].innerHTML
let path = Editor.files_menu_path_to_path(files_menu_path)
Editor.current_file_path = false //path //if I don't do this the next call to edit_file will think we're on new buffer and pop up the 3 choices dialog again.
Editor.edit_file(path)
}
}
else {} //if there is no new buffer, silently do nothing as I call Editor.remove("new buffer") sometimes legitimately when there is no new buffer
}
else {
let files = persistent_get("files_menu_paths")
//let the_file_to_remove = file_name_id.value
//the_file_to_remove = Editor.files_menu_path_to_path(the_file_to_remove)
let i = files.indexOf(path_to_remove)
if (i != -1) {
files.splice(i, 1)
persistent_set("files_menu_paths", files)
Editor.restore_files_menu_paths_and_last_file()
}
}
}
handle_show_when_new_buffer_choices = function(vals){
if(vals.clicked_button_value == "Save new buffer"){
Editor.save_as()
myCodeMirror.focus()
}
else if (vals.clicked_button_value == "Delete new buffer"){
Editor.remove()
myCodeMirror.focus()
}
}
Editor.show_when_new_buffer_choices = function(){
show_window({title: "New Buffer Choices",
content:
`You can only have one <b>new buffer</b>.<br/>
It is never saved without renaming it.
<p></p>
<input type="submit" value="Cancel"/>
<input type="submit" value="Save new buffer"/>
<input type="submit" value="Delete new buffer"/>`,
x: 100, y: 80,
width: 370,
height: 130,
callback: handle_show_when_new_buffer_choices
})
}
/*there is at most 1 "new buffer" buffer.
if it exists, it is always the Editor.current_file_path
whose value will be "new buffer".
It is never saved to the file system.
If you attempt to open another file or choose another file from the files menu.
you must choose to delete it, clear it, or save it to a real file
*/
handle_show_clear_new_buffer_choice = function(vals){
if(vals.clicked_button_value == "Yes"){
Editor.set_javascript("")
Editor.unmark_as_changed()
}
myCodeMirror.focus()
}
Editor.show_clear_new_buffer_choice = function(){
show_window({title: "New Buffer Choice",
content:
`You're already editing the one new buffer.<br/>
Clear its content?
<p></p>
<input type="submit" value="Yes"/>
<input type="submit" value="No"/>`,
x: 100, y: 80,
width: 330,
height: 140,
callback: handle_show_clear_new_buffer_choice
})
}
Editor.edit_new_file = function(){
Editor.edit_file("new buffer")
}
//content is passed when we're editing a file by clicking on SSH dir listing file
//and content is the new content to edit
/*Editor.edit_file = function(path, content){ //path could be "new buffer"
//first, deal with potentially saving the current buffer
if(Editor.current_file_path == "new buffer") { //Editor.current_file_path is null when we first launch dde.
Editor.set_files_menu_to_path() //set the files menu BACK to its previously selected file cause we can't get the new one
if (path == "new buffer"){
Editor.show_clear_new_buffer_choice()
}
else if (Editor.get_javascript().trim().length == 0) { //don't ask about deleting the new buf, just do it;
Editor.remove() //get rid of the current "new buffer"
const path_already_in_menu = Editor.set_files_menu_to_path(path)
if (!path_already_in_menu) { Editor.add_path_to_files_menu(path) }
let the_path = path
if(content) {
Editor.edit_file_aux(the_path, content)
return
}
else {
read_file_async(path, undefined, function(err, content) { //file_content will convert to windows format if needed
if(err) {
Editor.set_files_menu_to_path() //set the files menu BACK to its previously selected file cause we can't get the new one
dde_error(err.message)
}
else {
content = content.toString() //because sometimes the content passed in is buffer, not a string. This handles both.
Editor.edit_file_aux(the_path, content)
}
})}
}
else { Editor.show_when_new_buffer_choices() }
}
else {
path = convert_backslashes_to_slashes(path) //must store only slashes in files menu
if(Editor.current_file_path){ //Editor.current_file_path is null when we first launch dde.
Editor.store_selection_in_map()
if(
if ((Editor.current_file_path != "new buffer") &&
persistent_get("save_on_eval") &&
(Editor.current_file_path != path)){ //when we were originally on a new buffer, and user chose to delete it,
//the remove method sets Editor.current_file_path to path,
//and then we DON'T want to save_current_file because in the
//actual editor content now is the old content from the orig new buffer.
//so don't do the save in that condition.
Editor.save_current_file()
}
else if(!persistent_get("save_on_eval")){
let cur_content = Editor.get_javascript()
let cur_path = Editor.current_file_path
read_file_async(cur_path, //can't use read_file here as the current_file_path might
//be a dexter file that needs saving
undefined,
function(err, data){
let prev_content = data.toString()
if(cur_content != prev_content) {
let save_it = confirm(Editor.current_file_path + "\nhas unsaved changes.\n" +
"Click OK to save it before editing the other file.\n" +
"Click Cancel to not save it before editing the other file.")
if(save_it) {
write_file_async(cur_path, cur_content) //since this is async, we must already have the path and content to save as the 'cur' may have changed.
out(cur_path + " saved.")
}
}
})
}
}
if (path == "new buffer"){
Editor.edit_file_aux(path, (content? content: ""))
}
else {
const path_already_in_menu = Editor.set_files_menu_to_path(path)
if (!path_already_in_menu) { Editor.add_path_to_files_menu(path) }
if(content) {
Editor.edit_file_aux(path, content)
}
else {
let the_path = path
read_file_async(path, undefined, function(err, content) { //file_content will conver to windows format if needed
if(err) {
Editor.set_files_menu_to_path() //set the files menu BACK to its previously selected file cause we can't get the new one
dde_error(err.message)
}
else {
content = content.toString() //because sometimes the content passed in is buffer, not a string. This handles both.
Editor.edit_file_aux(the_path, content)
}
})
}
}
}
}*/
//path is the new path to edit,
// content is the new content that is being edited if any.
//usually content is not passed as that's gotten from path,
//but in the ssh context, it sometimes is passed.
Editor.edit_file = function(path, content){ //path could be "new buffer"
let new_path = convert_backslashes_to_slashes(path) //must store only slashes in files menu
let cur_path = Editor.current_file_path
let cur_content = Editor.get_javascript()
if(!Editor.current_buffer_needs_saving){
Editor.remove_new_buffer_from_files_menu() //does nothing if "new buffer" is not on files menu
//this only does something if cur_path is "new buffer" and its empty.
if(content) { Editor.edit_file_aux(new_path, content) }
else if(new_path === "new buffer") { Editor.edit_file_aux(new_path, "") }
else {
read_file_async(path, undefined, function(err, new_content) { //file_content will convert to windows format if needed
if(err) {
Editor.set_files_menu_to_path() //set the files menu BACK to its previously selected file cause we can't get the new one
dde_error(err.message)
}
else {
new_content = new_content.toString() //because sometimes the content passed in is buffer, not a string. This handles both.
Editor.edit_file_aux(new_path, new_content)
}
})}
}
//cur buffer needs saving
else if(cur_path === "new buffer") { //Editor.current_file_path is null when we first launch dde.
Editor.set_files_menu_to_path() //set the files menu BACK to its previously selected file cause we can't get the new one
if (path === "new buffer"){
if (Editor.get_javascript().trim().length == 0) { } //nothing to do as our cur buf is empty and we've chosen a new buff.
else { Editor.show_clear_new_buffer_choice() } //either we make the cur buff empty or we do nothing.
}
//cur buff is a new buffer, and the target path is not
else if (Editor.get_javascript().trim().length == 0) { //don't ask about deleting the new buf, just do it;
Editor.remove_new_buffer_from_files_menu() //get rid of the current "new buffer"
const path_already_in_menu = Editor.set_files_menu_to_path(new_path)
if (!path_already_in_menu) { Editor.add_path_to_files_menu(new_path) }
if(content) {
Editor.edit_file_aux(new_path, content)
}
else {
read_file_async(path, undefined, function(err, new_content) { //file_content will convert to windows format if needed
if(err) {
Editor.set_files_menu_to_path() //set the files menu BACK to its previously selected file cause we can't get the new one
dde_error(err.message)
}
else {
new_content = new_content.toString() //because sometimes the content passed in is buffer, not a string. This handles both.
Editor.edit_file_aux(new_path, new_content)
}
})}
}
//cur is new buffer, but it needs saving, we're trying to edit a new file.
//should we save the buffer first?
else {
let save_it = confirm( "The editor buffer has unsaved changes.\n" +
"Click OK to save it before editing the other file.\n" +
"Click Cancel to not save it before editing the other file.")
if(save_it) {
let file_to_save_buffer_to = choose_save_file()
if(file_to_save_buffer_to) {
write_file_async(file_to_save_buffer_to, cur_content) //since this is async, we must already have the path and content to save as the 'cur' may have changed.
out(file_to_save_buffer_to + " saved.")
Editor.add_path_to_files_menu(file_to_save_buffer_to)
Editor.remove_new_buffer_from_files_menu()
}
//else (user hit cancel in choose_save_file dialog).
// we just leave existing new buffer up and not saved.
}
else {
Editor.remove_new_buffer_from_files_menu() //the only time "new buffer"
//should be on the files menu, is if it is the currently selected one.
//user can choose File menu, "new" to get a fresh "new buffer".
}
//
read_file_async(new_path, undefined, function(err, new_content) { //file_content will convert to windows format if needed
if(err) {
Editor.set_files_menu_to_path() //set the files menu BACK to its previously selected file cause we can't get the new one
dde_error(err.message)
}
else {
new_content = new_content.toString() //because sometimes the content passed in is buffer, not a string. This handles both.
Editor.edit_file_aux(new_path, new_content)
}
})
}
}