Recent

Author Topic: [solution] Easy way to strip comments?  (Read 5965 times)

indydev

  • Full Member
  • ***
  • Posts: 114
Re: Easy way to strip comments?
« Reply #15 on: May 31, 2024, 05:29:53 pm »
Are Guids not always between `'` `'`s? If not, mine will also have that problem.

indydev

  • Full Member
  • ***
  • Posts: 114
Re: Easy way to strip comments?
« Reply #16 on: May 31, 2024, 05:36:35 pm »
I keep coming back to this. So let's get it into the IDE. Can we make a package?

Removed error prone code. Starting over...
« Last Edit: May 31, 2024, 08:58:40 pm by indydev »

cdbc

  • Hero Member
  • *****
  • Posts: 1497
    • http://www.cdbc.dk
Re: Easy way to strip comments?
« Reply #17 on: May 31, 2024, 06:23:01 pm »
Hi
Huh...  %)
I've never made a package, so I'm a bit baron in that region... Maybe someone can jump in with a magic wand...
Sorry mate, haven't got the foggiest...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Roland57

  • Sr. Member
  • ****
  • Posts: 450
    • msegui.net
Re: Easy way to strip comments?
« Reply #18 on: May 31, 2024, 06:49:38 pm »
I keep coming back to this. So let's get it into the IDE. Can we make a package?

I never made a package myself, so I cannot help.

You could also compile it as a simple console application and add it in Lazarus tools menu. Some ideas in this discussion.
« Last Edit: May 31, 2024, 06:56:56 pm by Roland57 »
My projects are on Gitlab and on Codeberg.

WooBean

  • Sr. Member
  • ****
  • Posts: 268
Re: Easy way to strip comments?
« Reply #19 on: May 31, 2024, 07:14:57 pm »
A natural way to add a feature "Strip of comments" would be modyfing a package JEDI Code Format.

A GUI interface will be like a picture below:
Platforms: Win7/64, Linux Mint Ulyssa/64

indydev

  • Full Member
  • ***
  • Posts: 114
Re: Easy way to strip comments?
« Reply #20 on: June 01, 2024, 12:15:09 am »
Well, I got a unit to finally compile and rebuilt the IDE, but it seems nothing happened. I just don't know what I am doing. I need to move on to other things for now. I post the code for someone who might come along and figure it out.

stripcomments.pas

Code: Pascal  [Select][+][-]
  1. unit StripComments;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Menus, IDECommands, SrcEditorIntf, IDEMsgIntf, RegExpr, IDEExternToolIntf, LazIDEIntf, MenuIntf;
  9.  
  10. procedure Register;
  11.  
  12. implementation
  13.  
  14. type
  15.   TStripComments = class
  16.   public
  17.     procedure AddPopupMenuItem(Sender: TObject);
  18.   end;
  19.  
  20. procedure TStripComments.AddPopupMenuItem(Sender: TObject);
  21. var
  22.   SourceEditor: TSourceEditorInterface;
  23.   FileName, NewFileName: String;
  24.   FileContent: TStringList;
  25.   RegEx: TRegExpr;
  26.   i: Integer;
  27.  
  28.   procedure RemoveExtraBlankLines(CodeLines: TStringList);
  29.   var
  30.     i: Integer;
  31.     BlankLineFound: Boolean;
  32.   begin
  33.     BlankLineFound := False;
  34.     i := 0;
  35.     while i < CodeLines.Count do
  36.     begin
  37.       if Trim(CodeLines[i]) = '' then
  38.       begin
  39.         if BlankLineFound then
  40.           CodeLines.Delete(i)
  41.         else
  42.         begin
  43.           BlankLineFound := True;
  44.           Inc(i);
  45.         end;
  46.       end
  47.       else
  48.       begin
  49.         BlankLineFound := False;
  50.         Inc(i);
  51.       end;
  52.     end;
  53.   end;
  54.  
  55. begin
  56.   SourceEditor := SourceEditorManagerIntf.ActiveEditor;
  57.   if Assigned(SourceEditor) then
  58.   begin
  59.     FileName := SourceEditor.FileName;
  60.     FileContent := TStringList.Create;
  61.     try
  62.       FileContent.Text := SourceEditor.SourceText;
  63.  
  64.       RegEx := TRegExpr.Create;
  65.       RegEx.ModifierM := True;
  66.       try
  67.         // Strip single-line comments
  68.         RegEx.Expression := '\/\/.*';
  69.         RegEx.ModifierI := True; // case-insensitive
  70.  
  71.         for i := 0 to FileContent.Count - 1 do
  72.         begin
  73.           if RegEx.Exec(FileContent[i]) then
  74.             FileContent[i] := StringReplace(FileContent[i], RegEx.Match[0], '', []);
  75.         end;
  76.  
  77.         // Strip multi-line and Pascal-style block comments
  78.         RegEx.Expression := '[^'']\{\s*[^\s\$].*?\}|\(\*.*?\*\)';
  79.         RegEx.ModifierS := True;
  80.         FileContent.Text := RegEx.Replace(FileContent.Text, '', False);
  81.  
  82.       finally
  83.         RegEx.Free;
  84.       end;
  85.  
  86.       // Remove extra blank lines
  87.       RemoveExtraBlankLines(FileContent);
  88.  
  89.       // Save the stripped content to a new file
  90.       NewFileName := ChangeFileExt(FileName, '_CmtFree.pas');
  91.       FileContent.SaveToFile(NewFileName);
  92.  
  93.       // Provide some progress messages
  94.       IDEMessagesWindow.AddCustomMessage(mluNone, 'Comments stripped and saved to: ' + NewFileName);
  95.       IDEMessagesWindow.AddCustomMessage(mluNone, 'Opening the new file...');
  96.  
  97.       // Open the new file in the IDE
  98.       LazarusIDE.DoOpenEditorFile(NewFileName, -1, -1, [ofAddToRecent]);
  99.  
  100.     finally
  101.       FileContent.Free;
  102.     end;
  103.   end
  104.   else
  105.   begin
  106.     IDEMessagesWindow.AddCustomMessage(mluError, 'No active source editor found.');
  107.   end;
  108. end;
  109.  
  110. procedure AddMenu;
  111. var
  112.   ParentMenu: TIDEMenuSection;
  113.   StripComments: TStripComments;
  114. begin
  115.   StripComments := TStripComments.Create;
  116.   // Find or create the parent menu section
  117.   ParentMenu := RegisterIDEMenuSection(mnuEdit, 'StripCommentsSection');
  118.  
  119.   // Register the new menu item in the existing editor popup menu section
  120.   RegisterIDEMenuCommand(ParentMenu, 'StripOutComments', 'Remove Comments', @StripComments.AddPopupMenuItem);
  121. end;
  122.  
  123. procedure Register;
  124. begin
  125.   AddMenu;
  126. end;
  127.  
  128. end.

