Recent

Author Topic: Is code completion playing dice?  (Read 3545 times)

WooBean

  • Full Member
  • ***
  • Posts: 243
Is code completion playing dice?
« on: March 03, 2023, 03:20:42 pm »
When I use in code editor a dot to see available record fields/functions I see unstable behavior of code completion mechanism. Is it by design?
Code: Pascal  [Select][+][-]
  1. //Lazarus 2.2.4 (rev lazarus_2_2_4) FPC 3.2.2 x86_64-win64-win32/win64
  2. program project1;
  3. {$mode objfpc}{$H+}
  4. {$modeswitch advancedrecords on}
  5. //{$mode delphi}
  6.  
  7. uses sysutils;
  8.  
  9.  
  10. type
  11.   myRec = record
  12.     a:byte;
  13.     x,y:double;
  14.     b:word;
  15.     function foo:string;
  16.     function afoo:string;
  17.     case byte of
  18.     0:(cc:byte);
  19.     1:(dd:int64);
  20.   end;
  21.  
  22.   function myRec.foo:string;
  23.   begin
  24.     result:=self.dd.ToString;
  25.     //after writing a dot after "self" I can see an order in code completion "offer":
  26.     //dd
  27.     //cc
  28.     //y
  29.     //x
  30.     //a
  31.     //afoo
  32.     //b
  33.     //foo
  34.  
  35.     //it was before writing main program code, after second attempt to enter a dot here set an order is as below
  36.     //foo
  37.     //dd
  38.     //cc
  39.     //y
  40.     //x
  41.     //a
  42.     //afoo
  43.     //b
  44.  
  45.     //after reloading Lazarus  attempt to enter a dot here makes an order as below
  46.  
  47.     //a
  48.     //afoo
  49.     //b
  50.     //cc
  51.     //dd
  52.     //foo
  53.     //x
  54.     //y
  55.    
  56.   end;
  57.  
  58.  
  59.   function myRec.afoo:string;
  60.   begin
  61.     result:=self.cc.ToString;
  62.   end;
  63.   var rec:myRec;
  64. begin
  65.  
  66.   rec.a:=1;
  67.   //after writing a dot after "rec" I can see the same order in code completion "offer"
  68.   //foo
  69.   //dd
  70.   //cc
  71.   //y
  72.   //x
  73.   //a
  74.   //afoo
  75.   //b
  76.  
  77.   //after reloading Lazarus order is
  78.   //a
  79.   //afoo
  80.   //b
  81.   //cc
  82.   //dd
  83.   //foo
  84.   //x
  85.   //y
  86.  
  87.   rec.b:=2;
  88.   rec.x:=pi;
  89.   rec.y:=2*pi;
  90.   rec.cc:=5;
  91.   writeln(rec.foo);
  92.  
  93.   readln;
  94. end.
  95.  

I would like to control a way how fields of a record are presented in code completion (at least to choose between order by name or by declaration sequence as it was in Delphi IDE - by popup menu). Is it possible?
« Last Edit: March 03, 2023, 03:33:17 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10010
  • Debugger - SynEdit - and more
    • wiki
Re: Is code completion playing dice?
« Reply #1 on: March 03, 2023, 03:41:41 pm »
Tools > Options: Codetools > Identifier Completion

There are 2 options about "sorting"
- recently used at top
- sort for scope

Try adjusting them. You may not like the "recently used" to be sorted to the top.

Scope likely means: locals first, then class, then unit, ....

As for alphabetic/declaration => no idea.

WooBean

  • Full Member
  • ***
  • Posts: 243
Re: Is code completion playing dice?
« Reply #2 on: March 03, 2023, 04:39:29 pm »
Tools > Options: Codetools > Identifier Completion

There are 2 options about "sorting"
- recently used at top
- sort for scope

Try adjusting them. You may not like the "recently used" to be sorted to the top.

Scope likely means: locals first, then class, then unit, ....

As for alphabetic/declaration => no idea.
Tried all the combinations - now all is sorted by name in each setting - ignoring my intentions, what a pity.
Archaic version of Delphi managed to do it - see a picture.
« Last Edit: March 03, 2023, 04:43:54 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Is code completion playing dice?
« Reply #3 on: March 03, 2023, 07:14:10 pm »
If you set - [X] Show recent identifier at top - it is forgotten when you restart Lazarus.

Lazarus probably cannot order items by declaration, only alphabetically + by scope.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

WooBean

  • Full Member
  • ***
  • Posts: 243
Re: Is code completion playing dice?
« Reply #4 on: March 04, 2023, 09:43:33 am »
Regarding "Scope likely means: locals first, then class, then unit, ...." I think that code completion during writing code in IDE with variable identifier of a record (which always starts by pressing "." ie. "recName.") should work only with two options - ordered by name or by declaration/definition. Order by scope in that case should mean definition order. Such approach I can see in Delphi and it is good.

