-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSynHighlighterWebMisc.pas
1792 lines (1505 loc) · 47.2 KB
/
SynHighlighterWebMisc.pas
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
{-------------------------------------------------------------------------------
SynWeb
Copyright (C) 2005-2011 Krystian Bigaj
*** MPL
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is Krystian Bigaj.
Alternatively, the contents of this file may be used under the terms
of the GNU Lesser General Public license (the "LGPL License"),
in which case the provisions of LGPL License are applicable instead of those
above. If you wish to allow use of your version of this file only
under the terms of the LGPL License and not to allow others to use
your version of this file under the MPL, indicate your decision by
deleting the provisions above and replace them with the notice and
other provisions required by the LGPL License. If you do not delete
the provisions above, a recipient may use your version of this file
under either the MPL or the LGPL License.
*** LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***
You may retrieve the latest version of this file at the SynWeb home page,
located at http://sourceforge.net/projects/synweb
Contact: krystian.bigaj@gmail.com
Homepage: http://flatdev.ovh.org
-------------------------------------------------------------------------------}
{$IFNDEF QSYNHIGHLIGHTERWEBMISC}
unit SynHighlighterWebMisc;
{$ENDIF}
{$I SynWeb.inc}
interface
uses
SysUtils,
StrUtils,
Graphics,
Classes,
Types,
Windows,
{$IFDEF SYN_CLX}
QSynEdit,
{$IFDEF UNISYNEDIT}
QSynUnicode,
{$ENDIF}
QSynEditTextBuffer,
QSynEditHighlighter,
QSynEditTypes,
QSynCompletionProposal,
QSynHighlighterWeb,
QSynHighlighterWebData,
QSynTokenMatch;
{$ELSE}
SynEdit,
{$IFDEF UNISYNEDIT}
SynUnicode,
{$ENDIF}
SynEditTextBuffer,
SynEditHighlighter,
SynEditTypes,
SynCompletionProposal,
SynHighlighterWeb,
SynHighlighterWebData,
SynTokenMatch;
{$ENDIF}
const
TSynWebTokenizedStartScanBefore = 20; { 20 lines before }
TBufferCoordStart: TBufferCoord = (Char: 1; Line: 1);
TSynWebTokenizerPhpWhitespaces = [stkPhpSpace, stkPhpComment, stkPhpDocComment, stkPhpDocCommentTag, stkNull];
type
{ TSynWebWordMarkerCustom }
TSynWebWordMarkerMode = (swwmSelectedText, swwmSelectedWord,
swwmCustomText, swwmCustomWord);
TSynWebWordMarkerCustom = class(TSynEditPlugin)
private
FIsWordSelected: Boolean;
FIsMultiLineSelection: Boolean;
FEnabled: Boolean;
FMode: TSynWebWordMarkerMode;
FCustomText: TSynWebString;
FCaseSensitive: Boolean;
protected
function IsWordSelected: Boolean;
function IsOverlapSelection(const ABufferStart, ABufferEnd: TBufferCoord): Boolean;
function GetHighlightText: TSynWebString;
procedure SetEnabled(const Value: Boolean);
procedure SetMode(const Value: TSynWebWordMarkerMode);
procedure SetCustomText(const Value: TSynWebString);
procedure SetCaseSensitive(const Value: Boolean);
procedure DoInvalidate;
procedure LinesInserted(FirstLine: Integer; Count: Integer); override;
procedure LinesDeleted(FirstLine: Integer; Count: Integer); override;
procedure AfterPaint(ACanvas: TCanvas; const AClip: TRect;
FirstLine: Integer; LastLine: Integer); override;
procedure DoPaintMarker(ACanvas: TCanvas; const ARect: TRect;
const AText: TSynWebString; const ABufferStart, ABufferEnd: TBufferCoord); virtual; abstract;
public
procedure AfterConstruction; override;
procedure NotifySelChanged;
property Enabled: Boolean read FEnabled write SetEnabled;
property Mode: TSynWebWordMarkerMode read FMode write SetMode;
property CustomText: TSynWebString read FCustomText write SetCustomText;
property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
end;
{ TSynWebWordMarker }
TSynWebWordMarkerPaintMode = (swwpFillRect, swwpFrameRect, swwpUnderline);
TSynWebWordMarker = class(TSynWebWordMarkerCustom)
protected
FBGColor: TColor;
FFGColor: TColor;
FPaintMode: TSynWebWordMarkerPaintMode;
procedure SetBGColor(const Value: TColor);
procedure SetFGColor(const Value: TColor);
procedure SetPaintMode(const Value: TSynWebWordMarkerPaintMode);
procedure DoPaintMarker(ACanvas: TCanvas; const ARect: TRect;
const AText: TSynWebString; const ABufferStart, ABufferEnd: TBufferCoord); override;
public
procedure AfterConstruction; override;
property BGColor: TColor read FBGColor write SetBGColor;
property FGColor: TColor read FFGColor write SetFGColor;
property PaintMode: TSynWebWordMarkerPaintMode read FPaintMode write SetPaintMode;
end;
{ TSynWebTokenizerInfo }
TSynWebTokenizerSet = set of TSynWebTokenKind;
TSynWebTokenizerMarker = (stlBefore, stlBegin, stlInside, stlEnd, stlAfter);
TSynWebTokenizerInfo = record
Kind: TSynWebTokenKind;
Token: TSynWebString;
Len: Integer;
Marker: TSynWebTokenizerMarker;
Range: Longword;
PhpKeyId: Integer;
PhpSymbolId: Integer;
EsSymbolId: Integer;
MLTagId: Integer;
MLTagKind: Integer;
case Boolean of
False: (
Char: Integer;
Line: Integer;
);
True: (
Pos: TBufferCoord;
)
end;
{ ISynWebTokenizer }
ISynWebTokenizer = interface(IInterface)
// protected
function GetMarkerPos: TBufferCoord;
procedure SetMarkerPos(const AMarkerPos: TBufferCoord);
// public
function Next: Boolean;
function NextAndSkip(const ASkip: TSynWebTokenizerSet = TSynWebTokenizerPhpWhitespaces): Boolean;
function Skip(const ASkip: TSynWebTokenizerSet = TSynWebTokenizerPhpWhitespaces): Boolean;
function IsEol: Boolean;
function IsEof: Boolean;
function HL: TSynWebBase;
function PreviousToken: TSynWebTokenizerInfo;
function CurrentToken: TSynWebTokenizerInfo;
property MarkerPos: TBufferCoord read GetMarkerPos write SetMarkerPos;
end;
{ TSynWebTokenizer }
TSynWebTokenizer = class(TInterfacedObject, ISynWebTokenizer)
protected
FLines: TSynWebStrings;
FLine: Integer;
FMarkerPos: TBufferCoord;
FHL: TSynWebBase;
FCurrentToken: TSynWebTokenizerInfo;
FLastToken: TSynWebTokenizerInfo;
procedure UpdateMaker(var ATokenInfo: TSynWebTokenizerInfo);
procedure GetCurrentTokenInfo(var ATokenInfo: TSynWebTokenizerInfo);
function GetMarkerPos: TBufferCoord;
procedure SetMarkerPos(const AMarkerPos: TBufferCoord);
public
constructor Create(ALines: TSynWebStrings; AHighlighter: TSynWebBase;
APos: TBufferCoord; ACustomRange: Pointer = nil); overload;
constructor Create(ALines: TSynWebStrings; AHighlighter: TSynWebBase;
ALine: Integer = 1); overload;
constructor Create(AEditor: TCustomSynEdit; ALine: Integer = 1); overload;
constructor Create(AEditor: TCustomSynEdit; APos: TBufferCoord); overload;
class function GetTokenCompletionAtCaret(AEditor: TCustomSynEdit): TSynWebString;
function Next: Boolean;
function NextAndSkip(const ASkip: TSynWebTokenizerSet): Boolean;
function Skip(const ASkip: TSynWebTokenizerSet): Boolean;
function IsEof: Boolean;
function IsEol: Boolean;
function HL: TSynWebBase;
function CurrentToken: TSynWebTokenizerInfo;
function PreviousToken: TSynWebTokenizerInfo;
end;
{ TSynWebSimpleTokenizer }
TSynWebSimpleTokenizer = class(TObject)
protected
FHL: TSynWebBase;
FOwnHL: Boolean;
FLines: TSynWebStrings;
FLine: Integer;
public
constructor Create(AHighlighter: TSynWebBase; ALines: TSynWebStrings;
AOwnHighlighter: Boolean = False; ALine: Integer = 0; ARange: Pointer = nil);
destructor Destroy; override;
procedure Next;
function IsEof: Boolean;
function IsEol: Boolean;
property HL: TSynWebBase read FHL;
property Line: Integer read FLine;
end;
//
{
SynEditGetMatchingToken(Ex) returns:
-2 : Close and open token found
-1 : Close token found
0 : Kind not found
+1 : Open token found
+2 : Open and close token found
}
function SynEditGetMatchingTag(ASynEdit: TCustomSynEdit; APoint: TBufferCoord;
var AMatch: TSynTokenMatched): Integer;
function SynEditGetMatchingTagEx(ASynEdit: TCustomSynEdit; APoint: TBufferCoord;
var AMatch: TSynTokenMatched): Integer;
function SynWebGetHighlighterTypeAt(ASynEdit: TCustomSynEdit;
ASynWeb: TSynWebBase; APos: TBufferCoord): TSynWebHighlighterTypes;
function SynWebGetHighlighterTypeAtCursor(ASynEdit: TCustomSynEdit;
ASynWeb: TSynWebBase): TSynWebHighlighterTypes;
function SynWebUpdateActiveHighlighter(ASynEdit: TCustomSynEdit;
ASynWeb: TSynWebBase): TSynWebHighlighterTypes;
function SynWebFillCompletionProposal(ASynEdit: TCustomSynEdit;
ASynWeb: TSynWebBase; ACompletion: TSynCompletionProposal;
var CurrentInput: TSynWebString): TSynWebHighlighterTypes;
type
PSynWebUnmatchedTag = ^TSynWebUnmatchedTag;
TSynWebUnmatchedTag = record
Pos: TBufferCoord;
Tag: TSynWebString;
end;
TSynWebUnmatchedTagArray = array of TSynWebUnmatchedTag;
function SynWebGetUnmatchedTags(ASynEdit: TCustomSynEdit;
var AUnmatchedTags: TSynWebUnmatchedTagArray; AAllowDuplicates: Boolean): Boolean; overload;
function SynWebGetUnmatchedTags(ALines: TSynWebStrings; APos: TBufferCoord;
ASynWeb: TSynWebMLSyn; var AUnmatchedTags: TSynWebUnmatchedTagArray; AAllowDuplicates: Boolean): Boolean; overload;
implementation
type
TSynTokenBuf = record
Pos: TBufferCoord;
Token: TSynWebString;
end;
var
FMatchStack: array of TSynTokenBuf;
function SynEditGetMatchingTag(ASynEdit: TCustomSynEdit; APoint: TBufferCoord;
var AMatch: TSynTokenMatched): Integer;
var
TagID: Integer;
Level, DeltaLevel, FMatchStackID: Integer;
H: TSynWebMLSyn;
bSpecial: Boolean;
function ScanToEndOfSpecialTag: Boolean;
begin
Result := False;
with ASynEdit, H do
begin
Next;
while True do
begin
while not GetEol do
begin
if (GetTokenID = stkMLError) and (GetToken = '/') then
begin
Next;
if GetEol then
Break;
if (GetTokenID = stkMLTag) and (GetToken = '>') then
begin
Result := True;
Exit;
end;
end else
if (GetTokenID in [stkMLTag, stkMLError]) and (GetToken = '>') then
begin
Next;
if MLGetTagID = -1 then
Exit;
Continue;
end;
Next;
end;
Inc(APoint.Line);
if APoint.Line >= Lines.Count then
Break;
SetLine(Lines[APoint.Line], APoint.Line);
end;
end;
end;
function CheckToken: Boolean;
begin
with H do
begin
if (GetTokenId = stkMLTagName) and (TagID = MLGetTagID) then
Inc(Level, MLGetTagKind);
if Level = 0 then
begin
SynEditGetMatchingTag := 2;
AMatch.CloseToken := GetToken;
AMatch.CloseTokenPos.Line := APoint.Line + 1;
AMatch.CloseTokenPos.Char := GetTokenPos + 1;
Result := True;
end else
begin
Next;
Result := False;
end;
end;
end;
procedure CheckTokenBack;
var
OldLine: Integer;
begin
with H do
begin
if (GetTokenId = stkMLTagName) and (TagID = MLGetTagID) then
case MLGetTagKind of
-1:
begin
Dec(Level);
if FMatchStackID >= 0 then
Dec(FMatchStackID);
end;
1:
begin
Inc(FMatchStackID);
if FMatchStackID >= Length(FMatchStack) then
SetLength(FMatchStack, Length(FMatchStack) + 32);
FMatchStack[FMatchStackID].Token := GetToken;
FMatchStack[FMatchStackID].Pos.Line := APoint.Line + 1;
FMatchStack[FMatchStackID].Pos.Char := GetTokenPos + 1;
if bSpecial then
begin
OldLine := APoint.Line;
if ScanToEndOfSpecialTag then
begin
Dec(FMatchStackID);
if OldLine <> APoint.Line then
begin
APoint.Line := OldLine;
while not GetEol do
Next;
end else
Next;
Exit;
end;
if OldLine <> APoint.Line then
begin
APoint.Line := OldLine;
while not GetEol do
Next;
Exit;
end;
end;
Inc(Level);
end;
end;
Next;
end;
end;
begin
Result := 0;
if not (ASynEdit.Highlighter is TSynWebMLSyn) then
Exit;
H := TSynWebMLSyn(ASynEdit.Highlighter);
with ASynEdit, H do
begin
if Engine = nil then
Exit;
Dec(APoint.Line);
Dec(APoint.Char);
if APoint.Line = 0 then
ResetRange
else
SetRange(TSynEditStringList(Lines).Ranges[APoint.Line - 1]);
SetLine(Lines[APoint.Line], APoint.Line);
while not GetEol and (APoint.Char >= GetTokenPos + Length(GetToken)) do
Next;
TagID := MLGetTagID;
if GetEol or (TagID = -1) or (GetTokenID <> stkMLTagName) or
(TSynWeb_TagsData[TagID] and (1 shl 31) <> 0) then
Exit;
bSpecial := TagID in [MLTagID_Script, MLTagID_Style];
case MLGetTagKind of
1:
begin
Result := 1;
AMatch.OpenToken := GetToken;
AMatch.OpenTokenPos.Line := APoint.Line + 1;
AMatch.OpenTokenPos.Char := GetTokenPos + 1;
end;
-1:
begin
Result := -1;
AMatch.CloseToken := GetToken;
AMatch.CloseTokenPos.Line := APoint.Line + 1;
AMatch.CloseTokenPos.Char := GetTokenPos + 1;
end;
end;
AMatch.TokenKind := GetTokenKind;
AMatch.TokenAttri := GetTokenAttribute;
if Result = 1 then
begin
if bSpecial and ScanToEndOfSpecialTag then
begin
Result := 0;
Exit;
end;
Level := 1;
Next;
while True do
begin
while not GetEol do
if CheckToken then
Exit;
Inc(APoint.Line);
if APoint.Line >= ASynEdit.Lines.Count then
Break;
SetLine(Lines[APoint.Line], APoint.Line);
end;
end else
begin
if Length(FMatchStack) < 32 then
SetLength(FMatchStack, 32);
FMatchStackID := -1;
Level := -1;
if APoint.Line = 0 then
ResetRange
else
SetRange(TSynEditStringList(Lines).Ranges[APoint.Line - 1]);
SetLine(Lines[APoint.Line], APoint.Line);
while not GetEol and (GetTokenPos < AMatch.CloseTokenPos.Char - 1) do
CheckTokenBack;
if FMatchStackID > -1 then
begin
Result := -2;
AMatch.OpenToken := FMatchStack[FMatchStackID].Token;
AMatch.OpenTokenPos := FMatchStack[FMatchStackID].Pos;
end else
while APoint.Line > 0 do
begin
DeltaLevel := -Level - 1;
Dec(APoint.Line);
if APoint.Line = 0 then
ResetRange
else
SetRange(TSynEditStringList(Lines).Ranges[APoint.Line - 1]);
SetLine(Lines[APoint.Line], APoint.Line);
FMatchStackID := -1;
while not GetEol do
CheckTokenBack;
if (DeltaLevel <= FMatchStackID) then
begin
Result := -2;
AMatch.OpenToken := FMatchStack[FMatchStackID - DeltaLevel].Token;
AMatch.OpenTokenPos := FMatchStack[FMatchStackID - DeltaLevel].Pos;
Exit;
end;
end;
end;
end;
end;
function SynEditGetMatchingTagEx(ASynEdit: TCustomSynEdit; APoint: TBufferCoord;
var AMatch: TSynTokenMatched): Integer;
begin
Result := SynEditGetMatchingTag(ASynEdit, APoint, AMatch);
if (Result = 0) and (APoint.Char > 1) then
begin
Dec(APoint.Char);
Result := SynEditGetMatchingTag(ASynEdit, APoint, AMatch);
end;
end;
function SynWebGetHighlighterTypeAt(ASynEdit: TCustomSynEdit;
ASynWeb: TSynWebBase; APos: TBufferCoord): TSynWebHighlighterTypes;
begin
with ASynEdit,ASynWeb do
begin
if APos.Line = 1 then
ResetRange
else
SetRange(TSynEditStringList(Lines).Ranges[CaretY-2]);
Result := GetActiveHighlighter(GetRange, Lines[CaretY-1], APos.Char, APos.Line);
end;
end;
function SynWebGetHighlighterTypeAtCursor(ASynEdit: TCustomSynEdit;
ASynWeb: TSynWebBase): TSynWebHighlighterTypes;
begin
Result := SynWebGetHighlighterTypeAt(ASynEdit, ASynWeb, ASynEdit.CaretXY);
end;
function SynWebUpdateActiveHighlighter(ASynEdit: TCustomSynEdit;
ASynWeb: TSynWebBase): TSynWebHighlighterTypes;
begin
with ASynEdit,ASynWeb do
begin
Result := SynWebGetHighlighterTypeAtCursor(ASynEdit, ASynWeb);
if Result <> ActiveHighlighters then
if ActiveHighlighterSwitch then
begin
ActiveHighlighters := Result;
Repaint;
end;
end;
end;
function SynWebFillCompletionProposal(ASynEdit: TCustomSynEdit;
ASynWeb: TSynWebBase; ACompletion: TSynCompletionProposal;
var CurrentInput: TSynWebString): TSynWebHighlighterTypes;
var
ct: TSynWebString;
ctk: TSynWebTokenKind;
procedure ScanTo(APos: TBufferCoord);
begin
Dec(APos.Line);
Dec(APos.Char);
with ASynEdit, ASynWeb do
begin
if APos.Line = 0 then
ResetRange
else
SetRange(TSynEditStringList(Lines).Ranges[APos.Line - 1]);
SetLine(Lines[APos.Line], APos.Line + 1);
while not GetEol and (GetTokenPos + GetTokenLen < APos.Char) do
Next;
end;
end;
procedure AddSimple(AInsert: String; AKind: String = ''; AKindColor: TColor = clBlack);
var
c: String;
begin
c := ColorToString(AKindColor);
if AKind <> '' then
ACompletion.ItemList.Add(Format('\color{%s}%s\column{}\color{clBlack}\style{+B}%s',[c, AKind, AInsert]))
else
ACompletion.ItemList.Add(AInsert);
ACompletion.InsertList.Add(AInsert);
end;
function HtmlGetTagKind(AID: Integer): Integer;
begin
if TSynWeb_TagsData[AID] and
(1 shl 31) = 0 then
Result := 1
else
if ASynWeb.InternalInstanceData.FOptions.FXmlMode then
Result := 0
else
Result := -1;
end;
procedure HtmlLoadTags(AClose: Boolean);
var
i: Integer;
ver: Longword;
lUnmatchedTag: TSynWebUnmatchedTagArray;
begin
if not AClose then
begin
if ASynWeb is TSynWebMLSyn then
if SynWebGetUnmatchedTags(ASynEdit.Lines, ASynEdit.CaretXY, TSynWebMLSyn(ASynWeb), lUnmatchedTag, True) then
for i := 0 to Length(lUnmatchedTag) - 1 do
AddSimple('</' + String(lUnmatchedTag[i].Tag) + '>', 'tag*',
ASynWeb.Engine.MLTagNameAttri.Foreground);
end;
ver := 1 shl Longword(ASynWeb.InternalInstanceData.FOptions.FMLVersion);
for i := 0 to High(TSynWeb_TagsData) do
if ((TSynWeb_TagsData[i] and ver) <> 0) and (not AClose or
(AClose and (TSynWeb_TagsData[i] and (1 shl 31) = 0))) and
(TSynWeb_TagsData[i] and (1 shl 29) = 0) then
begin
if AClose then
AddSimple('</' + String(TSynWeb_Tags[i]) + '>', 'tag',
ASynWeb.Engine.MLTagNameAttri.Foreground)
else
case HtmlGetTagKind(i) of
-1:
AddSimple('<' + String(TSynWeb_Tags[i]) + '>', 'tag',
ASynWeb.Engine.MLTagNameAttri.Foreground);
0:
AddSimple('<' + String(TSynWeb_Tags[i]) + ' />', 'tag',
ASynWeb.Engine.MLTagNameAttri.Foreground);
1:
AddSimple('<' + String(TSynWeb_Tags[i]) + '></' + String(TSynWeb_Tags[i]) + '>', 'tag',
ASynWeb.Engine.MLTagNameAttri.Foreground);
end;
end;
end;
procedure HtmlLoadSpecailEntity;
var
i: Integer;
ver: Longword;
begin
ver := 1 shl Longword(ASynWeb.InternalInstanceData.FOptions.FMLVersion);
for i := 0 to High(TSynWeb_SpecialData) do
if (TSynWeb_SpecialData[i] and ver) <> 0 then
AddSimple('&' + String(TSynWeb_Special[i]) + ';', 'entity', ASynWeb.Engine.MLEscapeAttri.Foreground);
end;
procedure HtmlLoad;
begin
HtmlLoadTags(False);
HtmlLoadTags(True);
HtmlLoadSpecailEntity;
AddSimple('<?php ; ?>', 'template');
AddSimple('<?= ; ?>', 'template');
AddSimple('<script language="php"></script>', 'template');
AddSimple('<script language="javascript" type="text/javascript"></script>', 'template');
AddSimple('<style type="text/css"></style>', 'template');
AddSimple('<!-- -->', 'template');
end;
procedure HtmlLoadAttrs(ATag: Integer);
var
i: Integer;
ver, ver2: Longword;
begin
if ATag = -1 then
Exit;
ver := Longword(ASynWeb.InternalInstanceData.FOptions.FMLVersion);
ver2 := 1 shl (ATag mod 32);
ATag := ATag div 32;
for i := 0 to High(TSynWeb_AttrsData) do
if (TSynWeb_AttrsData[i][ver][ATag] and ver2) <> 0 then
AddSimple(String(TSynWeb_Attrs[i]) + '=""', 'attribute', ASynWeb.Engine.MLTagAttri.Foreground);
end;
procedure Html;
begin
with ASynEdit, ASynWeb do
begin
case ctk of
stkMLTagName, stkMLTagNameUndef:
begin
HtmlLoad;
case MLGetTagKind of
-1:
CurrentInput := '</' + Copy(ct, 1, CaretX - 1 - GetTokenPos);
1:
CurrentInput := '<' + Copy(ct, 1, CaretX - 1 - GetTokenPos);
end;
end;
stkMLTag:
begin
HtmlLoad;
case MLGetTagKind of
-1:
CurrentInput := '</';
1:
CurrentInput := '<';
end;
end;
stkMLTagKey, stkMLTagKeyUndef:
begin
HtmlLoadAttrs(MLGetTagID);
CurrentInput := Copy(ct, 1, CaretX - 1 - GetTokenPos);
end;
else // case
case MLGetRange of
srsMLText:
HtmlLoad;
srsMLTagKey, srsMLTagKeyEq:
HtmlLoadAttrs(MLGetTagID);
end;
end;
end;
end;
procedure CssLoadTags;
var
i: Integer;
ver: Longword;
begin
ver := 1 shl Longword(ASynWeb.InternalInstanceData.FOptions.FMLVersion);
for i := 0 to High(TSynWeb_TagsData) do
if ((TSynWeb_TagsData[i] and ver) <> 0) and
(TSynWeb_TagsData[i] and (1 shl 30) = 0) and
(TSynWeb_TagsData[i] and (1 shl 29) = 0) then
AddSimple(String(TSynWeb_Tags[i]), 'tag', ASynWeb.Engine.MLTagNameAttri.Foreground);
end;
procedure CssLoadProp;
var
i: Integer;
ver: Longword;
begin
ver := 1 shl Longword(ASynWeb.InternalInstanceData.FOptions.FCssVersion);
for i := 0 to High(TSynWeb_CssPropsData) do
if (TSynWeb_CssPropsData[i] and ver) <> 0 then
AddSimple(String(TSynWeb_CssProps[i]) + ':', 'property', ASynWeb.Engine.CssPropAttri.Foreground);
end;
procedure CssLoadVal(AProp: Integer);
var
i: Integer;
ver, prop: Longword;
begin
if AProp = -1 then
Exit;
ver := Longword(ASynWeb.InternalInstanceData.FOptions.FCssVersion);
prop := 1 shl (AProp mod 32);
AProp := AProp div 32;
for i := 0 to High(TSynWeb_CssValsData) do
if (TSynWeb_CssValsData[i][ver][AProp] and prop) <> 0 then
AddSimple(String(TSynWeb_CssVals[i]), 'value', ASynWeb.Engine.CssValAttri.Foreground);
end;
procedure Css;
begin
with ASynEdit, ASynWeb do
case ctk of
stkCssSelector, stkCssSelectorUndef:
begin
CssLoadTags;
CurrentInput := Copy(ct, 1, CaretX - 1 - GetTokenPos);
end;
stkCssProp, stkCssPropUndef:
begin
CssLoadProp;
CurrentInput := Copy(ct, 1, CaretX - 1 - GetTokenPos);
end;
stkCssVal, stkCssValUndef:
begin
CssLoadVal(CssGetPropertyId);
CurrentInput := Copy(ct, 1, CaretX - 1 - GetTokenPos);
end
else // case
case CssGetRange of
srsCssRuleset:
begin
CssLoadTags;
CurrentInput := '';
end;
srsCssProp:
begin
CssLoadProp;
CurrentInput := '';
end;
srsCssPropVal:
begin
CssLoadVal(CssGetPropertyId);
CurrentInput := '';
end;
end;
end;
end;
procedure Php;
var
i: Integer;
v: Longword;
data: Longword;
begin
v := 1 shl Longword(ASynWeb.InternalInstanceData.FOptions.FPhpVersion);
for i := 0 to High(TSynWeb_PhpKeywords) do // functions
begin
data := TSynWeb_PhpKeywordsData[i];
if (Data and $0F = $08) and ((Data shr 16) and v <> 0) then
AddSimple(String(TSynWeb_PhpKeywords[i]) + '()', 'function', ASynWeb.Engine.PhpFunctionAttri.Foreground);
end;
for i := 0 to High(TSynWeb_PhpKeywords) do // keywords
begin
data := TSynWeb_PhpKeywordsData[i];
if (Data and $0F = $01) and ((Data shr 16) and v <> 0) then
AddSimple(String(TSynWeb_PhpKeywords[i]), 'keyword', ASynWeb.Engine.PhpKeyAttri.Foreground);
end;
CurrentInput := ct;
end;
begin
Result := SynWebGetHighlighterTypeAtCursor(ASynEdit, ASynWeb);
ACompletion.InsertList.Clear;
ACompletion.ItemList.Clear;
if ASynWeb.Engine = nil then
Exit;
with ASynEdit, ASynWeb do
begin
ScanTo(CaretXY);
if (GetTokenPos = 0) and (CaretX = 1) then
begin
ct := '';
ctk := stkNull;
end else
begin
ct := GetToken;
ctk := GetTokenID;
end;
end;
ct := Trim(ct);
if shtML in Result then
Html
else
if shtCss in Result then
Css
else
if [shtPhpInML, shtPhpInCss, shtPhpInEs] * Result <> [] then
Php;
end;
type
PSynWebTagItem = ^TSynWebTagItem;
TSynWebTagItem = record
Pos: TBufferCoord;
Marker: TSynWebTokenizerMarker;
Prev: PSynWebTagItem;
end;
function SynWebSortUnmatched(T1, T2: Pointer): Integer;
var
l1, l2: TBufferCoord;
begin
l1 := PSynWebUnmatchedTag(T2).Pos;
l2 := PSynWebUnmatchedTag(T1).Pos;
if l1.Line < l2.Line then
Result := -1
else
if l1.Line > l2.Line then
Result := 1
else
if l1.Char < l2.Char then
Result := -1
else
if l1.Char > l2.Char then
Result := 1
else
Result := 0;
end;
function SynWebGetUnmatchedTags(ASynEdit: TCustomSynEdit;
var AUnmatchedTags: TSynWebUnmatchedTagArray; AAllowDuplicates: Boolean): Boolean;
begin
Result := (ASynEdit.Highlighter is TSynWebMLSyn) and
SynWebGetUnmatchedTags(
ASynEdit.Lines,
ASynEdit.CaretXY,
TSynWebMLSyn(ASynEdit.Highlighter),
AUnmatchedTags,
AAllowDuplicates
);
end;
function SynWebGetUnmatchedTags(ALines: TSynWebStrings; APos: TBufferCoord;
ASynWeb: TSynWebMLSyn; var AUnmatchedTags: TSynWebUnmatchedTagArray; AAllowDuplicates: Boolean): Boolean;
var
lT: ISynWebTokenizer;
lTags: TSynWebStringList;
lTagLastIdx: Integer;
procedure OpenTag;
var
lTagItem: PSynWebTagItem;
lTagId: Integer;
begin
// If this is a HTML tag which doesn't need close tag, then simply don't open it
if ASynWeb.InternalInstanceData.FOptions.FMLVersion <= smlhvHtml401Frameset then
begin
lTagId := TSynWebHtmlSyn(ASynWeb).MLGetTagID;
if lTagId > -1 then
// If HTML_TAG(lTagId) IS EMPTY then
if TSynWeb_TagsData[lTagId] and (1 shl 31) <> 0 then
Exit;
end;
New(lTagItem);
lTagItem^.Marker := lT.CurrentToken.Marker;
lTagItem^.Pos := lT.CurrentToken.Pos;
if lTags.Find(lT.CurrentToken.Token, lTagLastIdx) then
begin
lTagItem^.Prev := PSynWebTagItem(lTags.Objects[lTagLastIdx]);
lTags.Objects[lTagLastIdx] := TObject(lTagItem);
end else
begin
lTagItem^.Prev := nil;
lTags.InsertObject(lTagLastIdx, lT.CurrentToken.Token, TObject(lTagItem));
end;
end;