forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighlightingFunctions.pb
1749 lines (1395 loc) · 55.6 KB
/
HighlightingFunctions.pb
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
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
; to be able to display the QuickHelp to the function where the cursor is currently inside, even when it is not over it
;
Structure QuickHelpStack
Word$ ; Word for the QuickHelp
Position.l ; Position in the input line
Parameter.l ; holds the parameter of the function we are currently working on.
EndStructure
Global Dim QuickHelpStack.QuickHelpStack(200)
Global NewList QuickHelpStructureStack.s()
Global NewList QuickHelpStructureList.s()
; Regular expressions to match a single number
;
CreateRegularExpression(#REGEX_HexNumber, "^[\+\-]?\$[0-9a-fA-F]+$")
CreateRegularExpression(#REGEX_BinNumber, "^[\+\-]?\%[01]+$")
CreateRegularExpression(#REGEX_DecNumber, "^[\+\-]?[0-9]+$")
CreateRegularExpression(#REGEX_FloatNumber, "^[\+\-]?[0-9]+\.[0-9]*$")
CreateRegularExpression(#REGEX_ScienceNumber, "^[\+\-]?[0-9]+\.?[0-9]*[eE][\+\-]?[0-9]+$")
; Retrieve the boundary of a word (start index and end index from a buffer, given the
; position in the buffer)
;
; NOTE: When modifying this function, be sure to report to standalone debugger
;
Procedure GetWordBoundary(*Buffer, BufferLength, Position, *StartIndex.INTEGER, *EndIndex.INTEGER, Mode)
*WordStart.Character = *Buffer
*WordEnd.Character = *Buffer + BufferLength * #CharSize
If Position >= 0 And Position < BufferLength+#CharSize And (Mode = 1 Or Position < BufferLength)
*Cursor.Character = *Buffer + Position*#CharSize
If Mode = 1 ; Needed for Auto-complete
*Cursor-#CharSize
ElseIf ValidCharacters(*Cursor\c) = 0 ; Needed, so F1 help works nicely
*Cursor-#CharSize
EndIf
While *Cursor >= *Buffer
If ValidCharacters(*Cursor\c) = 0 And (Mode = 0 Or (*Cursor\c <> '#' And *Cursor\c <> '*'))
*WordStart = *Cursor + #CharSize
Break
EndIf
*Cursor - #CharSize
Found = 1
Wend
*Cursor.Character = *Buffer + Position*#CharSize
While *Cursor\c
If ValidCharacters(*Cursor\c) = 0 And *Cursor\c <> '$'
*WordEnd = *Cursor - #CharSize
Break
EndIf
*Cursor + #CharSize
Found = 1
Wend
; Special case: "var1*var2" is detected as one word! (also handle 1*2*3*4*word correctly)
If Found
Repeat
*Cursor = FindMemoryCharacter(*WordStart+#CharSize, (*WordEnd - *WordStart) / #CharSize - 1, '*')
If *Cursor
If *Cursor <= *Buffer + Position*#CharSize ; the cursor is on the second word
*WordStart = *Cursor+#CharSize
ElseIf *Cursor < *WordEnd ; cursor on the first word
*WordEnd = *Cursor
EndIf
EndIf
Until *Cursor = 0
; Another special case: *#CONSTANT (https://www.purebasic.fr/english/viewtopic.php?f=4&t=40104)
*NextCharacter.Character = *WordStart+#CharSize
If *WordStart\c = '*' And *NextCharacter\c = '#'
*WordStart+1
EndIf
EndIf
EndIf
CompilerIf #PB_Compiler_Unicode
*StartIndex\i = (*WordStart - *Buffer)/#CharSize
*EndIndex\i = (*WordEnd - *Buffer)/#CharSize
CompilerElse
*StartIndex\i = *WordStart - *Buffer
*EndIndex\i = *WordEnd - *Buffer
CompilerEndIf
ProcedureReturn Found
EndProcedure
Procedure.s GetWord(*Buffer, BufferLength, Position)
If GetWordBoundary(*Buffer, BufferLength, Position, @StartIndex, @EndIndex, 0)
If EndIndex-StartIndex+1 > 0
ProcedureReturn PeekS(*Buffer + StartIndex * #CharSize, EndIndex-StartIndex+1)
EndIf
EndIf
EndProcedure
Procedure.s GetModulePrefix(*Buffer, BufferLength, Position)
If Position > 1 And Position <= BufferLength
*Cursor.Character = *Buffer + (Position - 1) * #CharSize
; skip space
While *Cursor > *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
; check for module operator
If *Cursor > *Buffer+#CharSize And *Cursor\c = ':' And PeekC(*Cursor - #CharSize) = ':'
*Cursor - 2*#CharSize
; skip space
While *Cursor > *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
; skip word
*LastChar = *Cursor
While *Cursor > *Buffer And ValidCharacters(*Cursor\c)
*Cursor - #CharSize
Wend
If *Cursor = *Buffer And ValidCharacters(*Cursor\c)
*FirstChar = *Buffer
Else
*FirstChar = *Cursor + #CharSize
EndIf
If *LastChar >= *FirstChar
CompilerIf #PB_Compiler_Unicode
ProcedureReturn PeekS(*FirstChar, (*LastChar - *FirstChar + #CharSize) / #CharSize)
CompilerElse
ProcedureReturn PeekS(*FirstChar, *LastChar - *FirstChar + #CharSize)
CompilerEndIf
EndIf
EndIf
EndIf
ProcedureReturn ""
EndProcedure
Procedure.s GetCurrentLine()
GetCursorPosition() ; Ensures than the fields are updated
ProcedureReturn GetLine(*ActiveSource\CurrentLine-1)
EndProcedure
Procedure.s GetCurrentWord()
Line$ = GetCurrentLine()
If Line$
CurrentWord$ = GetWord(@Line$, Len(Line$), *ActiveSource\CurrentColumnChars-1)
EndIf
ProcedureReturn CurrentWord$
EndProcedure
; Extract the number at the given position
; Does not guarantee that the output is a correct number.
; The regular expressions at the top can be used to verify this easily
Procedure.s GetNumber(Line$, Position) ; position is 0-based
If Position >= Len(Line$) ; 0-based
ProcedureReturn ""
EndIf
; Note: The PB compiler accepts spaces between some number parts, but
; for simplicity we only allow it without spaces (which should be the norm)
; Who writes a number like "1e - 10" ?
;
Line$ = UCase(Line$) ; simplify the checks
*Buffer = @Line$
*Pointer.Character = @Line$ + Position
; Walk back from the current location depending on the current cursor token
; to find the start of the number
; The further possible tokens depend on what token we start on
;
Select *Pointer\c
Case '%', '$'
; Hex and Bin numbers, there can only be a +, - before this
*Pointer - SizeOf(Character)
If *Pointer >= *Buffer And (*Pointer\c = '+' Or *Pointer\c = '-')
*Pointer - SizeOf(Character)
EndIf
Case '.'
; Before the dot can only be a decimal number and a sign
; The exponent part cannot contain a dot
*Pointer - SizeOf(Character)
While *Pointer >= *Buffer And (*Pointer\c >= '0' And *Pointer\c <= '9')
*Pointer - SizeOf(Character)
Wend
If *Pointer >= *Buffer And (*Pointer\c = '+' Or *Pointer\c = '-')
*Pointer - SizeOf(Character)
EndIf
Case '+', '-'
; A +/- can be in the exponent part, so we can be in the middle of the number or the beginning
; No need to check for possible Hex in this case, as there its only in the beginning
*Pointer - SizeOf(Character)
If *Pointer >= *Buffer And *Pointer\c = 'E'
*Pointer - SizeOf(Character)
While *Pointer >= *Buffer And (*Pointer\c >= '0' And *Pointer\c <= '9')
*Pointer - SizeOf(Character)
Wend
If *Pointer >= *Buffer And *Pointer\c = '.'
*Pointer - SizeOf(Character)
While *Pointer >= *Buffer And (*Pointer\c >= '0' And *Pointer\c <= '9')
*Pointer - SizeOf(Character)
Wend
EndIf
If *Pointer >= *Buffer And (*Pointer\c = '+' Or *Pointer\c = '-')
*Pointer - SizeOf(Character)
EndIf
EndIf
Case 'E'
; before the exponent part can be a full decimal number (with dot and sign)
; NOTE: E can also be in a Hex number, so accept both!
*Pointer - SizeOf(Character)
While *Pointer >= *Buffer And ((*Pointer\c >= '0' And *Pointer\c <= '9') Or (*Pointer\c >= 'A' And *Pointer\c <= 'F'))
*Pointer - SizeOf(Character)
Wend
If *Pointer >= *Buffer And *Pointer\c = '.'
; As we found a dot, it can no longer be a Hex number
*Pointer - SizeOf(Character)
While *Pointer >= *Buffer And (*Pointer\c >= '0' And *Pointer\c <= '9')
*Pointer - SizeOf(Character)
Wend
EndIf
If *Pointer >= *Buffer And *Pointer\c = '$' ; possible Hex start
*Pointer - SizeOf(Character)
EndIf
If *Pointer >= *Buffer And (*Pointer\c = '+' Or *Pointer\c = '-')
*Pointer - SizeOf(Character)
EndIf
Case 'A' To 'D', 'F'
; Can only be a hex number here
While *Pointer >= *Buffer And ((*Pointer\c >= '0' And *Pointer\c <= '9') Or (*Pointer\c >= 'A' And *Pointer\c <= 'F'))
*Pointer - SizeOf(Character)
Wend
If *Pointer >= *Buffer And *Pointer\c = '$'
*Pointer - SizeOf(Character)
EndIf
If *Pointer >= *Buffer And (*Pointer\c = '+' Or *Pointer\c = '-')
*Pointer - SizeOf(Character)
EndIf
Case '0' To '9'
; We can be in the part before, after the dot or after the E
; Or this could be a hex or bin number.
; Note: This first check walks right over the 'E' if we have a float number (with no extra sign), which is no problem
While *Pointer >= *Buffer And ((*Pointer\c >= '0' And *Pointer\c <= '9') Or (*Pointer\c >= 'A' And *Pointer\c <= 'F'))
*Pointer - SizeOf(Character)
Wend
; Possible sign for the exponent
If *Pointer >= *Buffer And (*Pointer\c = '+' Or *Pointer\c = '-')
*Pointer - SizeOf(Character)
EndIf
If *Pointer >= *Buffer
Select *Pointer\c
Case '%', '$'
; Now we know its hex/bin
*Pointer - SizeOf(Character)
Case '.'
; Its a normal float, or exponent without a sign
*Pointer - SizeOf(Character)
While *Pointer >= *Buffer And (*Pointer\c >= '0' And *Pointer\c <= '9')
*Pointer - SizeOf(Character)
Wend
Case 'E'
; Now we know its an exponent number (as it must have been "E+" or "E-")
*Pointer - SizeOf(Character)
While *Pointer >= *Buffer And (*Pointer\c >= '0' And *Pointer\c <= '9')
*Pointer - SizeOf(Character)
Wend
If *Pointer >= *Buffer And *Pointer\c = '.'
*Pointer - SizeOf(Character)
While *Pointer >= *Buffer And (*Pointer\c >= '0' And *Pointer\c <= '9')
*Pointer - SizeOf(Character)
Wend
EndIf
EndSelect
If *Pointer >= *Buffer And (*Pointer\c = '+' Or *Pointer\c = '-')
*Pointer - SizeOf(Character)
EndIf
EndIf
EndSelect
; *Pointer now points 1 char before the number start (or to *Buffer-1)
;
*Start = *Pointer + SizeOf(Character)
; Now we scan forward from here, which is much simpler
;
*Pointer = *Start
; First, a possible sign
If *Pointer\c = '+' Or *Pointer\c = '-'
*Pointer + SizeOf(Character)
EndIf
If *Pointer\c = '$' ; Hex number
*Pointer + SizeOf(Character)
While (*Pointer\c >= '0' And *Pointer\c <= '9') Or (*Pointer\c >= 'A' And *Pointer\c <= 'F')
*Pointer + SizeOf(Character)
Wend
ElseIf *Pointer\c = '%' ; Bin number
*Pointer + SizeOf(Character)
While *Pointer\c = '0' Or *Pointer\c = '1'
*Pointer + SizeOf(Character)
Wend
Else ; dec number
While *Pointer\c >= '0' And *Pointer\c <= '9'
*Pointer + SizeOf(Character)
Wend
If *Pointer\c = '.'
*Pointer + SizeOf(Character)
While *Pointer\c >= '0' And *Pointer\c <= '9'
*Pointer + SizeOf(Character)
Wend
EndIf
If *Pointer\c = 'E'
*Pointer + SizeOf(Character)
If *Pointer\c = '+' Or *Pointer\c = '-'
*Pointer + SizeOf(Character)
EndIf
While *Pointer\c >= '0' And *Pointer\c <= '9'
*Pointer + SizeOf(Character)
Wend
EndIf
EndIf
ProcedureReturn PeekS(*Start, (*Pointer - *Start) / SizeOf(Character))
EndProcedure
Procedure InsertComments()
If *ActiveSource\IsCode = 0
ProcedureReturn
EndIf
GetSelection(@LineStart, 0, @LineEnd, @RowEnd)
If RowEnd <= 1 And LineStart <> LineEnd ; when selecting a full line, it actually selects the newline too, this results in one extra line. Needs at least 2 lines to work: https://www.purebasic.fr/english/viewtopic.php?f=23&t=48512
LineEnd - 1
EndIf
SendEditorMessage(#SCI_BEGINUNDOACTION, 0, 0)
For index = LineStart-1 To LineEnd-1
SetLine(index, "; " + GetLine(index))
Next index
SendEditorMessage(#SCI_ENDUNDOACTION, 0, 0)
If PartialSourceScan(*ActiveSource, LineStart-1, LineEnd-1)
UpdateFolding(*ActiveSource, LineStart-1, LineEnd-1)
UpdateProcedureList()
UpdateVariableViewer()
EndIf
SetSelection(LineStart, 1, LineEnd, -1)
EndProcedure
Procedure RemoveComments()
If *ActiveSource\IsCode = 0
ProcedureReturn
EndIf
GetSelection(@LineStart, 0, @LineEnd, @RowEnd)
If RowEnd <= 1 ; when selecting a full line, it actually selects the newline too, this results in one extra line
LineEnd - 1
EndIf
SendEditorMessage(#SCI_BEGINUNDOACTION, 0, 0)
For index = LineStart-1 To LineEnd-1
Line$ = GetLine(index)
If Left(Line$, 2) = "; " ; This is how the IDE adds it, so also remove the space
SetLine(index, Right(Line$, Len(Line$)-2))
ElseIf Left(LTrim(RemoveString(Line$, Chr(9))), 1) = ";" ; the first none-whitespace is a ;
position = FindString(Line$, ";", 1)
If Mid(Line$, position+1, 1) = " "
SetLine(index, Left(Line$, position-1) + Right(Line$, Len(Line$)-(position+1))) ; remove the space as well
Else
SetLine(index, Left(Line$, position-1) + Right(Line$, Len(Line$)-position)) ; remove only the ;
EndIf
EndIf
Next index
SendEditorMessage(#SCI_ENDUNDOACTION, 0, 0)
If PartialSourceScan(*ActiveSource, LineStart-1, LineEnd-1)
UpdateFolding(*ActiveSource, LineStart-1, LineEnd-1)
UpdateProcedureList()
UpdateVariableViewer()
EndIf
SetSelection(LineStart, 1, LineEnd, -1)
EndProcedure
Procedure InsertTab()
GetSelection(@LineStart, 0, @LineEnd, @RowEnd)
If RowEnd <= 1 ; when selecting a full line, it actually selects the newline too, this results in one extra line
LineEnd - 1
EndIf
If RealTab
Add$ = Chr(9)
Else
Add$ = Space(TabLength)
EndIf
SendEditorMessage(#SCI_BEGINUNDOACTION, 0, 0)
For index = LineStart-1 To LineEnd-1
SetLine(index, Add$ + GetLine(index))
Next index
SendEditorMessage(#SCI_ENDUNDOACTION, 0, 0)
; This is required to update the keyword underline data
;
If PartialSourceScan(*ActiveSource, LineStart-1, LineEnd-1)
UpdateFolding(*ActiveSource, LineStart-1, LineEnd-1)
UpdateProcedureList()
UpdateVariableViewer()
EndIf
FlushEvents(); some event generated by the SetLine causes the selection to be lost, so flush it here
SetSelection(LineStart, 1, LineEnd, -1)
EndProcedure
Procedure RemoveTab()
GetSelection(@LineStart, 0, @LineEnd, @RowEnd)
If RowEnd <= 1 ; when selecting a full line, it actually selects the newline too, this results in one extra line
LineEnd - 1
EndIf
SendEditorMessage(#SCI_BEGINUNDOACTION, 0, 0)
For index = LineStart-1 To LineEnd-1
Line$ = GetLine(index)
If Left(Line$, 1) = Chr(9)
Line$ = Right(Line$, Len(Line$)-1)
Else
cut = 0
For i = 1 To TabLength
If Mid(Line$, i, 1) = " "
cut + 1
Else
Break
EndIf
Next i
Line$ = Right(Line$, Len(Line$)-cut)
EndIf
SetLine(index, Line$)
Next index
SendEditorMessage(#SCI_ENDUNDOACTION, 0, 0)
; This is required to update the keyword underline data
;
If PartialSourceScan(*ActiveSource, LineStart-1, LineEnd-1)
UpdateFolding(*ActiveSource, LineStart-1, LineEnd-1)
UpdateProcedureList()
UpdateVariableViewer()
EndIf
FlushEvents() ; some event generated by the SetLine causes the selection to be lost, so flush it here
SetSelection(LineStart, 1, LineEnd, -1)
EndProcedure
Procedure AutoIndent()
If *ActiveSource\IsCode = 0
ProcedureReturn
EndIf
GetSelection(@LineStart, 0, @LineEnd, @RowEnd)
If RowEnd <= 1 ; when selecting a full line, it actually selects the newline too, this results in one extra line
LineEnd - 1
EndIf
SendEditorMessage(#SCI_BEGINUNDOACTION, 0, 0)
UpdateIndent(LineStart-1, LineEnd-1)
SendEditorMessage(#SCI_ENDUNDOACTION, 0, 0)
; This is required to update the keyword underline data
;
If PartialSourceScan(*ActiveSource, LineStart-1, LineEnd-1)
UpdateFolding(*ActiveSource, LineStart-1, LineEnd-1)
UpdateProcedureList()
UpdateVariableViewer()
EndIf
FlushEvents(); some event generated by the SetLine causes the selection to be lost, so flush it here
SetSelection(LineStart, 1, LineEnd, -1)
EndProcedure
Procedure ShiftComments(IsRight)
If *ActiveSource\IsCode = 0
ProcedureReturn
EndIf
GetSelection(@LineStart, 0, @LineEnd, @RowEnd)
If RowEnd <= 1 ; when selecting a full line, it actually selects the newline too, this results in one extra line
LineEnd - 1
EndIf
If LineStart < LineEnd ; must be more than one line
; collect all lines and get info about min/max positions positions
Protected Dim Lines.s(LineEnd - LineStart)
MinColumn = $7FFFFFFF ; large positive number
MaxColumn = 0
MaxCodeColumn = 0
LineOffset = LineStart - 1
For i = LineStart-1 To LineEnd-1
Lines(i-LineOffset) = GetLine(i)
Comment = GetCommentPosition(Lines(i-LineOffset))
If Comment >= 0
Prefix$ = Left(Lines(i-LineOffset), Comment)
Column = CountColumns(Prefix$)
While Right(Prefix$, 1) = " " Or Right(Prefix$, 1) = Chr(9)
Prefix$ = Left(Prefix$, Len(Prefix$)-1)
Wend
CodeColumn = CountColumns(Prefix$)
MinColumn = Min(MinColumn, Column)
If ((Column-1) - ((Column-1) % TabLength)) > CodeColumn
MaxColumn = Max(MaxColumn, Column) ; ignore columns that cannot be moved left anymore
EndIf
MaxCodeColumn = Max(MaxCodeColumn, CodeColumn)
EndIf
Next i
If MinColumn < $7FFFFFFF ; check if there even are comments
SendEditorMessage(#SCI_BEGINUNDOACTION, 0, 0)
If IsRight
MinColumn = (MinColumn - (MinColumn % TabLength)) + TabLength
For i = LineStart-1 To LineEnd-1
Comment = GetCommentPosition(Lines(i-LineOffset))
If Comment >= 0
Prefix$ = Left(Lines(i-LineOffset), Comment)
Comment$ = Right(Lines(i-LineOffset), Len(Lines(i-LineOffset))-Comment)
Column = CountColumns(Prefix$)
If Column < MinColumn
If RealTab
SetLine(i, Prefix$ + Chr(9) + Comment$)
Else
SetLine(i, Prefix$ + Space(MinColumn-Column) + Comment$)
EndIf
EndIf
EndIf
Next i
Else
MaxColumn - 1
MaxColumn = (MaxColumn - (MaxColumn % TabLength))
If MaxColumn < 0
MaxColumn = 0
EndIf
For i = LineStart-1 To LineEnd-1
Comment = GetCommentPosition(Lines(i-LineOffset))
If Comment >= 0
Prefix$ = Left(Lines(i-LineOffset), Comment)
Comment$ = Right(Lines(i-LineOffset), Len(Lines(i-LineOffset))-Comment)
Column = CountColumns(Prefix$)
While Column > MaxColumn
If Right(Prefix$, 1) = " "
Prefix$ = Left(Prefix$, Len(Prefix$)-1)
Column - 1
ElseIf Right(Prefix$, 1) = Chr(9)
Prefix$ = Left(Prefix$, Len(Prefix$)-1)
Column = CountColumns(Prefix$) ; need to re-calculate the new position
Else
Break
EndIf
Wend
SetLine(i, Prefix$ + Comment$)
EndIf
Next i
EndIf
SendEditorMessage(#SCI_ENDUNDOACTION, 0, 0)
FlushEvents(); some event generated by the SetLine causes the selection to be lost, so flush it here
SetSelection(LineStart, 1, LineEnd, -1)
EndIf
EndIf
EndProcedure
; Skip *Item, and then to the end of the line or to the next ':'
; Returns the Position within the line and modifies *Line if needed
; Returns a position in bytes!
;
Procedure BlockSelect_SkipKeyword(*Item.SourceItem, *Line.INTEGER)
Column = *Item\Position + *Item\Length
LineCount = SendEditorMessage(#SCI_GETLINECOUNT, 0, 0)
Line$ = GetContinuationLine(*Line\i, @Offset)
*Pointer.PTR = @Line$ + Offset + Column
; scan forward
While *Pointer\c
If *Pointer\c = ':' And *Pointer\c[1] = ':'
*Pointer + 2*#CharSize ; a :: module separator
Column + 2
ElseIf *Pointer\c = ':'
Break ; found a command separator
ElseIf *Pointer\c = 13
*Line\i + 1
Column = 0
*Pointer + #CharSize
If *Pointer\c = 10
*Pointer + #CharSize
EndIf
ElseIf *Pointer\c = 10
*Line\i + 1
Column = 0
*Pointer + #CharSize
ElseIf *Pointer\c = '"'
*Pointer + #CharSize: Column + 1
While *Pointer\c And *Pointer\c <> '"' And *Pointer\c <> 13 And *Pointer\c <> 10
*Pointer + #CharSize: Column + 1
Wend
If *Pointer\c = '"'
*Pointer + #CharSize: Column + 1
EndIf
ElseIf *Pointer\c = '~' And *Pointer\c[1] = '"'
*Pointer + (2 * #CharSize): Column + 2
While *Pointer\c And *Pointer\c <> '"' And *Pointer\c <> 13 And *Pointer\c <> 10
If *Pointer\c = '\' And *Pointer\c[1] <> 13 And *Pointer\c[1] <> 10
*Pointer + (2 * #CharSize): Column + 2
Else
*Pointer + #CharSize: Column + 1
EndIf
Wend
If *Pointer\c = '"'
*Pointer + #CharSize: Column + 1
EndIf
ElseIf *Pointer\c = 39 ; '
*Pointer + #CharSize: Column + 1
While *Pointer\c And *Pointer\c <> 39 And *Pointer\c <> 13 And *Pointer\c <> 10
*Pointer + #CharSize: Column + 1
Wend
If *Pointer\c = 39
*Pointer + #CharSize: Column + 1
EndIf
ElseIf *Pointer\c = ';'
While *Pointer\c And *Pointer\c <> 13 And *Pointer\c <> 10
*Pointer + #CharSize: Column + 1
Wend
Else
*Pointer + #CharSize: Column + 1
EndIf
Wend
If *Pointer\c = 0 And *Line\i < LineCount - 1
; We reached the end of the last continuation line
; So include the newline to have an easy way to cut the entire block
*Line\i + 1
ProcedureReturn 0
Else
; We are somewhere in side the line at a ':'
ProcedureReturn Column
EndIf
EndProcedure
Procedure BlockSelect_AddBlock(*StartItem.SourceItem, StartLine, IncludeStart, *EndItem.SourceItem, EndLine, IncludeEnd)
Line$ = GetLine(StartLine)
If IncludeStart
; If the stuff before the start item is only whitespace then select the entire line
; The start is a block keyword, so there can be no line continuation here
StartColumn = BytesToChars(Line$, 0, Encoding, *StartItem\Position)
If StartColumn > 0 And RemoveString(RemoveString(Left(Line$, StartColumn), Chr(9)), " ") = ""
StartColumn = 0
EndIf
Else
; Skip the keyword
StartColumn = BytesToChars(Line$, 0, *ActiveSource\Parser\Encoding, BlockSelect_SkipKeyword(*StartItem, @StartLine))
EndIf
Line$ = GetLine(EndLine)
If IncludeEnd
; Skip the keyword
EndColumn = BytesToChars(Line$, 0, *ActiveSource\Parser\Encoding, BlockSelect_SkipKeyword(*EndItem, @EndLine))
Else
; If the stuff before the end item is only whitespace then select only the previous line
; The start is a block keyword, so there can be no line continuation here
EndColumn = BytesToChars(Line$, 0, Encoding, *EndItem\Position)
If EndLine > 0 And RemoveString(RemoveString(Left(Line$, EndColumn), Chr(9)), " ") = ""
EndLine - 1
EndColumn = SendEditorMessage(#SCI_LINELENGTH, EndLine, 0)
EndIf
EndIf
StartPosition = SendEditorMessage(#SCI_POSITIONFROMLINE, StartLine, 0) + StartColumn
EndPosition = SendEditorMessage(#SCI_POSITIONFROMLINE, EndLine, 0) + EndColumn
; Only add a stack element if it is different from the previous one!
;
If ListSize(BlockSelectionStack()) = 0 Or BlockSelectionStack()\StartPosition <> StartPosition Or BlockSelectionStack()\EndPosition <> EndPosition
AddElement(BlockSelectionStack())
BlockSelectionStack()\StartPosition = StartPosition
BlockSelectionStack()\EndPosition = EndPosition
EndIf
EndProcedure
Procedure BlockSelect_BuildStack()
StartPosition = SendEditorMessage(#SCI_GETSELECTIONSTART, 0, 0)
EndPosition = SendEditorMessage(#SCI_GETSELECTIONEND, 0, 0)
; The lowest element is the current selection
ClearList(BlockSelectionStack())
AddElement(BlockSelectionStack())
BlockSelectionStack()\StartPosition = StartPosition
BlockSelectionStack()\EndPosition = EndPosition
; look for blocks
Line = SendEditorMessage(#SCI_LINEFROMPOSITION, StartPosition, 0)
Column = StartPosition - SendEditorMessage(#SCI_POSITIONFROMLINE, Line, 0)
StartLine = Line
*StartItem.SourceItem = ClosestSourceItem(@*ActiveSource\Parser, @StartLine, Column)
IncludeSelf = #True
While FindBlockStart(@*ActiveSource\Parser, @StartLine, @*StartItem, IncludeSelf)
; Look forward to the next match for this item
NextLine = StartLine
*NextItem.SourceItem = *StartItem
If MatchKeywordForward(@*ActiveSource\Parser, @NextLine, @*NextItem) And *NextItem
; add a stack element for the 'inner' part of this block
BlockSelect_AddBlock(*StartItem, StartLine, #False, *NextItem, NextLine, #False)
If BackwardMatches(*StartItem\Keyword, 0) > 0
; This must be a middle keyword like 'ElseIf'
; add a stack element for the block including the 'ElseIf' statement
BlockSelect_AddBlock(*StartItem, StartLine, #True, *NextItem, NextLine, #False)
; Move back to the actual start statement (i.e. the 'If')
While BackwardMatches(*StartItem\Keyword, 0) > 0
If MatchKeywordBackward(@*ActiveSource\Parser, @StartLine, @*StartItem) = #False Or *StartItem = 0
; something is messed up here
; Since we have no start item to work with anymore, we completely abort the whole thing
ProcedureReturn
EndIf
Wend
EndIf
; match forward to the end of the block
While ForwardMatches(*NextItem\Keyword, 0) > 0
If MatchKeywordForward(@*ActiveSource\Parser, @NextLine, @*NextItem) = #False Or *NextItem = 0
; something is messed up here. skip this block and continue the outer look
*NextItem = 0
Break
EndIf
Wend
If *NextItem
; add a stack element for the whole IF/ElseIf/EndIf block (outer bounds)
BlockSelect_AddBlock(*StartItem, StartLine, #True, *NextItem, NextLine, #True)
EndIf
EndIf
; For the next lookup, do not include the *StartItem in the search anymore
IncludeSelf = #False
Wend
EndProcedure
Procedure SelectBlock()
If *ActiveSource And *ActiveSource <> *ProjectInfo And *ActiveSource\IsCode
; We build the full stack of all possible block selections from the current location first
; and then just move up/down this stack later with SelectBlock()/DeselectBlock()
;
; This avoids ambiguities which block to select which would happen if we tried to find the
; next block each time from the current selection
;
If ListSize(BlockSelectionStack()) = 0
BlockSelect_BuildStack()
FirstElement(BlockSelectionStack()) ; go to the element representing the current selection
EndIf
; move up the stack if there is another possible block to select
;
If NextElement(BlockSelectionStack())
FlushEvents() ; some event causes this to not work! (dirty hack)
BlockSelectionUpdated = #True ; don't clear the stack in the scintilla callback
; Swap the two positions. This causes the SCI_SETSEL message to put the caret at the block
; start and scroll it into view. This is generally more helpful than seeing the block end
SendEditorMessage(#SCI_SETSEL, BlockSelectionStack()\EndPosition, BlockSelectionStack()\StartPosition)
UpdateCursorPosition()
; scroll a bit if the selection start is now in the first line in view
If SendEditorMessage(#SCI_DOCLINEFROMVISIBLE, SendEditorMessage(#SCI_GETFIRSTVISIBLELINE, 0, 0)) = SendEditorMessage(#SCI_LINEFROMPOSITION, BlockSelectionStack()\StartPosition)
SendEditorMessage(#SCI_LINESCROLL, 0, -3)
EndIf
EndIf
EndIf
EndProcedure
Procedure DeselectBlock()
If *ActiveSource And *ActiveSource <> *ProjectInfo And *ActiveSource\IsCode
; Move down the stack (if possible). The lowest element is the original selection
If PreviousElement(BlockSelectionStack())
FlushEvents() ; some event causes this to not work! (dirty hack)
BlockSelectionUpdated = #True ; don't clear the stack in the scintilla callback
If ListIndex(BlockSelectionStack()) = 0
; no swapping here. restore the original selection
SendEditorMessage(#SCI_SETSEL, BlockSelectionStack()\StartPosition, BlockSelectionStack()\EndPosition)
Else
; swap like in the SelectBlock() function
SendEditorMessage(#SCI_SETSEL, BlockSelectionStack()\EndPosition, BlockSelectionStack()\StartPosition)
EndIf
UpdateCursorPosition()
EndIf
EndIf
EndProcedure
; Check if the given prefix string ends in the middle of a string, character constant or comment
; Returns
; 0 = no string or comment
; 1 = comment
; 2 = string
; 3 = char const
;
Procedure IsCommentOrString(Prefix$)
*Cursor.Character = @Prefix$
Repeat
Select *Cursor\c
Case 0
; not inside anything
ProcedureReturn 0
Case ';'
; comment
ProcedureReturn 1
Case '"'
; regular string
*Cursor + #CharSize
While *Cursor\c <> '"'
If *Cursor\c = 0
ProcedureReturn 2
Else
*Cursor + #CharSize
EndIf
Wend
*Cursor + #CharSize
Case 39
; char constant
*Cursor + #CharSize
While *Cursor\c <> 39
If *Cursor\c = 0
ProcedureReturn 3
Else
*Cursor + #CharSize
EndIf
Wend
*Cursor + #CharSize
Case '~'
*Cursor + #CharSize
If *Cursor\c = '"'
; escaped string
*Cursor\c + #CharSize
While *Cursor\c <> '"'
If *Cursor\c = 0
ProcedureReturn 2
ElseIf *Cursor\c = '\'
*Cursor + #CharSize
If *Cursor\c = 0
ProcedureReturn 2 ; check for prefix end right after the \
EndIf
*Cursor + #CharSize ; skip second char, even if sequence is invalid (all we care about is \" here)
Else
*Cursor + #CharSize
EndIf
Wend
*Cursor\c + #CharSize
EndIf
Default
; any other char
*Cursor + #CharSize
EndSelect
ForEver
EndProcedure
Procedure CheckSearchStringComment(line, column, IsAutoComplete) ; line & column are 0 based!
Result = IsCommentOrString(Left(GetLine(line), column))
If IsAutoComplete
If Result > 1 And AutoCompleteNoStrings
ProcedureReturn 0
ElseIf Result = 1 And AutoCompleteNoComments
ProcedureReturn 0
Else
ProcedureReturn 1
EndIf
Else
If Result > 1 And FindNoStrings