mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-15 13:40:48 +02:00
Make SynAutoCompletion inherit from SynEditPlucin
git-svn-id: trunk@35823 -
This commit is contained in:
parent
a47ef8234b
commit
563622504f
@ -376,34 +376,29 @@ type
|
|||||||
|
|
||||||
{ TSynAutoComplete }
|
{ TSynAutoComplete }
|
||||||
|
|
||||||
TSynAutoComplete = class(TComponent)
|
TSynAutoComplete = class(TLazSynMultiEditPlugin)
|
||||||
private
|
private
|
||||||
fEditstuffs: TList;
|
FExecCommandID: TSynEditorCommand;
|
||||||
FShortCut: TShortCut;
|
FShortCut: TShortCut;
|
||||||
fEditors: TList;
|
|
||||||
fAutoCompleteList: TStrings;
|
fAutoCompleteList: TStrings;
|
||||||
FEndOfTokenChr: string;
|
FEndOfTokenChr: string;
|
||||||
procedure SetAutoCompleteList(List: TStrings);
|
procedure SetAutoCompleteList(List: TStrings);
|
||||||
function GetEditor(i: integer): TCustomSynEdit;
|
|
||||||
function GetEdit: TCustomSynEdit;
|
|
||||||
procedure SetEdit(const Value: TCustomSynEdit);
|
|
||||||
protected
|
protected
|
||||||
|
procedure DoEditorAdded(AValue: TCustomSynEdit); override;
|
||||||
|
procedure DoEditorRemoving(AValue: TCustomSynEdit); override;
|
||||||
procedure SetShortCut(Value: TShortCut);
|
procedure SetShortCut(Value: TShortCut);
|
||||||
procedure Notification(AComponent: TComponent; Operation: TOperation);
|
|
||||||
override;
|
|
||||||
procedure EditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
|
|
||||||
virtual;
|
|
||||||
procedure EditorKeyPress(Sender: TObject; var Key: char); virtual;
|
|
||||||
procedure EditorUTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
|
|
||||||
virtual;
|
|
||||||
function GetPreviousToken(aEditor: TCustomSynEdit): string;
|
function GetPreviousToken(aEditor: TCustomSynEdit): string;
|
||||||
|
procedure TranslateKey(Sender: TObject; Code: word; SState: TShiftState;
|
||||||
|
var Data: pointer; var IsStartOfCombo: boolean; var Handled: boolean;
|
||||||
|
var Command: TSynEditorCommand; FinishComboOnly: Boolean;
|
||||||
|
var ComboKeyStrokes: TSynEditKeyStrokes);
|
||||||
|
procedure ProcessSynCommand(Sender: TObject; AfterProcessing: boolean;
|
||||||
|
var Handled: boolean; var Command: TSynEditorCommand;
|
||||||
|
var AChar: TUTF8Char; Data: pointer; HandlerData: pointer);
|
||||||
public
|
public
|
||||||
constructor Create(AOwner: TComponent); override;
|
constructor Create(AOwner: TComponent); override;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
procedure Execute(token: string; aEditor: TCustomSynEdit);
|
procedure Execute(token: string; aEditor: TCustomSynEdit);
|
||||||
property Editors[i: integer]: TCustomSynEdit read GetEditor;
|
|
||||||
procedure AddEditor(aEditor: TCustomSynEdit);
|
|
||||||
function RemoveEditor(aEditor: TCustomSynEdit): boolean;
|
|
||||||
function EditorsCount: integer;
|
function EditorsCount: integer;
|
||||||
function GetTokenList: string;
|
function GetTokenList: string;
|
||||||
function GetTokenValue(Token: string): string;
|
function GetTokenValue(Token: string): string;
|
||||||
@ -411,16 +406,18 @@ type
|
|||||||
property AutoCompleteList: TStrings read fAutoCompleteList
|
property AutoCompleteList: TStrings read fAutoCompleteList
|
||||||
write SetAutoCompleteList;
|
write SetAutoCompleteList;
|
||||||
property EndOfTokenChr: string read FEndOfTokenChr write FEndOfTokenChr;
|
property EndOfTokenChr: string read FEndOfTokenChr write FEndOfTokenChr;
|
||||||
property Editor: TCustomSynEdit read GetEdit write SetEdit;
|
|
||||||
property ShortCut: TShortCut read FShortCut write SetShortCut;
|
property ShortCut: TShortCut read FShortCut write SetShortCut;
|
||||||
|
property ExecCommandID: TSynEditorCommand read FExecCommandID write FExecCommandID;
|
||||||
|
property Editor;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure PrettyTextOut(c: TCanvas; x, y: integer; s: string);
|
procedure PrettyTextOut(c: TCanvas; x, y: integer; s: string);
|
||||||
|
|
||||||
const
|
const
|
||||||
ecSynCompletionExecute = ecPluginFirst + 0;
|
ecSynCompletionExecute = ecPluginFirst + 0;
|
||||||
|
ecSynAutoCompletionExecute = ecPluginFirst + 1;
|
||||||
|
|
||||||
ecSynCompletionCount = 1;
|
ecSynCompletionCount = 2;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
@ -1630,15 +1627,6 @@ end;
|
|||||||
|
|
||||||
{ TSynCompletion }
|
{ TSynCompletion }
|
||||||
|
|
||||||
type
|
|
||||||
TRecordUsedToStoreEachEditorVars = record
|
|
||||||
KeyPress: TKeyPressEvent;
|
|
||||||
KeyDown: TKeyEvent;
|
|
||||||
UTF8KeyPress: TUTF8KeyPressEvent;
|
|
||||||
NoNextKey: boolean;
|
|
||||||
end;
|
|
||||||
PRecordUsedToStoreEachEditorVars = ^TRecordUsedToStoreEachEditorVars;
|
|
||||||
|
|
||||||
procedure TSynCompletion.Backspace(Sender: TObject);
|
procedure TSynCompletion.Backspace(Sender: TObject);
|
||||||
var
|
var
|
||||||
F: TSynBaseCompletionForm;
|
F: TSynBaseCompletionForm;
|
||||||
@ -1858,35 +1846,13 @@ end;
|
|||||||
|
|
||||||
{ TSynAutoComplete }
|
{ TSynAutoComplete }
|
||||||
|
|
||||||
procedure TSynAutoComplete.AddEditor(aEditor: TCustomSynEdit);
|
|
||||||
var
|
|
||||||
p: PRecordUsedToStoreEachEditorVars;
|
|
||||||
begin
|
|
||||||
if fEditors.IndexOf(aEditor) = -1 then begin
|
|
||||||
fEditors.Add(aEditor);
|
|
||||||
new(p);
|
|
||||||
p^.KeyPress := aEditor.OnKeyPress;
|
|
||||||
p^.KeyDown := aEditor.OnKeyDown;
|
|
||||||
p^.UTF8KeyPress := aEditor.OnUTF8KeyPress;
|
|
||||||
p^.NoNextKey := false;
|
|
||||||
fEditstuffs.add(p);
|
|
||||||
aEditor.FreeNotification(self);
|
|
||||||
if not (csDesigning in ComponentState) then begin
|
|
||||||
aEditor.OnKeyDown := {$IFDEF FPC}@{$ENDIF}EditorKeyDown;
|
|
||||||
aEditor.OnKeyPress := {$IFDEF FPC}@{$ENDIF}EditorKeyPress;
|
|
||||||
aEditor.OnUTF8KeyPress := {$IFDEF FPC}@{$ENDIF}EditorUTF8KeyPress;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
constructor TSynAutoComplete.Create(AOwner: TComponent);
|
constructor TSynAutoComplete.Create(AOwner: TComponent);
|
||||||
begin
|
begin
|
||||||
inherited;
|
inherited;
|
||||||
fEditors := TList.Create;
|
|
||||||
fEditstuffs := TList.Create;
|
|
||||||
FEndOfTokenChr := '()[].';
|
FEndOfTokenChr := '()[].';
|
||||||
fAutoCompleteList := TStringList.Create;
|
fAutoCompleteList := TStringList.Create;
|
||||||
fShortCut := Menus.ShortCut(Ord(' '), [ssShift]);
|
fShortCut := Menus.ShortCut(Ord(' '), [ssShift]);
|
||||||
|
FExecCommandID := KeyOffset + ecSynAutoCompletionExecute;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSynAutoComplete.SetShortCut(Value: TShortCut);
|
procedure TSynAutoComplete.SetShortCut(Value: TShortCut);
|
||||||
@ -1896,75 +1862,13 @@ end;
|
|||||||
|
|
||||||
destructor TSynAutoComplete.destroy;
|
destructor TSynAutoComplete.destroy;
|
||||||
begin
|
begin
|
||||||
while feditors.count <> 0 do
|
|
||||||
{$IFDEF FPC}
|
|
||||||
RemoveEditor(TCustomSynEdit(feditors.last));
|
|
||||||
{$ELSE}
|
|
||||||
RemoveEditor(feditors.last);
|
|
||||||
{$ENDIF}
|
|
||||||
FreeAndNil(fEditors);
|
|
||||||
FreeAndNil(fEditstuffs);
|
|
||||||
FreeAndNil(fAutoCompleteList);
|
FreeAndNil(fAutoCompleteList);
|
||||||
inherited;
|
inherited;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSynAutoComplete.EditorKeyDown(Sender: TObject; var Key: Word;
|
|
||||||
Shift: TShiftState);
|
|
||||||
var
|
|
||||||
i: integer;
|
|
||||||
ShortCutKey: Word;
|
|
||||||
ShortCutShift: TShiftState;
|
|
||||||
begin
|
|
||||||
ShortCutToKey(FShortCut, ShortCutKey, ShortCutShift);
|
|
||||||
|
|
||||||
i := fEditors.IndexOf(Sender);
|
|
||||||
if i <> -1 then begin
|
|
||||||
if (Shift = ShortCutShift) and (Key = ShortCutKey) and
|
|
||||||
not (Sender as TCustomSynEdit).ReadOnly
|
|
||||||
then begin
|
|
||||||
Execute(GetPreviousToken(Sender as TCustomSynEdit), Sender as TCustomSynEdit);
|
|
||||||
Key := 0;
|
|
||||||
TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).NoNextKey := true;
|
|
||||||
end;
|
|
||||||
if assigned(TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).KeyDown) then
|
|
||||||
TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).KeyDown(sender, key, Shift);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure TSynAutoComplete.EditorKeyPress(Sender: TObject; var Key: char);
|
|
||||||
var
|
|
||||||
i: integer;
|
|
||||||
begin
|
|
||||||
i := fEditors.IndexOf(Sender);
|
|
||||||
if i <> -1 then begin
|
|
||||||
if TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).NoNextKey then begin
|
|
||||||
key := #0;
|
|
||||||
TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).NoNextKey := false;
|
|
||||||
end;
|
|
||||||
if assigned(TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).KeyPress) then
|
|
||||||
TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).KeyPress(sender, key);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure TSynAutoComplete.EditorUTF8KeyPress(Sender: TObject;
|
|
||||||
var UTF8Key: TUTF8Char);
|
|
||||||
var
|
|
||||||
i: integer;
|
|
||||||
begin
|
|
||||||
i := fEditors.IndexOf(Sender);
|
|
||||||
if i <> -1 then begin
|
|
||||||
if TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).NoNextKey then begin
|
|
||||||
UTF8Key := #0;
|
|
||||||
TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).NoNextKey := false;
|
|
||||||
end;
|
|
||||||
if assigned(TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).UTF8KeyPress) then
|
|
||||||
TRecordUsedToStoreEachEditorVars(fEditstuffs[i]^).UTF8KeyPress(sender, UTF8Key);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TSynAutoComplete.EditorsCount: integer;
|
function TSynAutoComplete.EditorsCount: integer;
|
||||||
begin
|
begin
|
||||||
result := fEditors.count;
|
Result := EditorCount;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSynAutoComplete.Execute(token: string; aEditor: TCustomSynEdit);
|
procedure TSynAutoComplete.Execute(token: string; aEditor: TCustomSynEdit);
|
||||||
@ -1976,8 +1880,6 @@ begin
|
|||||||
//Writeln('[TSynAutoComplete.Execute] Token is "',Token,'"');
|
//Writeln('[TSynAutoComplete.Execute] Token is "',Token,'"');
|
||||||
i := AutoCompleteList.IndexOf(token);
|
i := AutoCompleteList.IndexOf(token);
|
||||||
if i <> -1 then begin
|
if i <> -1 then begin
|
||||||
TRecordUsedToStoreEachEditorVars(
|
|
||||||
fEditstuffs[fEditors.IndexOf(aEditor)]^).NoNextKey := true;
|
|
||||||
for j := 1 to length(token) do
|
for j := 1 to length(token) do
|
||||||
aEditor.CommandProcessor(ecDeleteLastChar, ' ', nil);
|
aEditor.CommandProcessor(ecDeleteLastChar, ' ', nil);
|
||||||
inc(i);
|
inc(i);
|
||||||
@ -2010,22 +1912,6 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TSynAutoComplete.GetEdit: TCustomSynEdit;
|
|
||||||
begin
|
|
||||||
if EditorsCount > 0 then
|
|
||||||
result := Editors[0]
|
|
||||||
else
|
|
||||||
result := nil;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TSynAutoComplete.GetEditor(i: integer): TCustomSynEdit;
|
|
||||||
begin
|
|
||||||
if (i < 0) or (i >= EditorsCount) then
|
|
||||||
result := nil
|
|
||||||
else
|
|
||||||
result := TCustomSynEdit(fEditors[i]);
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TSynAutoComplete.GetPreviousToken(aEditor: TCustomSynEdit): string;
|
function TSynAutoComplete.GetPreviousToken(aEditor: TCustomSynEdit): string;
|
||||||
var
|
var
|
||||||
s: string;
|
s: string;
|
||||||
@ -2046,27 +1932,45 @@ begin
|
|||||||
result := '';
|
result := '';
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSynAutoComplete.Notification(AComponent: TComponent; Operation: TOperation);
|
procedure TSynAutoComplete.TranslateKey(Sender: TObject; Code: word; SState: TShiftState;
|
||||||
begin
|
var Data: pointer; var IsStartOfCombo: boolean; var Handled: boolean;
|
||||||
inherited Notification(AComponent, Operation);
|
var Command: TSynEditorCommand; FinishComboOnly: Boolean;
|
||||||
if (Operation = opRemove) and (fEditors <> nil) then
|
var ComboKeyStrokes: TSynEditKeyStrokes);
|
||||||
if fEditors.indexOf(AComponent) <> -1 then
|
|
||||||
RemoveEditor(AComponent as TCustomSynEdit);
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TSynAutoComplete.RemoveEditor(aEditor: TCustomSynEdit): boolean;
|
|
||||||
var
|
var
|
||||||
i: integer;
|
i: integer;
|
||||||
P : PRecordUsedToStoreEachEditorVars;
|
ShortCutKey: Word;
|
||||||
|
ShortCutShift: TShiftState;
|
||||||
begin
|
begin
|
||||||
i := fEditors.Remove(aEditor);
|
if (Code = VK_UNKNOWN) or Handled or FinishComboOnly or (FExecCommandID = ecNone) then exit;
|
||||||
result := i <> -1;
|
|
||||||
if result then begin
|
i := IndexOfEditor(Sender as TCustomSynEdit);
|
||||||
p := fEditStuffs[i]; //shane
|
if i >= 0 then begin
|
||||||
dispose(p); //shane
|
ShortCutToKey(FShortCut, ShortCutKey, ShortCutShift);
|
||||||
// dispose(fEditstuffs[i]); //commented out by shane
|
if (SState = ShortCutShift) and (Code = ShortCutKey) then begin
|
||||||
fEditstuffs.delete(i);
|
Command := FExecCommandID;
|
||||||
|
Handled := True;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TSynAutoComplete.ProcessSynCommand(Sender: TObject; AfterProcessing: boolean;
|
||||||
|
var Handled: boolean; var Command: TSynEditorCommand; var AChar: TUTF8Char; Data: pointer;
|
||||||
|
HandlerData: pointer);
|
||||||
|
var
|
||||||
|
i: integer;
|
||||||
|
begin
|
||||||
|
if Handled or (Command <> FExecCommandID) then
|
||||||
|
exit;
|
||||||
|
|
||||||
|
i := IndexOfEditor(Sender as TCustomSynEdit);
|
||||||
|
if i >= 0 then begin
|
||||||
|
with sender as TCustomSynEdit do begin
|
||||||
|
if not ReadOnly then begin
|
||||||
|
Editor := Sender as TCustomSynEdit; // Will set Form.SetCurrentEditor
|
||||||
|
Execute(GetPreviousToken(Sender as TCustomSynEdit), Sender as TCustomSynEdit);
|
||||||
|
Handled := True;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -2075,9 +1979,18 @@ begin
|
|||||||
fAutoCompleteList.Assign(List);
|
fAutoCompleteList.Assign(List);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSynAutoComplete.SetEdit(const Value: TCustomSynEdit);
|
procedure TSynAutoComplete.DoEditorAdded(AValue: TCustomSynEdit);
|
||||||
begin
|
begin
|
||||||
AddEditor(Value);
|
inherited DoEditorAdded(AValue);
|
||||||
|
AValue.RegisterCommandHandler(@ProcessSynCommand, nil);
|
||||||
|
AValue.RegisterKeyTranslationHandler(@TranslateKey);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TSynAutoComplete.DoEditorRemoving(AValue: TCustomSynEdit);
|
||||||
|
begin
|
||||||
|
inherited DoEditorRemoving(AValue);
|
||||||
|
AValue.UnregisterCommandHandler(@ProcessSynCommand);
|
||||||
|
AValue.UnRegisterKeyTranslationHandler(@TranslateKey);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TSynAutoComplete.GetTokenList: string;
|
function TSynAutoComplete.GetTokenList: string;
|
||||||
@ -2176,8 +2089,9 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
const
|
const
|
||||||
SynComplutionCommandStrs: array[0..0] of TIdentMapEntry = (
|
SynComplutionCommandStrs: array[0..1] of TIdentMapEntry = (
|
||||||
(Value: ecSynCompletionExecute; Name: 'ecSynCompletionExecute')
|
(Value: ecSynCompletionExecute; Name: 'ecSynCompletionExecute'),
|
||||||
|
(Value: ecSynAutoCompletionExecute; Name: 'ecSynAutoCompletionExecute')
|
||||||
);
|
);
|
||||||
|
|
||||||
function IdentToSynCompletionCommand(const Ident: string; var Cmd: longint): boolean;
|
function IdentToSynCompletionCommand(const Ident: string; var Cmd: longint): boolean;
|
||||||
|
Loading…
Reference in New Issue
Block a user