Recent

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

indydev

  • Full Member
  • ***
  • Posts: 105
Re: Easy way to strip comments?
« Reply #30 on: June 03, 2024, 04:56:14 pm »
See Updated Version in later post.

« Last Edit: June 03, 2024, 09:48:36 pm by indydev »

Roland57

  • Sr. Member
  • ****
  • Posts: 447
    • msegui.net
Re: Easy way to strip comments?
« Reply #31 on: June 03, 2024, 06:02:10 pm »
Congratulations for the clean package example (not yet tested the latest version).

For the bugs that you reported (comments in strings, strings in comments), I think it's impossible to do perfectly with regular expressions.  ;)
My projects are on Gitlab and on Codeberg.

indydev

  • Full Member
  • ***
  • Posts: 105
Re: Easy way to strip comments?
« Reply #32 on: June 03, 2024, 06:46:16 pm »
For the bugs that you reported (comments in strings, strings in comments), I think it's impossible to do perfectly with regular expressions.  ;)

Braced comments in strings and braced comments inside braced comments. Strings in comments are fine. But yes, without negative lookback in TRegExpr you are right (maybe even with negative lookback because you could have many nested braced comments). Working on a loop through characters version.

I discovered that some editors don't handle nested braced comments so you wouldn't have them in those editors, but the Lazarus editor does, so I want to handle that case.

Thanks for pointing out the error! I had many typos in the write up, and filenames and... ugh.

indydev

  • Full Member
  • ***
  • Posts: 105
Re: Easy way to strip comments?
« Reply #33 on: June 03, 2024, 09:46:16 pm »
StripComments is a Lazarus IDE package that adds a menu item for removing comments from the currently open unit in the editor. This package strips out line comments and block comments from an Object Pascal unit (both `{ }` and `(* *)` comment styles). 
  • Handles nested `{ { } }` blocks.
  • GUIDs are safe.
  • Compiler directives are safe.
  • JSON strings are safe.
  • Regular Expressions are safe.
  • `{ }` inside string literals are safe.

Installation

1. Unzip stripcomments.zip:

2. Navigate to the directory containing your package:

    cd path/to/your/package

3. Open a terminal and execute the following commands:

    lazbuild strpcomments.lpk
   
    lazbuild --add-package strpcomments.lpk
   
    lazbuild --build-ide= --build-mode=


Usage

After installation, the "Remove Comments" option will be available in the Lazarus IDE under the "Edit" menu by default, but you have the option to place it in the "Source" menu, the source editor popup menu, or all of them by uncommenting those options in the code.

The keyboard shortcut Ctrl+Shift+R will also perform the "Remove Comments" menu item.

License

This project is licensed under the MIT License. See the LICENSE inside unit file for details.

Warranty

None. Code is available for you. Will try to fix installation errors, and may add additional fixes if they affect my own work.

Thanks to cdbc, JuhaManninen, DomingoGP and Roland57 for encouragement, tips, and pointing out fails!
« Last Edit: June 03, 2024, 09:50:54 pm by indydev »

 

TinyPortal © 2005-2018