Recent

Author Topic: [Needs work] Problem with Jedi Code Format  (Read 1782 times)

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
[Needs work] Problem with Jedi Code Format
« on: September 30, 2022, 06:50:44 pm »
Lazarus >= 2.2

Could someone test the formatting of the following code with Ctrl+D on selected lines 15 to 17.
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. { JCF code formatting }
  4.  
  5. {$mode ObjFPC}{$H+}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes, SysUtils;
  11.  
  12. // implementation // Comment out of this line damages formatting of
  13.                   // following selection
  14.  
  15. procedure ini_dummy;  // In the editor, Select from this line
  16. begin
  17. end;                  // to this line then Ctrl+D (JCF) locally on the selection
  18.                       // -> the  procedure ini_dummy; line 15 disappears
  19. end.

I ask because, either I may have made a mistake while adding "features ..." to my JCF, or the problem is in the releases and some bug correction should be envisaged.
« Last Edit: October 01, 2022, 10:32:28 am by BrunoK »

dseligo

  • Hero Member
  • *****
  • Posts: 1180
Re: Problem with Jedi Code Format
« Reply #1 on: September 30, 2022, 09:53:25 pm »
I tried in Lazarus 2.2.2 (rev lazarus_2_2_2) FPC 3.2.2 x86_64-win64-win32/win64.

Line 18 is deleted, this one:
Code: Pascal  [Select][+][-]
  1.                       // -> the  procedure ini_dummy; line 15 disappears

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Problem with Jedi Code Format
« Reply #2 on: October 01, 2022, 02:50:54 am »
I tried in Lazarus 2.2.2 (rev lazarus_2_2_2) FPC 3.2.2 x86_64-win64-win32/win64.
Line 18 is deleted, this one:
Same thing in version 2.2.4.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Problem with Jedi Code Format
« Reply #3 on: October 01, 2022, 06:21:07 am »
When I try to parse the whole file I get (yeah, to be expected per the comment in the code...):

Code: Text  [Select][+][-]
  1. ./lazforum/60771/Unit2.pas(16,5) Error Exception TEParseError  Unexpected token, expected "IMPLEMENTATION"
  2. Near BEGIN

As far as Lazarus version, I don't use the IDE, but I do use "Jedi Code Format" JCF from the command line (which is in the Lazarus sources).

I'm using JCF built from Lazarus gitlab main.
Code: Text  [Select][+][-]
  1. $ cat ide/revision.inc
  2. // Created by Svn2RevisionInc
  3. const RevisionStr = 'main-2_3-2072-gdd3cb67d40';

When I add the missing implementation I get:

Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. { JCF code formatting }
  4.  
  5.     {$mode ObjFPC}{$H+}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes, SysUtils;
  11.  
  12. implementation // Comment out of this line damages formatting of
  13.  // following selection
  14.  
  15. procedure ini_dummy;  // In the editor, Select from this line
  16.   begin
  17.   end;                  // to this line then Ctrl+D (JCF) locally on the selection
  18.  // -> the  procedure ini_dummy; line 15 disappears
  19. end.

Granted, I don't have my settings tuned for comment alignment, and I do have my settings to remove blank lines...

But I don't see that it removed lines?

Or am I missing some from the original post on this topic?
« Last Edit: October 01, 2022, 06:25:14 am by Bogen85 »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Problem with Jedi Code Format
« Reply #4 on: October 01, 2022, 07:52:04 am »
Or am I missing some from the original post on this topic?
Yes: "...with Ctrl+D on selected lines 15 to 17".

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Problem with Jedi Code Format
« Reply #5 on: October 01, 2022, 08:16:26 am »
Or am I missing some from the original post on this topic?
Yes: "...with Ctrl+D on selected lines 15 to 17".

Ok... I don't run the GUI so I don't know how is going to work on lines where it can't parse the whole file... (since the whole file in question can't be parsed with interface commented out...)
« Last Edit: October 01, 2022, 08:18:16 am by Bogen85 »

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
[Needs work] Re: Problem with Jedi Code Format
« Reply #6 on: October 01, 2022, 10:30:24 am »
@dseligo, @ASerge

Thanks for the replies.

Will see if something can be done while updating my "features...".

DomingoGP

  • Jr. Member
  • **
  • Posts: 57
Re: [Needs work] Problem with Jedi Code Format
« Reply #7 on: October 01, 2022, 04:10:49 pm »
Yes its a bug.

While I was looking to fix this bug, I found that JCF leaks memory when it finds errors in the code to be formatted.
I have a patch almost ready that I will send to the issue tracker when it is ready and then I will send another one fixing this problem.

