Menu

[327ada]: / Source / VCL / ClassBrowsing / CodeCompletion.pas  Maximize  Restore  History

Download this file

428 lines (365 with data), 14.1 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
{
This file is part of Dev-C++
Copyright (c) 2004 Bloodshed Software
Dev-C++ is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Dev-C++ 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dev-C++; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit CodeCompletion;
interface
uses
{$IFDEF WIN32}
Windows, Classes, Forms, SysUtils, Controls, Graphics, CppParser,
cbutils, IntList, StatementList;
{$ENDIF}
{$IFDEF LINUX}
Xlib, Classes, QForms, SysUtils, QControls, QGraphics, CppParser,
U_IntList, QDialogs, Types;
{$ENDIF}
type
TCodeCompletion = class(TComponent)
private
fParser: TCppParser;
fFullCompletionStatementList: TList;
fCompletionStatementList: TList;
fMinWidth: integer;
fMinHeight: integer;
fMaxWidth: integer;
fMaxHeight: integer;
fPos: TPoint;
fColor: TColor;
fWidth: integer;
fHeight: integer;
fEnabled: boolean;
fShowCount: integer;
fOnKeyPress: TKeyPressEvent;
fOnResize: TNotifyEvent;
fOnlyGlobals: boolean;
fCurrentStatement: PStatement;
fHideDelay: integer; // allow empty list once, then hide when empty again
fIncludedFiles: TStringList;
fIsIncludedCacheFileName: String;
fIsIncludedCacheResult: boolean;
function ApplyClassFilter(Statement, CurrentClass: PStatement; InheritanceStatements: TList): boolean;
function ApplyMemberFilter(Statement, CurrentClass, ParentClass: PStatement; InheritanceStatements: TList): boolean;
procedure GetCompletionFor(Phrase: String);
procedure FilterList(const Member: String);
procedure SetPosition(Value: TPoint);
procedure OnFormResize(Sender: TObject);
function IsIncluded(const FileName: String): boolean;
function IsVisible: boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Search(const Phrase, Filename: String);
procedure Hide;
function SelectedStatement: PStatement;
property CurrentStatement: PStatement read fCurrentStatement write fCurrentStatement;
published
property ShowCount: integer read fShowCount write fShowCount;
property Parser: TCppParser read fParser write fParser;
property Position: TPoint read fPos write SetPosition;
property Color: TColor read fColor write fColor;
property Width: integer read fWidth write fWidth;
property Height: integer read fHeight write fHeight;
property Enabled: boolean read fEnabled write fEnabled;
property MinWidth: integer read fMinWidth write fMinWidth;
property MinHeight: integer read fMinHeight write fMinHeight;
property MaxWidth: integer read fMaxWidth write fMaxWidth;
property MaxHeight: integer read fMaxHeight write fMaxHeight;
property OnKeyPress: TKeyPressEvent read fOnKeyPress write fOnKeyPress;
property OnResize: TNotifyEvent read fOnResize write fOnResize;
property OnlyGlobals: boolean read fOnlyGlobals write fOnlyGlobals;
property Visible: boolean read IsVisible;
end;
procedure Register;
implementation
uses
CodeCompletionForm, Math;
{ TCodeCompletion }
procedure Register;
begin
RegisterComponents('Dev-C++', [TCodeCompletion]);
end;
constructor TCodeCompletion.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fIncludedFiles := TStringList.Create;
fIncludedFiles.Sorted := True;
fIncludedFiles.Duplicates := dupIgnore;
fCompletionStatementList := TList.Create;
fFullCompletionStatementList := TList.Create;
CodeComplForm := TCodeComplForm.Create(Self);
CodeComplForm.OnResize := OnFormResize;
fWidth := 320;
fHeight := 240;
fColor := clWindow;
fEnabled := True;
fOnlyGlobals := False;
fShowCount := 100; // keep things fast
fHideDelay := 0;
fIsIncludedCacheFileName := '';
fIsIncludedCacheResult := false;
end;
destructor TCodeCompletion.Destroy;
begin
FreeAndNil(CodeComplForm);
FreeAndNil(fCompletionStatementList);
FreeAndNil(fFullCompletionStatementList);
FreeAndNil(fIncludedFiles);
inherited Destroy;
end;
function TCodeCompletion.ApplyClassFilter(Statement, CurrentClass: PStatement; InheritanceStatements: TList): boolean;
begin
Result :=
(
(Statement^._Scope in [ssLocal, ssGlobal]) or // local or global var or
(
(Statement^._Scope = ssClassLocal) and // class var
(
(Statement^._Parent = CurrentClass) or // from current class
(
(InheritanceStatements.IndexOf(Statement^._Parent) <> -1) and
(Statement^._ClassScope <> scsPrivate)
) // or an inheriting class
)
)
) and
(IsIncluded(Statement^._FileName) or
IsIncluded(Statement^._DefinitionFileName));
end;
function TCodeCompletion.ApplyMemberFilter(Statement, CurrentClass, ParentClass: PStatement; InheritanceStatements:
TList): boolean;
var
cs: set of TStatementClassScope;
begin
Result := Statement^._Parent <> nil; // only members
if not Result then
Exit;
// all members of current class
Result := Result and ((ParentClass = CurrentClass) and (Statement^._Parent = ParentClass));
// all public and published members of var's class
Result := Result or
(
(ParentClass = Statement^._Parent) and
(not (Statement^._ClassScope in [scsProtected, scsPrivate]))
// or member of an inherited class
);
if (CurrentClass = nil) or (Statement^._Parent = CurrentClass) then
cs := [scsPrivate, scsProtected]
else
cs := [scsPrivate];
// all inherited class's non-private members
Result := Result or
(
(InheritanceStatements.IndexOf(Statement^._Parent) <> -1) and
(not (Statement^._ClassScope in cs)) // or member of an inherited class
);
end;
procedure TCodeCompletion.GetCompletionFor(Phrase: String);
var
Node: PStatementNode;
Statement: PStatement;
I: integer;
InheritanceStatements: TList;
ParentStatement, ParentTypeStatement, CurrentStatementParent: PStatement;
begin
// Reset filter cache
fIsIncludedCacheFileName := '';
fIsIncludedCacheResult := false;
// Pulling off the same trick as in TCppParser.FindStatementOf, but ignore everything after last operator
InheritanceStatements := TList.Create;
try
I := fParser.FindLastOperator(Phrase);
if I = 0 then begin
// only add globals and members of the current class
// Also consider classes the current class inherits from
fParser.GetMultipleInheritanceStatements(fCurrentStatement, InheritanceStatements);
Node := fParser.Statements.FirstNode;
while Assigned(Node) do begin
Statement := Node^.Data;
if ApplyClassFilter(Statement, fCurrentStatement, InheritanceStatements) then
fFullCompletionStatementList.Add(Statement);
Node := Node^.NextNode;
end;
end else begin
// Get parent of current statement
if Assigned(fCurrentStatement) then
CurrentStatementParent := fCurrentStatement^._Parent
else
CurrentStatementParent := nil;
// Find last operator
Delete(Phrase, I, MaxInt);
// Add statements of all the text before the last operator
ParentStatement := fParser.FindStatementOf(Phrase, fCurrentStatement);
if not Assigned(ParentStatement) then
Exit;
// Then determine which type it has (so we can use it as a parent)
if ParentStatement^._Kind = skClass then // already found type
ParentTypeStatement := ParentStatement
else
ParentTypeStatement := fParser.FindTypeDefinitionOf(ParentStatement^._Type, CurrentStatementParent);
// TODO: should not be nil
if not Assigned(ParentTypeStatement) then
Exit;
// Then add members of the ClassIDs and InheritanceIDs
fParser.GetMultipleInheritanceStatements(ParentTypeStatement, InheritanceStatements);
Node := fParser.Statements.FirstNode;
while Assigned(Node) do begin
Statement := Node^.Data;
if ApplyMemberFilter(Statement, fCurrentStatement, ParentTypeStatement, InheritanceStatements) then
fFullCompletionStatementList.Add(Statement);
Node := Node^.NextNode;
end;
end;
finally
InheritanceStatements.Free;
end;
end;
// Return 1 to show Item2 above Item1, otherwise -1
function ListSort(Item1, Item2: Pointer): Integer;
var
Statement1, Statement2: PStatement;
begin
Statement1 := PStatement(Item1);
Statement2 := PStatement(Item2);
// Show stuff from local headers first
if (Statement1^._InSystemHeader) and (not Statement2^._InSystemHeader) then begin
Result := 1;
end else if (not Statement1^._InSystemHeader) and (Statement2^._InSystemHeader) then begin
Result := -1;
// Show local statements first
end else if (Statement1^._Scope in [ssGlobal]) and not (Statement2^._Scope in [ssGlobal]) then begin
Result := 1;
end else if not (Statement1^._Scope in [ssGlobal]) and (Statement2^._Scope in [ssGlobal]) then begin
Result := -1;
// otherwise, sort by name
end else
Result := CompareText(Statement1^._Command, Statement2^._Command);
end;
procedure TCodeCompletion.FilterList(const Member: String);
var
I: integer;
begin
fCompletionStatementList.Clear;
if Member <> '' then begin // filter, case insensitive
fCompletionStatementList.Capacity := fFullCompletionStatementList.Count;
for I := 0 to fFullCompletionStatementList.Count - 1 do
if StartsText(Member, PStatement(fFullCompletionStatementList[I])^._Command) then
fCompletionStatementList.Add(fFullCompletionStatementList[I]);
end else
fCompletionStatementList.Assign(fFullCompletionStatementList);
fCompletionStatementList.Sort(@ListSort);
end;
procedure TCodeCompletion.Hide;
begin
OnKeyPress := nil;
CodeComplForm.Hide;
// Clear data, do not free pointed memory: data is owned by CppParser
fCompletionStatementList.Clear;
fFullCompletionStatementList.Clear;
CodeComplForm.lbCompletion.Items.BeginUpdate;
CodeComplForm.lbCompletion.Items.Clear;
CodeComplForm.lbCompletion.Items.EndUpdate;
fIncludedFiles.Clear; // is recreated anyway on reshow, so save some memory when hiding
end;
procedure TCodeCompletion.Search(const Phrase, Filename: String);
var
I: integer;
begin
if fEnabled then begin
Screen.Cursor := crHourglass;
// only perform full new search if just invoked
if not CodeComplForm.Showing then begin
fParser.GetFileIncludes(Filename, fIncludedFiles);
GetCompletionFor(Phrase);
end;
// Sort here by member
I := fParser.FindLastOperator(Phrase);
while (I > 0) and (I <= Length(Phrase)) and CharInSet(Phrase[i], ['.', ':', '-', '>']) do
Inc(I);
// filter fFullCompletionStatementList to fCompletionStatementList
FilterList(Copy(Phrase, I, MaxInt));
if fCompletionStatementList.Count > 0 then begin
CodeComplForm.lbCompletion.Items.BeginUpdate;
try
CodeComplForm.lbCompletion.Items.Clear;
// Only slow one hundred statements...
for I := 0 to min(fShowCount, fCompletionStatementList.Count - 1) do
CodeComplForm.lbCompletion.Items.AddObject('', fCompletionStatementList[I]);
finally
CodeComplForm.lbCompletion.Items.EndUpdate;
end;
CodeComplForm.Show;
CodeComplForm.lbCompletion.SetFocus;
if CodeComplForm.lbCompletion.Items.Count > 0 then
CodeComplForm.lbCompletion.ItemIndex := 0;
fHideDelay := 0;
end else if fHideDelay = 0 then begin
CodeComplForm.lbCompletion.Items.Clear;
fHideDelay := 1;
end else begin
Hide;
end;
Screen.Cursor := crDefault;
end;
end;
function TCodeCompletion.SelectedStatement: PStatement;
begin
if fEnabled then begin
if (fCompletionStatementList.Count > CodeComplForm.lbCompletion.ItemIndex) and (CodeComplForm.lbCompletion.ItemIndex
<> -1) then
Result := PStatement(fCompletionStatementList[CodeComplForm.lbCompletion.ItemIndex])
else begin
if fCompletionStatementList.Count > 0 then
Result := PStatement(fCompletionStatementList[0])
else
Result := nil;
end;
end else
Result := nil;
end;
procedure TCodeCompletion.SetPosition(Value: TPoint);
begin
fPos := Value;
if fPos.X + fWidth > Screen.Width then
CodeComplForm.Left := fPos.X - fWidth
else
CodeComplForm.Left := fPos.X;
if fPos.Y + fHeight > Screen.Height then
CodeComplForm.Top := fPos.Y - fHeight - 16
else
CodeComplForm.Top := fPos.Y;
end;
procedure TCodeCompletion.OnFormResize(Sender: TObject);
begin
if Enabled then begin
fWidth := CodeComplForm.Width;
fHeight := CodeComplForm.Height;
if Assigned(fOnResize) then
fOnResize(Self);
end;
end;
function TCodeCompletion.IsIncluded(const FileName: String): boolean;
begin
// Only do the slow check if the cache is invalid
if not SameStr(FileName, fIsIncludedCacheFileName) then begin
fIsIncludedCacheFileName := FileName;
fIsIncludedCacheResult := FastIndexOf(fIncludedFiles, FileName) <> -1;
end;
// Cache has been updated. Use it.
Result := fIsIncludedCacheResult;
end;
function TCodeCompletion.IsVisible: boolean;
begin
Result := fEnabled and CodeComplForm.Visible;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.