From aee59db892245dddf02d8ad7eae722f7557852d3 Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Mon, 22 Jan 2024 08:42:00 +0700 Subject: [PATCH] refactor(developer): update app context as well as cached context for KMN keyboard debugger Fixes #10214. --- ...veloper.UI.Debug.UfrmLdmlKeyboardDebug.pas | 53 +--------------- developer/src/tike/child/UfrmDebug.pas | 47 ++++---------- .../debug/Keyman.System.Debug.DebugUtils.pas | 63 +++++++++++++++++++ developer/src/tike/tike.dpr | 3 +- developer/src/tike/tike.dproj | 11 ++-- 5 files changed, 86 insertions(+), 91 deletions(-) create mode 100644 developer/src/tike/debug/Keyman.System.Debug.DebugUtils.pas diff --git a/developer/src/tike/child/Keyman.Developer.UI.Debug.UfrmLdmlKeyboardDebug.pas b/developer/src/tike/child/Keyman.Developer.UI.Debug.UfrmLdmlKeyboardDebug.pas index 9412a9f817f..8f086ee9b2b 100644 --- a/developer/src/tike/child/Keyman.Developer.UI.Debug.UfrmLdmlKeyboardDebug.pas +++ b/developer/src/tike/child/Keyman.Developer.UI.Debug.UfrmLdmlKeyboardDebug.pas @@ -34,6 +34,7 @@ interface Keyman.System.Debug.DebugCore, Keyman.System.Debug.DebugEvent, Keyman.System.Debug.DebugUIStatus, + Keyman.System.Debug.DebugUtils, Keyman.System.KeymanCore, Keyman.System.KeymanCoreDebug, UframeTextEditor, @@ -115,8 +116,6 @@ TfrmLdmlKeyboardDebug = class(TTikeForm) function HandleMemoKeydown(var Message: TMessage): Boolean; procedure SetCurrentEvent(Value: Integer); procedure Run; - function GetContextFromMemo( - IncludeMarkers: Boolean): TArray; protected function GetHelpTopic: string; override; @@ -291,52 +290,6 @@ procedure TfrmLdmlKeyboardDebug.memoMessage(Sender: TObject; var Message: TMessa end; end; -function TfrmLdmlKeyboardDebug.GetContextFromMemo(IncludeMarkers: Boolean): TArray; -var - n, i: Integer; - ch: Char; - dk: TDeadKeyInfo; -begin - n := 0; - SetLength(Result, Length(memo.Text)+1); - i := 1; - while i <= memo.SelStart + memo.SelLength do - begin - ch := memo.Text[i]; - if Uni_IsSurrogate1(ch) and (i < Length(memo.Text)) and - Uni_IsSurrogate2(memo.Text[i+1]) then - begin - Result[n]._type := KM_CORE_CT_CHAR; - Result[n].character := Uni_SurrogateToUTF32(ch, memo.Text[i+1]); - Inc(i); - end - else if Ord(ch) = $FFFC then - begin - if IncludeMarkers then - begin - Result[n]._type := KM_CORE_CT_MARKER; - dk := FDeadkeys.GetFromPosition(i-1); - Assert(Assigned(dk)); - Result[n].marker := dk.Deadkey.Value; - end - else - begin - Inc(i); - Continue; - end; - end - else - begin - Result[n]._type := KM_CORE_CT_CHAR; - Result[n].character := Ord(ch); - end; - Inc(i); - Inc(n); - end; - - Result[n]._type := KM_CORE_CT_END; -end; - function TfrmLdmlKeyboardDebug.SetKeyEventContext: Boolean; var context: pkm_core_context; @@ -360,14 +313,14 @@ function TfrmLdmlKeyboardDebug.SetKeyEventContext: Boolean; end; // Set the cached context - context_items := GetContextFromMemo(True); + context_items := GetContextFromMemo(memo, FDeadkeys, True); context := km_core_state_context(FDebugCore.State); Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK; if Result then begin // Set the app context - context_items := GetContextFromMemo(False); + context_items := GetContextFromMemo(memo, FDeadkeys, False); context := km_core_state_app_context(FDebugCore.State); Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK; end; diff --git a/developer/src/tike/child/UfrmDebug.pas b/developer/src/tike/child/UfrmDebug.pas index 1d271e62a05..0dad51d1bfc 100644 --- a/developer/src/tike/child/UfrmDebug.pas +++ b/developer/src/tike/child/UfrmDebug.pas @@ -34,6 +34,7 @@ interface Keyman.System.Debug.DebugCore, Keyman.System.Debug.DebugEvent, Keyman.System.Debug.DebugUIStatus, + Keyman.System.Debug.DebugUtils, Keyman.System.KeymanCore, Keyman.System.KeymanCoreDebug, UframeTextEditor, @@ -373,12 +374,7 @@ function TfrmDebug.SetKeyEventContext: Boolean; var context: pkm_core_context; context_items: TArray; - n, i: Integer; - ch: Char; - dk: TDeadKeyInfo; begin - context := km_core_state_context(FDebugCore.State); - if memo.SelLength > 0 then begin // When there is a selection, we'll treat it as @@ -392,41 +388,22 @@ function TfrmDebug.SetKeyEventContext: Boolean; // backspace in the case of backspace key, rather // than deleting the last character of the selection // (Keyman Core is not aware of selection). - km_core_context_clear(context); + km_core_state_context_clear(FDebugCore.State); Exit(True); end; - n := 0; - SetLength(context_items, Length(memo.Text)+1); - i := 1; - while i <= memo.SelStart + memo.SelLength do + // Set the cached context + context_items := GetContextFromMemo(memo, FDeadkeys, True); + context := km_core_state_context(FDebugCore.State); + Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK; + + if Result then begin - ch := memo.Text[i]; - if Uni_IsSurrogate1(ch) and (i < Length(memo.Text)) and - Uni_IsSurrogate2(memo.Text[i+1]) then - begin - context_items[n]._type := KM_CORE_CT_CHAR; - context_items[n].character := Uni_SurrogateToUTF32(ch, memo.Text[i+1]); - Inc(i); - end - else if Ord(ch) = $FFFC then - begin - context_items[n]._type := KM_CORE_CT_MARKER; - dk := FDeadkeys.GetFromPosition(i-1); - Assert(Assigned(dk)); - context_items[n].marker := dk.Deadkey.Value; - end - else - begin - context_items[n]._type := KM_CORE_CT_CHAR; - context_items[n].character := Ord(ch); - end; - Inc(i); - Inc(n); + // Set the app context + context_items := GetContextFromMemo(memo, FDeadkeys, False); + context := km_core_state_app_context(FDebugCore.State); + Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK; end; - - context_items[n]._type := KM_CORE_CT_END; - Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK; end; function TfrmDebug.ProcessKeyEvent(var Message: TMessage): Boolean; diff --git a/developer/src/tike/debug/Keyman.System.Debug.DebugUtils.pas b/developer/src/tike/debug/Keyman.System.Debug.DebugUtils.pas new file mode 100644 index 00000000000..d310a35eefc --- /dev/null +++ b/developer/src/tike/debug/Keyman.System.Debug.DebugUtils.pas @@ -0,0 +1,63 @@ +unit Keyman.System.Debug.DebugUtils; + +interface + +uses + debugdeadkeys, + Keyman.System.KeymanCore, + KeymanDeveloperDebuggerMemo; + +function GetContextFromMemo(Memo: TKeymanDeveloperDebuggerMemo; DeadKeys: TDebugDeadkeyInfoList; IncludeMarkers: Boolean): TArray; + +implementation + +uses + Unicode; + +function GetContextFromMemo(Memo: TKeymanDeveloperDebuggerMemo; DeadKeys: TDebugDeadkeyInfoList; IncludeMarkers: Boolean): TArray; +var + n, i: Integer; + ch: Char; + dk: TDeadKeyInfo; +begin + n := 0; + SetLength(Result, Length(Memo.Text)+1); + i := 1; + while i <= Memo.SelStart + Memo.SelLength do + begin + ch := Memo.Text[i]; + if Uni_IsSurrogate1(ch) and (i < Length(Memo.Text)) and + Uni_IsSurrogate2(Memo.Text[i+1]) then + begin + Result[n]._type := KM_CORE_CT_CHAR; + Result[n].character := Uni_SurrogateToUTF32(ch, Memo.Text[i+1]); + Inc(i); + end + else if Ord(ch) = $FFFC then + begin + if IncludeMarkers then + begin + Result[n]._type := KM_CORE_CT_MARKER; + dk := DeadKeys.GetFromPosition(i-1); + Assert(Assigned(dk)); + Result[n].marker := dk.Deadkey.Value; + end + else + begin + Inc(i); + Continue; + end; + end + else + begin + Result[n]._type := KM_CORE_CT_CHAR; + Result[n].character := Ord(ch); + end; + Inc(i); + Inc(n); + end; + + Result[n]._type := KM_CORE_CT_END; +end; + +end. diff --git a/developer/src/tike/tike.dpr b/developer/src/tike/tike.dpr index 3745871ee34..b4bcd29e1f4 100644 --- a/developer/src/tike/tike.dpr +++ b/developer/src/tike/tike.dpr @@ -298,7 +298,8 @@ uses Keyman.System.CopyDataHelper in 'main\Keyman.System.CopyDataHelper.pas', Keyman.Developer.System.ProjectOwningFile in 'main\Keyman.Developer.System.ProjectOwningFile.pas', Keyman.Developer.System.Main in 'main\Keyman.Developer.System.Main.pas', - Keyman.Developer.System.LaunchProjects in 'main\Keyman.Developer.System.LaunchProjects.pas'; + Keyman.Developer.System.LaunchProjects in 'main\Keyman.Developer.System.LaunchProjects.pas', + Keyman.System.Debug.DebugUtils in 'debug\Keyman.System.Debug.DebugUtils.pas'; {$R *.RES} {$R ICONS.RES} diff --git a/developer/src/tike/tike.dproj b/developer/src/tike/tike.dproj index f535283953c..b3cd83bdfef 100644 --- a/developer/src/tike/tike.dproj +++ b/developer/src/tike/tike.dproj @@ -471,12 +471,17 @@ + +
frmNewProjectParameters
+ dfm +
frmNewLDMLKeyboardProjectParameters
frmNewProject
+ @@ -578,11 +583,7 @@ - -
frmNewProjectParameters
- dfm -
- + Cfg_2