In my attempt to limit problems I stopped using IStringList, but I don't think it played a role in my difficulties.

and strpcomments.lpk

Code: Pascal  [Select][+][-]
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <CONFIG>
  3.   <Package Version="5">
  4.     <Name Value="strpcomments"/>
  5.     <Type Value="RunAndDesignTime"/>
  6.     <Author Value="IndyDev"/>
  7.     <CompilerOptions>
  8.       <Version Value="3.2.2"/>
  9.       <SearchPaths>
  10.         <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)/"/>
  11.       </SearchPaths>
  12.     </CompilerOptions>
  13.     <Version Major="1"/>
  14.     <Files>
  15.       <Item>
  16.         <Filename Value="stripcomments.pas"/>
  17.         <HasRegisterProc Value="True"/>
  18.         <UnitName Value="StripComments"/>
  19.       </Item>
  20.     </Files>
  21.     <RequiredPkgs>
  22.       <Item>
  23.         <PackageName Value="FCL"/>
  24.       </Item>
  25.       <Item>
  26.         <PackageName Value="IDEIntf"/>
  27.       </Item>
  28.     </RequiredPkgs>
  29.     <UsageOptions>
  30.       <UnitPath Value="$(PkgOutDir)"/>
  31.     </UsageOptions>
  32.     <PublishOptions>
  33.       <Version Value="2"/>
  34.       <UseFileFilters Value="True"/>
  35.     </PublishOptions>
  36.   </Package>
  37. </CONFIG>
« Last Edit: June 01, 2024, 04:06:27 pm by indydev »

alpine

  • Hero Member
  • *****
  • Posts: 1245
Re: Easy way to strip comments?
« Reply #21 on: June 01, 2024, 07:12:59 am »
BTW,  Lazarus already have search/replace by regex, including in files and directories. The only thing is how to make a search item in the MRU list permanent.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4515
  • I like bugs.
Re: Easy way to strip comments?
« Reply #22 on: June 01, 2024, 08:16:02 am »
Well, I got a unit to finally compile and rebuilt the IDE, but it seems nothing happened. I just don't know what I am doing. I need to move on to other things for now. I post the code for someone who might come along and figure it out.
I made a package using your code and installed it. Comments get stripped out as advertised!
Only the new '*_CmtFree.pas' file is not found and opened in editor initially. You must switch active program and then do the same thing in Lazarus again. Then it is opened.
Calling LazarusIDE.DoOpenEditorFile(NewFileName, -1, -1, [ofAddToRecent]);
after FileContent.Free;
makes no difference. Even adding a Sleep(1000) does not help.
It is a feature or bug in DoOpenEditorFile().

