JCF Incorrect indenting for try/finally lazarus / 5-Lazarus Extra Components ---------------------------------------------------------------------- award65 · 2024-10-26 09:16 I've been experimenting with Jedi Code Formatter, trying to find the options that suit me. My personal preference is what in the C world is called "Whitesmiths" style - begin/end pairs are indented at the same level as the block they contain, which to me (religious war coming...) makes sense as it matches the Pascal language definition. Anyway, this can be achieved with JCF using the "Extra indent for being/end inside procedures option", with this result: procedure SaveYamlObject(AEmitter: TYamlEmitter; const AName: string;   AValue: TOTPersistent); begin   AEmitter.ScalarEvent('', '', AName, True, False, yssPlainScalar);   if Assigned(AValue) then     begin     AValue.SaveToYaml(AEmitter);     end   else     begin     AEmitter.MappingStartEvent('', '', False, ympFlowMapping);     AEmitter.MappingEndEvent;     end; end; The downside is that it also messes up try/finally/except blocks, producing this....     emitter := TYamlEmitter.Create;       try       stream := TStringStream.Create;       emitter.SetOutput(stream);       yamlVer.Initialize;       emitter.StreamStartEvent;       emitter.DocumentStartEvent(yamlVer, nil, True);       owning.SaveToYaml(emitter);       emitter.DocumentEndEvent(True);       emitter.StreamEndEvent;       yamlText := stream.DataString;       finally       emitter.Free;       end; which should be formatted as:     emitter := TYamlEmitter.Create;     try       stream := TStringStream.Create;       emitter.SetOutput(stream);       yamlVer.Initialize;       emitter.StreamStartEvent;       emitter.DocumentStartEvent(yamlVer, nil, True);       owning.SaveToYaml(emitter);       emitter.DocumentEndEvent(True);       emitter.StreamEndEvent;       yamlText := stream.DataString;     finally       emitter.Free;       end; (ie the try/finally should NOT be indented). The cuplrit is in Indenter.pas:   // IndentBeginEnd option to indent begin/end words a bit extra   if FormattingSettings.Indent.IndentBeginEnd then   begin     if (pt.TokenType in [ttTry, ttExcept, ttFinally, ttBegin, ttEnd]) and InStatements(pt) then     begin       // filter out the begin/end that starts and ends a procedure       if not pt.HasParentNode(nBlock, 2) then       begin         Result := Result + FormattingSettings.Indent.IndentBeginEndSpaces;       end;     end;   end; Is there any good reason why ttTry, ttExcept and ttFinally are being indented here? This looks like a simple fix for me to make, but if I want a PR to be accepted for this should there be a new option added so that formatting for any existing users of this option won't be affected? Alistair. ---------------------------------------------------------------------- TRon · 2024-10-26 09:20 Quote from: award65 on October 26, 2024, 11:16:44 am...which to me (religious war coming...) makes sense as it matches the Pascal language definition. No flames from me (everyone has his/her own preferred style, and as long as I don't have to work with it  ) but it would have made a lot more sense if you would have used code-tags ---------------------------------------------------------------------- rvk · 2024-10-26 09:50 Quote from: award65 on October 26, 2024, 11:16:44 am Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SaveYamlObject(AEmitter: TYamlEmitter; const AName: string;  AValue: TOTPersistent);begin  AEmitter.ScalarEvent('', '', AName, True, False, yssPlainScalar); I'm just curious. If you say it matches pascal definition to indent begin, why did you not indent the begin below the procedure (highlighted line)? No, I'm not a fan of indenting begin itself. ---------------------------------------------------------------------- Lansdowne · 2024-10-26 10:00 I do not use any auto formatters, but I  don't think it's reasonable to call this option "Incorrect". Comparing your if...then...else example with the try..finally example,  'try' performs the dual role of  'if x then' and 'begin'. 'finally' performs the role of 'end' then 'else' then 'begin'. Then you have 'end' in both examples. The option gives priority to 'try' playing the role of 'begin', whereas you would wish it to give priority to its role as introducing the block, equivalent to 'if...then'. Personally I would see your preferred layout as "incorrect" because the final 'end;' is at an indent level that does not show a 'begin' at the same level N lines above.  So given your wish to pull out the 'try' and 'finally' lines, I would expect to pull out the 'end;' as well. And no, I wouldn't use code tags here, because I wouldn't want the code tag to change the way it presents the code under discussion. ---------------------------------------------------------------------- DomingoGP · 2024-10-26 16:32 Quote from: award65 on October 26, 2024, 11:16:44 amIs there any good reason why ttTry, ttExcept and ttFinally are being indented here? consider this code, the try and finally should have the same identation as the end in my opinion. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if condition then    try    d:=1;    finally    d:=2;    end;  Quote from: award65 on October 26, 2024, 11:16:44 amThis looks like a simple fix for me to make, but if I want a PR to be accepted for this should there be a new option added so that formatting for any existing users of this option won't be affected? Yes, at least one new option has to be introduced to maintain compatibility with the current code and I am not yet sure if more are needed to cover all corner cases. It's not a simple fix, there are many options to format the code, In your opinion, is this code format correct? I believe this can be achieved with just one new option. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure rr;begin  if condition then    begin    a := -1;    try      a := 0;  finally      a := 1;    end;    end;   if condition then  try    a := 0;  finally    a := 1;  end;end; ---------------------------------------------------------------------- award65 · 2024-10-27 09:05 Firstly, my apologies for not using code tags :-( I would format the example block as: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure rr;begin  if condition then    begin    a := -1;    try      a := 0;  finally      a := 1;      end;    end;   if condition then    try    a := 0;    finally      a := 1;      end;  The difference is I would indent the final "end" of the try/finally/except to align with the code in the finally/except clause. Many years ago when I was writing the internal Delphi coding standard at a previous job, we wrestled with how this should be formatted. Our conclusion at the time was that the implementers of try/finally/except f***ed up and chose an "un-Pascal" syntax by having a trailing "end" with no matching begin. We stuck with begin/end pairs always being indented to match the code they enclosed, and in the few cases where the language has an un-paired "end", we indented that to align with the code as well. And I would never write code like the second "if" - anything more complex than a simple statement would always be inside a begin/end pair. I'm happy to introduce another option to prevent the indenting of try/finally/except when the "Extra indent for begin/end inside procedures" is selected, but I would still contend that this is a bug in existing behaviour, as the option as listed in the UI says nothing about indenting try/finally/except in the first place... ---------------------------------------------------------------------- DomingoGP · 2024-10-27 11:38 I have the changes to support this style ready, I needed 4 new options to add to the section of the configuration file. False 2 False False Before committing I want to test it more and include the modifications in the lazarus GUI, but if you want to test it I include the patch here. Regards Domingo ---------------------------------------------------------------------- BrunoK · 2024-10-27 13:45 How / where does one parametrize JCF to get the following code : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure rr;begin  if condition then  try    a := 0;  finally    a := 1;  end;end;to be reformatted to : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure rr;begin  if condition then    try      a := 0;    finally      a := 1;    end;end;It seems that it insists aligning, when there is no begin, the try below the if. The column of end of the try .. finally ... end; should be logically at the same position as the try, like is the case for the case structure block. ---------------------------------------------------------------------- DomingoGP · 2024-10-27 14:06 Quote from: BrunoK on October 27, 2024, 02:45:06 pmHow / where does one parametrize JCF to get the following with the patch attached applied, you must set Code: [Select] True in the JCFsettings.cnf Quote from: BrunoK on October 27, 2024, 02:45:06 pmThe column of end of the try .. finally ... end; should be logically at the same position as the try, like is the case for the case structure block. I also thing the same, but the OP preffers the end aligned with the code. The option controls this. Code: [Select] False ---------------------------------------------------------------------- Thaddy · 2024-10-27 14:12 Quote from: BrunoK on October 27, 2024, 02:45:06 pmto be reformatted to : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure rr;begin// block start 1  if condition then//block start 2    try//block start 3      a := 0;    finally // block 3      a := 1;    end;//end block 3   //implied block 2 condition resolving not existing else, if block has no else.end;// block start 1 That is actually preferred by many. My comments explain how it should be done. ---------------------------------------------------------------------- BrunoK · 2024-10-27 16:53 Now starting with : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure rr;begin  if condition then  try  a := 0;  finally  a := 1;  end;end;Ctrl+d ends up with : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure rr;begin  if condition then      try      a := 0;      finally      a := 1;      end;end;What else should I change ? ---------------------------------------------------------------------- DomingoGP · 2024-10-27 17:04 I guess you need. Code: [Select] False 2 if this not work please attach your JCFSettings.cfg ---------------------------------------------------------------------- BrunoK · 2024-10-27 17:21 Quote from: DomingoGP on October 27, 2024, 06:04:24 pmif this not work please attach your JCFSettings.cfg ---------------------------------------------------------------------- DomingoGP · 2024-10-27 17:34 I modifed lines 27 and 43 and works fine for me. ---------------------------------------------------------------------- BrunoK · 2024-10-27 17:43 Quote from: DomingoGP on October 27, 2024, 06:34:10 pmI modifed lines 27 and 43 and works fine for me.That's it. Thank you very much. Bruno. ---------------------------------------------------------------------- DomingoGP · 2024-10-27 18:16 You are welcome! ---------------------------------------------------------------------- award65 · 2024-10-28 08:42 Hi Domingo, I've applied your patch (had to do it manually as a) I'm on Windows, and b) I'm using the current release Lazarus and not a dev version...). After setting IndentEndTryBlockAsCode and IndentExtraOrphanTryBlocks to True, I'm now getting the results I was wanting for the formatting of try/finally etc... Brilliant response, thanks, Alistair. ---------------------------------------------------------------------- DomingoGP · 2024-10-28 10:18 I'm glad the patch is working for you. Just now I made a commit in the main branch of lazarus with the changes. Regards Domingo