Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2017-01

Format code on Save

11 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

As the subject lines says. If you set up the Jedi Code Formatting options, is there a way to tell Lazarus to automatically format the unit when you Ctrl+S (Save)? Eclipse IDE does that (and much more fine grained if needed) with Java code, at it is actually super useful, and always results in neatly laid out code. So I was wondering if Lazarus IDE could do something similar with JCF, or would that require patches to accomplish?

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Mattias Gaertner
to Graeme Geldenhuys
Show quoted text 9 lines
Hi, As the subject lines says. If you set up the Jedi Code Formatting options, is there a way to tell Lazarus to automatically format the unit when you Ctrl+S (Save)? Eclipse IDE does that (and much more fine grained if needed) with Java code, at it is actually super useful, and always results in neatly laid out code. So I was wondering if Lazarus IDE could do something similar with JCF, or would that require patches to accomplish?

There is only an event before saving all yet.
I can add an event called before saving a source editor file. Would that help you?

Does the JCF formatter work with bookmarks, folds and breakpoints?

Mattias

Reply by email

Graeme Geldenhuys
to Mattias Gaertner
Mattias Gaertner wrote
I can add an event called before saving a source editor file. Would that help you?

I think a "after save" event is needed instead? Does JCF format code directly from memory (SynEdit's buffer), or does it read and format a file from disk. If the latter, then I guess a "after save" event would indeed be helpful.

Does the JCF formatter work with bookmarks, folds and breakpoints?

The JCF I'm using is pretty old. I'll have to double check the state of the JCF included with Lazarus, but I'm pretty sure its much newer than the one I use.

Would bookmarks, code-folding etc matter.... if JCF reads files from disk?

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Mattias Gaertner
to Graeme Geldenhuys
Show quoted text 8 lines
On 2017-01-25 13:15, Mattias Gaertner via Lazarus wrote: > I can add an event called before saving a source editor file. > Would that help you? I think a "after save" event is needed instead? Does JCF format code directly from memory (SynEdit's buffer), or does it read and format a file from disk. If the latter, then I guess a "after save" event would indeed be helpful.

I just took a quick look at the JCF plugin source: It works on the current source editor and uses ReplaceLines, so it keeps all marks and adds one undo step. Nice.

An 'after save' could be added, but that would mean save, execute jcf, load - loosing the undo stack and possibly moving marks.

[...]

Mattias

Reply by email

Graeme Geldenhuys
to Mattias Gaertner
Mattias Gaertner wrote
on the current source editor and uses ReplaceLines, so it keeps all marks and adds one undo step. Nice.

That is much better indeed. So a 'before save' would then be the useful one.

An 'after save' could be added, but that would mean save, execute jcf, load - loosing the undo stack and possibly moving marks.

No, no, it seems that would not be needed after all. Many thanks for looking at the code to actually see what jcf does.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Mattias Gaertner
to Graeme Geldenhuys
Show quoted text 5 lines
On 2017-01-25 14:15, Mattias Gaertner via Lazarus wrote: > on the current source editor and uses ReplaceLines, so it keeps all > marks and adds one undo step. Nice. That is much better indeed. So a 'before save' would then be the useful one.

I added the event.

uses LazIDEIntf
LazarusIDE.AddHandlerOnSaveEditorFile(@YourEvent);

Mattias

Reply by email

Graeme Geldenhuys
to Mattias Gaertner
Mattias Gaertner wrote 5 lines
I added the event. uses LazIDEIntf LazarusIDE.AddHandlerOnSaveEditorFile(@YourEvent);

A bit late, but thank you for adding that functionality. I've now managed to create my IDE add-on and it works.

But I noticed that with every save (Ctrl+S), the YourEvent gets called twice? Any idea why that is, and if it was by design or a bug?

For example, I added a simple writeln() in my event handler and ran Lazarus from the command line to see the output. I changed a unit and pressed Ctrl+S. The writeln() output is shown twice...

===========================
[lazarus]$ ./lazarus
Hint: (lazarus) [TMainIDE.ParseCmdLineOptions]
PrimaryConfigPath="/home/graemeg/.lazarus"
Hint: (lazarus) [TMainIDE.ParseCmdLineOptions]
SecondaryConfigPath="/etc/lazarus"
Hint: (lazarus) [TBuildManager.SetBuildTarget] Old=x86_64-freebsd-gtk2 New=x86_64-freebsd-gtk2 FPC=True LCL=False
PASS: We found the JCF command
PASS: We found the JCF command
===========================

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Mattias Gaertner
to Graeme Geldenhuys
Show quoted text 10 lines
On 2017-01-27 14:37, Mattias Gaertner via Lazarus wrote: [...] > LazarusIDE.AddHandlerOnSaveEditorFile(@YourEvent); A bit late, but thank you for adding that functionality. I've now managed to create my IDE add-on and it works. But I noticed that with every save (Ctrl+S), the YourEvent gets called twice? Any idea why that is, and if it was by design or a bug?

Check parameter SaveStep.

Mattias

Reply by email

Graeme Geldenhuys
to Mattias Gaertner
Mattias Gaertner wrote
> But I noticed that with every save (Ctrl+S), the YourEvent gets called > twice? Any idea why that is, and if it was by design or a bug? Check parameter SaveStep.

I tried both sefsBeforeWrite and sefsAfterWrite - it made no difference to the issue of the method being called twice.

Also, what does the AddHandlerOnSaveEditorFile()'s "AtLast" parameter mean and do?

I also set the Return result to mrOK.

Here is the whole implementation of the event handler and Register call.

==========================
procedure Register;
begin
  LazarusIDE.AddHandlerOnSaveEditorFile(@uJCFHandler.BeforeSaveCallJCF,
    False);
end;
function TJCFHandler.BeforeSaveCallJCF(Sender: TObject;
   aFile: TLazProjectFile; SaveStep: TSaveEditorFileStep;
   TargetFilename: string): TModalResult;
var
  cmd: TIDECommand;
begin
  SaveStep := sefsBeforeWrite;
  Result   := mrOk;
  cmd := IDECommandList.FindCommandByName('jcfCurrentEditorWindow');
  if Assigned(cmd) then
  begin
    writeln('PASS: We found the JCF command');
    ExecuteIDECommand(Sender, cmd.Command);
  end;
end;
==========================

I'm using Lazarus 1.7 r54157 FPC 3.0.1 x86_64-freebsd-gtk2.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Mattias Gaertner
to Graeme Geldenhuys
Show quoted text 8 lines
[...] > Check parameter SaveStep. I tried both sefsBeforeWrite and sefsAfterWrite - it made no difference to the issue of the method being called twice. Also, what does the AddHandlerOnSaveEditorFile()'s "AtLast" parameter mean and do?

Prepend or append to the list of handlers.

I also set the Return result to mrOK.

Good.

Show quoted text 17 lines
Here is the whole implementation of the event handler and Register call. ========================== procedure Register; begin LazarusIDE.AddHandlerOnSaveEditorFile(@uJCFHandler.BeforeSaveCallJCF, False); end; function TJCFHandler.BeforeSaveCallJCF(Sender: TObject; aFile: TLazProjectFile; SaveStep: TSaveEditorFileStep; TargetFilename: string): TModalResult; var cmd: TIDECommand; begin SaveStep := sefsBeforeWrite;

You set a non var parameter? Think again.

Mattias

Reply by email

Graeme Geldenhuys
to Mattias Gaertner
Mattias Gaertner wrote
> Also, what does the AddHandlerOnSaveEditorFile()'s "AtLast" parameter > mean and do? Prepend or append to the list of handlers.

Ah, thank. That would come in handy if the IDE Options ever have a GUI to manage such actions. eg: Eclipse has that, and that is also where I saw this feature (format code on save).

You set a non var parameter? Think again.

Ah, too many late nights. Thanks for that pointer.

Adding the following to the beginning of my event handler solves the problem.

  if SaveStep = sefsAfterWrite then
    Exit;

Thanks for all your help.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email