Recent

Author Topic: TSynCompletion failing when it's empty (bug?)  (Read 940 times)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1209
    • Burdjia
TSynCompletion failing when it's empty (bug?)
« on: May 15, 2025, 02:41:53 pm »
Using Lazarus 3.5.  It's easy to reproduce this error:
  • Open Lazarus.
  • Open the example examples/SynEdit/Completion/SynCompletionSample.lpi.
  • Compile and execute.
  • On the example open the autocompletion by pressing [Shift]+[Space].
  • Type some gliberish until the list is empty, then press [Space] or [Intro].
  • An exception raised.  If you continue executing, the program is frozen and should be killed.

The thing is that Lazarus' autocompletion doesn't fail (i.e. do the same in the IDE and it just closes the autocompletion window).  I've tried to find Lazarus' implementation but I wasn't able to find the unit.

Any solution or workaround?  Does Lazarus 4.0 fix this issue (I didn't test it yet because it doesn't appear in fpcupdeluxe)?
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11349
  • Debugger - SynEdit - and more
    • wiki
Re: TSynCompletion failing when it's empty (bug?)
« Reply #1 on: May 15, 2025, 04:25:29 pm »
Yes its a bug. Please report.

It will need some deeper review... There is some code that expects position to go -1. But also some code to explicitly prevent that.

I guess at some time some other bug must have been worked on, and that was partly changed, and now this is broken.


I don't have a quick workaround/fix.

I need to find some time and try to find out the history.

This part of SynEdit is unfortunately still very old. I never got round to clean it up (in a compatible manner). I expect that - outside the exact way of how the IDE uses it - it will have a few more issues. :(

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1209
    • Burdjia
Re: TSynCompletion failing when it's empty (bug?)
« Reply #2 on: May 15, 2025, 06:29:44 pm »
Yes its a bug. Please report.
I'd go to Lazarus' issue tracker but when I press New issue it insists I have to create or import a project.  Do I miss something?  (I created a GitLab account time ago but I didn't use it because I did found it utterly confusing, even following a tutorial %))

I don't have a quick workaround/fix.
Lazarus' debugger say where it fail.  I was planning to add a try...except to catch the exception and try to follow it from there.  If I can do something I'll keep you informed.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11349
  • Debugger - SynEdit - and more
    • wiki
Re: TSynCompletion failing when it's empty (bug?)
« Reply #3 on: May 15, 2025, 06:49:16 pm »
I know where it fails...

It checks for "Position >= 0".
So for an empty list, Position should be -1.

But there must have been a problem with that. https://gitlab.com/freepascal.org/lazarus/lazarus/-/commit/0e94a6b1db6d852c937325a44bdc182285dca767
Unfortunately, I did not describe what the issue was...



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11349
  • Debugger - SynEdit - and more
    • wiki
Re: TSynCompletion failing when it's empty (bug?)
« Reply #4 on: May 15, 2025, 06:55:20 pm »
You can try this patch
Code: Diff  [Select][+][-]
  1. diff --git a/components/synedit/syncompletion.pas b/components/synedit/syncompletion.pas
  2. index 4c3c7bff0c..b82f43d7c7 100644
  3. --- a/components/synedit/syncompletion.pas
  4. +++ b/components/synedit/syncompletion.pas
  5. @@ -1871,6 +1871,9 @@ procedure TSynCompletion.Validate(Sender: TObject; KeyChar: TUTF8Char;
  6.            NewBlockEnd := LogCaret;
  7.          end;
  8.          //debugln('TSynCompletion.Validate B Position=',dbgs(Position));
  9. +        if (ItemList.Count = 0) then
  10. +          Cancel(Sender)
  11. +        else
  12.          if Position>=0 then begin
  13.            if Assigned(FOnCodeCompletion) then
  14.            begin
  15. @@ -1886,10 +1889,7 @@ procedure TSynCompletion.Validate(Sender: TObject; KeyChar: TUTF8Char;
  16.              TextBetweenPointsEx[NewBlockBegin, NewBlockEnd, scamEnd] := ItemList[Position];
  17.              TCustomSynEdit(F.CurrentEditor).SetFocus;
  18.            end;
  19. -        end
  20. -        else
  21. -        if (ItemList.Count = 0) then
  22. -          Cancel(Sender);
  23. +        end;
  24.        finally
  25.          EndUpdate;
  26.          EndUndoBlock{$IFDEF SynUndoDebugBeginEnd}('TSynCompletion.Validate'){$ENDIF};
  27.  

I still need to test it, before committing. Just make sure it doesn't break the IDE.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1209
    • Burdjia
Re: TSynCompletion failing when it's empty (bug?)
« Reply #5 on: May 16, 2025, 11:18:06 am »
I've tested your patch and it works.

I would suggest a smaller change though:
Code: Diff  [Select][+][-]
  1. diff syncompletion.pas.orig syncompletion.pas
  2. 1885c1885
  3. <         if Position>=0 then begin
  4. ---
  5. >         if Position>0 then begin
  6. 1902d1901
  7. <         if (ItemList.Count = 0) then
  8.  
  9.  
Either one or the other works but my perfectionism votes for mine. ::)
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11349
  • Debugger - SynEdit - and more
    • wiki
Re: TSynCompletion failing when it's empty (bug?)
« Reply #6 on: May 16, 2025, 11:20:04 am »
Position can be 0, if there is 1 entry. Or whenever the first entry is selected.

At least it should be...

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1209
    • Burdjia
Re: TSynCompletion failing when it's empty (bug?)
« Reply #7 on: May 16, 2025, 11:38:11 am »
Position can be 0, if there is 1 entry. Or whenever the first entry is selected.

At least it should be...
You're right.  I'm doing more testing and there are some issues with my version of the fix:  it doesn't selects items from the list properly.  I needed some time to figure out why yours work and mine doesn't. :-[
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11349
  • Debugger - SynEdit - and more
    • wiki
Re: TSynCompletion failing when it's empty (bug?)
« Reply #8 on: May 16, 2025, 12:43:42 pm »
I am pretty confident may patch solves the problem.

But I think (I hadn't had time to dive in yet) it changes some event triggers in case that events are set, and an empty selection occurs.

Likely that is why the IDE does not crash.
But if the IDE prevents the crash by dealing with that situation inside of some event... Then changing the trigger of those events.... Well that needs to be checked carefully.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1209
    • Burdjia
Re: TSynCompletion failing when it's empty (bug?)
« Reply #9 on: May 30, 2025, 12:50:32 pm »
At the moment I've applied your patch to my project and released a new version of my (very early) IDE.

Sorry for the SPAM. :-[
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11349
  • Debugger - SynEdit - and more
    • wiki
Re: TSynCompletion failing when it's empty (bug?)
« Reply #10 on: May 30, 2025, 02:19:02 pm »
The patch was added to 4.99 in the meantime.

 

TinyPortal © 2005-2018