What is the aim to propose idenitifiers for fields (and functions/procedures) of record in that situation in an order based on previous actions of a programmer during writing code? Pure playing dice? 
« Last Edit: March 04, 2023, 09:45:10 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10010
  • Debugger - SynEdit - and more
    • wiki
Re: Is code completion playing dice?
« Reply #5 on: March 04, 2023, 09:53:27 am »
"scope ..." was an assumption on my side. I did not check if it is so.

But that aside.

Currently there is simply no option how to sort identifiers that are in the same group by "sort for scope". Or at least I haven't seen it.
IMHO both alphabetically and by-declaration are valid ideas.

I have no idea how much work it would be though. You can add a feature request on the issue tracker, but that might only be for the long term....

You could look at code completion (IIRC ide/sourceeditor.pp is a good start point for the search). The list is retrieved from codetools (package). I don't know if the sorting can be skipped, and if in that case maybe they come in the desired order.... If not, it will be a lot of work.

WooBean

  • Full Member
  • ***
  • Posts: 243
Re: Is code completion playing dice?
« Reply #6 on: June 12, 2023, 11:22:13 am »
Next steps ...

I have managed (?) to understand how codetools work in aspect of identifier completion. The list of identifiers is generated from source code buffer in context of cursor position. Initially this list is ordered in reversed order of apperance (ie. a record defined as record a,b,c:integer; end is diveded to fields ordered c,b,a). But this list is then ordered/sorted according by two (or three - including parameters) settings - sort by history and sort by scope  (if they are set to TRUE). There is yet one sorting always active - by alphabetic order - no change setting possible.
I would like to get list of idendifiers prepared for displaying for a hint / popup window ordered as they are defined in source code. Just to get it possible I prepared a first step (changes in code tools). Can someone judge whether it is OK?
Note: Lazarus 2.2.4
« Last Edit: June 12, 2023, 11:38:13 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10010
  • Debugger - SynEdit - and more
    • wiki
Re: Is code completion playing dice?
« Reply #7 on: June 12, 2023, 12:34:34 pm »
I have taken a look at your file. It describes how to add a new checkbox to the options, and how to store that checkbox.

I assume
Code: Pascal  [Select][+][-]
  1. SortForAlphaOrder = true
is for the current order?
And switching it off, would be the new order?

That is ok, but personally I would suggest to make "SortByDeclaration" as now option. That way to change away from the current behaviour people will turn an option on (rather than off).

In any case, one important bit.
Code: Pascal  [Select][+][-]
  1. XMLConfig.SetDeleteValue('CodeToolsOptions/IdentifierCompletion/SortForAlphaOrder',
  2.       FIdentComplSortForAlphaOrder, TRUE);
The "TRUE" means that is the default. If the value is true, then nothing will be written into the xml file.
And the "XMLConfig.GetValue(...., TRUE)" must then have the same default, so if the XML has no value, "true" will be read.

The "FIdentComplSortForAlphaOrder" must also  be initialized (that should happen for either true or false anyway). There should be a method where all the options are initialized.

===> The important bit is: If the setting is at its default => the xml should not contain anything.

For all else, your suggestions look ok.
Of course you still need the code that do the actual work.



If you have further suggestions, please provide them as patch. Even if you just put comments (or dummy code) in there.

If I apply a patch, I can open the files, and have tools to navigate to the lines that changed.

WooBean

  • Full Member
  • ***
  • Posts: 243
Re: Is code completion playing dice?
« Reply #8 on: June 12, 2023, 01:31:55 pm »
@Martin
Quote
..
That is ok, but personally I would suggest to make "SortByDeclaration" as now option. That way to change away from the current behaviour people will turn an option on (rather than off).
It is a little complicated as just to have effect of displaying identifiers "by declaration" we shoud unmark both "Sort by history" and "Sort by scope" and unmark (now not existing option "Sort alphabetically").

When  "Sort alphabetically" option will be set to FALSE any setting "Sort by history" or "Sort by scope" to TRUE will/may produce something not expected in list of identifiers.

Too much freedom hurts (joke). Maybe some hint in proper area (Identifier Cempletion/Sorting) would be a solution.

Thanks for your time.

« Last Edit: June 12, 2023, 01:51:54 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10010
  • Debugger - SynEdit - and more
    • wiki
Re: Is code completion playing dice?
« Reply #9 on: June 12, 2023, 02:19:09 pm »
@Martin
Quote
..
That is ok, but personally I would suggest to make "SortByDeclaration" as now option. That way to change away from the current behaviour people will turn an option on (rather than off).
It is a little complicated as just to have effect of displaying identifiers "by declaration" we shoud unmark both "Sort by history" and "Sort by scope" and unmark (now not existing option "Sort alphabetically").

