Author Topic: Remove c++ line comments from selection  (Read 4288 times)

kniped1

  • Community Member
  • Posts: 20
  • Hero Points: 1
Remove c++ line comments from selection
« on: April 19, 2022, 07:01:14 PM »
This macro removes all line comments (//) from the selected text. If the line is only a comment, it removes the entire line.

On a side note, in my current implementation of this macro, it works when I manually select a block of text, but it doesn't seem to work when I use select all. Does anyone know why that might be?

Graeme

  • Senior Community Member
  • Posts: 2812
  • Hero Points: 347
Re: Remove c++ line comments from selection
« Reply #1 on: April 20, 2022, 10:53:55 AM »
Maybe the select all problem is because the cursor is at the end of the file.  Try adding a begin_select() call at the start to move the cursor to the start of the selection.

On my machine, a line containing only a comment doesn't actually get deleted - but the comment does get deleted, leaving a blank line.  I'm using Windows 11.

If the // is immediately preceded by a non whitespace character the comment doesn't get deleted  - I don't know why this is.  Also it doesn't seem to allow for // in a string.

I have a couple of suggestions.  If I record a macro to search for any character (search string is set to . and reg expression enabled) and select colour coding of "comment" it finds the start of the next comment - if it's not already in a comment.  If I record a macro doing this I get the following

_command last_recorded_macro() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _macro('R',1);
   if (find('.',"LIPCC,,")) stop();
}

The search function you're using can also search for comments.

There's also a macro you can call to tell if the cursor is in a comment
_in_comment()
and it can distinguish between block comment and line comment.
I notice that Ctrl Z undo manages to undo all changes in one go with your current method.  If you change to some manually looping code, Ctrl Z might undo one by one - I don't know.