BTW, uploading your package here zipped instead of copy/pasting individual file contents would make testing easier.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4515
  • I like bugs.
Re: Easy way to strip comments?
« Reply #23 on: June 01, 2024, 01:48:11 pm »
InvalidateFileStateCache(NewFilename);
before LazarusIDE.DoOpenEditorFile() fixes the issue. Unit LazFileCache must be added to uses section.

This is not intuitive. I try to get DoOpenEditorFile() fixed so that no explicit InvalidateFileStateCache() call is needed.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

indydev

  • Full Member
  • ***
  • Posts: 114
Re: Easy way to strip comments?
« Reply #24 on: June 01, 2024, 04:55:40 pm »
Hmmm. I can't seem to get this over the finish line.

I have made the changes you listed, but I still get the Unable to read file "unit1_cmtfree.pas". message.

Code: Pascal  [Select][+][-]
  1. ...
  2.       // Open the new file in the IDE
  3.       IDEMessagesWindow.AddCustomMessage(mluNone, 'Comments stripped and saved to: ' + NewFileName);
  4.       IDEMessagesWindow.AddCustomMessage(mluNone, 'Opening the new file...');
  5.  
  6.     finally
  7.       FileContent.Free;
  8.     end;
  9.     InvalidateFileStateCache(NewFilename);     // Newly added as suggested
  10.     LazarusIDE.DoOpenEditorFile(NewFileName, -1, -1, [ofAddToRecent]);  // Moved to be after FileContent.Free
  11.   end
  12.   else
  13.   begin
  14.     IDEMessagesWindow.AddCustomMessage(mluError, 'No active source editor found.');
  15.   end;
  16. end;
  17.  

I have made some changes to the RegEx.Expression. For some reason I had removed the compiler directive check and the `'` string check was never added. It should be:

RegEx.Expression := '[^'']\{\s*[^\s\$].*?\}|\(\*.*?\*\)';

When I can get this to fully work, I will follow your suggestion to zip it all up and upload it ...and maybe make a gitlab page.

Thank you so much for helping out here! It is really appreciated.
« Last Edit: June 01, 2024, 05:26:12 pm by indydev »

lainz

  • Hero Member
  • *****
  • Posts: 4591
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Easy way to strip comments?
« Reply #25 on: June 01, 2024, 05:51:13 pm »
Next add-on, autocomplete comments with AI.  :)

indydev

  • Full Member
  • ***
  • Posts: 114
Re: Easy way to strip comments?
« Reply #26 on: June 01, 2024, 07:27:37 pm »
I have made some changes to the RegEx.Expression. For some reason I had removed the compiler directive check and the `'` string check was never added. It should be:

RegEx.Expression := '[^'']\{\s*[^\s\$].*?\}|\(\*.*?\*\)';


Another expression change. If you set the 2nd regular expression to:

RegEx.Expression := '[^'']\{\s*[^\s\$].*?\}|\(\*.*?\*\)|\/\/([^\n\r])+';

You can remove the single-line comment block above it.

Still trying to get the package to fully work. It removes the comments and makes the file, just struggles to open the unit--at least for me.

DomingoGP

  • Jr. Member
  • **
  • Posts: 70
Re: Easy way to strip comments?
« Reply #27 on: June 01, 2024, 10:22:14 pm »

Maybe you can try another aproach to open the modified code in the ide. ( not tested here, but works in my package https://github.com/DomingoGP/lazIdeDiffCompareFiles ).

Code: Pascal  [Select][+][-]
  1. uses
  2.  ,  projectintf;    //<< Add to uses for FileDescriptorText.
  3.  
  4.   LazarusIDE.DoNewEditorFile(FileDescriptorText, NewFileName, FileContent.Text, [nfOpenInEditor, nfIsNotPartOfProject]);
  5.  

indydev

  • Full Member
  • ***
  • Posts: 114
Re: Easy way to strip comments?
« Reply #28 on: June 02, 2024, 12:06:56 am »
That worked!! Thank you so much.

Cleaning things up, and testing other menu locations so anyone can put it where they want. Have some obligations so won't get to it until tomorrow.

Of course anyone can take the information here and put it together on their own if they wish.

Roland57

  • Sr. Member
  • ****
  • Posts: 450
    • msegui.net
Re: Easy way to strip comments?
« Reply #29 on: June 03, 2024, 08:25:58 am »
Hello! FYI, after I installed the package (Linux, Lazarus 3.2), I could no longer start Lazarus (access violation). Maybe I made a wrong manipulation...  :-\
My projects are on Gitlab and on Codeberg.

 

TinyPortal © 2005-2018