Recent

Author Topic: [forget it] A request or maybe its already there?  (Read 3172 times)

jamie

  • Hero Member
  • *****
  • Posts: 6791
[forget it] A request or maybe its already there?
« on: October 29, 2024, 01:40:14 am »
Feature request or show me where it is?

 At times I am deep in a long list of code in a procedure where the VAR section is out of site! I need to add a variable to my local list of that procedure as I go along but it requires me to scroll up and set it or use the bookmarks so I can return.

 The auto-complete code to do this just does not seem to work well in-place so I have a much better idea that would allow for one to view what is currently listed in the VAR section and add a new variable without leaving the location of my current editing spot.

  It would be nice to use a Hot-Key or control sequence to popup a dial window with the current list of the variables in that block, I suppose we could have tree structure to show the global variable section of the unit, too, or even maybe some local procedures to that block.

  I use Code-Explorer but that does not cut the mustard as it does not allow you to insert a new variable on the fly. Also, it does not really give you a good view of your variables.

 This needs to show the VAR section block and allow you to insert a new variable and check against duplicates etc.

 and when closing the dialog could even elect to auto-insert that new variable in-place where you were!

How a bout it?
« Last Edit: November 05, 2024, 12:20:57 am by jamie »
The only true wisdom is knowing you know nothing

Josh

  • Hero Member
  • *****
  • Posts: 1350
Re: A request or maybe its already there?
« Reply #1 on: October 29, 2024, 01:52:59 am »
Hi

If you put a valid statement with new var name
ie
fred:='gt';

place the cursor on fred, right click->refactoring->declare variable, it then asks where you want to declare it and its type

Alt-O R D

if its already defined you get a message
« Last Edit: October 29, 2024, 01:58:42 am by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: A request or maybe its already there?
« Reply #2 on: October 29, 2024, 02:16:40 am »
Ok, it would be nice to also see the current list of variables there, too.

let's see, create a variable that may or may not exist, who knows, right click, then right click again, hum...

I still would like to see the whole list there and so I can simply insert as if it was a mini editor.

I'll take your suggestions under advisement in any case!

My Right-Click+Refactoring does not show an option to declare a variable..

Sorry.
I still want the to popup window in place that shows the current local list and able to insert what I want.

« Last Edit: October 29, 2024, 02:21:27 am by jamie »
The only true wisdom is knowing you know nothing

Josh

  • Hero Member
  • *****
  • Posts: 1350
Re: A request or maybe its already there?
« Reply #3 on: October 29, 2024, 02:35:50 am »
hi

if i right click the fred part, refactoring, declare variable or ALT+O then press R then press D

if anchordocking then you can see a list of vars in the procedure on the right in code explorer.

if not anchor docking open code explorer

The best way to get accurate information on the forum is to post something wrong and wait for corrections.

440bx

  • Hero Member
  • *****
  • Posts: 4907
Re: A request or maybe its already there?
« Reply #4 on: October 29, 2024, 02:38:19 am »
I often run into the problem you described... here is how I solve it...

First, I often set a bookmark to the beginning of the "var" section.

Second and, most important, I usually have 2 editor windows open side by side.  The left side shows the code part, the right side shows the "var" part.  That way, I only have to switch from one window to the other to declare the variable and, that gives you a full page of already declared variables.  (see the attachement.)

Also, I do the same using the editor I use when I am really coding (instead of just making minor adjustments - usually when testing/debugging - which is when I use the Lazarus editor.)  Effectively, I always 2 editors running with side by side windows showing different parts of a function/procedures.  The main editor usually has several dozen windows open, some belonging to the project I'm working on and many other windows from other projects that I copy/paste code from.

I find having a file showing in two different editing windows to be the most convenient solution because the text isn't jumping around to another location.  Everything stays put, which makes it much easier to deal with.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

LV

  • Full Member
  • ***
  • Posts: 197
Re: A request or maybe its already there?
« Reply #5 on: October 29, 2024, 01:03:02 pm »
I’d like to share my decision. It may seem naive, but it’s convenient for me. I have code blocks containing a long list of variables, which I transferred to *.inc files included using the directive {$I somevarlist.inc}. The source code editor features tabs for the module I'm working on and tabs for the *.Inc files. With just one click of the mouse, I can easily switch between the tabs!

I still want the to popup window in place that shows the current local list and able to insert what I want.
this is also possible

« Last Edit: October 29, 2024, 02:01:48 pm by LV »

n7800

  • Full Member
  • ***
  • Posts: 200
Re: A request or maybe its already there?
« Reply #6 on: October 31, 2024, 09:39:30 pm »
Just to be clear, there are some atypical cases:
  • there can be several "var" sections
  • sections can be separated by large nested procedures
  • they can be very large, especially if they contain array initialization
  • they can contain single-line comments and even multi-line ones

n7800

  • Full Member
  • ***
  • Posts: 200
Re: A request or maybe its already there?
« Reply #7 on: October 31, 2024, 09:44:38 pm »
My Right-Click+Refactoring does not show an option to declare a variable..

You need to install the "Cody" package for it.

440bx

  • Hero Member
  • *****
  • Posts: 4907
Re: A request or maybe its already there?
« Reply #8 on: October 31, 2024, 09:45:20 pm »
@n7800,

All of those are possibilities that are encountered sooner or later.  I find that multiple "var" sections and multi-line comments in "var" sections are fairly common (they are common in my code.) ;)

