mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-12 20:21:04 +02:00
SynEdit: SynCompletion, refactor usage of OnUtf8Key (no longer internally used. Moved event to be called before any built in processing
git-svn-id: trunk@42773 -
This commit is contained in:
parent
471ec807d9
commit
f0e13de7ac
@ -146,6 +146,8 @@ type
|
|||||||
procedure SetCurrentString(const Value: string);
|
procedure SetCurrentString(const Value: string);
|
||||||
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
|
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
|
||||||
procedure KeyPress(var Key: char); override;
|
procedure KeyPress(var Key: char); override;
|
||||||
|
procedure AddCharAtCursor(AUtf8Char: TUTF8Char); virtual;
|
||||||
|
procedure DeleteCharBeforoCursor; virtual;
|
||||||
procedure Paint; override;
|
procedure Paint; override;
|
||||||
procedure AppDeactivated(Sender: TObject); // Because Form.Deactivate isn't called
|
procedure AppDeactivated(Sender: TObject); // Because Form.Deactivate isn't called
|
||||||
procedure Deactivate; override;
|
procedure Deactivate; override;
|
||||||
@ -236,6 +238,16 @@ type
|
|||||||
property OnDragResized: TNotifyEvent read FOnDragResized write FOnDragResized;
|
property OnDragResized: TNotifyEvent read FOnDragResized write FOnDragResized;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
TSynBaseCompletionFormClass = class of TSynBaseCompletionForm;
|
||||||
|
|
||||||
|
{ TSynCompletionForm }
|
||||||
|
|
||||||
|
TSynCompletionForm = class(TSynBaseCompletionForm)
|
||||||
|
protected
|
||||||
|
procedure AddCharAtCursor(AUtf8Char: TUTF8Char); override;
|
||||||
|
procedure DeleteCharBeforoCursor; override;
|
||||||
|
end;
|
||||||
|
|
||||||
{ TSynBaseCompletion }
|
{ TSynBaseCompletion }
|
||||||
|
|
||||||
TSynBaseCompletion = class(TLazSynMultiEditPlugin)
|
TSynBaseCompletion = class(TLazSynMultiEditPlugin)
|
||||||
@ -292,6 +304,8 @@ type
|
|||||||
procedure SetOnKeyNextChar(const AValue: TNotifyEvent);
|
procedure SetOnKeyNextChar(const AValue: TNotifyEvent);
|
||||||
function GetOnKeyPrevChar: TNotifyEvent;
|
function GetOnKeyPrevChar: TNotifyEvent;
|
||||||
procedure SetOnKeyPrevChar(const AValue: TNotifyEvent);
|
procedure SetOnKeyPrevChar(const AValue: TNotifyEvent);
|
||||||
|
protected
|
||||||
|
function GetCompletionFormClass: TSynBaseCompletionFormClass; virtual;
|
||||||
public
|
public
|
||||||
constructor Create(AOwner: TComponent); override;
|
constructor Create(AOwner: TComponent); override;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
@ -351,10 +365,8 @@ type
|
|||||||
FExecCommandID: TSynEditorCommand;
|
FExecCommandID: TSynEditorCommand;
|
||||||
FEndOfTokenChr: string;
|
FEndOfTokenChr: string;
|
||||||
FOnCodeCompletion: TCodeCompletionEvent;
|
FOnCodeCompletion: TCodeCompletionEvent;
|
||||||
procedure backspace(Sender: TObject);
|
|
||||||
procedure Cancel(Sender: TObject);
|
procedure Cancel(Sender: TObject);
|
||||||
procedure Validate(Sender: TObject; KeyChar: TUTF8Char; Shift: TShiftState);
|
procedure Validate(Sender: TObject; KeyChar: TUTF8Char; Shift: TShiftState);
|
||||||
procedure UTF8KeyPress(Sender: TObject; var Key: TUTF8Char); // called by the form
|
|
||||||
function GetPreviousToken(FEditor: TCustomSynEdit): string;
|
function GetPreviousToken(FEditor: TCustomSynEdit): string;
|
||||||
protected
|
protected
|
||||||
procedure OnFormPaint(Sender: TObject);
|
procedure OnFormPaint(Sender: TObject);
|
||||||
@ -369,9 +381,12 @@ type
|
|||||||
procedure ProcessSynCommand(Sender: TObject; AfterProcessing: boolean;
|
procedure ProcessSynCommand(Sender: TObject; AfterProcessing: boolean;
|
||||||
var Handled: boolean; var Command: TSynEditorCommand;
|
var Handled: boolean; var Command: TSynEditorCommand;
|
||||||
var AChar: TUTF8Char; Data: pointer; HandlerData: pointer);
|
var AChar: TUTF8Char; Data: pointer; HandlerData: pointer);
|
||||||
|
function GetCompletionFormClass: TSynBaseCompletionFormClass; override;
|
||||||
public
|
public
|
||||||
constructor Create(AOwner: TComponent); override;
|
constructor Create(AOwner: TComponent); override;
|
||||||
function EditorsCount: integer; deprecated; // use EditorCount
|
function EditorsCount: integer; deprecated; // use EditorCount
|
||||||
|
procedure AddCharAtCursor(AUtf8Char: TUTF8Char);
|
||||||
|
procedure DeleteCharBeforoCursor;
|
||||||
published
|
published
|
||||||
property ShortCut: TShortCut read FShortCut write SetShortCut;
|
property ShortCut: TShortCut read FShortCut write SetShortCut;
|
||||||
property EndOfTokenChr: string read FEndOfTokenChr write FEndOfTokenChr;
|
property EndOfTokenChr: string read FEndOfTokenChr write FEndOfTokenChr;
|
||||||
@ -431,6 +446,22 @@ implementation
|
|||||||
var
|
var
|
||||||
KeyOffset: integer;
|
KeyOffset: integer;
|
||||||
|
|
||||||
|
{ TSynCompletionForm }
|
||||||
|
|
||||||
|
procedure TSynCompletionForm.AddCharAtCursor(AUtf8Char: TUTF8Char);
|
||||||
|
begin
|
||||||
|
inherited AddCharAtCursor(AUtf8Char);
|
||||||
|
if CurrentEditor <> nil then
|
||||||
|
(CurrentEditor as TCustomSynEdit).CommandProcessor(ecChar, AUtf8Char, nil);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TSynCompletionForm.DeleteCharBeforoCursor;
|
||||||
|
begin
|
||||||
|
if CurrentEditor <> nil then
|
||||||
|
(CurrentEditor as TCustomSynEdit).CommandProcessor(ecDeleteLastChar, #0, nil);
|
||||||
|
inherited DeleteCharBeforoCursor;
|
||||||
|
end;
|
||||||
|
|
||||||
{ TSynBaseCompletionFormSizeDrag }
|
{ TSynBaseCompletionFormSizeDrag }
|
||||||
|
|
||||||
procedure TSynBaseCompletionFormSizeDrag.MouseDown(Button: TMouseButton; Shift: TShiftState;
|
procedure TSynBaseCompletionFormSizeDrag.MouseDown(Button: TMouseButton; Shift: TShiftState;
|
||||||
@ -504,7 +535,7 @@ end;
|
|||||||
|
|
||||||
{ TSynBaseCompletionForm }
|
{ TSynBaseCompletionForm }
|
||||||
|
|
||||||
constructor TSynBaseCompletionForm.Create(AOwner: TComponent);
|
constructor TSynBaseCompletionForm.Create(AOwner: Tcomponent);
|
||||||
begin
|
begin
|
||||||
ControlStyle := ControlStyle + [csNoDesignVisible];
|
ControlStyle := ControlStyle + [csNoDesignVisible];
|
||||||
FResizeLock := 1; // prevent DoResize (on Handle Creation) do reset LinesInWindow
|
FResizeLock := 1; // prevent DoResize (on Handle Creation) do reset LinesInWindow
|
||||||
@ -720,7 +751,7 @@ begin
|
|||||||
VK_BACK:
|
VK_BACK:
|
||||||
if (Shift = []) and (Length(CurrentString) > 0) then begin
|
if (Shift = []) and (Length(CurrentString) > 0) then begin
|
||||||
if Assigned(OnKeyDelete) then OnKeyDelete(Self);
|
if Assigned(OnKeyDelete) then OnKeyDelete(Self);
|
||||||
CurrentString := UTF8Copy(CurrentString, 1, UTF8Length(CurrentString) - 1);
|
DeleteCharBeforoCursor;
|
||||||
end;
|
end;
|
||||||
VK_TAB:
|
VK_TAB:
|
||||||
begin
|
begin
|
||||||
@ -759,7 +790,7 @@ begin
|
|||||||
#33..'z':
|
#33..'z':
|
||||||
begin
|
begin
|
||||||
if Key<>#0 then
|
if Key<>#0 then
|
||||||
CurrentString := CurrentString + key;
|
AddCharAtCursor(key);
|
||||||
Key:=#0;
|
Key:=#0;
|
||||||
end;
|
end;
|
||||||
#8: ;
|
#8: ;
|
||||||
@ -776,6 +807,16 @@ begin
|
|||||||
//debugln('TSynBaseCompletionForm.KeyPress END Key="',DbgStr(Key),'"');
|
//debugln('TSynBaseCompletionForm.KeyPress END Key="',DbgStr(Key),'"');
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TSynBaseCompletionForm.AddCharAtCursor(AUtf8Char: TUTF8Char);
|
||||||
|
begin
|
||||||
|
CurrentString := CurrentString + AUtf8Char;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TSynBaseCompletionForm.DeleteCharBeforoCursor;
|
||||||
|
begin
|
||||||
|
CurrentString := UTF8Copy(CurrentString, 1, UTF8Length(CurrentString) - 1);
|
||||||
|
end;
|
||||||
|
|
||||||
{$IFDEF HintClickWorkaround}
|
{$IFDEF HintClickWorkaround}
|
||||||
procedure TSynBaseCompletionForm.HintWindowMouseDown(Sender: TObject;
|
procedure TSynBaseCompletionForm.HintWindowMouseDown(Sender: TObject;
|
||||||
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
|
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
|
||||||
@ -990,34 +1031,36 @@ begin
|
|||||||
{$IFDEF VerboseKeys}
|
{$IFDEF VerboseKeys}
|
||||||
debugln('TSynBaseCompletionForm.UTF8KeyPress A UTF8Key="',DbgStr(UTF8Key),'" ',dbgsName(TObject(TMethod(OnUTF8KeyPress).Data)));
|
debugln('TSynBaseCompletionForm.UTF8KeyPress A UTF8Key="',DbgStr(UTF8Key),'" ',dbgsName(TObject(TMethod(OnUTF8KeyPress).Data)));
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
|
if Assigned(OnUTF8KeyPress) then
|
||||||
|
OnUTF8KeyPress(Self, UTF8Key);
|
||||||
|
if UTF8Key='' then
|
||||||
|
exit;
|
||||||
|
|
||||||
if UTF8Key=#8 then
|
if UTF8Key=#8 then
|
||||||
begin
|
begin
|
||||||
// backspace
|
// backspace
|
||||||
end else
|
end
|
||||||
|
else
|
||||||
|
if (Length(UTF8Key)>=1) and (not (UTF8Key[1] in ['a'..'z','A'..'Z','0'..'9','_'])) then
|
||||||
begin
|
begin
|
||||||
if (Length(UTF8Key)>=1) and (not (UTF8Key[1] in ['a'..'z','A'..'Z','0'..'9','_'])) then
|
// non identifier character
|
||||||
|
// if it is special key then eat it
|
||||||
|
if (Length(UTF8Key) = 1) and (UTF8Key[1] < #32) then
|
||||||
begin
|
begin
|
||||||
// non identifier character
|
if Assigned(OnCancel) then
|
||||||
|
OnCancel(Self);
|
||||||
// if it is special key then eat it
|
end
|
||||||
if (Length(UTF8Key) = 1) and (UTF8Key[1] < #32) then
|
else
|
||||||
begin
|
if Assigned(OnValidate) then
|
||||||
if Assigned(OnCancel) then
|
OnValidate(Self, UTF8Key, []);
|
||||||
OnCancel(Self);
|
UTF8Key := '';
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if Assigned(OnValidate) then
|
if (UTF8Key<>'') then
|
||||||
OnValidate(Self, UTF8Key, []);
|
begin
|
||||||
UTF8Key := '';
|
// identifier character
|
||||||
end else
|
AddCharAtCursor(UTF8Key);
|
||||||
if (UTF8Key<>'') then
|
UTF8Key := '';
|
||||||
begin
|
|
||||||
// identifier character
|
|
||||||
CurrentString := CurrentString + UTF8Key;
|
|
||||||
if Assigned(OnUTF8KeyPress) then
|
|
||||||
OnUTF8KeyPress(Self, UTF8Key);
|
|
||||||
UTF8Key := '';
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
{$IFDEF VerboseKeys}
|
{$IFDEF VerboseKeys}
|
||||||
debugln('TSynBaseCompletionForm.UTF8KeyPress END UTF8Key="',DbgStr(UTF8Key),'"');
|
debugln('TSynBaseCompletionForm.UTF8KeyPress END UTF8Key="',DbgStr(UTF8Key),'"');
|
||||||
@ -1260,7 +1303,7 @@ constructor TSynBaseCompletion.Create(AOwner: TComponent);
|
|||||||
begin
|
begin
|
||||||
FWidth := 262;
|
FWidth := 262;
|
||||||
inherited Create(AOwner);
|
inherited Create(AOwner);
|
||||||
Form := TSynBaseCompletionForm.Create(nil); // Do not create with owner, or the designer will make it visible
|
Form := GetCompletionFormClass.Create(nil); // Do not create with owner, or the designer will make it visible
|
||||||
Form.Width := FWidth;
|
Form.Width := FWidth;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -1327,6 +1370,11 @@ begin
|
|||||||
Form.OnKeyPrevChar:=AValue;
|
Form.OnKeyPrevChar:=AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TSynBaseCompletion.GetCompletionFormClass: TSynBaseCompletionFormClass;
|
||||||
|
begin
|
||||||
|
Result := TSynBaseCompletionForm;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TSynBaseCompletion.Execute(s: string; x, y: integer);
|
procedure TSynBaseCompletion.Execute(s: string; x, y: integer);
|
||||||
var
|
var
|
||||||
CurSynEdit: TCustomSynEdit;
|
CurSynEdit: TCustomSynEdit;
|
||||||
@ -1650,17 +1698,6 @@ end;
|
|||||||
|
|
||||||
{ TSynCompletion }
|
{ TSynCompletion }
|
||||||
|
|
||||||
procedure TSynCompletion.Backspace(Sender: TObject);
|
|
||||||
var
|
|
||||||
F: TSynBaseCompletionForm;
|
|
||||||
begin
|
|
||||||
F := Sender as TSynBaseCompletionForm;
|
|
||||||
if F.CurrentEditor <> nil then begin
|
|
||||||
(F.CurrentEditor as TCustomSynEdit).CommandProcessor(ecDeleteLastChar, #0,
|
|
||||||
nil);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure TSynCompletion.OnFormPaint(Sender: TObject);
|
procedure TSynCompletion.OnFormPaint(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
|
|
||||||
@ -1739,24 +1776,9 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSynCompletion.UTF8KeyPress(Sender: TObject; var Key: TUTF8Char);
|
|
||||||
var
|
|
||||||
F: TSynBaseCompletionForm;
|
|
||||||
begin
|
|
||||||
//debugln('TSynCompletion.UTF8KeyPress Key="',DbgStr(Key),'"');
|
|
||||||
F := Sender as TSynBaseCompletionForm;
|
|
||||||
if F.CurrentEditor <> nil then begin
|
|
||||||
with F.CurrentEditor as TCustomSynEdit do begin
|
|
||||||
CommandProcessor(ecChar, Key, nil);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
constructor TSynCompletion.Create(AOwner: TComponent);
|
constructor TSynCompletion.Create(AOwner: TComponent);
|
||||||
begin
|
begin
|
||||||
inherited Create(AOwner);
|
inherited Create(AOwner);
|
||||||
Form.OnUTF8KeyPress := @UTF8KeyPress;
|
|
||||||
Form.OnKeyDelete := @Backspace;
|
|
||||||
Form.OnValidate := @Validate;
|
Form.OnValidate := @Validate;
|
||||||
Form.OnCancel := @Cancel;
|
Form.OnCancel := @Cancel;
|
||||||
Form.OnPaint:=@OnFormPaint;
|
Form.OnPaint:=@OnFormPaint;
|
||||||
@ -1816,6 +1838,11 @@ begin
|
|||||||
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TSynCompletion.GetCompletionFormClass: TSynBaseCompletionFormClass;
|
||||||
|
begin
|
||||||
|
Result := TSynCompletionForm;
|
||||||
|
end;
|
||||||
|
|
||||||
function TSynCompletion.GetPreviousToken(FEditor: TCustomSynEdit): string;
|
function TSynCompletion.GetPreviousToken(FEditor: TCustomSynEdit): string;
|
||||||
var
|
var
|
||||||
s: string;
|
s: string;
|
||||||
@ -1867,6 +1894,16 @@ begin
|
|||||||
result := EditorCount;
|
result := EditorCount;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TSynCompletion.AddCharAtCursor(AUtf8Char: TUTF8Char);
|
||||||
|
begin
|
||||||
|
Form.AddCharAtCursor(AUtf8Char);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TSynCompletion.DeleteCharBeforoCursor;
|
||||||
|
begin
|
||||||
|
Form.DeleteCharBeforoCursor;
|
||||||
|
end;
|
||||||
|
|
||||||
{ TSynAutoComplete }
|
{ TSynAutoComplete }
|
||||||
|
|
||||||
constructor TSynAutoComplete.Create(AOwner: TComponent);
|
constructor TSynAutoComplete.Create(AOwner: TComponent);
|
||||||
|
@ -2189,9 +2189,6 @@ begin
|
|||||||
TheForm.OnValidate(Sender,UTF8Key,[]);
|
TheForm.OnValidate(Sender,UTF8Key,[]);
|
||||||
//debugln('TSourceNotebook.OnSynCompletionKeyPress B');
|
//debugln('TSourceNotebook.OnSynCompletionKeyPress B');
|
||||||
UTF8Key:='';
|
UTF8Key:='';
|
||||||
end else begin
|
|
||||||
Editor.CommandProcessor(ecChar,UTF8Key,nil);
|
|
||||||
UTF8Key:='';
|
|
||||||
end;
|
end;
|
||||||
//debugln('TSourceNotebook.OnSynCompletionKeyPress B UTF8Key=',dbgstr(UTF8Key));
|
//debugln('TSourceNotebook.OnSynCompletionKeyPress B UTF8Key=',dbgstr(UTF8Key));
|
||||||
end;
|
end;
|
||||||
|
Loading…
Reference in New Issue
Block a user