forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcedureBrowser.pb
More file actions
584 lines (448 loc) · 19.7 KB
/
ProcedureBrowser.pb
File metadata and controls
584 lines (448 loc) · 19.7 KB
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
;--------------------------------------------------------------------------------------------
; 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.
;--------------------------------------------------------------------------------------------
Global Backup_ProcedureBrowserSort, Backup_DisplayProtoType
Procedure UpdateProcedureList()
; The Issues tool usually needs an update two when the ProcedureBrowser does,
; so just do this always from here for simplicity
UpdateIssueList()
If *ActiveSource = *ProjectInfo Or *ActiveSource\IsCode = 0
ClearList(ProcedureList())
ClearGadgetItems(#GADGET_ProcedureBrowser)
ProcedureReturn
EndIf
If ProcedureBrowserMode = 1
; Create our procedurelist from the SourceItems of the current source
;
ClearList(ProcedureList())
InsideMacro = 0
ModulePrefix$ = ""
If *ActiveSource\Parser\SourceItemArray
For i = 0 To *ActiveSource\Parser\SourceItemCount-1
*Item.SourceItem = *ActiveSource\Parser\SourceItemArray\Line[i]\First
While *Item
If InsideMacro = 0
Select *Item\Type
Case #ITEM_Module, #ITEM_DeclareModule
ModulePrefix$ = *Item\Name$ + "::"
Case #ITEM_EndModule, #ITEM_EndDeclareModule
ModulePrefix$ = ""
Case #ITEM_Procedure
AddElement(ProcedureList())
ProcedureList()\Name$ = ModulePrefix$ + *Item\Name$
ProcedureList()\Line = i+1
ProcedureList()\Type = 0
Procedurelist()\Prototype$ = *Item\Prototype$
Case #ITEM_Macro
AddElement(ProcedureList())
ProcedureList()\Name$ = ModulePrefix$ + *Item\Name$
ProcedureList()\Line = i+1
ProcedureList()\Type = 1
Procedurelist()\Prototype$ = *Item\Prototype$
EndSelect
EndIf
; we recognize comment marks even inside macros, as they cannot screw anything,
; since they are comments
If *Item\Type = #ITEM_CommentMark
AddElement(ProcedureList())
ProcedureList()\Name$ = *Item\Name$
ProcedureList()\Line = i+1
ProcedureList()\Type = 2
ElseIf *Item\Type = #ITEM_Issue
If SelectElement(Issues(), *Item\Issue)
If Issues()\InBrowser
AddElement(ProcedureList())
ProcedureList()\Name$ = *Item\Name$
ProcedureList()\Line = i+1
ProcedureList()\Type = 3
EndIf
EndIf
ElseIf *Item\Type = #ITEM_Macro
InsideMacro = 1
ElseIf *Item\Type = #ITEM_MacroEnd
InsideMacro = 0
EndIf
*Item = *Item\Next
Wend
Next i
EndIf
; first sort the list
;
If ListSize(ProcedureList()) > 1
Repeat
Done = 1
FirstElement(ProcedureList())
*Previous.ProcedureInfo = @ProcedureList()
While NextElement(ProcedureList())
Change = 0
If ProcedureBrowserSort = 0
If ProcedureList()\Line < *Previous\Line
Change = 1
EndIf
ElseIf ProcedureBrowserSort = 1
If ProcedureList()\Type = *Previous\Type
If ProcedureList()\Line < *Previous\Line
Change = 1
EndIf
ElseIf ProcedureList()\Type < *Previous\Type
Change = 1
EndIf
ElseIf ProcedureBrowserSort = 2
If CompareMemoryString(@ProcedureList()\Name$, @*Previous\Name$, #PB_String_NoCase) < 0
Change = 1
EndIf
ElseIf ProcedureBrowserSort = 3
If ProcedureList()\Type = *Previous\Type
If CompareMemoryString(@ProcedureList()\Name$, @*Previous\Name$, #PB_String_NoCase) < 0
Change = 1
EndIf
ElseIf ProcedureList()\Type < *Previous\Type
Change = 1
EndIf
EndIf
If Change
SwapElements(ProcedureList(), *Previous, @ProcedureList())
Done = 0
EndIf
*Previous = @ProcedureList()
Wend
Until Done
EndIf
;StartGadgetFlickerFix(#GADGET_ProcedureBrowser)
; preserve the selection if possible
OldIndex = GetGadgetState(#GADGET_ProcedureBrowser)
NewIndex = -1
If OldIndex <> -1
OldText$ = GetGadgetItemText(#GADGET_ProcedureBrowser, OldIndex)
EndIf
ClearGadgetItems(#GADGET_ProcedureBrowser)
ForEach ProcedureList()
If ProcedureList()\Type = 0
Text$ = ProcedureList()\Name$
If DisplayPrototype
Text$ + ProcedureList()\Prototype$
EndIf
ElseIf ProcedureList()\Type = 1
Text$ = "+ " + ProcedureList()\Name$
If DisplayPrototype
Text$ + ProcedureList()\Prototype$
EndIf
ElseIf ProcedureList()\Type = 2
Text$ = "> " + ProcedureList()\Name$
Else
Text$ = ProcedureList()\Name$
EndIf
AddGadgetItem(#GADGET_ProcedureBrowser, -1, Text$)
; check if this is our old selection
If CompareMemoryString(@OldText$, @Text$, #PB_String_NoCase) = 0 And OldIndex <> -1
NewIndex = CountGadgetItems(#GADGET_ProcedureBrowser)-1
EndIf
Next ProcedureList()
CompilerIf #CompileLinux
SetGadgetState(#GADGET_ProcedureBrowser, -1)
CompilerElse
; restore old selection
If NewIndex <> -1
SetGadgetState(#GADGET_ProcedureBrowser, NewIndex)
EndIf
CompilerEndIf
;StopGadgetFlickerFix(#GADGET_ProcedureBrowser)
EndIf
EndProcedure
Procedure FindProcedureFromSorted(*Parser.ParserData, *Name, Type, ModuleName$)
Bucket = GetBucket(*Name)
*Item.SourceItem = *Parser\Modules(UCase(ModuleName$))\Indexed[Type]\Bucket[Bucket]
While *Item
Select CompareMemoryString(*Name, @*Item\Name$, #PB_String_NoCase)
Case #PB_String_Equal
ProcedureReturn *Item
Case #PB_String_Greater:
*Item = *Item\NextSorted
Default
Break
EndSelect
Wend
ProcedureReturn 0
EndProcedure
Procedure JumpToProcedure() ; return 1 if a jump was done
Protected *Found.SourceItem
If *ActiveSource\IsCode = 0
ProcedureReturn 0
EndIf
; update the information
UpdateCursorPosition()
SortParserData(@*ActiveSource\Parser, *ActiveSource)
; get the current token
*StartItem.SourceItem = LocateSourceItem(@*ActiveSource\Parser, *ActiveSource\CurrentLine-1, *ActiveSource\CurrentColumnBytes-1)
If *StartItem = 0 Or *StartItem\Type <> #ITEM_UnknownBraced
; not a procedure
ProcedureReturn 0
EndIf
; Note: We cannot just search all modules, because there could be private implementations
; matching our name, but not publicly visible. So we must first look at the public parts if
; the searched procedure is declared and then look in the implementation for the actual procedure
;
Protected NewList OpenModules.s(), NewList CandidateModules.s()
*ModuleStart.SourceItem = *StartItem
ModuleStartLine = *ActiveSource\CurrentLine - 1
If *StartItem\ModulePrefix$ <> ""
AddElement(OpenModules())
OpenModules() = *StartItem\ModulePrefix$
ElseIf FindModuleStart(@*ActiveSource\Parser, @ModuleStartLine, @*ModuleStart, OpenModules())
; we are inside this module, so directly make it a candidate for implementation search
AddElement(CandidateModules())
CandidateModules() = "IMPL::" + *ModuleStart\Name$
Else
; directly make the main module a candidate
AddElement(CandidateModules())
CandidateModules() = ""
EndIf
; Now check the open modules if they have a public declare that we look for
; Here we look for #ITEM_Declare only!
;
ForEach OpenModules()
; check active source first
If *ActiveSource <> *ProjectInfo
If FindProcedureFromSorted(@*ActiveSource\Parser, @*StartItem\Name$, #ITEM_Declare, OpenModules())
AddElement(CandidateModules())
CandidateModules() = "IMPL::" + OpenModules()
Continue ; no need to look more at this module
EndIf
EndIf
; check projects
If AutoCompleteProject And *ActiveSource\ProjectFile
ForEach ProjectFiles()
If ProjectFiles()\Source = 0 And ProjectFiles()\Parser\SortedValid
*Found = FindProcedureFromSorted(@ProjectFiles()\Parser, @*StartItem\Name$, #ITEM_Declare, OpenModules())
ElseIf ProjectFiles()\Source And ProjectFiles()\Source <> *ActiveSource And ProjectFiles()\Source\Parser\SortedValid
*Found = FindProcedureFromSorted(@ProjectFiles()\Source\Parser, @*StartItem\Name$, #ITEM_Declare, OpenModules())
Else
*Found = 0
EndIf
If *Found
AddElement(CandidateModules())
CandidateModules() = "IMPL::" + OpenModules()
Break
EndIf
Next ProjectFiles()
EndIf
; check all other open sources now...
;
If AutoCompleteAllFiles
ForEach FileList()
If @FileList() <> *ProjectInfo And @FileList() <> *ActiveSource And FileList()\Parser\SortedValid And (AutoCompleteProject = 0 Or FileList()\ProjectFile = 0)
If FindProcedureFromSorted(@FileList()\Parser, @*StartItem\Name$, #ITEM_Declare, OpenModules())
AddElement(CandidateModules())
CandidateModules() = "IMPL::" + OpenModules()
Break
EndIf
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource) ; important!
EndIf
Next OpenModules()
; Now look at the implementations of the actual candidate modules
; Here we look for the actual #ITEM_Procedure
;
ForEach CandidateModules()
; check active source first
If *ActiveSource <> *ProjectInfo
*Found = FindProcedureFromSorted(@*ActiveSource\Parser, @*StartItem\Name$, #ITEM_Procedure, CandidateModules())
If *Found
Line = *Found\SortedLine + 1
If *ActiveSource\CurrentLine <> Line ; do not jump if the procedure header was double-clicked
ChangeActiveLine(Line, -5)
; Unfold the procedure block if it was folded
SendEditorMessage(#SCI_ENSUREVISIBLE, Line)
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndIf
EndIf
; check projects
If AutoCompleteProject And *ActiveSource\ProjectFile
ForEach ProjectFiles()
If ProjectFiles()\Source = 0 And ProjectFiles()\Parser\SortedValid
*Found = FindProcedureFromSorted(@ProjectFiles()\Parser, @*StartItem\Name$, #ITEM_Procedure, CandidateModules())
ElseIf ProjectFiles()\Source And ProjectFiles()\Source <> *ActiveSource And ProjectFiles()\Source\Parser\SortedValid
*Found = FindProcedureFromSorted(@ProjectFiles()\Source\Parser, @*StartItem\Name$, #ITEM_Procedure, CandidateModules())
Else
*Found = 0
EndIf
If *Found
Line = *Found\SortedLine + 1 ; item will be invalid once the file is loaded!
If LoadSourceFile(ProjectFiles()\FileName$) ; need to load the file
ChangeActiveLine(Line, -5)
; Unfold the procedure block if it was folded
SendEditorMessage(#SCI_ENSUREVISIBLE, Line)
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndIf
Next ProjectFiles()
EndIf
; check all other open sources now...
;
If AutoCompleteAllFiles
ForEach FileList()
If @FileList() <> *ProjectInfo And @FileList() <> *ActiveSource And FileList()\Parser\SortedValid And (AutoCompleteProject = 0 Or FileList()\ProjectFile = 0)
*Found = FindProcedureFromSorted(@FileList()\Parser, @*StartItem\Name$, #ITEM_Procedure, CandidateModules())
If *Found
Line = *Found\SortedLine+1
ChangeActiveSourcecode() ; changes *ActiveSource to the current FileList() element
ChangeActiveLine(Line, -5)
; Unfold the procedure block if it was folded
SendEditorMessage(#SCI_ENSUREVISIBLE, Line)
ProcedureReturn 1
EndIf
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource) ; important!
EndIf
Next CandidateModules()
ProcedureReturn 0
EndProcedure
;- ToolsPanel plugin functions
Procedure ProcedureBrowser_CreateFunction(*Entry.ToolsPanelEntry, PanelItemID)
ListViewGadget(#GADGET_ProcedureBrowser, 0, 0, 0, 0)
If *Entry\IsSeparateWindow = 0 Or NoIndependentToolsColors = 0
ToolsPanel_ApplyColors(#GADGET_ProcedureBrowser)
EndIf
ProcedureBrowserMode = 1 ; indicate that the procedurebrowser is present
If *ActiveSource
UpdateProcedureList()
EndIf
EndProcedure
Procedure ProcedureBrowser_DestroyFunction(*Entry.ToolsPanelEntry)
ProcedureBrowserMode = 0 ; indicate that the procedurebrowser is not enabled
EndProcedure
Procedure ProcedureBrowser_ResizeHandler(*Entry.ToolsPanelEntry, PanelWidth, PanelHeight)
If *Entry\IsSeparateWindow
ResizeGadget(#GADGET_ProcedureBrowser, 5, 5, PanelWidth-10, PanelHeight-10)
Else
ResizeGadget(#GADGET_ProcedureBrowser, 0, 0, PanelWidth, PanelHeight)
EndIf
EndProcedure
Procedure ProcedureBrowser_EventHandler(*Entry.ToolsPanelEntry, EventGadgetID)
If *ActiveSource <> *ProjectInfo And (*ActiveSource\IsForm = 0 Or (*ActiveSource\IsForm = 1 And FormWindows()\current_view = 0))
;
; On OSX, for some reason we get events when you type in the Scintilla, so only react when the browser is focused
; as else the Caret jumps around to the selected procedure all the time
;
Protected EventValid = #False
CompilerIf #CompileMac
If EventGadgetID = #GADGET_ProcedureBrowser And GetActiveGadget() = #GADGET_ProcedureBrowser
EventValid = #True
EndIf
CompilerElse
If EventGadgetID = #GADGET_ProcedureBrowser
EventValid = #True
EndIf
CompilerEndIf
If EventValid
index = GetGadgetState(#GADGET_ProcedureBrowser)
If index <> -1
SelectElement(ProcedureList(), index)
ChangeActiveLine(ProcedureList()\Line, 0)
; Unfold the procedure block if it was folded
SendEditorMessage(#SCI_ENSUREVISIBLE, ProcedureList()\Line)
CompilerIf #CompileMac Or #CompileLinux
; remove selection so the we won't get more jumps to the procedure start! (OSX/Linux specific problem)
SetGadgetState(#GADGET_ProcedureBrowser, -1)
CompilerEndIf
EndIf
EndIf
EndIf
EndProcedure
Procedure ProcedureBrowser_PreferenceLoad(*Entry.ToolsPanelEntry)
PreferenceGroup("ProcedureBrowser")
ProcedureBrowserSort = ReadPreferenceLong ("Sort", 0) ; 0=by line, nogroup, 1=by line, group, 2=byname, nogroup 3 = byname, group
DisplayProtoType = ReadPreferenceLong ("Prototype", 0)
EndProcedure
Procedure ProcedureBrowser_PreferenceSave(*Entry.ToolsPanelEntry)
PreferenceComment("")
PreferenceGroup("ProcedureBrowser")
WritePreferenceLong("Sort", ProcedureBrowserSort)
WritePreferenceLong("Prototype", DisplayProtoType)
EndProcedure
Procedure ProcedureBrowser_PreferenceStart(*Entry.ToolsPanelEntry)
Backup_ProcedureBrowserSort = ProcedureBrowserSort
Backup_DisplayProtoType = DisplayProtoType
EndProcedure
Procedure ProcedureBrowser_PreferenceApply(*Entry.ToolsPanelEntry)
ProcedureBrowserSort = Backup_ProcedureBrowserSort
DisplayProtoType = Backup_DisplayProtoType
EndProcedure
Procedure ProcedureBrowser_PreferenceCreate(*Entry.ToolsPanelEntry)
Top = 10
CheckBoxGadget(#GADGET_Preferences_ProcedureBrowserSort, 10, 10, 300, 25, Language("Preferences","ProcedureSort"))
CheckBoxGadget(#GADGET_Preferences_ProcedureBrowserGroup, 10, 40, 300, 25, Language("Preferences","ProcedureGroup"))
CheckBoxGadget(#GADGET_Preferences_ProcedureProtoType, 10, 70, 300, 25, Language("Preferences", "ProcedurePrototype"))
GetRequiredSize(#GADGET_Preferences_ProcedureBrowserSort, @Width, @Height)
Width = Max(Width, GetRequiredWidth(#GADGET_Preferences_ProcedureBrowserGroup))
Width = Max(Width, GetRequiredWidth(#GADGET_Preferences_ProcedureProtoType))
ResizeGadget(#GADGET_Preferences_ProcedureBrowserSort, 10, 10, Width, Height)
ResizeGadget(#GADGET_Preferences_ProcedureBrowserGroup, 10, 15+Height, Width, Height)
ResizeGadget(#GADGET_Preferences_ProcedureProtoType, 10, 20+Height*2, Width, Height)
If Backup_ProcedureBrowserSort >= 2
SetGadgetState(#GADGET_Preferences_ProcedureBrowserSort, 1)
EndIf
SetGadgetState(#GADGET_Preferences_ProcedureBrowserGroup, Backup_ProcedureBrowserSort % 2)
SetGadgetState(#GADGET_Preferences_ProcedureProtoType, Backup_DisplayProtoType)
EndProcedure
Procedure ProcedureBrowser_PreferenceDestroy(*Entry.ToolsPanelEntry)
Backup_ProcedureBrowserSort = 2*GetGadgetState(#GADGET_Preferences_ProcedureBrowserSort)
Backup_ProcedureBrowserSort + GetGadgetState(#GADGET_PReferences_ProcedureBrowserGroup)
Backup_DisplayProtoType = GetGadgetState(#GADGET_Preferences_ProcedureProtoType)
EndProcedure
Procedure ProcedureBrowser_PreferenceEvents(*Entry.ToolsPanelEntry, EventGadgetID)
;
; nothing here
;
EndProcedure
Procedure ProcedureBrowser_PreferenceChanged(*Entry.ToolsPanelEntry, IsConfigOpen)
If IsConfigOpen
Sort = 2*GetGadgetState(#GADGET_Preferences_ProcedureBrowserSort)
Sort + GetGadgetState(#GADGET_PReferences_ProcedureBrowserGroup)
If Sort <> ProcedureBrowserSort
ProcedureReturn 1
ElseIf GetGadgetState(#GADGET_Preferences_ProcedureProtoType) <> DisplayProtoType
ProcedureReturn 1
EndIf
Else
If ProcedureBrowserSort <> Backup_ProcedureBrowserSort Or DisplayProtoType <> Backup_DisplayProtoType
ProcedureReturn 1
EndIf
EndIf
ProcedureReturn 0
EndProcedure
;- Initialisation code
; This will make this Tool available to the editor
;
ProcedureBrowser_VT.ToolsPanelFunctions
ProcedureBrowser_VT\CreateFunction = @ProcedureBrowser_CreateFunction()
ProcedureBrowser_VT\DestroyFunction = @ProcedureBrowser_DestroyFunction()
ProcedureBrowser_VT\ResizeHandler = @ProcedureBrowser_ResizeHandler()
ProcedureBrowser_VT\EventHandler = @ProcedureBrowser_EventHandler()
ProcedureBrowser_VT\PreferenceLoad = @ProcedureBrowser_PreferenceLoad()
ProcedureBrowser_VT\PreferenceSave = @ProcedureBrowser_PreferenceSave()
ProcedureBrowser_VT\PreferenceStart = @ProcedureBrowser_PreferenceStart()
ProcedureBrowser_VT\PreferenceApply = @ProcedureBrowser_PreferenceApply()
ProcedureBrowser_VT\PreferenceCreate = @ProcedureBrowser_PreferenceCreate()
ProcedureBrowser_VT\PreferenceDestroy = @ProcedureBrowser_PreferenceDestroy()
ProcedureBrowser_VT\PreferenceEvents = @ProcedureBrowser_PreferenceEvents()
ProcedureBrowser_VT\PreferenceChanged = @ProcedureBrowser_PreferenceChanged()
AddElement(AvailablePanelTools())
AvailablePanelTools()\FunctionsVT = @ProcedureBrowser_VT
AvailablePanelTools()\NeedPreferences = 1
AvailablePanelTools()\NeedConfiguration = 1
AvailablePanelTools()\PreferencesWidth = 320
AvailablePanelTools()\PreferencesHeight = 100
AvailablePanelTools()\NeedDestroyFunction = 1
AvailablePanelTools()\ToolID$ = "ProcedureBrowser"
AvailablePanelTools()\PanelTitle$ = "ProcedureBrowserShort"
AvailablePanelTools()\ToolName$ = "ProcedureBrowserLong"