In the meantime if anyone wants to fix this bug here are the changes to fix it (patch copied from TortoiseSVN not sure if it can be applied directly). The final patch will probably not be identical, I will move the StrTrimLastEof function to jcfStringUtils.

Regards
Domingo

Code: [Select]
diff --git a/components/jcf2/IdePlugin/lazarus/jcfidemain.pas b/components/jcf2/IdePlugin/lazarus/jcfidemain.pas
index 3c151210ff..e79940a621 100644
--- a/components/jcf2/IdePlugin/lazarus/jcfidemain.pas
+++ b/components/jcf2/IdePlugin/lazarus/jcfidemain.pas
@@ -230,6 +230,26 @@ begin
   LazarusIDE.DoOpenIDEOptions(TfFiles);
 end;
 
+function StrTrimLastEndOfLine(const aStr:string):string;
+var
+  len:integer;
+  c:char;
+begin
+  len:=length(aStr);
+  // TRIM right spaces
+  //  while (len>0) and (aStr[len]=' ') do
+  //    dec(len);
+  if (len>0) and (aStr[len]=#10) then
+  begin
+    dec(len);
+    if (len>0) and (aStr[len]=#13) then
+      dec(len);
+    result:=Copy(aStr,1,len);
+  end
+  else
+    result:=aStr;
+end;
+
 procedure TJcfIdeMain.DoFormatSelection(Sender: TObject);
 var
   srcEditor: TSourceEditorInterface;
@@ -252,7 +272,7 @@ var
   BlockBegin, BlockEnd: TPoint;
   fcConverter: TConverter;
   lineStartOffset,lineEndOffset: integer;
-  wi: integer;
+  wi,EndY: integer;
   outputstr: string;
 begin
   if (SourceEditorManagerIntf = nil) or (SourceEditorManagerIntf.ActiveEditor = nil) then
@@ -278,6 +298,7 @@ begin
       while (wI > 1) and (fcConverter.OutputCode[wI] in [#10, #13, ' ']) do
         Dec(wI);
       outputstr := Copy(fcConverter.OutputCode, 1, wI);
+//ShowMessage('['+outputstr+']'+IntToStr(BlockBegin.Y)+'  '+IntToStr(BlockEnd.Y));
       DiffMergeEditor(srcEditor,outputstr,BlockBegin.Y,BlockEnd.Y);
     end
     else
@@ -286,15 +307,25 @@ begin
       BlockBegin := srcEditor.BlockBegin;
       BlockBegin.X := 1;     // full lines.
       BlockEnd := srcEditor.BlockEnd;
+      EndY:=BlockEnd.Y;
       if BlockEnd.X > 1 then
-        BlockEnd.Y := BlockEnd.Y + 1;
+        BlockEnd.Y := BlockEnd.Y + 1
+      else
+      begin
+        if EndY>1 then
+          EndY:=EndY-1;
+      end;
       BlockEnd.X := 1;
       srcEditor.SelectText(BlockBegin, BlockEnd); //extend selection to full lines.
       fcConverter.InputCode := srcEditor.GetText(True);  // only selected text.
       fcConverter.GuiMessages := true;
       fcConverter.ConvertUsingFakeUnit;
       if not fcConverter.ConvertError then
-        DiffMergeEditor(srcEditor,fcConverter.OutputCode,BlockBegin.Y,BlockEnd.Y);
+      begin
+        outputstr:=StrTrimLastEndOfLine(fcConverter.OutputCode);
+//ShowMessage('['+outputstr+']'+IntToStr(BlockBegin.Y)+'  '+IntToStr(EndY));
+        DiffMergeEditor(srcEditor,outputstr,BlockBegin.Y,EndY);
+      end;
     end;
   finally
     fcConverter.Free;

« Last Edit: October 01, 2022, 04:14:32 pm by DomingoGP »

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: [Needs work] Problem with Jedi Code Format
« Reply #8 on: October 01, 2022, 07:04:46 pm »
JCF? only if you use the Lazarus IDE.
Specialize a type, not a var.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: [Needs work] Problem with Jedi Code Format
« Reply #9 on: October 01, 2022, 07:44:50 pm »
JCF? only if you use the Lazarus IDE.

Not sure what you mean. I use JCF from the command line. The command line project for JCF is in the Lazarus distribution.

DomingoGP

  • Jr. Member
  • **
  • Posts: 57
Re: [Needs work] Problem with Jedi Code Format
« Reply #10 on: October 01, 2022, 08:55:15 pm »
Quote
JCF? only if you use the Lazarus IDE.

Memory leaks both command line JCF and IDE.

Format selection bug only the IDE, the command line only formats the whole files.

 

TinyPortal © 2005-2018