That's why I like having a second window and using bookmarks.  That way all the movement occurs in that secondary window and not in the main window where I am editing code.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

n7800

  • Full Member
  • ***
  • Posts: 200
Re: A request or maybe its already there?
« Reply #9 on: October 31, 2024, 09:57:08 pm »
By the way, there is a similar function "Source > Complete Code" [Ctrl+Shift+C], which can also create a declaration for a variable. It does this without asking for the type and location, but this is often not needed, so it can be even more convenient.

There is also a function "Search > Find start of Code Block" [Ctrl+Q, M]. It is enough to press several times to end up at the main "begin" at the beginning of the function, before which the "var" section is usually located. To go back, you can use the cursor history ("Jump back") by pressing [Alt+Left] several times.

n7800

  • Full Member
  • ***
  • Posts: 200
Re: A request or maybe its already there?
« Reply #10 on: October 31, 2024, 10:11:01 pm »
I think the best option would be a keyboard shortcut for quickly switching to the "var" section of the current procedure (or switching between them, if there are several).

Unlike dialog/popup windows, it allows you to see the original variables (with comments), edit them, continue using any shortcuts and feature of the editor - completion, scrolling, folding, etc. After editing, you can easily go back using "Jump back" [Alt+Left].

This command can even be placed in the editor toolbar, for quick access with the mouse.

However, the question arises - is it worth creating the same commands for quick transition to other sections (type, const)?
« Last Edit: November 02, 2024, 01:41:50 pm by n7800 »

n7800

  • Full Member
  • ***
  • Posts: 200
Re: A request or maybe its already there?
« Reply #11 on: October 31, 2024, 10:11:55 pm »
By the way, can I ask the author of the topic to choose a better title for the topic? ))

For example: "Feature request: Quick way to declare a variable" or something like that...
« Last Edit: November 02, 2024, 01:41:55 pm by n7800 »

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: A request or maybe its already there?
« Reply #12 on: October 31, 2024, 10:51:22 pm »
Ultimate solution/idea: inline variables + shortcut to move them to the var section

At least Refactoring -> Declare variable should detect the variable name in the current line, without having to place the cursor on the specific word you want as a variable name
« Last Edit: October 31, 2024, 10:57:53 pm by Fibonacci »

mas steindorff

  • Hero Member
  • *****
  • Posts: 553
Re: A request or maybe its already there?
« Reply #13 on: October 31, 2024, 11:33:29 pm »
I tend to always use the same Var name for locals so I have the basics covered by memory. I is an integer, str:string, ftmp :double; ... Then when I need to expand , I add a "lv_" to the name so that code completion can kick in.  When the Var/Const/types are defined outside of the function, I use the two window approach.   
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: A request or maybe its already there?
« Reply #14 on: October 31, 2024, 11:42:20 pm »
At least Refactoring -> Declare variable should detect the variable name in the current line, without having to place the cursor on the specific word you want as a variable name

How about that?

Code: Diff  [Select][+][-]
  1. diff --git a/components/codetools/ide/declarevardlg.pas b/components/codetools/ide/declarevardlg.pas
  2. index 326c7cbc08..103604e36a 100644
  3. --- a/components/codetools/ide/declarevardlg.pas
  4. +++ b/components/codetools/ide/declarevardlg.pas
  5. @@ -137,6 +137,7 @@ var
  6.    IsKeyword: boolean;
  7.    NewExprType: TExpressionType;
  8.    IsSubIdentifier: boolean;
  9. +  x, oldx: integer;
  10.  begin
  11.    Result:=false;
  12.    NewType:='';
  13. @@ -147,6 +148,22 @@ begin
  14.    ExistingDefinition:=CleanFindContext;
  15.    PossibleContexts:=nil;
  16.  
  17. +  oldx := -1;
  18. +  try
  19. +  x := Pos(':=', SourceEditorManagerIntf.ActiveEditor.CurrentLineText)-1;
  20. +  while x > 0 do begin
  21. +    // find first non-space
  22. +    if SourceEditorManagerIntf.ActiveEditor.CurrentLineText[x] <> ' ' then break;
  23. +    dec(x);
  24. +  end;
  25. +  inc(x);
  26. +  // found var name, move cursor onto it
  27. +  if x >= 0 then begin
  28. +    // save old X and return here later
  29. +    oldx := SourceEditorManagerIntf.ActiveEditor.CursorTextXY.X;
  30. +    SourceEditorManagerIntf.ActiveEditor.CursorTextXY := TPoint.Create(x, SourceEditorManagerIntf.ActiveEditor.CursorTextXY.Y);
  31. +  end;
  32. +
  33.    if (ParseTilCursor(Tool,CleanPos,CursorNode,Handled,true,@CodePos)<>cupeSuccess)
  34.    and not Handled then begin
  35.      ErrorNotAtAnIdentifier;
  36. @@ -193,6 +210,10 @@ begin
  37.          ErrorNotAtAnIdentifier;
  38.      end;
  39.    end;
  40. +  finally
  41. +    if oldx <> -1 then
  42. +      SourceEditorManagerIntf.ActiveEditor.CursorTextXY := TPoint.Create(oldx, SourceEditorManagerIntf.ActiveEditor.CursorTextXY.Y);
  43. +  end;
  44.  end;
  45.  
  46.  { TCodyClipboardDeclareVar }
  47.  
« Last Edit: October 31, 2024, 11:51:14 pm by Fibonacci »

 

TinyPortal © 2005-2018