forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoComplete.pb
1519 lines (1239 loc) · 54.7 KB
/
AutoComplete.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.
;--------------------------------------------------------------------------------------------
; NOTE: The AutoComplete seemed very slow on some systems, probably because the
; much access to the Gadget content to add an item sorted takes simply too long.
;
; So we now use a LinkedList to build the whole list without sorting, then
; we sort it all at once before filling the gadget. This should give a speed increase.
;
;
Global NewList AutoCompleteModules.s()
Global NewList AutoCompleteList.s()
Global NewList AutoCompleteStack.s() ; sub-structures in structure mode
Global NewList DummyList.s() ; for use in FindStructure() when content is not needed
Global AutoComplete_Start, AutoComplete_End ; range of displayed items in the gadget
Global AutoComplete_CurrentMaxWidth, AutoComplete_CurrentMaxHeight
Global AutoComplete_IsStructure
Global AutoComplete_StructureStart
Global AutoComplete_IsModule
Global AutoComplete_ModuleStart
; cache recent AutoComplete selections for structure and module autocomplete (to use in case no word is entered yet)
; the cache is on a per-module or per-structure basis
Global AutoComplete_CurrentStructure$ ; may include module prefix
Global AutoComplete_CurrentModule$
Global NewMap AutoComplete_LastStructureItem.s() ; key and value is lowercase
Global NewMap AutoComplete_LastModuleItem.s()
Procedure CreateAutoCompleteWindow()
;
; Create the autocomplete window (hidden), so it is later only hidden/shown
;
; Note: I tried to remove the taskbar icon for linux, but the GTK_WINDOW_POPUP hack
; (which does that well) has other trouble. (for example the window focus is not
; properly managed) So we do not use this.
If OpenWindow(#WINDOW_AutoComplete, 0, 0, 0, 0, "", #PB_Window_Invisible | #PB_Window_BorderLess, WindowID(#WINDOW_Main))
ListViewGadget(#GADGET_AutoComplete_List, 0, 0, 0, 0)
CompilerIf #CompileLinuxGtk2
; remove window taskbar icon
; Note: this marks the window as a "menu", so the WM does not add the taskbar icon,
; but for normal windows this should probably not be used, as i don't know what
; it does to window borders etc of a normal toplevel window
;
; NOTE: Marking the Window as MENU creates a lot of Gtk errors and a hell of a slowdown
; when using compiz/XGL. Somehow it treats such windows very different, so do not use it!
;
; *Widget.GtkWidget = WindowID(#WINDOW_AutoComplete)
; gdk_window_set_type_hint_(*Widget\window, #GDK_WINDOW_TYPE_HINT_MENU)
; The "documented" way to remove the taskbar icon. Dunno why i did not find this before
; This is just a "hint", but most Linux window managers should support it.
; There are only problems on Gtk-Windows as far as i can find.
;
*Widget.GtkWidget = WindowID(#WINDOW_AutoComplete)
gtk_window_set_skip_taskbar_hint_(*Widget, #True)
gtk_window_set_skip_pager_hint_(*Widget, #True)
; disable the automatic search feature of the TreeView
gtk_tree_view_set_enable_search_(GadgetID(#GADGET_AutoComplete_List), #False)
CompilerEndIf
CompilerIf #CompileMac
; key handling for OSX
AutoComplete_SetupRawKeyHandler()
CompilerEndIf
; These shortcuts are now in the Preferences shortcut list, so they get added automatically below.
; AddKeyboardShortcut(#WINDOW_AutoComplete, #PB_Shortcut_Escape, #MENU_AutoComplete_Abort)
; AddKeyboardShortcut(#WINDOW_AutoComplete, #PB_Shortcut_Return, #MENU_AutoComplete_Ok)
; AddKeyboardShortcut(#WINDOW_AutoComplete, #PB_Shortcut_Tab, #MENU_AutoComplete_Ok)
; put all main window shortcuts here too.
CreateKeyboardShortcuts(#WINDOW_AutoComplete)
AutoComplete_SetFocusCallback()
AutoCompleteWindowReady = 1
EndIf
EndProcedure
Procedure AutoComplete_AddItem(*Item.SourceItem)
AddElement(AutoCompleteList())
AutoCompleteList() = *Item\Name$
If AutoCompleteAddBrackets
Select *Item\Type
Case #ITEM_Procedure, #ITEM_Declare
If RemoveString(RemoveString(*Item\Prototype$, " "), Chr(9)) = "()"
AutoCompleteList() + "()"
Else
AutoCompleteList() + "("
EndIf
Case #ITEM_Macro
If RemoveString(RemoveString(*Item\Prototype$, " "), Chr(9)) = "()"
AutoCompleteList() + "()"
ElseIf FindString(*Item\Prototype$, "(", 1) <> 0 ; do not add brackets for none function macros
AutoCompleteList() + "("
EndIf
Case #ITEM_Import
If RemoveString(RemoveString(StringField(*Item\Type$, 2, Chr(9)), " "), Chr(9)) = "()"
AutoCompleteList() + "()"
Else
AutoCompleteList() + "("
EndIf
Case #ITEM_Array, #ITEM_Map, #ITEM_UnknownBraced
AutoCompleteList() + "("
Case #ITEM_LinkedList
AutoCompleteList() + "()"
Case #ITEM_DeclareModule
AutoCompleteList() + "::"
EndSelect
EndIf
EndProcedure
; Constants are separate as they are clearly marked by the first # char
; This way we only have to lookup one kind of item for this
;
Procedure AutoComplete_AddConstantsFromSorted(*Parser.ParserData, Bucket, *Ignore.SourceItem)
If *Parser\SortedValid
If Bucket < 0
; display full list, so we have to add from all buckets
;
ForEach AutoCompleteModules()
For Bucket = 0 To #PARSER_VTSize-1
*Item.SourceItem = *Parser\Modules(UCase(AutoCompleteModules()))\Sorted\Constants[Bucket]
While *Item
If *Item <> *Ignore
AddElement(AutoCompleteList())
AutoCompleteList() = *Item\Name$
EndIf
*Item = *Item\NextSorted
Wend
Next Bucket
Next AutoCompleteModules()
Else
; We now add all items that match the first char, even when
; the exact match option is on, so we just have to lookup the one bucket
; and add all
;
ForEach AutoCompleteModules()
*Item.SourceItem = *Parser\Modules(UCase(AutoCompleteModules()))\Sorted\Constants[Bucket]
While *Item
If *Item <> *Ignore
AddElement(AutoCompleteList())
AutoCompleteList() = *Item\Name$
EndIf
*Item = *Item\NextSorted
Wend
Next AutoCompleteModules()
EndIf
EndIf
EndProcedure
Procedure AutoComplete_AddFromSorted(*Parser.ParserData, Bucket, *Ignore.SourceItem, SingleModuleOnly)
If *Parser\SortedValid
If Bucket < 0
; display full list, so we have to add from all buckets
;
For Type = 0 To #ITEM_LastSorted
If AutocompleteOptions(Type) And Type <> #ITEM_Constant ; constants are separate
If Type = #ITEM_DeclareModule
; module names are indexed in the main module
; do not show modules if a prefix:: is provided before the word
If SingleModuleOnly = 0
For Bucket = 0 To #PARSER_VTSize-1
*Item.SourceItem = *Parser\MainModule\Indexed[Type]\Bucket[Bucket]
While *Item
If *Item <> *Ignore
AutoComplete_AddItem(*Item)
EndIf
*Item = *Item\NextSorted
Wend
Next Bucket
EndIf
Else
; other stuff
ForEach AutoCompleteModules()
For Bucket = 0 To #PARSER_VTSize-1
*Item.SourceItem = *Parser\Modules(UCase(AutoCompleteModules()))\Indexed[Type]\Bucket[Bucket]
While *Item
If *Item <> *Ignore
AutoComplete_AddItem(*Item)
EndIf
*Item = *Item\NextSorted
Wend
Next Bucket
Next AutoCompleteModules()
EndIf
EndIf
Next Type
Else
; We now add all items that match the first char, even when
; the exact match option is on, so we just have to lookup the one bucket
; and add all
;
For Type = 0 To #ITEM_LastSorted
If AutocompleteOptions(Type) And Type <> #ITEM_Constant ; constants are separate
If Type = #ITEM_DeclareModule
; module names are indexed in the main module
; do not show modules if a prefix:: is provided before the word
If SingleModuleOnly = 0
*Item.SourceItem = *Parser\MainModule\Indexed[Type]\Bucket[Bucket]
While *Item
If *Item <> *Ignore
AutoComplete_AddItem(*Item)
EndIf
*Item = *Item\NextSorted
Wend
EndIf
Else
; other stuff
ForEach AutoCompleteModules()
*Item.SourceItem = *Parser\Modules(UCase(AutoCompleteModules()))\Indexed[Type]\Bucket[Bucket]
While *Item
If *Item <> *Ignore
AutoComplete_AddItem(*Item)
EndIf
*Item = *Item\NextSorted
Wend
Next AutoCompleteModules()
EndIf
EndIf
Next Type
EndIf
EndIf
EndProcedure
Procedure AutoComplete_FillNormal(WordStart$, ModulePrefix$)
; Declares are treated like procedures in AutoComplete and they have no
; separate prefs item, so just sync the settings for simplicity
;
AutocompleteOptions(#ITEM_Declare) = AutocompleteOptions(#ITEM_Procedure)
FirstChar$ = Left(WordStart$, 1)
If AutoCompleteCharMatchOnly = 1 And FirstChar$ <> "#" And FirstChar$ <> "*"
Length = 1
Else ; for constants, compare the second char!
Length = 2
EndIf
; We may not add the SourceItem right under the cursor, so check
; if there is one
;
*CurrentItem = LocateSourceItem(@*ActiveSource\Parser, *ActiveSource\CurrentLine-1, *ActiveSource\CurrentColumnBytes-2)
; Check if we are inside a module here
;
ClearList(AutoCompleteModules())
If AutoComplete_IsModule
; a module prefix is given. so exclusively look at the public part of this module
SingleModuleOnly = #True
AddElement(AutoCompleteModules())
AutoCompleteModules() = ModulePrefix$
Else
; scan for open modules
SingleModuleOnly = #False
*ModuleStart.SourceItem = *CurrentItem
ModuleStartLine = *ActiveSource\CurrentLine - 1
If *ModuleStart = 0
*ModuleStart = ClosestSourceItem(@*ActiveSource\Parser, @ModuleStartLine, *ActiveSource\CurrentColumnBytes-2)
EndIf
If *ModuleStart And FindModuleStart(@*ActiveSource\Parser, @ModuleStartLine, @*ModuleStart, AutoCompleteModules())
AddElement(AutoCompleteModules())
AutoCompleteModules() = *ModuleStart\Name$
If *ModuleStart\Type = #ITEM_Module
AddElement(AutoCompleteModules())
AutoCompleteModules() = "IMPL::" + *ModuleStart\Name$
EndIf
Else
AddElement(AutoCompleteModules())
AutoCompleteModules() = ""
EndIf
EndIf
If AutoCompleteCharMatchOnly = 0 Or SingleModuleOnly
; add all entries in these cases
Bucket = -1
Else
Bucket = GetBucket(@WordStart$) ; for adding sorted stuff
EndIf
; Note: Our LinkedList now contains all words that start with the wanted character even if
; the AutoCompleteCharMatchOnly=2. We will filter this when displaying in the gadget
; This way the list never needs to be rebuild while the window is open
; (as the window closes when the first char is erased.)
; fill the list:
;
If FirstChar$ = "#" Or WordStart$ = "" ; constants
; add PB constants
If AutocompletePBOptions(#PBITEM_Constant) And SingleModuleOnly = #False
For i = 0 To ConstantListSize-1
If AutoCompleteCharMatchOnly = 0 Or CompareMemoryString(@WordStart$, @ConstantList(i), #PB_String_NoCase, Length) = #PB_String_Equal
AddElement(AutoCompleteList())
AutoCompleteList() = ConstantList(i)
EndIf
Next i
EndIf
If AutoCompleteOptions(#ITEM_Constant)
; Add the constants from this source
AutoComplete_AddConstantsFromSorted(@*ActiveSource\Parser, Bucket, *CurrentItem)
; Add constants from project sources
If AutoCompleteProject And *ActiveSource\ProjectFile
ForEach ProjectFiles()
If ProjectFiles()\Source = 0
AutoComplete_AddConstantsFromSorted(@ProjectFiles()\Parser, Bucket, *CurrentItem)
ElseIf ProjectFiles()\Source And ProjectFiles()\Source <> *ActiveSource
AutoComplete_AddConstantsFromSorted(@ProjectFiles()\Source\Parser, Bucket, *CurrentItem)
EndIf
Next ProjectFiles()
EndIf
; Add constants from open files
If AutoCompleteAllFiles
ForEach FileList()
If @FileList() <> *ProjectInfo And @FileList() <> *ActiveSource And (AutoCompleteProject = 0 Or FileList()\ProjectFile = 0)
AutoComplete_AddConstantsFromSorted(@FileList()\Parser, Bucket, *CurrentItem)
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource) ; important!
EndIf
EndIf
EndIf
If FirstChar$ <> "#" ; functions or anything else
; add PB items
If AutocompletePBOptions(#PBITEM_Keyword) And SingleModuleOnly = #False
For i = 1 To #NbBasicKeywords
If AutoCompleteCharMatchOnly = 0 Or CompareMemoryString(@WordStart$, @BasicKeywordsReal(i), 1, Length) = 0
AddElement(AutoCompleteList())
If AutoCompleteAddSpaces
AutoCompleteList() = BasicKeywordsReal(i) + BasicKeywordsSpaces(i)
Else
AutoCompleteList() = BasicKeywordsReal(i)
EndIf
EndIf
Next i
EndIf
If AutocompletePBOptions(#PBITEM_ASMKeyword) And SingleModuleOnly = #False
For i = 1 To NbASMKeywords
If AutoCompleteCharMatchOnly = 0 Or CompareMemoryString(@WordStart$, @ASMKeywords(i), 1, Length) = 0
AddElement(AutoCompleteList())
AutoCompleteList() = ASMKeywords(i)
EndIf
Next i
EndIf
If AutocompletePBOptions(#PBITEM_Function) And SingleModuleOnly = #False
For i = 0 To NbBasicFunctions-1
If AutoCompleteCharMatchOnly = 0 Or CompareMemoryString(@WordStart$, @BasicFunctions(i)\Name$, 1, Length) = 0
AddElement(AutoCompleteList())
If AutoCompleteAddBrackets
If Left(BasicFunctions(i)\Proto$, 2) = "()"
AutoCompleteList() = BasicFunctions(i)\Name$+"()"
Else
AutoCompleteList() = BasicFunctions(i)\Name$+"("
EndIf
Else
AutoCompleteList() = BasicFunctions(i)\Name$
EndIf
EndIf
Next i
EndIf
If AutocompletePBOptions(#PBITEM_APIFunction) And SingleModuleOnly = #False
For i = 0 To NbApiFunctions-1
If AutoCompleteCharMatchOnly = 0 Or CompareMemoryString(@WordStart$, @APIFunctions(i)\Name$, 1, Length) = 0
AddElement(AutoCompleteList())
AutoCompleteList() = APIFunctions(i)\Name$+"_"
If AutoCompleteAddBrackets
AutoCompleteList() + "("
EndIf
EndIf
Next i
EndIf
If AutocompletePBOptions(#PBITEM_Structure) And SingleModuleOnly = #False
For i = 0 To StructureListSize-1
If AutoCompleteCharMatchOnly = 0 Or CompareMemoryString(@WordStart$, @StructureList(i), 1, Length) = 0
AddElement(AutoCompleteList())
AutoCompleteList() = StructureList(i)
EndIf
Next i
EndIf
If AutocompletePBOptions(#PBITEM_Interface) And SingleModuleOnly = #False
For i = 0 To InterfaceListSize-1
If AutoCompleteCharMatchOnly = 0 Or CompareMemoryString(@WordStart$, @InterfaceList(i), 1, Length) = 0
AddElement(AutoCompleteList())
AutoCompleteList() = InterfaceList(i)
EndIf
Next i
EndIf
; Add items from current source (Global scope)
;
; Update the current source sorted data and use that
; It does nothing if the data is already sorted, and the sorting
; does not do much more than we would need to do here anyway (walk all source and check scope)
; so this should be fast enough
;
SortParserData(@*ActiveSource\Parser, *ActiveSource)
AutoComplete_AddFromSorted(@*ActiveSource\Parser, Bucket, *CurrentItem, SingleModuleOnly)
; Add items from the current source (local scope)
; For this, check backwards to see if there is even a procedure
; (do this only if needed)
;
If SingleModuleOnly = #False And (AutocompleteOptions(#ITEM_Variable) | AutocompleteOptions(#ITEM_Array) | AutocompleteOptions(#ITEM_LinkedList) | AutocompleteOptions(#ITEM_Map))
*ProcedureStart = 0
If *CurrentItem
; we have a current item
*Item.SourceItem = *CurrentItem
Line = *ActiveSource\CurrentLine-1
Parser_PreviousItem(*ActiveSource\Parser, *Item, Line)
Else
; no current item, find the nearest one to start
*Item.SourceItem = 0
Line = *ActiveSource\CurrentLine-1
While *Item = 0 And Line > 0
Line - 1
*Item = *ActiveSource\Parser\SourceItemArray\Line[Line]\Last
Wend
EndIf
If FindProcedureStart(@*ActiveSource\Parser, @Line, @*Item)
; we are inside a procedure, so add all local stuff
; (if it is also global, no problem as it is already in the list then)
; No need to re-check anything with always global scope (like import, structure, etc)
While *Item
Select *Item\Type
Case #ITEM_ProcedureEnd
Break
Case #ITEM_Macro
While *Item And *Item\Type <> #ITEM_MacroEnd
Parser_NextItem(*ActiveSource\Parser, *Item, Line)
Wend
If *Item = 0 ; Important check, as Parser_NextItem() below does not check this!
Break
EndIf
Case #ITEM_Keyword
SkipTo = 0
Select *Item\Keyword
Case #KEYWORD_Structure: SkipTo = #KEYWORD_EndStructure
Case #KEYWORD_Interface: SkipTo = #KEYWORD_EndInterface
Case #KEYWORD_Import : SkipTo = #KEYWORD_EndImport
Case #KEYWORD_ImportC : SkipTo = #KEYWORD_EndImport
EndSelect
If SkipTo
While *Item And (*Item\Type <> #ITEM_Keyword Or *Item\Keyword <> SkipTo)
Parser_NextItem(*ActiveSource\Parser, *Item, Line)
Wend
If *Item = 0 ; Important check, as Parser_NextItem() below does not check this!
Break
EndIf
EndIf
Case #ITEM_Variable
If *Item <> *CurrentItem And AutocompleteOptions(#ITEM_Variable) And *Item\Scope <> #SCOPE_GLOBAL And *Item\Scope <> #SCOPE_THREADED
; add shared too, as it can be an implicit declaration
AutoComplete_AddItem(*Item)
EndIf
Case #ITEM_Array, #ITEM_LinkedList, #ITEM_Map
If *Item <> *CurrentItem And AutocompleteOptions(*Item\Type) And *Item\Scope <> #SCOPE_GLOBAL And *Item\Scope <> #SCOPE_THREADED
AutoComplete_AddItem(*Item)
EndIf
Case #ITEM_UnknownBraced
If *Item <> *CurrentItem And *Item\Scope = #SCOPE_Shared
; its a shared array, list or map. need to resolve it to find out which
EndIf
EndSelect
Parser_NextItem(*ActiveSource\Parser, *Item, Line)
Wend
EndIf
EndIf
; Add items from project sources
If AutoCompleteProject And *ActiveSource\ProjectFile
ForEach ProjectFiles()
If ProjectFiles()\Source = 0
AutoComplete_AddFromSorted(@ProjectFiles()\Parser, Bucket, *CurrentItem, SingleModuleOnly)
ElseIf ProjectFiles()\Source And ProjectFiles()\Source <> *ActiveSource
AutoComplete_AddFromSorted(@ProjectFiles()\Source\Parser, Bucket, *CurrentItem, SingleModuleOnly)
EndIf
Next ProjectFiles()
EndIf
; Add items from open files
If AutoCompleteAllFiles
ForEach FileList()
If @FileList() <> *ProjectInfo And @FileList() <> *ActiveSource And (AutoCompleteProject = 0 Or FileList()\ProjectFile = 0)
AutoComplete_AddFromSorted(@FileList()\Parser, Bucket, *CurrentItem, SingleModuleOnly)
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource) ; important!
EndIf
EndIf
EndProcedure
; AutoCompleteStack() contains any sub-structures that we are in
;
Procedure AutoComplete_FillStructured(WordStart$, StructName$, *BaseItem.SourceItem, BaseItemLine)
If *BaseItem And StructName$ = "" ; normal structure mode
; Only accept BaseItems that can be a structure
;
If *BaseItem\Type = #ITEM_Variable Or *BaseItem\Type = #ITEM_Array Or *BaseItem\Type = #ITEM_LinkedList Or *BaseItem\Type = #ITEM_Map Or *BaseItem\Type = #ITEM_UnknownBraced
; resolve the type to know the structure
;
Type$ = ResolveItemType(*BaseItem, BaseItemLine, @OutType.i)
If Type$ = "" Or (Not (OutType = #ITEM_Variable Or OutType = #ITEM_Array Or OutType = #ITEM_LinkedList Or OutType = #ITEM_Map))
ProcedureReturn
EndIf
Else
ProcedureReturn
EndIf
IsOffsetOf = 0
ElseIf StructName$ <> "" ; OffsetOf mode
Type$ = StructName$
IsOffsetOf = 1
Else
ProcedureReturn
EndIf
; walk the AutoCompleteStack() to resolve sub-structures
; at the same time, add/prepare the structure items for display
;
ResetList(AutoCompleteStack())
AutoComplete_CurrentStructure$ = Type$
Repeat
If NextElement(AutoCompleteStack())
Subitem$ = AutoCompleteStack()
Else
Subitem$ = ""
EndIf
SubItemFound = 0
ClearList(AutoCompleteList())
If FindStructure(Type$, AutoCompleteList())
; process the items
ForEach AutoCompleteList()
Select StructureFieldKind(AutoCompleteList())
Case "Array": Bracket$ = "("
Case "List" : Bracket$ = "()"
Case "Map" : Bracket$ = "("
Default
If FindString(AutoCompleteList(), "[", 1) ; static array
Bracket$ = "["
Else
Bracket$ = ""
EndIf
EndSelect
Entry$ = StructureFieldName(AutoCompleteList()) ; this cuts any "List " prefix
EntryType$ = StructureFieldType(AutoCompleteList())
Debug "Entry: " + Entry$
If CompareMemoryString(@Entry$, @"StructureUnion", #PB_String_NoCase) = #PB_String_Equal
DeleteElement(AutoCompleteList()) ; ignore this
Continue
ElseIf CompareMemoryString(@Entry$, @"EndStructureUnion", #PB_String_NoCase) = #PB_String_Equal
DeleteElement(AutoCompleteList()) ; ignore this
Continue
EndIf
If Subitem$ And CompareMemoryString(@Subitem$, @Entry$, #PB_String_NoCase) = #PB_String_Equal
; Repeat outer loop with the new sub-type
Type$ = EntryType$
AutoComplete_CurrentStructure$ = EntryType$
SubItemFound = 1
Break
Else
; store the processed entry
If AutoCompleteAddBrackets And IsOffsetOf = 0 ; any brackes in OffsetOf are a syntax error (with structures)
If Bracket$
AutoCompleteList() = Entry$ + Bracket$
Else
*ProtoItem.SourceItem = FindPrototype(EntryType$)
If *ProtoItem And *ProtoItem\Prototype$
If RemoveString(RemoveString(*ProtoItem\Prototype$, " "), Chr(9)) = "()"
AutoCompleteList() = Entry$ + "()"
Else
AutoCompleteList() = Entry$ + "("
EndIf
ElseIf FindStructure(EntryType$, DummyList()) Or FindInterface(EntryType$, DummyList())
AutoCompleteList() = Entry$ + "\"
Else
AutoCompleteList() = Entry$
EndIf
EndIf
Else
AutoCompleteList() = Entry$
EndIf
EndIf
Next AutoCompleteList()
; If we get here and SubItem$ is not empty, it means we have a subitem without a match
; so do not offer any choices then
If SubItem$ And SubItemFound = 0
ClearList(AutoCompleteList())
ProcedureReturn
EndIf
ElseIf FindInterface(Type$, AutoCompleteList())
If Subitem$
; Interfaces can have no further nesting
ClearList(AutoCompleteList())
ProcedureReturn
Else
; process the items
ForEach AutoCompleteList()
Entry$ = InterfaceFieldName(AutoCompleteList())
If AutoCompleteAddBrackets
; for OffsetOF, we always need a "()", so add that then
If IsOffsetOf Or Left(RemoveString(RemoveString(AutoCompleteList(), Chr(9)), " "), 2) = "()"
AutoCompleteList() = Entry$ + "()"
Else
AutoCompleteList() = Entry$ + "("
EndIf
Else
AutoCompleteList() = Entry$
EndIf
Next AutoCompleteList()
EndIf
Else
; error, structure type not found
ProcedureReturn
EndIf
Until Subitem$ = ""
EndProcedure
; checks for the case of "OffsetOf(StructName\" in AutoComplete.
; TypeOf and SizeOf are treated the same way
; Returns "StructName" if true or empty string otherwise
;
Procedure.s AutoComplete_IsOffsetOf(Line$, Column)
; search the line backwards
*Buffer = @Line$
*Cursor.Character = *Buffer + Column * #CharSize
; Skip any whitespace
While *Cursor >= *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
; need a '\' now
If *Cursor >= *Buffer And *Cursor\c = '\'
*Cursor - #CharSize
; whitespace
While *Cursor >= *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
; need the struct name now
*NameEnd = *Cursor
While *Cursor >= *Buffer And ValidCharacters(*Cursor\c)
*Cursor - #CharSize
Wend
If *Cursor >= *Buffer And *Cursor <> *NameEnd
; No need to be concerned with Line$ = "NAME\" only here, as that would not
; be an OffsetOf case anyway.
Name$ = PeekS(*Cursor + #CharSize, (*NameEnd - *Cursor) / #CharSize)
; whitespace
While *Cursor >= *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
; possible module operator
If *Cursor >= *Buffer + 1 And *Cursor\c = ':' And PeekC(*Cursor-#CharSize) = ':'
*Cursor - 2*#CharSize
; whitespace
While *Cursor >= *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
; the prefix
*NameEnd = *Cursor
While *Cursor >= *Buffer And ValidCharacters(*Cursor\c)
*Cursor - #CharSize
Wend
Name$ = PeekS(*Cursor + #CharSize, (*NameEnd - *Cursor) / #CharSize) + "::" + Name$
; whitespace
While *Cursor >= *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
EndIf
; need a '(' now
If *Cursor >= *Buffer And *Cursor\c = '('
*Cursor - #CharSize
; whitespace
While *Cursor >= *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
; need "OffsetOf" now (we must be on the 'F' now then)
If *Cursor >= *Buffer + 7*#CharSize And CompareMemoryString(*Cursor-7*#CharSize, @"OffsetOf", #PB_String_NoCase, 8) = #PB_String_Equal
; check what precedes the "OffsetOf"
If *Cursor = *Buffer + 7*#CharSize Or ValidCharacters(PeekC(*Cursor - 8*#CharSize)) = 0
; success
ProcedureReturn Name$
EndIf
; same for TypeOf or SizeOf
ElseIf *Cursor >= *Buffer + 5*#CharSize And CompareMemoryString(*Cursor-5*#CharSize, @"SizeOf", #PB_String_NoCase, 6) = #PB_String_Equal
If *Cursor = *Buffer + 5*#CharSize Or ValidCharacters(PeekC(*Cursor - 6*#CharSize)) = 0
; success
ProcedureReturn Name$
EndIf
ElseIf *Cursor >= *Buffer + 5*#CharSize And CompareMemoryString(*Cursor-5*#CharSize, @"TypeOf", #PB_String_NoCase, 6) = #PB_String_Equal
If *Cursor = *Buffer + 5*#CharSize Or ValidCharacters(PeekC(*Cursor - 6*#CharSize)) = 0
; success
ProcedureReturn Name$
EndIf
EndIf
EndIf
EndIf
EndIf
ProcedureReturn ""
EndProcedure
Procedure OpenAutoCompleteWindow()
;
; Note: We have a problem here:
; On linux, OpenWindow() contains a PB_FlushEvents(), which can cause the ScintillaCallback
; to receive event. Now if the user has autopopup on, and types fast, it could trigger
; multiple instances of this procedure to be running. (the next one is triggered while the first calls OpenWindow etc)
;
; To solve it, we change the autocomplete handling to open the window only once (see CreateAutoCompleteWindow()),
; and then only show/hide it, which will involve no FlushEvents() and thus should work safely
;
If AutoCompleteWindowReady And *ActiveSource And *ActiveSource\IsCode
; First get the current word or check for Structured Autocomplete
;
AutoComplete_IsStructure = #False
AutoComplete_IsModule = #False
AutoComplete_StructureStart = 0
AutoComplete_ModuleStart = 0
*BaseItem.SourceItem = 0
WordStart$ = ""
GetCursorPosition() ; Ensures than the fields are updated
Line$ = GetCurrentLine()
If ScanLine(*ActiveSource, *ActiveSource\CurrentLine-1) ; make sure the current line is up to date!
UpdateFolding(*ActiveSource, *ActiveSource\CurrentLine-1, *ActiveSource\CurrentLine+2)
; Note: A full update of procedure browser etc at this point would be too costly,
; as it would mean a re-update with every typed char if autocomplete is set to auto popup.
; So we set a flag and defere the update until the active line is changed
;
*ActiveSource\ParserDataChanged = #True
EndIf
If Line$
SortParserData(@*ActiveSource\Parser, *ActiveSource)
Column = *ActiveSource\CurrentColumnChars-1
found = GetWordBoundary(@Line$, Len(Line$), Column, @StartIndex, @EndIndex, 1)
; This is for normal and structure mode
If found
WordStart$ = Mid(Line$, StartIndex+1, Column-StartIndex)
ModulePrefix$ = GetModulePrefix(@Line$, Len(Line$), StartIndex)
Else
ModulePrefix$ = GetModulePrefix(@Line$, Len(Line$), Column)
EndIf
; This is the structure mode check
If found
Column = StartIndex-1
Else
Column - 1 ; Column is the char after the cursor, but we want the one before it
EndIf
; check the special OffsetOf case
StructName$ = AutoComplete_IsOffsetOf(Line$, Column)
If StructName$
; resolve any module stuff
BaseItemLine = *ActiveSource\CurrentLine-1
*BaseItem.SourceItem = ClosestSourceItem(@*ActiveSource\Parser, @BaseItemLine, CharsToBytes(Line$, 0, *ActiveSource\Parser\Encoding, Column))
If *BaseItem
StructName$ = ResolveStructureType(@*ActiveSource\Parser, *BaseItem, BaseItemLine, StructName$)
EndIf
If StructName$
*BaseItem = 0
AutoComplete_IsStructure = 1
ClearList(AutoCompleteStack()) ; only OffsetOf(Struct\Field) is allowed
EndIf
EndIf
If AutoComplete_IsStructure = 0
; Shared function to detect if this is a structure and get its base item
StructName$ = ""
BaseItemLine = *ActiveSource\CurrentLine-1
AutoComplete_IsStructure = LocateStructureBaseItem(Line$, Column, @*BaseItem.SourceItem, @BaseItemLine, AutoCompleteStack())
EndIf
If AutoComplete_IsStructure
; need to find the location of the '\' to know when to close the window on backspaces
*Buffer = @Line$
*Cursor.Character = *Buffer + Column * #CharSize
While *Cursor >= *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
If *Cursor >= *Buffer And *Cursor\c = '\'
AutoComplete_StructureStart = (*Cursor - *Buffer) / #CharSize
EndIf
ElseIf ModulePrefix$ <> ""
; need to find the location of the :: to know when the window should be closed (like with structures)
AutoComplete_IsModule = #True
AutoComplete_CurrentModule$ = ModulePrefix$
*Buffer = @Line$
*Cursor.Character = *Buffer + Column * #CharSize
While *Cursor >= *Buffer And (*Cursor\c = ' ' Or *Cursor\c = 9)
*Cursor - #CharSize
Wend
If *Cursor >= *Buffer And *Cursor\c = ':'
AutoComplete_ModuleStart = (*Cursor - *Buffer) / #CharSize
EndIf
EndIf
EndIf
CompilerIf #PB_Compiler_Debugger
Debug "--------- Autocomplete ---------------"
Debug "Current line: " + Line$
Debug "Current word: " + WordStart$
Debug "Module prefix: " + ModulePrefix$
Debug "Structure item: " + Str(*BaseItem)
Debug "Structure name: " + StructName$ + " (for OffsetOf only)"
If *BaseItem
Debug "Structure item name: " + *BaseItem\Name$
Debug "Strurture item input token: " + Str(*BaseItem\Type)
Debug "Structure item resolved type: " + ResolveItemType(*BaseItem, BaseItemLine, @OutType)
Debug "Structure item output token: " + Str(OutType)
EndIf
Debug "Structure Stack"
ForEach AutocompleteStack()
Debug " " + AutoCompleteStack()
Next
Debug "------------Structure query-----------"
Protected NewList _test.s()
Debug "Result = " + Str(FindStructure(WordStart$, _test()))
ForEach _test()
Debug " " + _test()
Next
Debug "------------Interface query-----------"
ClearList(_test.s())
Debug "Result = " + Str(FindInterface(WordStart$, _test()))
ForEach _test()
Debug " " + _test()
Next
Debug "--------------------------------------"
CompilerEndIf
ClearList(AutoCompleteList())
AutoComplete_Start = 0
AutoComplete_End = -1
If AutoComplete_IsStructure
AutoComplete_FillStructured(WordStart$, StructName$, *BaseItem, BaseItemLine)
ElseIf WordStart$ <> "" Or AutoComplete_IsModule
AutoComplete_FillNormal(WordStart$, ModulePrefix$)
; AutomationEvent_AutoComplete(AutoCompleteList()) ; send the event only for normal autocomplete, not for structured (as there modification makes no sense anyway)
EndIf
If AutoComplete_IsStructure Or AutoComplete_IsModule Or WordStart$ <> ""
If ListSize(AutoCompleteList()) > 0
; NOW we do the sorting ...
SortList(AutoCompleteList(), 2)
; cut double values
Last$ = ""
ForEach AutoCompleteList()
; Note: @List() returns the list element, not string pointer, therefore the PeekI()
If CompareMemoryString(@Last$, PeekI(@AutoCompleteList()), 1) = 0
DeleteElement(AutoCompleteList())
Else
Last$ = AutoCompleteList()
EndIf
Next AutoCompleteList()
; Now we check the charmatch options
; For Options 0 and 1, the list is exactly what must be displayed now,
; for the 2 option we need to filter
;
If (AutoComplete_IsStructure Or AutoComplete_IsModule) And AutoCompleteCharMatchOnly = 1 And WordStart$ <> ""
; AutoComplete_FillStructured() and salso the module mode always adds the whole structure, so we must cut some stuff here
AutoComplete_Start = -1
ResetList(AutoCompleteList())
While NextElement(AutoCompleteList())
If CompareMemoryString(PeekI(@AutoCompleteList()), @WordStart$, 1, 1) >= 0
AutoComplete_Start = ListIndex(AutoCompleteList())
Break
EndIf
Wend
AutoComplete_End = -1
If AutoComplete_Start <> -1 And LastElement(AutoCompleteList())
Repeat
If CompareMemoryString(PeekI(@AutoCompleteList()), @WordStart$, 1, 1) <= 0
AutoComplete_End = ListIndex(AutoCompleteList())
Break
EndIf
Until Not PreviousElement(AutoCompleteList())
EndIf
ElseIf AutoCompleteCharMatchOnly <> 2
AutoComplete_Start = 0
AutoComplete_End = ListSize(AutoCompleteList())-1