forked from pyscripter/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cCodeCompletion.pas
901 lines (826 loc) · 30.3 KB
/
cCodeCompletion.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
{-----------------------------------------------------------------------------
Unit Name: cCodeCompletion
Author: Kiriakos Vlahos
Purpose: Code completion support classes
History:
-----------------------------------------------------------------------------}
unit cCodeCompletion;
interface
Uses
Types,
Classes,
Contnrs,
SynEditTypes,
SynEditHighlighter;
type
TBaseCodeCompletionSkipHandler = class
function SkipCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes) : Boolean; virtual; abstract;
end;
TBaseCodeCompletionHandler = class
procedure Initialize; virtual;
procedure Finalize; virtual;
function HandleCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes;
out InsertText, DisplayText : string) : Boolean; virtual; abstract;
function GetInfo(CCItem: string) : string; virtual; abstract;
end;
TCodeCompletion = class
SkipHandlers : TObjectList;
CompletionHandlers : TObjectList;
constructor Create;
destructor Destroy; override;
procedure RegisterSkipHandler(Handler : TBaseCodeCompletionSkipHandler);
procedure RegisterCompletionHandler(Handler : TBaseCodeCompletionHandler);
end;
TIDECompletion = class
class var EditorCodeCompletion : TCodeCompletion;
class var InterpreterCodeCompletion : TCodeCompletion;
class constructor Create;
class destructor Destroy;
end;
implementation
uses
SysUtils,
StrUtils,
SynHighlighterPython,
SynRegExpr,
uCommonFunctions,
cPyBaseDebugger,
cPyDebugger,
cPythonSourceScanner,
cRefactoring,
dmCommands,
VarPyth,
JclStrings, PythonEngine, StringResources, gnugettext;
procedure GetModuleList(Path: Variant; out InsertText, DisplayText : string);
Var
i: Integer;
S: string;
SortedNameSpace: TStringList;
begin
SortedNameSpace := TStringList.Create;
SortedNameSpace.CaseSensitive := True;
SortedNameSpace.Duplicates := dupIgnore; // Remove duplicates
SortedNameSpace.Sorted := True;
try
InternalInterpreter.GetModulesOnPath(Path, SortedNameSpace);
InsertText := SortedNameSpace.Text;
for i := 0 to SortedNameSpace.Count - 1 do
begin
S := SortedNameSpace[i];
DisplayText := DisplayText + Format('\Image{%d}\hspace{2}%s', [16, S]);
if i < SortedNameSpace.Count - 1 then
DisplayText := DisplayText + #10;
end;
finally
SortedNameSpace.Free;
end;
end;
procedure ProcessNamespace(SortedNameSpace, NameSpace : TStringList; out InsertText, DisplayText : string);
Var
i: Integer;
ImageIndex : integer;
S: string;
CE: TBaseCodeElement;
begin
//fSortedNameSpace is freed in Close event
SortedNameSpace.Sorted := True;
SortedNameSpace.AddStrings(NameSpace);
SortedNameSpace.Sorted := False;
SortedNameSpace.CustomSort(ComparePythonIdents);
InsertText := SortedNameSpace.Text;
for i := 0 to SortedNameSpace.Count - 1 do
begin
S := SortedNameSpace[i];
CE := SortedNameSpace.Objects[i] as TBaseCodeElement;
if not Assigned(CE) then
begin
// Keyword
ImageIndex := Integer(TCodeImages.Keyword);
DisplayText := DisplayText + Format('\Image{%d}\hspace{2}\color{clHotlight}%s', [ImageIndex, S]);
end
else
begin
if (CE is TParsedModule) or (CE is TModuleImport) then
ImageIndex := Integer(TCodeImages.Module)
else if CE is TParsedFunction then
begin
if CE.Parent is TParsedClass then
ImageIndex := Integer(TCodeImages.Method)
else
ImageIndex := Integer(TCodeImages.Func)
end
else if CE is TParsedClass then
ImageIndex := Integer(TCodeImages.Klass)
else
begin // TVariable or TParsedVariable
if Assigned(CE) and (CE.Parent is TParsedClass) then
ImageIndex := Integer(TCodeImages.Field)
else
ImageIndex := Integer(TCodeImages.Variable);
end;
DisplayText := DisplayText + Format('\Image{%d}\hspace{2}%s',
[ImageIndex, S]);
end;
if i < SortedNameSpace.Count - 1 then
DisplayText := DisplayText + #10;
end;
end;
{ TRegExpressions }
type
TRegExpressions = class
class var RE_Import : TRegExpr;
class var RE_From : TRegExpr;
class var RE_FromImport : TRegExpr;
class var RE_For : TRegExpr;
class var RE_ImportAs : TRegExpr;
class var RE_FromImportAs : TRegExpr;
class var RE_String : TRegExpr;
class constructor Create;
class destructor Destroy;
end;
class constructor TRegExpressions.Create;
begin
RE_Import := CompiledRegExpr(
Format('^\s*import +(%s( +as +%s)? *, *)*(%s)?$',
[DottedIdentRe, IdentRE, DottedIdentRE]));
RE_From := CompiledRegExpr(
Format('^\s*from +(\.*)(%s)?$', [DottedIdentRE]));
RE_FromImport := CompiledRegExpr(
Format('^\s*from +(\.*)(%s)? +import +\(? *(%s( +as +%s)? *, *)*(%s)?$',
[DottedIdentRe, IdentRE, IdentRe, IdentRe]));
RE_For := CompiledRegExpr(
Format('^\s*for +(%s *, *)*(%s)?$', [IdentRe, IdentRe]));
RE_ImportAs := CompiledRegExpr(
Format('^\s*import +(%s( +as +%s)? *, *)*%s +as +(%s)?$',
[DottedIdentRe, IdentRE, DottedIdentRE, IdentRE]));
RE_FromImportAs := CompiledRegExpr(
Format('^\s*from +(\.*)(%s)? +import +\(? *(%s( +as +%s)? *, *)*%s +as +(%s)?$',
[DottedIdentRe, IdentRE, IdentRe, IdentRe, IdentRE]));
RE_String := CompiledRegExpr(
Format('[''"]\.(%s)?$', [IdentRe, IdentRe]));
end;
class destructor TRegExpressions.Destroy;
begin
RE_Import.Free;
RE_From.Free;
RE_FromImport.Free;
RE_For.Free;
RE_ImportAs.Free;
RE_FromImportAs.Free;
RE_String.Free;
end;
{ TStringAndCommentSkipHandler }
type
TStringAndCommentSkipHandler = class(TBaseCodeCompletionSkipHandler)
function SkipCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes) : Boolean; override;
end;
function TStringAndCommentSkipHandler.SkipCodeCompletion(const Line,
FileName: string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr: TSynHighlighterAttributes): Boolean;
begin
Result := Assigned(HighlighterAttr) and
((HighlighterAttr = Highlighter.StringAttribute) or
(HighlighterAttr = Highlighter.CommentAttribute) or
((Highlighter is TSynPythonSyn) and
(HighlighterAttr = TSynPythonSyn(Highlighter).CodeCommentAttri) or
(HighlighterAttr = TSynPythonSyn(Highlighter).MultiLineStringAttri) or
(HighlighterAttr = TSynPythonSyn(Highlighter).DocStringAttri)));
end;
{ TForStatementSkipHandler }
type
TForStatementSkipHandler = class(TBaseCodeCompletionSkipHandler)
function SkipCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes) : Boolean; override;
end;
function TForStatementSkipHandler.SkipCodeCompletion(const Line,
FileName: string; Caret: TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes): Boolean;
begin
Result := TRegExpressions.RE_For.Exec(Copy(Line, 1, Caret.Char - 1));
end;
{ TImportAsStatementSkipHandler }
type
TImportAsStatementSkipHandler = class(TBaseCodeCompletionSkipHandler)
function SkipCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes) : Boolean; override;
end;
function TImportAsStatementSkipHandler.SkipCodeCompletion(const Line,
FileName: string; Caret: TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes): Boolean;
begin
Result := TRegExpressions.RE_ImportAs.Exec(Copy(Line, 1, Caret.Char - 1)) or
TRegExpressions.RE_FromImportAs.Exec(Copy(Line, 1, Caret.Char - 1));
end;
{ TImportStatementHandler }
type
TImportStatementHandler = class(TBaseCodeCompletionHandler)
protected
PythonPathAdder: IInterface;
fFileName : string;
fModulePrefix : string;
fPathDepth : integer;
public
procedure Finalize; override;
function HandleCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes;
out InsertText, DisplayText : string) : Boolean; override;
function GetInfo(CCItem: string) : string; override;
end;
procedure TImportStatementHandler.Finalize;
begin
inherited;
PythonPathAdder := nil;
fFileName := '';
fModulePrefix := '';
fPathDepth := 0;
end;
function TImportStatementHandler.GetInfo(CCItem: string): string;
Var
Module : string;
ParsedModule: TParsedModule;
begin
Module := CCItem;
if fModulePrefix <> '' then
Module := fModulePrefix + '.' + Module;
if PyScripterRefactor.InitializeQuery then
begin
ParsedModule :=
PyScripterRefactor.ResolveModuleImport(Module, fFileName, fPathDepth);
if Assigned(ParsedModule) then
Result := GetLineRange(ParsedModule.DocString, 1, 20);
PyScripterRefactor.FinalizeQuery;
end;
end;
function TImportStatementHandler.HandleCodeCompletion(const Line,
FileName: string; Caret : TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes; out InsertText,
DisplayText: string): Boolean;
Var
FNameVar : Variant;
Dir, DottedModName : string;
begin
Result := TRegExpressions.RE_Import.Exec(Copy(Line, 1, Caret.Char - 1));
if Result then
begin
// autocomplete import statement
fFileName := FileName;
// Add the file path to the Python path - Will be automatically removed
if fFileName <> '' then
PythonPathAdder := InternalInterpreter.AddPathToPythonPath
(ExtractFileDir(fFileName));
DottedModName := TRegExpressions.RE_Import.Match[3];
if CharPos(DottedModName, '.') > 0 then begin
fModulePrefix := DottedModName;
Delete(fModulePrefix, LastDelimiter('.', fModulePrefix), MaxInt);
FNameVar := InternalInterpreter.PyInteractiveInterpreter.findModuleOrPackage(fModulePrefix, None);
if not VarIsNone(FNameVar) then begin
Dir := FNameVar;
Dir := ExtractFileDir(Dir);
GetModuleList(VarPythonCreate([Dir]), InsertText, DisplayText);
end;
end
else
begin
fModulePrefix := '';
GetModuleList(None, InsertText, DisplayText);
end;
end
end;
{ TFromStatementHandler }
type
TFromStatementHandler = class(TImportStatementHandler)
public
function HandleCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes;
out InsertText, DisplayText : string) : Boolean; override;
end;
function TFromStatementHandler.HandleCodeCompletion(const Line,
FileName: string; Caret : TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes; out InsertText,
DisplayText: string): Boolean;
Var
i : integer;
FNameVar : Variant;
Dir, DottedModName : string;
SArray : TStringDynArray;
begin
Result := TRegExpressions.RE_From.Exec(Copy(Line, 1, Caret.Char - 1));
if Result then
begin
// autocomplete from statement
fModulePrefix := '';
fFileName := FileName;
fPathDepth := TRegExpressions.RE_From.MatchLen[1];
DottedModName := TRegExpressions.RE_From.Match[2];
if fPathDepth > 0 then
begin
if FileName = '' then begin
Result := True; // No completion
Exit;
end;
// relative import
Dir := fFileName;
for i := 1 to fPathDepth do
Dir := ExtractFileDir(Dir);
PythonPathAdder := InternalInterpreter.AddPathToPythonPath(Dir);
if (DottedModName <> '') and (CharPos(DottedModName, '.') > 0) then
begin
fModulePrefix := DottedModName;
Delete(fModulePrefix, LastDelimiter('.', fModulePrefix), MaxInt);
SArray := SplitString(DottedModName, '.');
for i := Low(SArray) to High(SArray) - 1 do
Dir := Dir + PathDelim + SArray[i];
end;
GetModuleList(VarPythonCreate([Dir]), InsertText, DisplayText);
end
else
begin
// Add the file path to the Python path - Will be automatically removed
if fFileName <> '' then
PythonPathAdder := InternalInterpreter.AddPathToPythonPath
(ExtractFileDir(fFileName));
if CharPos(DottedModName, '.') > 0 then begin
fModulePrefix := DottedModName;
Delete(fModulePrefix, LastDelimiter('.', fModulePrefix), MaxInt);
FNameVar := InternalInterpreter.PyInteractiveInterpreter.findModuleOrPackage(fModulePrefix, None);
if not VarIsNone(FNameVar) then begin
Dir := FNameVar;
Dir := ExtractFileDir(Dir);
GetModuleList(VarPythonCreate([Dir]), InsertText, DisplayText);
end;
end
else
begin
GetModuleList(None, InsertText, DisplayText);
end;
end;
end;
end;
{ TFromImportModuleHandler }
type
TFromImportModuleHandler = class(TImportStatementHandler)
public
function HandleCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes;
out InsertText, DisplayText : string) : Boolean; override;
end;
function TFromImportModuleHandler.HandleCodeCompletion(const Line,
FileName: string; Caret: TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes; out InsertText,
DisplayText: string): Boolean;
Var
Dir : string;
i : integer;
ParsedModule : TParsedModule;
begin
Result := TRegExpressions.RE_FromImport.Exec(Copy(Line, 1, Caret.Char - 1));
if Result then
begin
// autocomplete from statement
fPathDepth := TRegExpressions.RE_FromImport.MatchLen[1];
if TRegExpressions.RE_FromImport.MatchLen[2] > 0 then
begin
// from ...module import identifiers
fFileName := FileName;
if PyScripterRefactor.InitializeQuery then
begin
ParsedModule := PyScripterRefactor.ResolveModuleImport
(TRegExpressions.RE_FromImport.Match[2], fFileName, fPathDepth);
if Assigned(ParsedModule) then
begin
if (fPathDepth > 0) and ParsedModule.IsPackage then
begin
Dir := ExtractFileDir(ParsedModule.FileName);
if Dir <> '' then
GetModuleList(VarPythonCreate([Dir]), InsertText, DisplayText);
end
else
// will be handled by FromImportNamespace
Result := False
end;
PyScripterRefactor.FinalizeQuery;
end;
end
else
begin
// from ... import modules
fModulePrefix := '';
fFileName := FileName;
if fPathDepth > 0 then
begin
if FileName = '' then begin
Result := True; // No completion
Exit;
end;
Dir := fFileName;
for i := 1 to fPathDepth do
Dir := ExtractFileDir(Dir);
GetModuleList(VarPythonCreate([Dir]), InsertText, DisplayText);
end;
end;
end
end;
{ TNamespaceCompletionHandler }
type
TNamespaceCompletionHandler = class(TBaseCodeCompletionHandler)
protected
fFileName : string;
fSortedNameSpace : TStringList;
public
constructor Create;
destructor Destroy; override;
procedure Finalize; override;
function HandleCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes;
out InsertText, DisplayText : string) : Boolean; override;
function GetInfo(CCItem: string) : string; override;
end;
constructor TNamespaceCompletionHandler.Create;
begin
fSortedNameSpace := TStringList.Create;
fSortedNameSpace.CaseSensitive := True;
fSortedNameSpace.Duplicates := dupIgnore; // Remove duplicates
end;
destructor TNamespaceCompletionHandler.Destroy;
begin
fSortedNameSpace.Free;
inherited;
end;
procedure TNamespaceCompletionHandler.Finalize;
begin
inherited;
fFileName := '';
fSortedNameSpace.Clear;
PyScripterRefactor.FinalizeQuery;
end;
function TNamespaceCompletionHandler.GetInfo(CCItem: string): string;
Var
Index : integer;
CE : TBaseCodeElement;
begin
Index := fSortedNameSpace.IndexOf(CCItem);
if Index >=0 then
begin
CE := fSortedNameSpace.Objects[Index] as TBaseCodeElement;
if not Assigned(CE) then
Result := _(SPythonKeyword)
else if Assigned(CE) and (CE is TCodeElement) then
Result := GetLineRange(TCodeElement(CE).DocString, 1, 20);
end;
end;
function TNamespaceCompletionHandler.HandleCodeCompletion(const Line,
FileName: string; Caret : TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes; out InsertText,
DisplayText: string): Boolean;
Var
TmpX, Index : Integer;
lookup, ErrMsg: string;
ParsedModule, ParsedBuiltInModule: TParsedModule;
Scope: TCodeElement;
Def: TBaseCodeElement;
NameSpace, KeywordList: TStringList;
Keywords: Variant;
PythonPathAdder : IInterface;
begin
fFileName := FileName;
InsertText := '';
DisplayText := '';
TmpX := Caret.Char;
if TmpX > Length(Line) then
TmpX := Length(Line)
else
Dec(TmpX);
lookup := GetWordAtPos(Line, TmpX, IdentChars + ['.'], True, False);
Index := CharLastPos(lookup, WideChar('.'));
// Add the file path to the Python path - Will be automatically removed
PythonPathAdder := InternalInterpreter.AddPathToPythonPath
(ExtractFileDir(fFileName));
if PyScripterRefactor.InitializeQuery then
begin
// GetParsedModule
ParsedModule := PyScripterRefactor.GetParsedModule(fFileName, None);
Scope := nil;
if Assigned(ParsedModule) then
Scope := ParsedModule.GetScopeForLine(Caret.Line);
if Assigned(ParsedModule) and Assigned(Scope) then
begin
NameSpace := TStringList.Create;
try
if Index > 0 then
begin
lookup := Copy(lookup, 1, Index - 1);
Def := PyScripterRefactor.FindDottedDefinition(lookup,
ParsedModule, Scope, ErrMsg);
if Assigned(Def) and (Def.ClassType = TVariable) then
Def := PyScripterRefactor.GetVarType(TVariable(Def), ErrMsg);
if Assigned(Def) then (Def as TCodeElement).GetNamespace(NameSpace);
end else begin
// extract namespace from current scope and its parents
while Assigned(Scope) do
begin
Scope.GetNamespace(NameSpace);
Scope := Scope.Parent as TCodeElement;
end;
// builtins (could add keywords as well)
ParsedBuiltInModule := PyScripterRefactor.GetParsedModule(GetPythonEngine.BuiltInModuleName, None);
ParsedBuiltInModule.GetNamespace(NameSpace);
if CommandsDataModule.PyIDEOptions.CompleteKeywords then
begin
Keywords := Import('keyword').kwlist;
Keywords := BuiltinModule.tuple(Keywords);
KeywordList := TStringList.Create;
try
GetPythonEngine.PyTupleToStrings(ExtractPythonObjectFrom(Keywords), KeywordList);
NameSpace.AddStrings(KeywordList);
finally
KeywordList.Free;
end;
end
end;
ProcessNamespace(fSortedNameSpace, NameSpace, InsertText, DisplayText);
finally
NameSpace.Free;
end;
end;
end;
Result := InsertText <> '';
end;
{ TStringCompletionHandler }
type
TStringCompletionHandler = class(TNamespaceCompletionHandler)
function HandleCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes;
out InsertText, DisplayText : string) : Boolean; override;
end;
function TStringCompletionHandler.HandleCodeCompletion(const Line,
FileName: string; Caret: TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes; out InsertText,
DisplayText: string): Boolean;
Var
ParsedBuiltInModule: TParsedModule;
NameSpace: TStringList;
Index : integer;
CE : TCodeElement;
begin
Result := TRegExpressions.RE_String.Exec(Copy(Line, 1, Caret.Char - 1));
if Result then begin
if PyScripterRefactor.InitializeQuery then
begin
// GetParsedModule
ParsedBuiltInModule := PyScripterRefactor.GetParsedModule(GetPythonEngine.BuiltInModuleName, None);
if Assigned(ParsedBuiltInModule) then begin
NameSpace := TStringList.Create;
try
ParsedBuiltInModule.GetNameSpace(NameSpace);
Index := NameSpace.IndexOf('str');
if Index > 0 then
begin
CE := NameSpace.Objects[Index] as TCodeElement;
NameSpace.Clear;
CE.GetNameSpace(NameSpace);
ProcessNamespace(fSortedNameSpace, NameSpace, InsertText, DisplayText);
end;
finally
NameSpace.Free;
end;
end;
end;
end;
end;
{ TFromImportNamespaceHandler }
type
TFromImportNamespaceHandler = class(TNamespaceCompletionHandler)
function HandleCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes;
out InsertText, DisplayText : string) : Boolean; override;
end;
function TFromImportNamespaceHandler.HandleCodeCompletion(const Line,
FileName: string; Caret: TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes; out InsertText,
DisplayText: string): Boolean;
Var
ParsedModule : TParsedModule;
NameSpace : TStringList;
fPathDepth : integer;
begin
Result := TRegExpressions.RE_FromImport.Exec(Copy(Line, 1, Caret.Char - 1));
if Result then
begin
// autocomplete from statement
fPathDepth := TRegExpressions.RE_FromImport.MatchLen[1];
if (fPathDepth > 0) and (FileName = '') then begin
Result := True; // No Completion
Exit;
end;
if TRegExpressions.RE_FromImport.MatchLen[2] > 0 then
begin
// from ...module import identifiers
fFileName := FileName;
if PyScripterRefactor.InitializeQuery then
begin
ParsedModule := PyScripterRefactor.ResolveModuleImport
(TRegExpressions.RE_FromImport.Match[2], fFileName, fPathDepth);
if Assigned(ParsedModule) then
begin
if (fPathDepth > 0) and ParsedModule.IsPackage then
// will be handled by FromImportModules
Result := False
else
begin
NameSpace := TStringList.Create;
try
ParsedModule.GetNamespace(NameSpace);
ProcessNamespace(fSortedNameSpace, NameSpace, InsertText, DisplayText);
finally
NameSpace.Free;
end;
end;
end;
end;
end
else
begin
Result := False;
end;
end
end;
{ TLiveNamespaceCompletionHandler }
type
TLiveNamespaceCompletionHandler = class(TBaseCodeCompletionHandler)
private
fNameSpaceDict : TBaseNameSpaceItem;
fNameSpace : TStringList;
public
procedure Finalize; override;
function HandleCodeCompletion(const Line, FileName : string; Caret : TBufferCoord;
Highlighter : TSynCustomHighlighter; HighlighterAttr : TSynHighlighterAttributes;
out InsertText, DisplayText : string) : Boolean; override;
function GetInfo(CCItem: string) : string; override;
end;
procedure TLiveNamespaceCompletionHandler.Finalize;
begin
FreeAndNil(fNameSpace);
FreeAndNil(fNameSpaceDict);
end;
function TLiveNamespaceCompletionHandler.GetInfo(CCItem: string): string;
Var
Index: integer;
NameSpaceItem: TBaseNameSpaceItem;
begin
Index := fNameSpace.IndexOf(CCItem);
if Index >=0 then
begin
NameSpaceItem := fNameSpace.Objects[Index] as TBaseNameSpaceItem;
if not Assigned(NameSpaceItem) then
Result := _(SPythonKeyword)
else
Result := GetLineRange(NameSpaceItem.DocString, 1, 20);
end;
end;
function TLiveNamespaceCompletionHandler.HandleCodeCompletion(const Line,
FileName: string; Caret: TBufferCoord; Highlighter: TSynCustomHighlighter;
HighlighterAttr: TSynHighlighterAttributes; out InsertText,
DisplayText: string): Boolean;
Var
i, TmpX, Index, ImageIndex : integer;
lookup : string;
NameSpaceItem : TBaseNameSpaceItem;
KeywordList : TStringList;
Keywords : Variant;
begin
// Clean-up of FNameSpace and FNameSpaceDict takes place in the Close event
TmpX := Caret.Char;
if TmpX > length(Line) then
TmpX := length(Line)
else dec(TmpX);
lookup := GetWordAtPos(Line, TmpX, IdentChars+['.'], True, False, True);
Index := CharLastPos(lookup, '.');
fNameSpaceDict := nil;
if Index > 0 then begin
lookup := Copy(lookup, 1, Index-1);
if CharPos(lookup, ')') > 0 then
lookup := '' // Issue 422 Do not evaluate functions
else if IsDigits(lookup) then
lookup := '' // Issue 478 User is typing a number
end else
lookup := ''; // Completion from global namespace
if (Index <= 0) or (lookup <> '') then begin
if PyControl.DebuggerState = dsInactive then
fNameSpaceDict := PyControl.ActiveInterpreter.NameSpaceFromExpression(lookup)
else
fNameSpaceDict := PyControl.ActiveDebugger.NameSpaceFromExpression(lookup);
end;
DisplayText := '';
InsertText := '';
fNameSpace := TStringList.Create;
if Assigned(fNameSpaceDict) then begin
for i := 0 to fNameSpaceDict.ChildCount - 1 do begin
NameSpaceItem := fNameSpaceDict.ChildNode[i];
fNameSpace.AddObject(NameSpaceItem.Name, NameSpaceItem);
end;
end;
if (Index <= 0) and CommandsDataModule.PyIDEOptions.CompleteKeywords then begin
Keywords := Import('keyword').kwlist;
Keywords := BuiltinModule.tuple(Keywords);
KeywordList := TStringList.Create;
try
GetPythonEngine.PyTupleToStrings(ExtractPythonObjectFrom(Keywords), KeywordList);
fNameSpace.AddStrings(KeywordList);
finally
KeywordList.Free;
end;
end;
fNameSpace.CustomSort(ComparePythonIdents);
for i := 0 to fNameSpace.Count - 1 do begin
InsertText := InsertText + fNameSpace[i];
NameSpaceItem := fNameSpace.Objects[i] as TBaseNameSpaceItem;
if not Assigned(NameSpaceItem) then
DisplayText := DisplayText + Format('\Image{%d}\hspace{2}\color{clHotlight}%s',
[Integer(TCodeImages.Keyword), fNameSpace[i]])
else
begin
if NameSpaceItem.IsModule then
ImageIndex := Integer(TCodeImages.Module)
else if NameSpaceItem.IsMethod
{or NameSpaceItem.IsMethodDescriptor} then
ImageIndex := Integer(TCodeImages.Method)
else if NameSpaceItem.IsFunction
{or NameSpaceItem.IsBuiltin} then
ImageIndex := Integer(TCodeImages.Func)
else if NameSpaceItem.IsClass then
ImageIndex := Integer(TCodeImages.Klass)
else begin
if Index > 0 then
ImageIndex := Integer(TCodeImages.Field)
else
ImageIndex := Integer(TCodeImages.Variable);
end;
DisplayText := DisplayText + Format('\Image{%d}\hspace{2}%s', [ImageIndex, NameSpaceItem.Name]);
end;
if i < fNameSpace.Count - 1 then begin
DisplayText := DisplayText + #10;
InsertText := InsertText + #10;
end;
end;
Result := InsertText <> '';
end;
{ TCodeCompletion }
constructor TCodeCompletion.Create;
begin
SkipHandlers := TObjectList.Create(True);
CompletionHandlers := TObjectList.Create(True);
end;
destructor TCodeCompletion.Destroy;
begin
SkipHandlers.Free;
CompletionHandlers.Free;
end;
procedure TCodeCompletion.RegisterCompletionHandler(Handler : TBaseCodeCompletionHandler);
begin
CompletionHandlers.Add(Handler);
end;
procedure TCodeCompletion.RegisterSkipHandler(Handler : TBaseCodeCompletionSkipHandler);
begin
SkipHandlers.Add(Handler);
end;
{ TIDECompletion }
class constructor TIDECompletion.Create;
begin
EditorCodeCompletion := TCodeCompletion.Create;
InterpreterCodeCompletion := TCodeCompletion.Create;
// Register handlers - Order may be important
EditorCodeCompletion.RegisterSkipHandler(TStringAndCommentSkipHandler.Create);
EditorCodeCompletion.RegisterSkipHandler(TForStatementSkipHandler.Create);
EditorCodeCompletion.RegisterSkipHandler(TImportAsStatementSkipHandler.Create);
EditorCodeCompletion.RegisterCompletionHandler(TImportStatementHandler.Create);
EditorCodeCompletion.RegisterCompletionHandler(TFromStatementHandler.Create);
EditorCodeCompletion.RegisterCompletionHandler(TFromImportModuleHandler.Create);
EditorCodeCompletion.RegisterCompletionHandler(TFromImportNamespaceHandler.Create);
EditorCodeCompletion.RegisterCompletionHandler(TStringCompletionHandler.Create);
EditorCodeCompletion.RegisterCompletionHandler(TNamespaceCompletionHandler.Create);
InterpreterCodeCompletion.RegisterSkipHandler(TStringAndCommentSkipHandler.Create);
InterpreterCodeCompletion.RegisterSkipHandler(TForStatementSkipHandler.Create);
InterpreterCodeCompletion.RegisterSkipHandler(TImportAsStatementSkipHandler.Create);
InterpreterCodeCompletion.RegisterCompletionHandler(TImportStatementHandler.Create);
InterpreterCodeCompletion.RegisterCompletionHandler(TFromStatementHandler.Create);
InterpreterCodeCompletion.RegisterCompletionHandler(TFromImportModuleHandler.Create);
InterpreterCodeCompletion.RegisterCompletionHandler(TFromImportNamespaceHandler.Create);
InterpreterCodeCompletion.RegisterCompletionHandler(TStringCompletionHandler.Create);
InterpreterCodeCompletion.RegisterCompletionHandler(TLiveNamespaceCompletionHandler.Create);
end;
class destructor TIDECompletion.Destroy;
begin
EditorCodeCompletion.Free;
InterpreterCodeCompletion.Free;
end;
{ TBaseCodeCompletionHandler }
procedure TBaseCodeCompletionHandler.Finalize;
begin
end;
procedure TBaseCodeCompletionHandler.Initialize;
begin
end;
end.