-
Notifications
You must be signed in to change notification settings - Fork 4
/
code-editor.jc
5088 lines (5044 loc) · 157 KB
/
code-editor.jc
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
import "system.jc"
import "gui2d.jc"
import "javascript.jc"
import "text-box.jc"
import "tex-like.jc"
import "hunspell.jc"
import "smart-find.jc"
import "code-parser.jc"
import "g-l.jc"
import "leak-detection.jc"
import "encoding.jc"
import System.Math.*
import System.Algorithm.*
import System.Console.*
import Gui2D.detail.*
import Javascript.*
import TextBox.*
import TextBox.detail.*
import TexLike.*
import SmartFind.*
import CodeParser.*
import Encoding.*
import GL.*
USE_BLIT_FRAMEBUFFER=0
//text -> i64[]
auto WrapTextSimple(CHyphenator hyp,string sblock, TFont font,i64 w_line, int m_tab_width)
styles=new CStyleHolder(){m_styles:[new CStyle(){font:font}],m_enable_ligatures:0}
ret=new i64[]
foreach s,I in sblock.TokenizeEx("\n",sblock.TOKENIZE_ALLOW_EMPTY|sblock.TOKENIZE_INCLUDE_SEPARATOR)
n0=ret.n
(atoms,tail_style_id,indent_point_atom)=TextToLayoutAtom(hyp,s,m_tab_width,w_line,0,styles,CEmbededObject[].NULL)
line_breaks=GreedyLayoutGeneral(atoms,w_line)
GenerateTextLayout(
styles,
s,atoms,line_breaks,tail_style_id,
int(I),0LL,w_line,s.EndsWith("\n"),0,
inline(int style_id, int ccnt0,int ccnt1){
//char range
assert(ccnt0>=0&&ccnt1>ccnt0)
ret.push((i64(ccnt1)<<32)+i64(ccnt0))
//atom_i=ret.back()
//value1=int(atom_i>>32)
//value0=int(atom_i)
//assert(value0>=0&&value1>value0)
},
inline(i64 dw,int ccnt){
//space
assert(dw>=0LL&&dw==i64(int(dw)))
ret.push((i64(0x80000000|int(ccnt))<<32)+i64(dw))
//atom_i=ret.back()
//value1=int(atom_i>>32)
//value0=int(atom_i)
//if value1>=0:
// if !(value0>=0&&value1>value0):
// Writeln(ccnt,' ',dw)
// assert(value0>=0&&value1>value0)
},
inline(i64 h_up,i64 h_line,int aflag_ccnt){
//line
//there is only one style, we know the heights
assert(aflag_ccnt>=0)
ret.push((-1LL<<32)+i64(aflag_ccnt))
//atom_i=ret.back()
//value1=int(atom_i>>32)
//value0=int(atom_i)
//if value1>=0:assert(value0>=0&&value1>value0)
},
inline(int oid, int ccnt){
//object
//do nothing
})
if n0<ret.n:
//shove back real newlines to \n
high_dword=int(ret[n0]>>32)
if high_dword==-1:
ccnt=(int(ret[n0])&ATOM_MASK_CCNT)
if ccnt>0&&sblock[ccnt-1]=='\n':
ret[n0]-=1LL
return ret
/*
or-ing tokens
<
<script
failure case:
a lot of \\ \"
token ambiguity cross the boundary
ignore
---
an escape char mode
self-zeroing token eater
it breaks the tokenization assumption
*/
REAL_TYPE_MOV=0;
REAL_TYPE_XOR=1;
REAL_TYPE_ADD=2;
//DEFAULT_WRAP_WIDTH_IN_CHARS=2048LL
/*
at 65536 chars we have about 27 bits in m_wrap_width_in_fp, that leaves 37 bits for line numbers
this is well enough for a wiki dump
*/
DEFAULT_WRAP_WIDTH_IN_CHARS=65536LL
GRACEFUL_WORD_SIZE=256
MAX_ALLOWED_INDENTATION=20
MAX_SPECIAL_COLOR_ID_SIZE=iptr(GRACEFUL_WORD_SIZE)
struct TBracketType
int type
int[] tok0,tok1
//0+=0 for non-inc
struct TBDFATransition
i32 m_and
i32 m_xor
i16 m_delta
u16 m_next_state
struct TColorRule
string color_name
//int bid
//int extend_0,extend_1
int bmask
int extend_flags
struct TKeyword
string s
string color_name
class CLanguageDescription
//int[] m_enabled_from_inside
int[] m_all_key_states
int[] m_token_enabling_masks
int[] m_id_from_key_state
u8[] m_char_map
u16[] m_bigchar_dfa
i16[] m_bigchar_dfa_ops
int m_total_state_size
int m_context_size
int m_n_char_types
int m_n_key_states
u8[] m_token_lengths
int m_n_tokens
//the initial state is always 0
/////////
//TBracketType[] m_bracket_types
TBDFATransition[] m_bracket_dfa
TColorRule[] m_coloring_rules
/////////
string m_default_color_name
string[] m_word_color_names
int[] m_word_dfa
u8[] m_word_char_map
uint4 m_word_char_set
int m_n_word_char_types
int[] m_word_dfa_initial_state
int[] m_word_dfa_initial_state_triggered
string m_trigger_chars
/////////
int m_color_id_spell_check
/////////
int[] m_key_decl_color_ids
int m_kd_color
auto DrawTilde(TFont fnt_tilde,float x0,float x1,float y,int C)
//just use a glyph - '~' in the icon font!
//its height is 0.03*h, width is 1*h...
h_tilde_raw=g_renderer.GetCharacterHeight(fnt_tilde)
w_tilde=g_renderer.GetCharacterAdvance(fnt_tilde,int('~'))
for(x=x0;x<x1;x+=w_tilde)
need_clipping=0
if x+w_tilde>x1:
need_clipping=1
g_renderer.PushCliprect(x,y,x1-x,h_tilde_raw)
g_renderer.DrawChar(fnt_tilde,x,y,C,int('~'))
if need_clipping:
g_renderer.PopCliprect()
HL_DISPLAY_MODE_EMBOLDEN=2
HL_DISPLAY_MODE_TILDE=3
OVERLAY_AE=0
OVERLAY_IME=1
//U_ELLIPSIS=0x2026
struct TEllipsisItem
float x,y,scale
int color
struct TOverlayItem
i64 flatx
i64 y,delta_x
string s_text
int type
int sel0,sel1
i64 w_strikeout
struct TTildeItem
i64 ccnt0,ccnt1
int color
class CCodeEmbededObject
CLocator loc
JSObject obj
i64 dy//<measured in # lines
class CLayouter_programmer
FLAG_HAS_TAB=1
FLAG_HAS_ENTER=2
/////////
m_wrap_width_in_fp=0LL
m_tab_width_in_fp=0LL
m_tab_width=4
m_wrap_width=0.f
m_font=TFont()
m_font_emboldened=TFont()
m_bgcolor_selection=0xff800000
m_color_overlay=0xff000000
m_color_embolden=0xff000000
m_color_virtual_diff_bold=0xff000000
//////////////
m_enable_advanced_wrapping=0
CHyphenator m_hyp
//////////////
__pointer m_spell_checker
string m_spell_checker_name
long2[] m_current_frame_spell_errors
TTildeItem[] m_current_frame_tildes
float3[] m_current_frame_strikeouts
int m_temporarily_disable_spell_check
int m_enable_ceo_rendering
//////////////
//JSObject m_style
CLanguageDescription m_lang
int[] m_cached_colors
int m_cached_colors_p_word_part
int m_cached_color_virtual_hyphen
//////////////
//int[] m_char_width_cache_ascii
//int[int] m_char_width_cache
int[int] m_char_kerning_cache
/////////////////
m_hidden_ranges=new CLocator[]
m_ceos=new CCodeEmbededObject[]
i64 m_w_ellipsis
m_ellipsis_items=new TEllipsisItem[]
/////////////////
CFileIndex m_file_index
/////////////////
charWidth=function(int ch){
//if u32(ch)<u32(0x80):
// ret=m_char_width_cache_ascii[ch]
// if !ret:
// ret=max(int(float2fixed(g_renderer.GetCharacterAdvance(m_font,int(ch)))),1)+1
// m_char_width_cache_ascii[ch]=ret
//else
// ret=m_char_width_cache[ch]
// if !ret:
// ret=max(int(float2fixed(g_renderer.GetCharacterAdvance(m_font,int(ch)))),1)+1
// m_char_width_cache[ch]=ret
//return ret-1
return max(int(float2fixed(g_renderer.GetCharacterAdvance(m_font,int(ch)))),1)
}
charKerning=function(int ch0,int ch1){
if u32(ch0)>=65536u||u32(ch1)>=65536u:return 0
key=(ch0<<16)+ch1
ret=m_char_kerning_cache[key]
if !ret:
//we can get negative/0/positive, use 0x80000000 as the improbable
ret=int(float2fixed(g_renderer.GetKerning(m_font,ch0,ch1)))^0x80000000
return ret^0x80000000
}
charHeight=inline(){
return float2fixed(g_renderer.GetCharacterHeight(m_font))
}
/////////////////
//this thing isn't line-only
name=function(){return "renderer"}
stateSize=function(){return 5}
/////////////////
isLineOnly=function(){
return m_enable_advanced_wrapping
}
PreprocessBlock=function(CEditableText caller,i64[] a,string s){
if m_enable_advanced_wrapping:
//-' '-1LL to avoid OOB values
return WrapTextSimple(m_hyp,s, m_font,m_wrap_width_in_fp-charWidth(int(' '))-1LL, m_tab_width)
else
return i64[].NULL
}
/////////////////
//final format: pre-tab width, has-tabbed-ness, tab-enter width, has-enter-ness, post-enter width, max width
//do not use a render-central design
AddString=function(i64[] a,i64[] pp,string s_context,string s,iptr ofs){
w_pre_tab=a[0]
w_tab_enter=a[1]
w_post_enter=a[2]
flags=int(a[3])
w_max=a[4]
if m_enable_advanced_wrapping:
//the fancy mode only uses w_pre_tab
w_pre_tab=_RenderText(MODE_BB,0LL,a,pp,s,ofs, u8[].NULL, 0LL,0LL,0LL,0LL,0.f,0.f,0.f)
w_max=a[4]
//flags|=FLAG_HAS_ENTER
else
foreach ch,I in Utf8Chars(s)
if I>=ofs:break
if ch=='\t':
if !(flags&FLAG_HAS_ENTER):
if !(flags&FLAG_HAS_TAB):
flags|=FLAG_HAS_TAB
else
w_tab_enter=(w_tab_enter/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
else
w_post_enter=(w_post_enter/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
else if ch=='\n':
w_curline=0LL
if !(flags&FLAG_HAS_ENTER):
flags|=FLAG_HAS_ENTER
w_curline=w_pre_tab
if flags&FLAG_HAS_TAB:
w_curline=(w_curline/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
w_curline+=w_tab_enter
else
y=w_post_enter/m_wrap_width_in_fp
w_curline=w_post_enter-y*m_wrap_width_in_fp
w_post_enter=(y+1LL)*m_wrap_width_in_fp
if w_max<w_curline:w_max=w_curline
else if ch!='\r':
dx=charWidth(ch)
if !(flags&FLAG_HAS_ENTER):
if !(flags&FLAG_HAS_TAB):
w_pre_tab+=dx
else
w_tab_enter+=dx
else
w_post_enter+=dx
w_curline=0LL
if !(flags&FLAG_HAS_ENTER):
w_curline=w_pre_tab
if flags&FLAG_HAS_TAB:
w_curline=(w_curline/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
w_curline+=w_tab_enter
else
y=w_post_enter/m_wrap_width_in_fp
w_curline=w_post_enter-y*m_wrap_width_in_fp
if w_max<w_curline:w_max=w_curline
a[0]=w_pre_tab
a[1]=w_tab_enter
a[2]=w_post_enter
a[3]=i64(flags)
a[4]=w_max
}
Add=function(i64[] a,i64[] b){
a_w_pre_tab=a[0]
a_w_tab_enter=a[1]
a_w_post_enter=a[2]
a_flags=int(a[3])
a_w_max=int(a[4])
b_w_pre_tab=b[0]
b_w_tab_enter=b[1]
b_w_post_enter=b[2]
b_flags=int(b[3])
b_w_max=int(b[4])
if a_flags&FLAG_HAS_ENTER:
//a_w_post_enter
a_w_post_enter+=b_w_pre_tab
if b_flags&FLAG_HAS_TAB:
a_w_post_enter=(a_w_post_enter/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
a_w_post_enter+=b_w_tab_enter
y=a_w_post_enter/m_wrap_width_in_fp
w_curline=a_w_post_enter-y*m_wrap_width_in_fp
if a_w_max<w_curline:a_w_max=w_curline
if b_flags&FLAG_HAS_ENTER:
a_w_post_enter=(y+1LL)*m_wrap_width_in_fp
a_w_post_enter+=b_w_post_enter
else if a_flags&FLAG_HAS_TAB:
a_w_tab_enter+=b_w_pre_tab
if b_flags&FLAG_HAS_TAB:
a_w_tab_enter=(a_w_tab_enter/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
a_w_tab_enter+=b_w_tab_enter
if b_flags&FLAG_HAS_ENTER:
a_flags|=FLAG_HAS_ENTER
a_w_post_enter=b_w_post_enter
else
a_w_pre_tab+=b_w_pre_tab
a_flags=b_flags
a_w_tab_enter=b_w_tab_enter
a_w_post_enter=b_w_post_enter
//retest the first line for max - merging single-line a with non-single-line b
if m_enable_advanced_wrapping:
//don't max w_curline into it, it's shit in this mode
a_w_max=a[4]
else
w_curline=a_w_pre_tab
if a_flags&FLAG_HAS_TAB:
w_curline=(w_curline/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
w_curline+=a_w_tab_enter
if a_w_max<w_curline:a_w_max=w_curline
a[0]=a_w_pre_tab
a[1]=a_w_tab_enter
a[2]=a_w_post_enter
a[3]=i64(a_flags)
a[4]=max(a_w_max,b_w_max)
}
GetFlatX=inline(i64[] a){
w_pre_tab=a[0]
w_tab_enter=a[1]
w_post_enter=a[2]
flags=int(a[3])
x=w_pre_tab
if flags&FLAG_HAS_TAB:
x=(x/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
x+=w_tab_enter
if flags&FLAG_HAS_ENTER:
x=(x/m_wrap_width_in_fp+1LL)*m_wrap_width_in_fp
x+=w_post_enter
return x
}
GetFlatXEnMasse=inline(i64[] a){
auto ret=new i64[a.n/5]
for i=0:ret.n-1
w_pre_tab=a[i*5+0]
w_tab_enter=a[i*5+1]
w_post_enter=a[i*5+2]
flags=int(a[i*5+3])
x=w_pre_tab
if flags&FLAG_HAS_TAB:
x=(x/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
x+=w_tab_enter
if flags&FLAG_HAS_ENTER:
x=(x/m_wrap_width_in_fp+1LL)*m_wrap_width_in_fp
x+=w_post_enter
ret[i]=x
return ret
}
XYToX=inline(i64 x,i64 y){
hc=charHeight()
return max(min(x,m_wrap_width_in_fp-1LL),0LL)+(y/hc)*m_wrap_width_in_fp
}
/////////////////
//compare known-positive doubles as ints
Compare=function(CEditableText ed,i64[] a,i64[] b){
return __C_compare(GetFlatX(a),AddHiddenRanges(ed,b[0],b[1]))
}
SeekInBlock=function(CEditableText ed,i64 ccnt_base,i64[] a,i64[] pp,string s,i64[] b){
if m_enable_advanced_wrapping:
return iptr(_RenderText(MODE_SEEK,0LL,a,pp,s,s.n, u8[].NULL, b[0],b[1],0LL,0LL,0.f,0.f,0.f))
//seeked into hidden text?
x_goal=AddHiddenRanges(ed,b[0],b[1])
x=GetFlatX(a)
is_end=0
if b[0]>=m_wrap_width_in_fp:
is_end=1
if b.n>2:
//hint
x_last_known=AddHiddenRanges(ed,b[2],b[3])
ccnt_last_known=b[4]
if u64(ccnt_last_known-ccnt_base)<u64(s.n):
p=iptr(ccnt_last_known-ccnt_base)
x=x_last_known
foreach ch,I in Utf8Chars(s[p:])
if ch=='\t':
x=(x/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
else if ch=='\n':
x=(x/m_wrap_width_in_fp+1LL)*m_wrap_width_in_fp
else if ch!='\r':
dx=charWidth(ch)
x+=dx
if x>x_goal:
if is_end:
is_end=0
else
return I+p
return s.n
foreach ch,I in Utf8Chars(s)
if ch=='\t':
x=(x/m_tab_width_in_fp+1LL)*m_tab_width_in_fp
else if ch=='\n':
x=(x/m_wrap_width_in_fp+1LL)*m_wrap_width_in_fp
else if ch!='\r':
dx=charWidth(ch)
x+=dx
if x>x_goal:
if is_end:
is_end=0
else
return I
return s.n
}
/////////////////
//long3 m_caret_overlay
m_overlays=new TOverlayItem[]
//JSObject m_overlay_obj
i64 m_caret_offset
JSObject m_tentative_editops
JSObject m_virtual_diffs
TOverlayItem[] m_precomputed_overlays_for_tentative_editops//need to reset in JS
i64[] m_overlay_embolden_ranges
SetTextStyle=function(JSObject obj){
m_lang=obj["language"].as(CLanguageDescription)
m_font=obj["font"].as(TFont)
m_font_emboldened=obj["font_emboldened"].as(TFont)
//m_char_width_cache_ascii=new int[128]
//m_char_width_cache=new int[int]
m_char_kerning_cache=new int[int]
m_tab_width=obj["tab_width"].or(8)
m_wrap_width=obj["wrap_width"].or(0.f)
m_tab_width_in_fp=i64(m_tab_width)*charWidth(int(' '))
m_hyp=obj["hyphenator"].as(CHyphenator)
m_enable_advanced_wrapping=!!m_hyp
if !(m_wrap_width>0.f):
m_wrap_width_in_fp=DEFAULT_WRAP_WIDTH_IN_CHARS*charWidth(int(' '))
m_wrap_width_in_fp=(m_wrap_width_in_fp/m_tab_width_in_fp)*m_tab_width_in_fp
//Writeln(m_wrap_width_in_fp)
else
m_wrap_width_in_fp=(float2fixed(m_wrap_width)/m_tab_width_in_fp)*m_tab_width_in_fp
obj["displayed_wrap_width"]=fixed2double(m_wrap_width_in_fp)
////////////
BeginRender(obj)
/////////
sname=obj["m_spell_checker"].as(string)
if sname&&sname.n:
m_spell_checker=Hunspell.getSpeller(sname)
if m_spell_checker:
m_spell_checker_name=sname
}
BeginRender=function(JSObject obj){
if Platform.BUILD=="debug":
if !m_lang:
obj.GetContext().evalVoid("UI.DumpCallStack()","<stack>")
m_cached_colors_p_word_part=int(m_lang.m_coloring_rules.n+1)
m_cached_colors=new int[m_cached_colors_p_word_part+m_lang.m_word_color_names.n]
m_cached_colors[0]=obj[m_lang.m_default_color_name].or(0xff000000)
foreach crule,I in m_lang.m_coloring_rules
m_cached_colors[I+1]=obj[crule.color_name].or(0xff000000)
foreach color_name,I in m_lang.m_word_color_names
m_cached_colors[m_cached_colors_p_word_part+I]=obj[color_name].or(0xff000000)
m_bgcolor_selection=int(obj["bgcolor_selection"].or(u32(m_bgcolor_selection)))
m_color_overlay=int(obj["color_overlay"].or(u32(m_color_overlay)))
m_color_embolden=int(obj["color"].or(u32(m_color_embolden)))
m_cached_color_virtual_hyphen=int(obj["color_hyphen"].or(u32(m_cached_color_virtual_hyphen)))
if m_spell_checker&&!m_temporarily_disable_spell_check:
m_current_frame_spell_errors=new long2[]
m_current_frame_tildes=new TTildeItem[]
m_current_frame_strikeouts=new float3[]
m_w_ellipsis=float2fixed(obj["w_ellipsis"].as(float))
m_ellipsis_items.clear()
edobj=obj["ed"].as(JSObject);
if edobj:
m_file_index=edobj["m_file_index"].as(CFileIndex)
else
m_file_index=CFileIndex.NULL
m_color_virtual_diff_bold=obj["color_virtual_diff_bold"].as(int)
}
SetCaretOverlay=function(CEditableText caller,i64 x,i64 y,JSObject obj){
m_overlays.clear()
//m_overlay_obj=obj
w_overlayd=0.
m_caret_offset=0LL
if obj:
s=obj["text"].as(string)
if s:
w_overlayd=GetStringWidth(m_font,s)
flatx=AddHiddenRanges(caller,x,y)
overlay_sel0=obj["start"].or(0L)
overlay_sel1=overlay_sel0+obj["length"].or(0L)
cmap=new iptr[]
foreach chj,J in Utf8Chars(s)
cmap.push(J)
cmap.push(s.n)
overlay_sel0=min(overlay_sel0,cmap.n-1)
overlay_sel1=min(overlay_sel1,cmap.n-1)
x_sel0=0LL
x_sel1=0LL
if uptr(overlay_sel0)<uptr(cmap.n)&&uptr(overlay_sel1)<uptr(cmap.n):
//measuring
x_sel0=double2fixed(GetStringWidth(m_font,s[:cmap[overlay_sel0]-1]))
x_sel1=double2fixed(GetStringWidth(m_font,s[:cmap[overlay_sel1]-1]))
m_caret_offset=max(x_sel0,x_sel1)
m_overlays.push(TOverlayItem(){
flatx:flatx,
y:(flatx+m_wrap_width_in_fp-1LL)/m_wrap_width_in_fp-1LL,
delta_x:double2fixed(w_overlayd),
s_text:s,
type:obj["type"].or("IME")=="IME"?OVERLAY_IME:OVERLAY_AE,
sel0:x_sel0,
sel1:x_sel1,
w_strikeout:0LL,
})
if !m_precomputed_overlays_for_tentative_editops:
prec_overlays=new TOverlayItem[]
m_precomputed_overlays_for_tentative_editops=prec_overlays
if m_tentative_editops:
n3=m_tentative_editops.length()
n=n3/3
ccnts=new i64[]
for i=0:3:n3-3
ccnt_i=m_tentative_editops[i+0].as(i64)
ccnts.push(ccnt_i)
ccnts.push(ccnt_i+m_tentative_editops[i+1].as(i64))
//////
hid=caller.m_handler_registration["renderer"].as(int)
a=caller.GetStateEnMasse(hid,ccnts)
flatxs=GetFlatXEnMasse(a)//[GetFlatX(a[i*5:]) for i=0:n*2-1]
for i=0:n-1
s_i=m_tentative_editops[i*3+2].as(string)
if !s_i:
s_i=""
else if s_i.IndexOf('\t')>=0||s_i.IndexOf('\n')>=0:
s_i=s_i.Replace(["\t","\u2192","\n","\u21b5"])
w_overlayd_i=GetStringWidth(m_font,s_i)
prec_overlays.push(TOverlayItem(){
flatx:flatxs[i*2+1],
y:(flatxs[i*2+1]+m_wrap_width_in_fp-1LL)/m_wrap_width_in_fp-1LL,
delta_x:double2fixed(w_overlayd_i),
s_text:s_i,
type:OVERLAY_AE,
sel0:0LL,
sel1:0LL,
w_strikeout:flatxs[i*2+1]-flatxs[i*2],
})
if m_precomputed_overlays_for_tentative_editops:
m_overlays.push(m_precomputed_overlays_for_tentative_editops)
m_overlay_embolden_ranges=new i64[]
if m_virtual_diffs:
ccnts=new i64[]
n3=m_virtual_diffs.length()
for i=0:3:n3-3
ccnt_j=m_virtual_diffs[i+0].as(i64)
sz_j=m_virtual_diffs[i+1].as(i64)
ccnts.push(ccnt_j)
ccnts.push(ccnt_j+sz_j)
n=n3/3
hid=caller.m_handler_registration["renderer"].as(int)
a=caller.GetStateEnMasse(hid,ccnts)
flatxs=GetFlatXEnMasse(a)//[GetFlatX(a[i*5:]) for i=0:n*2-1]
for i=0:n-1
ccnt_j=m_virtual_diffs[i*3+0].as(i64)
sz_j=m_virtual_diffs[i*3+1].as(i64)
if sz_j:
m_overlay_embolden_ranges.push(ccnt_j)
m_overlay_embolden_ranges.push(ccnt_j+sz_j)
s_i=m_virtual_diffs[i*3+2].as(string)
if !s_i:
s_i=""
else if s_i.IndexOf('\t')>=0:
s_i=s_i.Replace(["\t"," "])
w_overlayd_i=GetStringWidth(m_font,s_i)
m_overlays.push(TOverlayItem(){
flatx:flatxs[i*2+1],
y:(flatxs[i*2+1]+m_wrap_width_in_fp-1LL)/m_wrap_width_in_fp-1LL,
delta_x:double2fixed(w_overlayd_i),
s_text:s_i,
type:OVERLAY_AE,
sel0:0LL,
sel1:0LL,
w_strikeout:-double2fixed(w_overlayd_i),
})
m_overlays.Sortby(inline(item){return item.flatx})
w_overlay=double2fixed(w_overlayd)
//m_caret_overlay=long3(,0LL,w_overlay)
//m_caret_overlay.y=(m_caret_overlay.x+m_wrap_width_in_fp-1LL)/m_wrap_width_in_fp*m_wrap_width_in_fp
//m_caret_offset=0LL
return w_overlay
}
GetCaretOffset=function(){
return m_caret_offset
}
AddOverlayOffset=function(i64 x0,i64 y){
flatx0=x0+y*m_wrap_width_in_fp
p=m_overlays.InlineBisect(inline(a){return a.flatx<=flatx0})
p--
x=x0
for(;p>=0;p--)
if m_overlays[p].y==y:
x+=m_overlays[p].delta_x
else
break
return x
}
TestOverlay=inline(p_overlay, fnt_scaled,scale,x,projectXY){
if p_overlay>=m_overlays.n:return 0
oi=m_overlays[p_overlay]
if x!=oi.flatx:return 0
s_overlay=oi.s_text
auto prj_x=projectXY(x)
x_sel0=fixed2double(oi.sel0)
x_sel1=fixed2double(oi.sel1)
if x_sel0>x_sel1:
(x_sel0,x_sel1)=(x_sel1,x_sel0)
if oi.w_strikeout:
auto dy_strikeout=fnt_scaled.h*fnt_scaled.pfnt.y_baseline_per_height*0.46875f
flatx_s0=x-oi.w_strikeout
flatx_s1=x
if flatx_s0>flatx_s1:
tmp=flatx_s0
flatx_s0=flatx_s1
flatx_s1=tmp
y_s0=(flatx_s0)/m_wrap_width_in_fp
y_s1=(flatx_s1)/m_wrap_width_in_fp
for yi=y_s0:y_s1
fx0=max(yi*m_wrap_width_in_fp,flatx_s0)
fx1=min((yi+1)*m_wrap_width_in_fp-1,flatx_s1)
//Writeln('--- ',yi,' ',fx0,' ',fx1)
auto prj_fx0=projectXY(fx0)
auto prj_fx1=projectXY(fx1)
m_current_frame_strikeouts.push(float3(prj_fx0.x,prj_fx0.y,prj_fx1.x-prj_fx0.x))
//selection - pre-draw the highlight
if x_sel0<x_sel1:
g_renderer.DrawRectangle(prj_x.x+float(x_sel0)*scale,prj_x.y,float((x_sel1-x_sel0)*scale),g_renderer.GetCharacterHeight(fnt_scaled), m_bgcolor_selection)
xo=x
foreach chj,J in Utf8Chars(s_overlay)
dx_j=charWidth(chj)
prj_xo=projectXY(xo)
//g_renderer.DrawChar(fnt_scaled, prj_xo.x,prj_xo.y,m_color_overlay, chj)
g_renderer.FastDrawChar(prj_xo.x,prj_xo.y,m_color_overlay, chj)
xo+=dx_j
if oi.type==OVERLAY_IME:
y_underline=prj_x.y+fnt_scaled.h*(fnt_scaled.pfnt.y_baseline_per_height+0.02f)
g_renderer.DrawRectangle(prj_x.x,y_underline,fixed2float(oi.delta_x)*scale,max(fnt_scaled.h*0.05f,1.f), m_color_overlay)
return 1
}
RenderText=function(CEditableText ed,i64 ccnt_base,i64[] a,i64[] pp,string s, u8[] color_ids, i64 scroll_x,i64 scroll_y,i64 clip_w,i64 clip_h,float screen_x,float screen_y,float scale){
//word coloring - update color_ids
//get a GRACEFUL_WORD_SIZE context on each side - do it per-char
word_dfa=m_lang.m_word_dfa
word_char_map=m_lang.m_word_char_map
word_char_set=m_lang.m_word_char_set
n_word_char_types=m_lang.m_n_word_char_types
color_id_spell_check=m_lang.m_color_id_spell_check
key_decl_color_ids=m_lang.m_key_decl_color_ids
kd_color=m_lang.m_kd_color
//start from word_ccnt0, do per-char enum until we go out of block
word_ccnt0=ccnt_base
budget=GRACEFUL_WORD_SIZE
foreach ch,I in ed.enumUtf8Backward(ccnt_base)
if !isWordChar(ch):break
word_ccnt0=I
budget--
if budget<0:break
ccnt_block_end=ccnt_base+i64(s.n)
ch_before_word=ed.GetUtf8CharNeighborhood(word_ccnt0)[0]
trigger_chars=m_lang.m_trigger_chars
word_ccnt0_bak=word_ccnt0-1
s_word=new string
file_index=m_file_index
if file_index:
PrecomputeIndices(file_index)
color_table=file_index.m_temp_color_table
else
color_table=int[int].NULL
s_temp_getid=new char[MAX_SPECIAL_COLOR_ID_SIZE]
n_tokens=m_lang.m_n_tokens
isCustomWordChar=inline(int ch){
if ch>=128:
return int(TextBox.detail.g_unicode_wordchars.Bisect(function(int c0){return c0<=ch})&1)
else
return TextBox.detail.isInCharSet(word_char_set,ch)
}
for(;;)
//Writeln(word_ccnt0)
ch_first=0
foreach ch,I,I2 in ed.enumUtf8Forward(word_ccnt0)
word_ccnt0=I
if isCustomWordChar(ch):
ch_first=ch
break
ch_before_word=ch
word_ccnt0=I2
if word_ccnt0>=ccnt_block_end:
break
if word_ccnt0>=ccnt_block_end:break
//Writeln(word_ccnt0,' ',word_ccnt0_bak)
if word_ccnt0<=word_ccnt0_bak:
//blocked by invalid utf8
break
word_ccnt0_bak=word_ccnt0
//Writeln(word_ccnt0,' ',ccnt_block_end,' ',s[word_ccnt0])
//now we know we have a word
//non-word chars should immediately terminate the DFA
color_id=0
cid=color_ids[max(iptr(word_ccnt0-ccnt_base),0L)]
if u32(cid)<u32(m_lang.m_word_dfa_initial_state.n):
st=m_lang.m_word_dfa_initial_state[cid]
if trigger_chars&&ch_before_word<128&&trigger_chars.IndexOf(char(ch_before_word))>=0:
//it's immediately after the trigger char, use the alternative initial state
st=m_lang.m_word_dfa_initial_state_triggered[cid]
else
st=~0
if st>=0:
budget=GRACEFUL_WORD_SIZE
foreach ch,I in ed.enumCharsForward(word_ccnt0)//it's 8-bit here, not UTF8!
//and ch is signed
//non-word chars would inevitably terminate the DFA
chi=int(u8(ch))
st=word_dfa[st+int(word_char_map[chi])]
if st<0:break
budget--
if budget<0:break
if st>=0:st=word_dfa[st+1]
//Writeln(word_ccnt0,' ',cid,' ',st)
if color_table&&st==~kd_color&&key_decl_color_ids:
//parser-based highlighting
s_temp_getid.n=MAX_SPECIAL_COLOR_ID_SIZE
p_s_temp_getid=0L
foreach ch,I in ed.enumCharsForward(word_ccnt0)//it's 8-bit here, not UTF8!
if p_s_temp_getid>=MAX_SPECIAL_COLOR_ID_SIZE:break
chi=int(u8(ch))
if !CharSet.has(CharSet.idbody,chi):break
s_temp_getid[p_s_temp_getid++]=char(ch)
if p_s_temp_getid&&!CharSet.has(CharSet.idhead,int(u8(s_temp_getid[0]))):
p_s_temp_getid=0L
if p_s_temp_getid:
s_temp_getid.n=p_s_temp_getid
id_temp=getid(s_temp_getid)
key_decl_type=color_table[id_temp]-1
//Writeln('>>> ',s_temp_getid,' ',id_temp,' ',key_decl_type)
if key_decl_type>=0:
st=~key_decl_color_ids[key_decl_type]
color_id=~st
need_spell_check=0
if m_spell_checker&&!m_temporarily_disable_spell_check:
ccnt_before_word=word_ccnt0
if color_id==color_id_spell_check:
s_word.n=0
need_spell_check=1
is_number=(u32(ch_first-int('0'))<10u)
//. inside numbers
foreach ch,I,I2 in ed.enumUtf8Forward(word_ccnt0)
word_ccnt0=I
if isCustomWordChar(ch)||is_number&&ch=='.':
ofs=iptr(word_ccnt0-ccnt_base)
if uptr(ofs)<uptr(color_ids.n):
if color_id>0:
color_ids[ofs]=m_cached_colors_p_word_part+color_id
if need_spell_check:
if isCJK(ch):
need_spell_check=0
if ch>=65536:
//surrogate pair - 2nd
s_word.push(char(((ch>>18)&0xf)+0xf0))
s_word.push(char(0x80+((ch>>12)&63)))
s_word.push(char(0x80+((ch>>6)&63)))
s_word.push(char(0x80+(ch&63)))
else if ch>=2048:
s_word.push(char(((ch>>12)&0xf)+0xe0))
s_word.push(char(0x80+((ch>>6)&63)))
s_word.push(char(0x80+(ch&63)))
else if ch>=128:
s_word.push(char((ch>>6)+0xc0))
s_word.push(char(0x80+(ch&63)))
else
s_word.push(char(ch))
else
break
ch_before_word=ch
word_ccnt0=I2
if word_ccnt0>=ccnt_block_end:
break
if need_spell_check:
//spell check
if s_word.n>1:
s_word.push(char(0))
if !Hunspell.spell(m_spell_checker,s_word):
is_bad=1
if s_word[0]>='A'&&s_word[0]<='Z':
s_word[0]|=i8(0x20)
if Hunspell.spell(m_spell_checker,s_word):
is_bad=0
if is_bad&&!m_temporarily_disable_spell_check:
//not a word
m_current_frame_spell_errors.push(long2(ccnt_before_word,word_ccnt0))
///////
fnt_scaled=m_font
fnt_scaled.h*=scale
//smart emboldening for SRGB
//coulddo: per-color emboldening
if g_renderer.m_srgb_supported:
fnt_scaled.SmartEmbolden(m_color_embolden)
hc=charHeight()
x=GetFlatX(a)
tab_width=m_tab_width_in_fp
wrap_width=m_wrap_width_in_fp
iy=x/wrap_width
ix=x-iy*wrap_width
dx_extra=0LL
dy_extra=0LL
p_overlay=m_overlays.InlineBisect(inline(a){return a.flatx<x})
inline projectXY(i64 x0)
auto y=x0/wrap_width
auto x=x0-y*wrap_width
x+=dx_extra
y=(y+dy_extra)*hc
return float2(fixed2float(x-scroll_x)*scale+screen_x,fixed2float(y-scroll_y)*scale+screen_y)
inline testOverlay()
while TestOverlay(p_overlay, fnt_scaled,scale,x,projectXY):
dx_extra+=m_overlays[p_overlay].delta_x
p_overlay++
//if m_overlay_obj&&x==m_caret_overlay.x:
// DrawOverlay(fnt_scaled,scale,x,projectXY)
// dx_extra+=m_caret_overlay.z
x_right_clip=scroll_x+clip_w
g_renderer.SetFastDrawCharFont(fnt_scaled)
if m_enable_advanced_wrapping:
//we won't need x_right_clip here anyway
_RenderText(MODE_RENDER,ccnt_base,a,pp,s,s.n, color_ids, scroll_x,scroll_y,clip_w,clip_h,screen_x,screen_y,scale)
//additional emboldens
if m_overlay_embolden_ranges&&m_overlay_embolden_ranges.n:
C_virtual_diff_bold=m_color_virtual_diff_bold
for i=0:2:m_overlay_embolden_ranges.n-2
EmboldenTextRange(
ed,
m_overlay_embolden_ranges[i+0],
m_overlay_embolden_ranges[i+1],
C_virtual_diff_bold,
scroll_x,scroll_y,clip_w,clip_h,screen_x,screen_y,scale)
return
//the \t\n ignorant hiding
HideChars(ed,ccnt_base,color_ids,s)
enable_hidden=m_enable_hidden
//maintain an embedded object list
p_next_ceo=0L
ccnt_next_ceo=-1LL
if m_ceos.n:
//virtual overlay
hid=ed.m_handler_registration["renderer"].as(int)
SortEmbededObjects()
p_next_ceo=m_ceos.InlineBisect(inline(a){return a.loc.ccnt<ccnt_base})
if p_next_ceo<m_ceos.n:
ccnt_next_ceo=m_ceos[p_next_ceo].loc.ccnt
//it's OK to have a negative dx_extra
//maintain x normally
I_query_valid_point=-1LL
if enable_hidden>0:
//query at the first non-hidden char
if !m_hidden_ranges_prepared:
PrepareHiddenRanges(ed)
p=m_hidden_ccnts.Bisect(function(i64 a){return a<=ccnt_base})
if p<m_hidden_ccnts.n:
if p&1:
I_query_valid_point=m_hidden_ccnts[p]-ccnt_base
///////////
(ix_visual,iy_visual)=SubHiddenRanges(ed,x)
dx_extra=ix_visual-ix
dy_extra=iy_visual-iy
//if x>m_caret_overlay.x&&x<m_caret_overlay.y:
// x+=m_caret_overlay.z
ix_with_overlay=AddOverlayOffset(ix,iy)
dx_extra+=ix_with_overlay-ix
for ch,I in Utf8Chars(s)
if I==I_query_valid_point:
//the head query was invalid, reset the stuff
(ix_visual,iy_visual)=SubHiddenRanges(ed,x)
dx_extra=ix_visual-ix
dy_extra=iy_visual-iy
cidi=color_ids[I]
if cidi!=u8(255):
testOverlay()
//if ccnt_base+I>=980&&ccnt_base+I<=990:
// Writeln(' ',ccnt_base+I,' ',dx_extra,' ',dy_extra,' ',cidi,' ',ch)
if ch=='\r':
//nothing
else if ch=='\n':
if ccnt_base+I==ccnt_next_ceo:
dy_extra+=m_ceos[p_next_ceo].dy
p_next_ceo++
if p_next_ceo<m_ceos.n:
ccnt_next_ceo=m_ceos[p_next_ceo].loc.ccnt
if cidi==u8(255):
dy_extra--
dx_extra+=ix
else
dx_extra=0LL
x=(x/wrap_width+1LL)*wrap_width
iy=x/wrap_width
ix=x-iy*wrap_width
if (iy+dy_extra)*hc-scroll_y>=clip_h:break
else if ch==' ':
dx_i=charWidth(ch)
x+=dx_i
ix+=dx_i
if cidi==u8(255):
dx_extra-=dx_i
if ix>=wrap_width:
if cidi==u8(255):
dy_extra--
dx_extra+=ix
else
dx_extra=0LL
ix-=wrap_width
iy++
else if ch=='\t':
if cidi==u8(255):
dx_extra+=x
x=(x/tab_width+1LL)*tab_width
dx_extra-=x
else
x=(x/tab_width+1LL)*tab_width
iy=x/wrap_width
ix=x-iy*wrap_width
else
dx_i=charWidth(ch)
if cidi!=u8(255):
if ix+dx_extra<x_right_clip:
prj_x=float2(fixed2float(ix+dx_extra-scroll_x)*scale+screen_x,fixed2float((iy+dy_extra)*hc-scroll_y)*scale+screen_y)