-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.nim
1506 lines (1349 loc) · 47.3 KB
/
editor.nim
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
# MIT License
#
# Copyright (c) 2019 - 2021 Can Joshua Lehmann
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sequtils, strutils, os, sugar, times
import unicode, tables, algorithm
import utils, ui_utils, termdiff, highlight/highlight
import buffer, window_manager
# Types
type
# Editor
PromptField = object
title: string
entry: Entry
PromptKind = enum PromptNone, PromptActive, PromptInactive, PromptInfo
PromptCallbackProc = proc (editor: Editor, inputs: seq[seq[Rune]])
Prompt = object
case kind: PromptKind:
of PromptNone: discard
of PromptActive, PromptInactive:
title: string
fields: seq[PromptField]
selected_field: int
callback: PromptCallbackProc
of PromptInfo:
lines: seq[string]
# QuickOpen
FileEntry = object
name: string
path: string
changed: bool
QuickOpen = ref object
entry: Entry
files: seq[FileEntry]
shown_files: seq[FileEntry]
list: List
# FindDef
FindDef = ref object
entry: Entry
shown_symbols: seq[Symbol]
symbols: seq[Symbol]
list: List
editor: Editor
# Dialog
DialogKind = enum DialogNone, DialogQuickOpen, DialogFindDef
Dialog = object
case kind: DialogKind:
of DialogNone: discard
of DialogFindDef:
find_def: FindDef
of DialogQuickOpen:
quick_open: QuickOpen
# Editor
Editor* = ref object of Window
buffer*: Buffer
prompt: Prompt
dialog: Dialog
app*: App
scroll: Index2d
detach_scroll: bool
cursors*: seq[Cursor]
jump_stack: seq[int]
cursor_hook_id: int
window_size: Index2d
autocompleter: Autocompleter
completions: seq[Symbol]
completion_time: Time
Tool* = object
name*: string
callback*: proc (editor: Editor)
var editor_tools*: seq[Tool]
# Dialog / QuickOpen
proc `<`(a, b: FileEntry): bool = a.name < b.name
proc is_hidden(path: string): bool =
let dirs = path.split("/")
for dir in dirs[0 ..< ^1]:
if dir.startswith("."):
return true
return false
proc index_files(dir: string): seq[string] =
for item in walk_dir(dir):
case item.kind:
of pcFile: result.add(item.path)
of pcDir: result &= index_files(item.path)
else: discard
proc index_project(dir: string): seq[FileEntry] =
let paths = index_files(dir)
return paths
.map(path => FileEntry(
path: path,
name: path.relative_path(dir)
))
.filter((entry: FileEntry) => not entry.name.is_hidden())
.sorted()
proc display_name(entry: FileEntry): string =
result = entry.name
if entry.changed:
result &= "*"
proc index_project(): seq[FileEntry] =
index_project(get_current_dir())
proc update_list(quick_open: QuickOpen) =
quick_open.shown_files = quick_open.files
.filter(entry => ($quick_open.entry.text).to_lower() in entry.name.to_lower())
quick_open.list.items = quick_open.shown_files
.map(entry => entry.display_name().to_runes())
quick_open.list.selected = quick_open.list.selected.max(0).min(quick_open.list.items.len - 1)
proc load_file(editor: Editor, path: string)
proc open_selected(quick_open: QuickOpen, editor: Editor) =
if quick_open.list.selected < quick_open.shown_files.len and
quick_open.list.selected >= 0:
let path = quick_open.shown_files[quick_open.list.selected].path
editor.load_file(path)
editor.dialog = Dialog(kind: DialogNone)
proc process_mouse(quick_open: QuickOpen, editor: Editor, mouse: Mouse): bool =
var mouse_rel = mouse
mouse_rel.x -= len("Search: ")
mouse_rel.y -= 2
if mouse.y == 1:
quick_open.entry.process_mouse(mouse_rel)
return
elif mouse.y == 0:
if mouse.x < len("Search:"):
return true
return
case mouse.kind:
of MouseUp:
if quick_open.list.process_mouse(mouse_rel):
quick_open.open_selected(editor)
else: discard quick_open.list.process_mouse(mouse_rel)
proc process_key(quick_open: QuickOpen, editor: Editor, key: Key) =
case key.kind:
of KeyReturn:
quick_open.open_selected(editor)
of LIST_KEYS:
quick_open.list.process_key(key)
else:
quick_open.entry.process_key(key)
quick_open.update_list()
proc render(quick_open: QuickOpen, box: Box, ren: var TermRenderer) =
ren.move_to(box.min.x, box.min.y)
let
label = "Search:"
title = " ".repeat(label.len + 1) & strutils.align_left("Quick Open", box.size.x - label.len - 1)
ren.put(title, fg=Color(base: ColorBlack), bg=Color(base: ColorWhite, bright: true))
ren.move_to(box.min.x, box.min.y + 1)
ren.put(label, fg=Color(base: ColorBlack), bg=Color(base: ColorWhite, bright: true))
ren.put(" ")
quick_open.entry.render(ren)
for y in 2..<box.size.y:
ren.move_to(box.min.x, y + box.min.y)
ren.put(repeat(" ", label.len), fg=Color(base: ColorBlack), bg=Color(base: ColorWhite, bright: true))
ren.put(" ")
quick_open.list.render(Box(
min: box.min + Index2d(x: label.len + 1, y: 2),
max: box.max
), ren)
proc make_quick_open(app: App): owned QuickOpen =
var
files = index_project()
.map(entry => FileEntry(
path: entry.path,
name: entry.name,
changed: app.is_changed(entry.path)
))
known_files: seq[FileEntry] = @[]
unknown_files: seq[FileEntry] = @[]
for entry in files:
if app.languages.detect_language(entry.name) == nil:
unknown_files.add(entry)
else:
known_files.add(entry)
files = known_files & unknown_files
return QuickOpen(
entry: make_entry(app.copy_buffer),
files: files,
shown_files: files,
list: make_list(files.map(entry => entry.display_name()))
)
# Dialog / Find Def
proc matches(sym: Symbol, pattern: string): bool =
let
parts = pattern.to_lower().split(" ")
name = to_lower($sym.name)
result = true
for part in parts:
if part notin name and
part notin $sym.kind:
return false
proc format_sym_kind(kind: SymbolKind): string =
result = ($kind)[len("Sym")..^1].to_lower_ascii()
proc update_list(find_def: FindDef) =
find_def.shown_symbols = find_def.symbols.filter(def =>
def.matches($find_def.entry.text))
var kind_width = 0
for sym in find_def.shown_symbols:
kind_width = kind_width.max(sym.kind.format_sym_kind().len)
find_def.list.items = @[]
for sym in find_def.shown_symbols:
find_def.list.items.add(to_runes(
strutils.align_left(sym.kind.format_sym_kind(), kind_width + 1) &
$sym.name
))
if not find_def.list.detached:
find_def.list.view = 0
if find_def.list.selected >= find_def.list.items.len:
find_def.list.selected = find_def.list.items.len - 1
if find_def.list.selected < 0:
find_def.list.selected = 0
proc jump*(editor: Editor, to: int)
proc jump*(editor: Editor, start, stop: int)
proc jump_to_selected(find_def: FindDef, editor: Editor) =
if find_def.list.selected < 0 or
find_def.list.selected >= find_def.shown_symbols.len:
return
let
def = find_def.shown_symbols[find_def.list.selected]
pos = editor.buffer.to_index(def.pos)
editor.jump(pos)
editor.dialog = Dialog(kind: DialogNone)
proc process_mouse(find_def: FindDef, editor: Editor, mouse: Mouse): bool =
let sidebar_width = len("Search:")
if mouse.x < sidebar_width and mouse.y == 0:
return true
var mouse_rel = mouse
mouse_rel.x -= sidebar_width + 1
mouse_rel.y -= 1
if mouse.y == 1:
find_def.entry.process_mouse(mouse_rel)
return
mouse_rel.y -= 1
case mouse.kind:
of MouseUp:
if find_def.list.process_mouse(mouse_rel):
find_def.jump_to_selected(editor)
else:
discard find_def.list.process_mouse(mouse_rel)
proc process_key(find_def: FindDef, editor: Editor, key: Key) =
case key.kind:
of LIST_KEYS:
find_def.list.process_key(key)
of KeyReturn:
find_def.jump_to_selected(editor)
else:
find_def.entry.process_key(key)
find_def.update_list()
proc render(find_def: FindDef, box: Box, ren: var TermRenderer) =
let sidebar_width = len("Search:")
render_border("Find Definition", sidebar_width, box, ren)
find_def.list.render(Box(
min: box.min + Index2d(x: sidebar_width + 1, y: 2),
max: box.max
), ren)
ren.move_to(box.min + Index2d(y: 1))
ren.put("Search:",
fg=Color(base: ColorBlack),
bg=Color(base: ColorWhite)
)
ren.move_to(box.min + Index2d(x: sidebar_width + 1, y: 1))
find_def.entry.render(ren)
proc set_defs(find_def: FindDef, defs: seq[Symbol]) =
find_def.symbols = defs
find_def.update_list()
proc make_find_def(editor: Editor): FindDef =
let find_def = FindDef(
entry: editor.app.copy_buffer.make_entry(),
list: make_list(),
editor: editor
)
if editor.autocompleter != nil:
editor.autocompleter.list_defs(
editor.buffer,
proc (defs: seq[Symbol]) =
find_def.set_defs(defs)
)
return find_def
# Dialog
proc process_key(dialog: Dialog, editor: Editor, key: Key) =
case dialog.kind:
of DialogNone: discard
of DialogQuickOpen: dialog.quick_open.process_key(editor, key)
of DialogFindDef: dialog.find_def.process_key(editor, key)
proc process_mouse(dialog: Dialog, editor: Editor, mouse: Mouse): bool =
case dialog.kind:
of DialogNone: discard
of DialogQuickOpen:
return dialog.quick_open.process_mouse(editor, mouse)
of DialogFinddef:
return dialog.find_def.process_mouse(editor, mouse)
proc render(dialog: Dialog, box: Box, ren: var TermRenderer) =
case dialog.kind:
of DialogNone: discard
of DialogQuickOpen:
dialog.quick_open.render(box, ren)
of DialogFindDef:
dialog.find_def.render(box, ren)
# Prompt
proc compute_size(prompt: Prompt): int =
case prompt.kind:
of PromptNone: return 0
of PromptActive, PromptInactive: return prompt.fields.len + 1
of PromptInfo: return prompt.lines.len
proc process_key(prompt: var Prompt, key: Key) =
if prompt.kind != PromptActive:
return
case key.kind:
of KeyArrowUp:
prompt.selected_field -= 1
if prompt.selected_field < 0:
prompt.selected_field = 0
of KeyArrowDown:
prompt.selected_field += 1
if prompt.selected_field >= prompt.fields.len:
prompt.selected_field = prompt.fields.len - 1
else:
prompt.fields[prompt.selected_field].entry.process_key(key)
# Editor
proc get_inputs(prompt: Prompt): seq[seq[Rune]] =
prompt.fields.map(field => field.entry.text)
proc show_info*(editor: Editor, lines: seq[string]) =
editor.prompt = Prompt(kind: PromptInfo, lines: lines)
proc show_prompt*(editor: Editor,
title: string,
fields: seq[string],
callback: PromptCallbackProc = nil) =
editor.prompt = Prompt(
kind: PromptActive,
title: title,
selected_field: 0,
fields: fields.map(title => PromptField(
title: title,
entry: make_entry(editor.app.copy_buffer)
)),
callback: callback
)
editor.completions = @[]
proc hide_prompt*(editor: Editor) =
editor.prompt = Prompt(kind: PromptNone)
proc primary_cursor*(editor: Editor): Cursor = editor.cursors[^1]
proc merge_cursors(editor: Editor) =
editor.cursors = editor.cursors.merge_cursors()
proc update_cursor(editor: Editor, index: int, pos_raw: int, shift: bool) =
let pos = max(min(pos_raw, editor.buffer.len), 0)
case editor.cursors[index].kind:
of CursorInsert:
if shift:
editor.cursors[index] = Cursor(
kind: CursorSelection,
start: editor.cursors[index].pos,
stop: pos
)
else:
editor.cursors[index].pos = pos
of CursorSelection:
if shift:
editor.cursors[index].stop = pos
else:
editor.cursors[index] = Cursor(kind: CursorInsert, pos: pos)
proc is_under_cursor(editor: Editor, pos: int): bool =
for cursor in editor.cursors:
if cursor.is_under(pos):
return true
return false
proc make_cursor_hook(editor: Editor): CursorHook =
return proc (start: int, delta: int) {.closure.} =
for it, cursor in editor.cursors:
case cursor.kind:
of CursorInsert:
if cursor.pos >= start:
editor.cursors[it].pos += delta
of CursorSelection:
if cursor.start >= start:
editor.cursors[it].start += delta
if cursor.stop >= start:
editor.cursors[it].stop += delta
proc update_scroll(editor: Editor, size: Index2d, detach: bool) =
let pos = editor.buffer.to_2d(editor.primary_cursor().get_pos())
if not detach:
while (pos.y - editor.scroll.y) < 4:
editor.scroll.y -= 1
while (pos.y - editor.scroll.y) >= size.y - 4:
editor.scroll.y += 1
editor.scroll.y = max(editor.scroll.y, 0).min(editor.buffer.lines.len)
proc jump*(editor: Editor, to: int) =
editor.jump_stack.add(editor.primary_cursor().get_pos())
editor.cursors = @[Cursor(kind: CursorInsert, pos: to)]
editor.completions = @[]
editor.completion_time = get_time()
proc jump*(editor: Editor, start, stop: int) =
editor.jump_stack.add(editor.primary_cursor().get_pos())
editor.cursors = @[Cursor(kind: CursorSelection, start: start, stop: stop)]
editor.completions = @[]
editor.completion_time = get_time()
proc goto_line(editor: Editor, inputs: seq[seq[Rune]]) =
var line: int
try:
line = parse_int($inputs[0]) - 1
except ValueError:
editor.show_info(@["Not a number: " & $inputs[0]])
return
if line == -1:
editor.show_info(@["Invalid line: " & $(line + 1)])
return
elif line >= editor.buffer.lines.len:
editor.show_info(@["Invalid line: " & $(line + 1)])
return
if line < -1:
line = editor.buffer.lines.len + line + 1
editor.jump(editor.buffer.lines[line])
editor.hide_prompt()
proc find_pattern(editor: Editor, inputs: seq[seq[Rune]]) =
var pos = editor.buffer.text.find(inputs[0], editor.primary_cursor().get_pos + 1)
if pos == -1:
pos = editor.buffer.text.find(inputs[0])
if pos != -1:
editor.jump(pos)
proc set_indent_width(editor: Editor, inputs: seq[seq[Rune]]) =
try:
let width = parse_int($inputs[0])
if width <= 1:
editor.show_info(@["Invalid Indent Width: " & $inputs[0]])
return
editor.buffer.indent_width = width
editor.hide_prompt()
except ValueError:
editor.show_info(@["Invalid Indent Width: " & $inputs[0]])
proc save_as(editor: Editor, inputs: seq[seq[Rune]]) =
if inputs[0].len == 0:
return
let
path = absolute_path($inputs[0])
(dir, file) = split_path(path)
if not exists_dir(dir):
editor.show_info(@[dir & " does not exist"])
return
if editor.buffer.file_path != "" and
editor.buffer.file_path in editor.app.buffers:
editor.app.buffers.del(editor.buffer.file_path)
editor.buffer.set_path(path, editor.app.languages)
editor.app.buffers[path] = editor.buffer
try:
editor.buffer.save()
except IOError as err:
editor.show_info(@["Error: " & err.msg])
return
editor.hide_prompt()
let comp = editor.app.get_autocompleter(editor.buffer.language)
if not comp.is_nil:
editor.autocompleter = comp
comp.track(editor.buffer)
proc select_all(editor: Editor) =
editor.cursors = @[Cursor(
kind: CursorSelection,
start: 0,
stop: editor.buffer.len
)]
proc delete_selections(editor: Editor) =
for it, cursor in editor.cursors:
if cursor.kind != CursorSelection:
continue
let cur = cursor.sort()
editor.buffer.delete(cur.start, cur.stop)
editor.cursors[it] = Cursor(kind: CursorInsert, pos: cur.start)
proc copy(editor: Editor) =
for cursor in editor.cursors:
if cursor.kind == CursorSelection:
let
cur = cursor.sort()
text = editor.buffer.slice(cur.start, cur.stop)
editor.app.copy_buffer.copy(text)
proc insert(editor: Editor, chr: Rune) =
for it, cursor in editor.cursors:
case cursor.kind:
of CursorInsert:
editor.buffer.insert(cursor.pos, chr)
of CursorSelection:
let cur = cursor.sort()
editor.cursors[it] = Cursor(kind: CursorInsert, pos: cur.start + 1)
editor.cursors[it] = Cursor(kind: CursorInsert, pos: cur.stop)
editor.buffer.replace(cur.start, cur.stop, @[chr])
proc insert(editor: Editor, str: seq[Rune]) =
for it, cursor in editor.cursors:
case cursor.kind:
of CursorInsert:
editor.buffer.insert(cursor.pos, str)
of CursorSelection:
let cur = cursor.sort()
editor.cursors[it] = Cursor(kind: CursorInsert, pos: cur.stop)
editor.buffer.replace(cur.start, cur.stop, str)
proc load_file(editor: Editor, path: string) =
editor.buffer.unregister_hook(editor.cursor_hook_id)
editor.buffer = editor.app.new_buffer(path)
editor.cursor_hook_id = editor.buffer.register_hook(editor.make_cursor_hook())
editor.hide_prompt()
editor.cursors = @[Cursor(kind: CursorInsert, pos: 0)]
editor.autocompleter = editor.app.get_autocompleter(editor.buffer.language)
proc new_buffer(editor: Editor) =
editor.buffer.unregister_hook(editor.cursor_hook_id)
editor.buffer = new_buffer()
editor.cursor_hook_id = editor.buffer.register_hook(editor.make_cursor_hook())
editor.hide_prompt()
editor.cursors = @[Cursor(kind: CursorInsert, pos: 0)]
editor.autocompleter = nil
proc completion_query(editor: Editor): seq[Rune] =
if editor.autocompleter == nil:
return
var pos = editor.primary_cursor().get_pos() - 1
while pos >= 0 and
editor.buffer[pos] notin editor.autocompleter.triggers and
editor.buffer[pos] notin editor.autocompleter.finish:
result = editor.buffer[pos] & result
pos -= 1
proc filter_completions(editor: Editor): seq[Symbol] =
if editor.completions.len == 0:
return @[]
let query = editor.completion_query()
if query.len == 0:
return editor.completions
return editor.completions.search(query)
proc compute_line_numbers_width(editor: Editor): int
method process_mouse(editor: Editor, mouse: Mouse): bool =
if editor.dialog.kind != DialogNone:
return editor.dialog.process_mouse(editor, mouse)
editor.detach_scroll = true
let
line_numbers_width = editor.compute_line_numbers_width() + 1
prompt_size = editor.prompt.compute_size()
pos = editor.scroll + Index2d(x: mouse.x - line_numbers_width - 1, y: mouse.y - 1)
if mouse.y >= editor.window_size.y - prompt_size:
let field = mouse.y - (editor.window_size.y - prompt_size) - 1
if editor.prompt.kind != PromptActive and
editor.prompt.kind != PromptInactive:
return
case mouse.kind:
of MouseDown:
editor.prompt.kind = PromptActive
if mouse.button == 0:
if field >= 0 and field < editor.prompt.fields.len:
editor.prompt.selected_field = field
var mouse_rel = mouse
mouse_rel.x = mouse.x - editor.prompt.fields[field].title.len
mouse_rel.y = 0
editor.prompt.fields[field].entry.process_mouse(mouse_rel)
of MouseMove, MouseUp:
let selected = editor.prompt.selected_field
var mouse_rel = mouse
mouse_rel.x = mouse.x - editor.prompt.fields[selected].title.len
mouse_rel.y = 0
editor.prompt.fields[selected].entry.process_mouse(mouse_rel)
else: discard
return
elif mouse.y == 0:
if mouse.x < line_numbers_width:
return true
return
case mouse.kind:
of MouseDown:
if mouse.button == 0:
case mouse.clicks:
of 1:
editor.jump(editor.buffer.display_to_index(Index2d(
x: pos.x.max(0),
y: pos.y.min(editor.buffer.lines.len - 1).max(0)
)))
of 2:
let
index = editor.buffer.display_to_index(Index2d(
x: pos.x.max(0),
y: pos.y.min(editor.buffer.lines.len - 1).max(0)
))
(start, stop) = editor.buffer.word_range(index)
editor.jump(start, stop)
of 3:
let
line = pos.y.min(editor.buffer.lines.len - 1).max(0)
(start, stop) = editor.buffer.line_range(line)
if start != -1 and stop != -1:
editor.jump(start, stop)
else: discard
if editor.prompt.kind == PromptActive:
editor.prompt.kind = PromptInactive
of MouseUp, MouseMove:
if (mouse.kind == MouseUp and
mouse.button == 0 and
mouse.clicks == 1) or
(mouse.kind == MouseMove and mouse.buttons[0]):
let stop = editor.buffer.display_to_index(Index2d(
x: pos.x.max(0),
y: pos.y.min(editor.buffer.lines.len - 1).max(0)
))
case editor.primary_cursor().kind:
of CursorInsert:
let start = editor.primary_cursor().pos
if start != stop:
editor.cursors = @[Cursor(kind: CursorSelection, start: start, stop: stop)]
of CursorSelection:
let start = editor.primary_cursor().start
editor.cursors = @[Cursor(kind: CursorSelection, start: start, stop: stop)]
of MouseScroll:
editor.scroll.y += mouse.delta * 2
else: discard
proc select_next(editor: Editor) =
if editor.primary_cursor().kind == CursorSelection:
let
cur = editor.primary_cursor().sort()
text = editor.buffer.slice(cur.start, cur.stop)
var pos = editor.buffer.text.find(text, cur.stop)
if pos == -1:
pos = editor.buffer.text.find(text)
if pos != -1 and pos != cur.start:
editor.cursors.add(Cursor(kind: CursorSelection, start: pos, stop: pos + text.len))
editor.merge_cursors()
proc jump_back(editor: Editor) =
if editor.jump_stack.len > 0:
editor.cursors = @[Cursor(kind: CursorInsert, pos: editor.jump_stack.pop())]
proc cut(editor: Editor) =
editor.copy()
editor.delete_selections()
proc paste(editor: Editor) =
editor.delete_selections()
editor.insert(editor.app.copy_buffer.paste())
proc show_find(editor: Editor) =
editor.show_prompt("Find", @["Pattern: "], callback=find_pattern)
proc show_goto(editor: Editor) =
editor.show_prompt("Go to Line", @["Line: "], callback=goto_line)
proc show_set_indent_width(editor: Editor) =
editor.show_prompt(
"Set Indent Width",
@["Width: "],
callback=set_indent_width
)
proc show_save_as(editor: Editor) =
editor.show_prompt(
"Save As",
@["File Name:"],
callback=proc (editor: Editor, inputs: seq[seq[Rune]]) =
let path = $inputs[0]
if exists_file(path):
editor.show_prompt(
"A file named " & path &
" already exists. Do you want to replace it?",
@["Replace (y/n): "],
callback=proc (editor: Editor, replace_inputs: seq[seq[Rune]]) =
if to_lower($replace_inputs[0]).starts_with("y"):
editor.save_as(inputs)
else:
editor.hide_prompt()
)
else:
editor.save_as(inputs)
)
proc save(editor: Editor) =
if editor.buffer.file_path == "":
editor.show_save_as()
else:
try:
editor.buffer.save()
except IOError as err:
editor.show_info(@["Error: " & err.msg])
return
editor.show_info(@["File saved."])
proc show_quick_open(editor: Editor) =
editor.dialog = Dialog(
kind: DialogQuickOpen,
quick_open: make_quick_open(editor.app)
)
proc show_find_def(editor: Editor) =
editor.dialog = Dialog(
kind: DialogFindDef,
find_def: make_find_def(editor)
)
proc only_primary_cursor(editor: Editor) =
var cur = editor.primary_cursor()
editor.cursors = @[cur]
proc pop_cursor(editor: Editor) =
if editor.cursors.len > 1:
discard editor.cursors.pop()
proc jump_to_matching_brackets(editor: Editor, select: bool = false) =
for cursor in editor.cursors.mitems:
if cursor.kind != CursorInsert:
continue
let match_pos = editor.buffer.match_bracket(cursor.pos)
if match_pos != -1:
if select:
cursor = Cursor(kind: CursorSelection,
start: cursor.pos, stop: match_pos
)
if cursor.start < cursor.stop:
cursor.stop += 1
else:
cursor.start += 1
else:
cursor = Cursor(kind: CursorInsert,
pos: match_pos
)
proc trigger_autocomplete(editor: Editor, chr: Rune) =
let
pos = editor.primary_cursor().get_pos()
start_time = get_time()
editor.autocompleter.complete(
editor.buffer, pos, chr,
proc (comps: seq[Symbol]) =
if editor.completion_time > start_time:
return
editor.completions = comps
editor.completion_time = start_time
)
method process_key(editor: Editor, key: Key) =
if editor.autocompleter != nil:
editor.autocompleter.poll()
if key.kind != KeyUnknown and key.kind != KeyNone:
editor.detach_scroll = false
if key.kind == KeyChar and key.ctrl and key.chr == Rune('e'):
if editor.dialog.kind != DialogNone:
editor.dialog = Dialog(kind: DialogNone)
elif editor.prompt.kind != PromptNone:
editor.hide_prompt()
else:
editor.completions = @[]
editor.completion_time = get_time()
return
if editor.dialog.kind != DialogNone:
editor.dialog.process_key(editor, key)
return
elif editor.prompt.kind == PromptActive:
if key.kind == KeyReturn:
editor.prompt.callback(editor, editor.prompt.get_inputs())
return
editor.prompt.process_key(key)
return
defer: editor.buffer.finish_undo_frame()
var clear_completions = true
case key.kind:
of KeyArrowLeft:
for it, cursor in editor.cursors:
var delta = -1
if key.ctrl:
delta = (editor.buffer.skip(cursor.get_pos() - 1, -1) + 1) - cursor.get_pos()
update(editor.cursors[it], delta, editor.buffer.len, key.shift)
of KeyArrowRight:
for it, cursor in editor.cursors:
var delta = 1
if key.ctrl:
delta = (editor.buffer.skip(cursor.get_pos(), 1)) - cursor.get_pos()
update(editor.cursors[it], delta, editor.buffer.len, key.shift)
of KeyArrowUp:
if key.ctrl and key.alt:
return
if key.alt and key.shift:
var index = editor.buffer.to_2d(editor.primary_cursor().get_pos())
while true:
index.y -= 1
if index.y < 0:
break
let cursor = Cursor(kind: CursorInsert, pos: editor.buffer.to_index(index))
if cursor notin editor.cursors:
editor.cursors.add(cursor)
break
else:
for it, cursor in editor.cursors:
var index = editor.buffer.to_2d(cursor.get_pos())
index.y -= 1
index.y = max(index.y, 0)
editor.update_cursor(it, editor.buffer.to_index(index), key.shift)
of KeyArrowDown:
if key.ctrl and key.alt:
return
if key.alt and key.shift:
var index = editor.buffer.to_2d(editor.primary_cursor().get_pos())
while true:
index.y += 1
if index.y > editor.buffer.lines.len - 1:
break
let cursor = Cursor(kind: CursorInsert, pos: editor.buffer.to_index(index))
if cursor notin editor.cursors:
editor.cursors.add(cursor)
break
else:
for it, cursor in editor.cursors:
var index = editor.buffer.to_2d(cursor.get_pos())
index.y += 1
index.y = min(index.y, editor.buffer.lines.len - 1)
editor.update_cursor(it, editor.buffer.to_index(index), key.shift)
of KeyReturn:
for it, cursor in editor.cursors:
if cursor.kind == CursorSelection:
continue
let
indent_level = editor.buffer.indentation(cursor.pos)
indent = editor.buffer.make_indent(indent_level)
editor.buffer.insert(cursor.pos, editor.buffer.newline_style.to_runes() & indent)
of KeyPageDown:
for it, cursor in editor.cursors:
var pos = editor.buffer.to_2d(cursor.get_pos())
pos.y += editor.window_size.y
pos.y = pos.y.min(editor.buffer.lines.len - 1)
editor.update_cursor(it, editor.buffer.to_index(pos), key.shift)
of KeyPageUp:
for it, cursor in editor.cursors:
var pos = editor.buffer.to_2d(cursor.get_pos())
pos.y -= editor.window_size.y
pos.y = pos.y.max(0)
editor.update_cursor(it, editor.buffer.to_index(pos), key.shift)
of KeyHome:
for it, cursor in editor.cursors:
var index = editor.buffer.to_2d(cursor.get_pos())
index.x = 0
editor.update_cursor(it, editor.buffer.to_index(index), key.shift)
of KeyEnd:
for it, cursor in editor.cursors:
var index = editor.buffer.to_2d(cursor.get_pos())
if index.y == editor.buffer.lines.len - 1:
editor.cursors[it] = Cursor(kind: CursorInsert, pos: editor.buffer.len)
continue
index.y += 1
index.y = min(index.y, editor.buffer.lines.len - 1)
index.x = 0
let pos = (editor.buffer.to_index(index) - 1).max(0)
editor.update_cursor(it, pos, key.shift)
of KeyBackspace:
clear_completions = false
for it, cursor in editor.cursors:
case cursor.kind
of CursorSelection:
let cur = cursor.sort()
if editor.completions.len > 0 and editor.autocompleter != nil and not clear_completions:
for chr in editor.buffer.text.substr(cur.start, cur.stop):
if chr in editor.autocompleter.triggers:
clear_completions = true
break
editor.buffer.delete(cur.start, cur.stop)
editor.cursors[it] = Cursor(kind: CursorInsert, pos: cur.start)
of CursorInsert:
if cursor.pos > 0:
if editor.completions.len > 0 and editor.autocompleter != nil and not clear_completions:
for chr in editor.buffer.text.substr(cursor.pos - 1, cursor.pos):
if chr in editor.autocompleter.triggers:
clear_completions = true
break
editor.buffer.delete(cursor.pos - 1, cursor.pos)
of KeyDelete:
for it, cursor in editor.cursors:
case cursor.kind:
of CursorSelection:
let cur = cursor.sort()
editor.buffer.delete(cur.start, cur.stop)
editor.cursors[it] = Cursor(kind: CursorInsert, pos: cur.start)
of CursorInsert:
if cursor.pos >= editor.buffer.len:
continue
editor.buffer.delete(cursor.pos, cursor.pos + 1)
of KeyEscape:
editor.only_primary_cursor()
of KeyPaste:
editor.insert(key.text)
of KeyChar:
if key.ctrl:
case key.chr:
of Rune('i'):
let comps = editor.filter_completions()
if comps.len > 0:
let
text = comps[0].name
query = editor.completion_query()
for cursor in editor.cursors:
if cursor.kind != CursorInsert:
continue
editor.buffer.replace(cursor.pos - query.len, cursor.pos, text)