Recent

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

WooBean

  • Full Member
  • ***
  • Posts: 187
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: 8926
  • 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: 187
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: 3234
  • 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: 187
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: 8926
  • 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.

 

TinyPortal © 2005-2018