MG: accelerated the wordcompletion

git-svn-id: trunk@306 -
This commit is contained in:
lazarus 2001-07-01 13:18:58 +00:00
parent 02ecb0641f
commit 1d99470ec3
3 changed files with 66 additions and 33 deletions

View File

@ -201,6 +201,7 @@ type
function GetFEditor: TCustomSynEdit;
function GetEditor(i: integer): TCustomSynEdit;
protected
procedure OnFormPaint(Sender: TObject);
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
procedure SetShortCut(Value: TShortCut);
@ -314,7 +315,6 @@ procedure TSynBaseCompletionForm.KeyDown(var Key: Word;
var
i: integer;
begin
// Writeln('[TSynBaseCompletionForm] KeyDown ',Key);
case Key of
// added the VK_XXX codes to make it more readable / maintainable
VK_RETURN:
@ -344,8 +344,8 @@ begin
SelectNext;
VK_BACK:
if (Shift = []) and (Length(CurrentString) > 0) then begin
CurrentString := Copy(CurrentString, 1, Length(CurrentString) - 1);
if Assigned(OnKeyDelete) then OnKeyDelete(Self);
CurrentString := Copy(CurrentString, 1, Length(CurrentString) - 1);
end;
end;
Paint;
@ -422,11 +422,10 @@ begin
end
else
Begin
Canvas.Brush.Color := Color;
Canvas.Font.Color := clBlack;
Canvas.Brush.Color := Color;
Canvas.Font.Color := clBlack;
end;
if not Assigned(OnPaintItem)
or not OnPaintItem(ItemList[Scroll.Position + i], Canvas, 0, FFontHeight * i)
then
@ -721,9 +720,11 @@ end;
procedure TSynBaseCompletion.SetWidth(Value: Integer);
begin
writeln('TSynBaseCompletion.SetWidth START ',Value);
FWidth := Value;
Form.Width := FWidth;
Form.SetNbLinesInWindow(Form.FNbLinesInWindow);
writeln('TSynBaseCompletion.SetWidth END ',Value);
end;
procedure TSynBaseCompletion.Deactivate;
@ -804,7 +805,7 @@ type
end;
PRecordUsedToStoreEachEditorVars = ^TRecordUsedToStoreEachEditorVars;
procedure TSynCompletion.backspace(Senter: TObject);
procedure TSynCompletion.Backspace(Senter: TObject);
var
F: TSynBaseCompletionForm;
begin
@ -815,6 +816,13 @@ begin
end;
end;
procedure TSynCompletion.OnFormPaint(Sender: TObject);
begin
//writeln('TSynCompletion.OnFormShow A');
//LCLLinux.ShowCaret(Editor.Handle);
//writeln('TSynCompletion.OnFormShow END');
end;
procedure TSynCompletion.Cancel(Senter: TObject);
var
F: TSynBaseCompletionForm;
@ -888,6 +896,7 @@ begin
Form.OnKeyDelete := {$IFDEF FPC}@{$ENDIF}backspace;
Form.OnValidate := {$IFDEF FPC}@{$ENDIF}validate;
Form.OnCancel := {$IFDEF FPC}@{$ENDIF}Cancel;
Form.OnPaint:=@OnFormPaint;
FEndOfTokenChr := '()[].';
fEditors := TList.Create;
fEditstuffs := TList.Create;
@ -942,7 +951,6 @@ begin
dec(i);
end;
result := copy(s, i + 1, FEditor.CaretX - i - 1);
end;
end
else
@ -964,7 +972,7 @@ begin
end;
end;
destructor TSynCompletion.destroy;
destructor TSynCompletion.Destroy;
begin
// necessary to get Notification called before fEditors is freed
Form.Free;

View File

@ -1351,7 +1351,9 @@ begin
with aCompletion do
Begin
EndOfTokenChr:='()[]';
writeln('AAAA 1');
Width:=400;
writeln('AAAA 2');
OnExecute := @ccExecute;
OnCancel := @ccCancel;
OnCodeCompletion := @ccComplete;
@ -1465,10 +1467,13 @@ begin
i:=0;
while (i<FSourceEditorList.Count) do begin
if Editors[i]<>TempEditor then dec(SourceIndex);
if SourceIndex=0 then
if SourceIndex=0 then begin
Source:=Editors[i].EditorComponent.Lines;
exit;
end;
inc(i);
end;
Source:=nil;
end;
end;
@ -1575,7 +1580,6 @@ End;
Procedure TSourceNotebook.ccCancel(Sender : TObject);
var ActSE: TSourceEditor;
begin
writeln('TSourceNotebook.ccCancel ',sCompl<>nil,' ',GetActiveSE<>nil);
if sCompl=nil then exit;
sCompl.Deactivate;
sCompl:=nil;
@ -1719,7 +1723,6 @@ Begin
ctWordCompletion:
begin
ccSelection:='';
aWordCompletion.GetWordList(S, Prefix, false, 30);
end;
ctTemplateCompletion:

View File

@ -65,10 +65,10 @@ end;
procedure TWordCompletion.GetWordList(AWordList:TStrings; const Prefix:String;
CaseSensitive:boolean; MaxResults:integer);
var i,line,x,PrefixLen,MaxHash:integer;
PrefixLow,s:string;
SourceText:TStringList;
HashList:^integer;// index list. Every entry points to a word in the AWordList
var i, j, Line, x, PrefixLen, MaxHash, LineLen: integer;
UpPrefix, LineText, UpLineText, NewWord: string;
SourceText: TStringList;
HashList: ^integer;// index list. Every entry points to a word in the AWordList
SourceTextIndex:integer;
LastCharType:TCharType;
@ -77,10 +77,6 @@ var i,line,x,PrefixLen,MaxHash:integer;
var a,Hash,HashTry:integer;
ALowWord:string;
begin
if CaseSensitive then begin
if copy(AWord,1,PrefixLen)<>Prefix then exit;
end else if lowercase(copy(AWord,1,PrefixLen))<>PrefixLow then exit
else if (AWord=Prefix) then exit;
ALowWord:=lowercase(AWord);
Hash:=0;
a:=0;
@ -115,45 +111,71 @@ begin
for i:=0 to MaxHash-1 do HashList[i]:=-1;
PrefixLen:=length(Prefix);
AWordList.Capacity:=MaxResults;
PrefixLow:=lowercase(Prefix);
UpPrefix:=uppercase(Prefix);
// first add all recently used words
i:=FWordBuffer.Count-1;
while (i>=0) and (AWordList.Count<MaxResults) do begin
Add(FWordBuffer[i]);
NewWord:=FWordBuffer[i];
if CaseSensitive then begin
if copy(NewWord,1,PrefixLen)<>Prefix then
exit;
end else if uppercase(copy(NewWord,1,PrefixLen))<>UpPrefix then
exit
else if (NewWord=Prefix) then exit;
Add(NewWord);
dec(i);
end;
if AWordList.Count>=MaxResults then exit;
// then search in all sources for more words that could fit
SourceTextIndex:=0;
if Assigned(FOnGetSource) then begin
SourceText:=nil;
FOnGetSource(SourceText,SourceTextIndex);
repeat
SourceText:=nil;
FOnGetSource(SourceText,SourceTextIndex);
if SourceText<>nil then begin
line:=0;
while (line<SourceText.Count) do begin
s:=SourceText[line];
Line:=0;
UpLineText:='';
while (Line<SourceText.Count) do begin
LineText:=SourceText[line];
LineLen:=length(LineText);
if not CaseSensitive then
UpLineText:=uppercase(LineText);
x:=1;
LastCharType:=ctNone;
while (x<=length(s)) do begin
if (LastCharType=ctNone) and (CharTable[s[x]]=ctWordBegin) then
begin
while (x<=LineLen) do begin
if (LastCharType=ctNone) and (CharTable[LineText[x]]=ctWordBegin)
then begin
// word found
i:=x;
repeat
inc(i);
until (i>length(s)) or (CharTable[s[i]]=ctNone);
Add(copy(s,x,i-x));
until (i>LineLen) or (CharTable[LineText[i]]=ctNone);
if i-x>=PrefixLen then begin
if CaseSensitive then begin
j:=1;
while (j<=PrefixLen) and (Prefix[j]=LineText[x+j-1]) do
inc(j);
if (j>PrefixLen) and (copy(LineText,x,i-x)<>Prefix) then
Add(copy(LineText,x,i-x));
end else begin
j:=1;
while (j<=PrefixLen) and (UpPrefix[j]=UpLineText[x+j-1]) do
inc(j);
if (j>PrefixLen) and (copy(LineText,x,i-x)<>Prefix) then
Add(copy(LineText,x,i-x))
end;
end;
if AWordList.Count>=MaxResults then exit;
x:=i;
end else
inc(x);
LastCharType:=CharTable[s[x-1]];
LastCharType:=CharTable[LineText[x-1]];
end;
inc(line);
end;
end;
inc(SourceTextIndex);
SourceText:=nil;
FOnGetSource(SourceText,SourceTextIndex);
until SourceText=nil;
end;