-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBricxccAutoComplete.pas
456 lines (423 loc) · 13.2 KB
/
BricxccAutoComplete.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
(*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Portions created by John Hansen are Copyright (C) 2009-2013 John Hansen.
* All Rights Reserved.
*
*)
unit BricxccAutoComplete;
{$I SynEdit.inc}
interface
uses
Classes, SynEdit, SynEditKeyCmds,
{$IFDEF SYN_KYLIX}
Qt, QMenus, Types;
{$ELSE}
Windows, Menus;
{$ENDIF}
type
TCustomSynAutoComplete = class(TComponent)
protected
fAutoCompleteList: TStrings;
fCompletions: TStrings;
fCompletionComments: TStrings;
fCompletionValues: TStrings;
fEditor: TCustomSynEdit;
fEditors: TList;
fEOTokenChars: string;
fCaseSensitive: boolean;
fParsed: boolean;
procedure CompletionListChanged(Sender: TObject);
function GetCompletions: TStrings;
function GetCompletionComments: TStrings;
function GetCompletionValues: TStrings;
function GetEditorCount: integer;
function GetNthEditor(Index: integer): TCustomSynEdit;
procedure ParseCompletionList; virtual;
procedure SetAutoCompleteList(Value: TStrings); virtual;
procedure SetEditor(Value: TCustomSynEdit);
procedure SynEditCommandHandler(Sender: TObject; AfterProcessing: boolean;
var Handled: boolean; var Command: TSynEditorCommand; var AChar: char;
Data: pointer; HandlerData: pointer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AddCompletion(AToken, AValue, AComment: string);
function AddEditor(AEditor: TCustomSynEdit): boolean;
procedure Execute(AEditor: TCustomSynEdit); virtual;
procedure ExecuteCompletion(AToken: string; AEditor: TCustomSynEdit);
virtual;
procedure Loaded; override;
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
function RemoveEditor(AEditor: TCustomSynEdit): boolean;
public
property AutoCompleteList: TStrings read fAutoCompleteList
write SetAutoCompleteList;
property CaseSensitive: boolean read fCaseSensitive write fCaseSensitive;
property Completions: TStrings read GetCompletions;
property CompletionComments: TStrings read GetCompletionComments;
property CompletionValues: TStrings read GetCompletionValues;
property Editor: TCustomSynEdit read fEditor write SetEditor;
property EditorCount: integer read GetEditorCount;
property Editors[Index: integer]: TCustomSynEdit read GetNthEditor;
property EndOfTokenChr: string read fEOTokenChars write fEOTokenChars;
end;
TSynEditAutoComplete = class(TCustomSynAutoComplete)
published
property AutoCompleteList;
property CaseSensitive;
property Editor;
property EndOfTokenChr;
end;
implementation
uses
SysUtils, SynEditTypes;
{ TCustomSynAutoComplete }
procedure TCustomSynAutoComplete.AddCompletion(AToken, AValue, AComment: string);
begin
if AToken <> '' then begin
fCompletions.Add(AToken);
fCompletionComments.Add(AComment);
fCompletionValues.Add(AValue);
end;
end;
function TCustomSynAutoComplete.AddEditor(AEditor: TCustomSynEdit): boolean;
var
i: integer;
begin
if AEditor <> nil then begin
i := fEditors.IndexOf(AEditor);
if i = -1 then begin
AEditor.FreeNotification(Self);
fEditors.Add(AEditor);
if ComponentState * [csDesigning, csLoading] = [] then
AEditor.RegisterCommandHandler(SynEditCommandHandler, nil);
end;
Result := TRUE;
end else
Result := FALSE;
end;
procedure TCustomSynAutoComplete.CompletionListChanged(Sender: TObject);
begin
fParsed := FALSE;
end;
constructor TCustomSynAutoComplete.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fAutoCompleteList := TStringList.Create;
TStringList(fAutoCompleteList).OnChange := CompletionListChanged;
fCompletions := TStringList.Create;
fCompletionComments := TStringList.Create;
fCompletionValues := TStringList.Create;
fEditors := TList.Create;
fEOTokenChars := '()[]{}.';
end;
destructor TCustomSynAutoComplete.Destroy;
begin
fEditors.Free;
fCompletions.Free;
fCompletionComments.Free;
fCompletionValues.Free;
fAutoCompleteList.Free;
inherited Destroy;
end;
procedure TCustomSynAutoComplete.Execute(AEditor: TCustomSynEdit);
var
s: string;
i, j: integer;
begin
if AEditor <> nil then begin
// get token
s := AEditor.LineText;
j := AEditor.CaretX;
i := j - 1;
if i <= Length(s) then begin
while (i > 0) and (s[i] > ' ') and (Pos(s[i], fEOTokenChars) = 0) do
Dec(i);
Inc(i);
s := Copy(s, i, j - i);
ExecuteCompletion(s, AEditor);
end;
end;
end;
procedure TCustomSynAutoComplete.ExecuteCompletion(AToken: string;
AEditor: TCustomSynEdit);
var
i, j, Len, IndentLen: integer;
s: string;
IdxMaybe, NumMaybe: integer;
p: TBufferCoord;
NewCaretPos: boolean;
Temp: TStringList;
begin
if not fParsed then
ParseCompletionList;
Len := Length(AToken);
if (Len > 0) and (AEditor <> nil) and not AEditor.ReadOnly
and (fCompletions.Count > 0)
then begin
// find completion for this token - not all chars necessary if unambiguous
i := fCompletions.Count - 1;
IdxMaybe := -1;
NumMaybe := 0;
if fCaseSensitive then begin
while i > -1 do begin
s := fCompletions[i];
if AnsiCompareStr(s, AToken) = 0 then
break
else if AnsiCompareStr(Copy(s, 1, Len), AToken) = 0 then begin
Inc(NumMaybe);
IdxMaybe := i;
end;
Dec(i);
end;
end else begin
while i > -1 do begin
s := fCompletions[i];
if AnsiCompareText(s, AToken) = 0 then
break
else if AnsiCompareText(Copy(s, 1, Len), AToken) = 0 then begin
Inc(NumMaybe);
IdxMaybe := i;
end;
Dec(i);
end;
end;
if (i = -1) and (NumMaybe = 1) then
i := IdxMaybe;
if i > -1 then begin
// select token in editor
p := AEditor.CaretXY;
{begin} //mh 2000-11-08
AEditor.BeginUpdate;
try
AEditor.BlockBegin := BufferCoord(p.Char - Len, p.Line);
AEditor.BlockEnd := p;
// indent the completion string if necessary, determine the caret pos
IndentLen := p.Char - Len - 1;
p := AEditor.BlockBegin;
NewCaretPos := FALSE;
Temp := TStringList.Create;
try
Temp.Text := fCompletionValues[i];
// indent lines
if (IndentLen > 0) and (Temp.Count > 1) then begin
s := StringOfChar(' ', IndentLen);
for i := 1 to Temp.Count - 1 do
Temp[i] := s + Temp[i];
end;
// find first '|' and use it as caret position
for i := 0 to Temp.Count - 1 do begin
s := Temp[i];
j := Pos('|', s);
if j > 0 then begin
Delete(s, j, 1);
Temp[i] := s;
NewCaretPos := TRUE;
Inc(p.Line, i);
if i = 0 then
Inc(p.Char, j - 1)
else
p.Char := j;
break;
end;
end;
s := Temp.Text;
// strip the trailing #13#10 that was appended by the stringlist
i := Length(s);
if (i >= 2) and (s[i - 1] = #13) and (s[i] = #10) then
SetLength(s, i - 2);
finally
Temp.Free;
end;
// replace the selected text and position the caret
AEditor.SelText := s;
if NewCaretPos then
AEditor.CaretXY := p;
finally
AEditor.EndUpdate;
end;
{end} //mh 2000-11-08
end;
end;
end;
function TCustomSynAutoComplete.GetCompletions: TStrings;
begin
if not fParsed then
ParseCompletionList;
Result := fCompletions;
end;
function TCustomSynAutoComplete.GetCompletionComments: TStrings;
begin
if not fParsed then
ParseCompletionList;
Result := fCompletionComments;
end;
function TCustomSynAutoComplete.GetCompletionValues: TStrings;
begin
if not fParsed then
ParseCompletionList;
Result := fCompletionValues;
end;
function TCustomSynAutoComplete.GetEditorCount: integer;
begin
Result := fEditors.Count;
end;
function TCustomSynAutoComplete.GetNthEditor(Index: integer): TCustomSynEdit;
begin
if (Index >= 0) and (Index < fEditors.Count) then
Result := fEditors[Index]
else
Result := nil;
end;
procedure TCustomSynAutoComplete.Loaded;
var
i: integer;
O: TObject;
begin
inherited Loaded;
for i := 0 to fEditors.Count - 1 do begin
O := TObject(fEditors[i]);
(O as TCustomSynEdit).RegisterCommandHandler(SynEditCommandHandler, nil);
end;
end;
procedure TCustomSynAutoComplete.Notification(AComponent: TComponent;
Operation: TOperation);
var
i: integer;
begin
inherited Notification(AComponent, Operation);
if Operation = opRemove then
begin
i := fEditors.IndexOf(AComponent);
if i > -1 then
RemoveEditor(AComponent as TCustomSynEdit);
end;
end;
procedure TCustomSynAutoComplete.ParseCompletionList;
var
BorlandDCI: boolean;
i, j, Len: integer;
s, sCompl, sComment, sComplValue: string;
procedure SaveEntry;
begin
fCompletions.Add(sCompl);
sCompl := '';
fCompletionComments.Add(sComment);
sComment := '';
fCompletionValues.Add(sComplValue);
sComplValue := '';
end;
begin
fCompletions.Clear;
fCompletionComments.Clear;
fCompletionValues.Clear;
if fAutoCompleteList.Count > 0 then begin
s := fAutoCompleteList[0];
BorlandDCI := (s <> '') and (s[1] = '[');
sCompl := '';
sComment := '';
sComplValue := '';
for i := 0 to fAutoCompleteList.Count - 1 do begin
s := fAutoCompleteList[i];
Len := Length(s);
if BorlandDCI then begin
// the style of the Delphi32.dci file
if (Len > 0) and (s[1] = '[') then begin
// save last entry
if sCompl <> '' then
SaveEntry;
// new completion entry
j := 2;
while (j <= Len) and (s[j] > ' ') do
Inc(j);
sCompl := Copy(s, 2, j - 2);
// start of comment in DCI file
while (j <= Len) and (s[j] <= ' ') do
Inc(j);
if (j <= Len) and (s[j] = '|') then
Inc(j);
while (j <= Len) and (s[j] <= ' ') do
Inc(j);
sComment := Copy(s, j, Len);
if sComment[Length(sComment)] = ']' then
SetLength(sComment, Length(sComment) - 1);
end else begin
if sComplValue <> '' then
sComplValue := sComplValue + #13#10;
sComplValue := sComplValue + s;
end;
end else begin
// the original style
if (Len > 0) and (s[1] <> '=') then begin
// save last entry
if sCompl <> '' then
SaveEntry;
// new completion entry
sCompl := s;
end else if (Len > 0) and (s[1] = '=') then begin
if sComplValue <> '' then
sComplValue := sComplValue + #13#10;
sComplValue := sComplValue + Copy(s, 2, Len);
end;
end;
end;
if sCompl <> '' then //mg 2000-11-07
SaveEntry;
end;
end;
function TCustomSynAutoComplete.RemoveEditor(AEditor: TCustomSynEdit): boolean;
var
i: integer;
begin
if AEditor <> nil then
begin
i := fEditors.IndexOf(AEditor);
if (i > -1) then
begin
if fEditor = AEditor then
fEditor := nil;
fEditors.Delete(i);
if ComponentState * [csDesigning, csLoading] = [] then
AEditor.UnregisterCommandHandler(SynEditCommandHandler);
end;
end;
Result := False;
end;
procedure TCustomSynAutoComplete.SetAutoCompleteList(Value: TStrings);
begin
fAutoCompleteList.Assign(Value);
fParsed := FALSE;
end;
procedure TCustomSynAutoComplete.SetEditor(Value: TCustomSynEdit);
begin
if Value <> fEditor then
begin
if fEditor <> nil then
RemoveEditor(fEditor);
fEditor := nil;
if (Value <> nil) and AddEditor(Value) then
fEditor := Value;
end;
end;
procedure TCustomSynAutoComplete.SynEditCommandHandler(Sender: TObject;
AfterProcessing: boolean; var Handled: boolean;
var Command: TSynEditorCommand; var AChar: char; Data: pointer;
HandlerData: pointer);
begin
if not AfterProcessing and not Handled and (Command = ecAutoCompletion) then
begin
Handled := TRUE;
Execute(Sender as TCustomSynEdit);
end;
end;
end.