Recent

Author Topic: [SOLVED] Ability to move current line up or down?  (Read 4555 times)

knuckles

  • Full Member
  • ***
  • Posts: 122
[SOLVED] Ability to move current line up or down?
« on: February 23, 2018, 08:27:11 pm »
As the title, is it possible in the editor to move the line where the caret is up or down?

In Visual Studio for example I believe Alt+Up and Alt+Down will move the line up and down respectively in the editor. This would be a really useful feature of the SynEdit / Lazarus editor if we could do this, if this is already possible then please advise how to do so :)
« Last Edit: February 24, 2018, 02:53:21 pm by knuckles »

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Ability to move current line up or down?
« Reply #1 on: February 23, 2018, 09:29:47 pm »
Like CTRL + Arrow up and CTRL + Arrow down?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Ability to move current line up or down?
« Reply #2 on: February 23, 2018, 09:49:19 pm »
I bet he means something like
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnUpClick(Sender :TObject);
  2. begin
  3.   SynEdit1.Lines.Exchange(SynEdit1.CaretY-1, SynEdit1.CaretY-2 );
  4.   SynEdit1.CaretY := SynEdit1.CaretY - 1;
  5. end;
  6.  
  7. procedure TForm1.btnDownClick(Sender :TObject);
  8. begin
  9.   SynEdit1.Lines.Exchange(SynEdit1.CaretY-1, SynEdit1.CaretY );
  10.   SynEdit1.CaretY := SynEdit1.CaretY + 1;
  11. end;  

which is easy to wrap it in an IDE addon......... be right back
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Ability to move current line up or down?
« Reply #3 on: February 23, 2018, 10:34:53 pm »
Ah, now I get it.
Can't you use Editor Macros for that?

EditorMacros.xml in your config directory:
(Ctrl+Alt+Up is 1 line up and Ctrl+Alt+Down is 1 line down)

The upside of this method is that indenting is automatically adjusted which isn't the case with SynEdit1.Lines.Exchange I think.

Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <CONFIG>
  3.   <EditorMacros Count="2">
  4.     <Macro1 Name="MoveSelectedLineUp">
  5.       <Code Value="begin
  6. ecSelectLine;
  7. ecSelDown;
  8. ecSelLineStart;
  9. ecCut;
  10. ecUp;
  11. ecPaste;
  12. ecUp;
  13. end.
  14. "/>
  15.       <KeyA Key1="38" Shift1="5"/>
  16.     </Macro1>
  17.     <Macro2 Name="MoveSelectedLineDown">
  18.       <Code Value="begin
  19. ecSelectLine;
  20. ecSelDown;
  21. ecSelLineStart;
  22. ecCut;
  23. ecDown;
  24. ecPaste;
  25. ecUp;
  26. end.
  27. "/>
  28.       <KeyA Key1="40" Shift1="5"/>
  29.     </Macro2>
  30.   </EditorMacros>
  31. </CONFIG>
« Last Edit: February 23, 2018, 10:38:21 pm by rvk »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Ability to move current line up or down?
« Reply #4 on: February 23, 2018, 10:51:44 pm »
Find attached a package that installs two extra commands to move the line under the cursor up or down it is written fast and tested little, but if you compile it in lazarus you will find two commands under the source menu (the two commands before the last) and have the shortcuts ctrl+alt+up,ctrl+alt+Down which can be changed in the application options I guess, I did not test it.
TODO (some one else I guess)
1) auto enable/disable depending on the cursor position
2) add undo/redo steps in the editor.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

knuckles

  • Full Member
  • ***
  • Posts: 122
Re: Ability to move current line up or down?
« Reply #5 on: February 24, 2018, 02:51:17 pm »
Awesome taazz ;)

Would love to see this officially implemented in future Lazarus/Synedit releases.

knuckles

  • Full Member
  • ***
  • Posts: 122
Re: Ability to move current line up or down?
« Reply #6 on: February 24, 2018, 02:53:10 pm »
Have not tried your way rvk but I did look before at the editor macro shortcuts, never thought of editing it myself as you have shown.

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: [SOLVED] Ability to move current line up or down?
« Reply #7 on: February 24, 2018, 03:16:53 pm »
I've also recorded the macro first. Ctrl+Shift+R is record on/off. After that you can look in View>Editor Macros to see how they look. And they worked fine. I moved them to IDE and showed you the file so it's easier to put them in.

I do find the macro method more intuitive because if you move a line up and above the block, the indentation is directly adjusted automatically. So you don't need to adjust it.

If you begin with this (highlighted line to be moved up)
Code: Pascal  [Select][+][-]
  1. // other line
  2. try
  3.   // line to move
  4. finally
  5. end;

Using the Lazarus Add-in you'll get this if you move the line from inside the try to above the try (I think, I haven't tried the Add-in).
Code: Pascal  [Select][+][-]
  1. // other line
  2.   // line to move
  3. try
  4. finally
  5. end;

And with the macro I got this
Code: Pascal  [Select][+][-]
  1. // other line
  2. // line to move
  3. try
  4. finally
  5. end;

So, if the method of taazz is going to make it as official "move-line" feature, it would also need to adjust the indentation automatically like it is done during normal copy-methods (and macro method).

And now that I think of it... this should be made that you can also do this with an entire block. So you select a complete block of code and press Ctrl+Alt+Up to move it up (and adjusting the indentation accordingly). Moving a complete block would even be more useful than one line (although I always use cut/paste).

In notepad++ this already works for Line and blocks with Ctrl+Shift+Up/Down.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: [SOLVED] Ability to move current line up or down?
« Reply #8 on: February 24, 2018, 04:58:36 pm »
Look at http://wiki.lazarus.freepascal.org/Editor_Macros_PascalScript

with this you can write a macro that:
- does not need to use the clipboard
- can detect if a selection exists, and act according

knuckles

  • Full Member
  • ***
  • Posts: 122
Re: [SOLVED] Ability to move current line up or down?
« Reply #9 on: February 24, 2018, 07:44:17 pm »
Agreed, I just went to move an entire block (about 4 lines of code) up and it only moved the active line up, ctrl+z (undo) clearly doesn't work but was to be expected and already mentioned by taazz.

In regards to the indentation I never considered this at the time of writing, but yes I also agree Editor Macros are most likely the ideal way to go, would love to see this feature integrated in one of the future Lazarus releases :)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: [SOLVED] Ability to move current line up or down?
« Reply #10 on: February 24, 2018, 08:20:47 pm »
Patch (on mantis) would be welcome (ping me if there is no reaction to such a patch).

Should not be much work. But must include adding IDE shortcuts (no defaults needed, just entries for config)

See svn revision: 50091 SynEdit/IDE: zoom via keyboard
This shows how to add the shortcuts.

ecSwapLineUp
ecSwapLineDown

or similar.

Don't call it "move", because that is too easy mistaken with cursor movement, or scroll movement....

If you like you can make 2 pairs
ecSwapLineUp  // ignore selection
ecSwapSelectedLinesUp

Better names can be proposed....

------------
About undo.
Do NOT access the synedit.lines object.
Use TextBetweenPoints and similar.

Or better use FTheLinesView.EditInsert and FTheLinesView.EditDelete and similar.

Those take care of undo.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: [SOLVED] Ability to move current line up or down?
« Reply #11 on: November 04, 2019, 12:47:36 pm »
Lazarus trunk 2.1 now has line-up/down/duplicate keys.

 

TinyPortal © 2005-2018