When  "Sort alphabetically" option will be set to FALSE any setting "Sort by history" or "Sort by scope" to TRUE will/may produce something not expected in list of identifiers.

"Sort by Scope" can always be mixed with any other sort order (assuming that in (at least some) scope(s) there is more than one identifier.


"Sort by history", depends. There always can be identifiers that are not yet in history. Though, of course eventually the remaining sort order will disappear.... Unless an option makes the history limited to N entries. (Usually the last ~3 entries are common to be used again soon).


"Sort by declaration" of course includes some scope anyway.
Well, it has too.
Without scope, the first possible identifier (even in a different unit / e.g. in the base class) would be the first declared identifier... not really desirable.

So when enabling ""Sort by declaration" then the "Sort by scope" checkbox has to be disabled. (it can't be changed by the user.)

Or alternatively (maybe better), the current "Sort by Scope" becomes a drop-down (or radio-group): "Alphabetically", "Scope/Alphabetically", "Scope/Declaration".

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10010
  • Debugger - SynEdit - and more
    • wiki
Re: Is code completion playing dice?
« Reply #10 on: June 12, 2023, 02:21:31 pm »
Note that making it a drop down (or radio), might change how the current settings are stored.

That is the boolean would be removed, and become part of an enum. => That can be done. However, before going to deep into that, I would suggest getting the actual functionality.

WooBean

  • Full Member
  • ***
  • Posts: 243
Re: Is code completion playing dice?
« Reply #11 on: June 13, 2023, 06:52:25 pm »
I am pleased to announce ... (sorry for too high tone) I have reached the goal to display identifier completion (part of CodeTools) using items declaration order.
How to do it is presented in attached files. Technically I tried to follow coding style I met in needed units and make as less addings as possible.

I have no experience how to share/publish a solution for changing Lazarus. If this will be found useful I can post modified units files of CodeTools package (Lazarus 2.2.4).

WooBean
 



 
« Last Edit: June 13, 2023, 09:35:23 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10010
  • Debugger - SynEdit - and more
    • wiki
Re: Is code completion playing dice?
« Reply #12 on: June 13, 2023, 09:30:00 pm »
First of all, I will be away for a few days. I may be on the forum or not, but I likely wont deal with any code ...


Ideally a patch, or better yet a git merge request.

Which version of Lazarus do you have? Any new code should really be based on the git main branch (aka "trunk") => you can get that with FpcUpDeluxe.

If you do FpcUpDeluxe then you also have a git dir, in wich you can edit the files, and "git diff" will give you a patch, that you can post.

(Other tools like WinMerge, can also produce patches: you do need the unmodified file too, so the files can be compared)





WooBean

  • Full Member
  • ***
  • Posts: 243
Is code completion playing dice?
« Reply #13 on: June 19, 2023, 07:27:14 pm »
Issue created at GitLab and should be considered SOLVED (thanks to Martin).
Lazarus users may see final effect when trunk becomes stable, I hope.

See https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/40332

Note: <SOLVED> removed
« Last Edit: June 20, 2023, 06:55:46 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10010
  • Debugger - SynEdit - and more
    • wiki
Re: Is code completion playing dice?
« Reply #14 on: June 20, 2023, 06:37:49 pm »
From the issue report.
Quote
'To reverse the order. This should be done in TIdentifierList.CompareIdentListItems' - I think there is too late - items order is partially predefined before CompareIdentListItems is used for sorting. (BTW - as it is proceeded during Application.OnIdle event I have difficulties to follow control flow in debugged Lazarus IDE).
I expect some clues from Code Explorer Tab - it can display structures much better than code/identifier completion is able now. Yet remark, if source code contains errors even Code Explorer becomes stupid.

Afaik Code explorer also uses codetools.

"OnIdle" => That would be the pascal parsing? Or what?
The actual collecting of the idents happens when you press ctrl-space.
Well ok, if you type a dot, and wait => then it is in a TTimer. (not Idle but similar)

If you want it being called without having to interact with the IDE => write a testcase.


I am not 100% sure what final order you want. But you do have the position in the source. So that might help. "Item.Node.StartPos"

A little help with debugging, if you are in CompareIdentListItems and want to inspect an item.
Or anywhere were you have a NODE and a TOOL.

In Lazarus 2.3:
Code: Text  [Select][+][-]
  1. @Item1.Tool.Src[Item1.FNode.StartPos]
In older Lazarus you need to typecast this to pchar

Only in 2.3
Code: Text  [Select][+][-]
  1. Item1.Tool.Src[Item1.FNode.StartPos..Item1.FNode.EndPos]

 

TinyPortal © 2005-2018