Author Topic: Comment for closing paren  (Read 6978 times)

Dan112123

  • Community Member
  • Posts: 44
  • Hero Points: 2
Comment for closing paren
« on: July 03, 2008, 12:36:13 am »
Did anybody write a macro that would put a comment at the end paren of a block with the name of the block?

For example for this code:

for ()
{
}

if I run the macro  at the }  I would get

for ()
{
} // for

I really liked that functinality in SourceInsight and kind of miss it.

Graeme

  • Senior Community Member
  • Posts: 2323
  • Hero Points: 305
Re: Comment for closing paren
« Reply #1 on: July 03, 2008, 02:45:18 am »
An editor I sometimes use shows the "start" of the block as a "pseudo comment" on screen at the end of the block for you, without requiring it to be physically in the source file, with the pseudo comment shown in a different colour.

Graeme

AxeMaster

  • Community Member
  • Posts: 13
  • Hero Points: 0
Re: Comment for closing paren
« Reply #2 on: July 03, 2008, 05:38:31 pm »
It's fairly trivial to write an alias to do what you want...  The only problem is that it may over-ride or block some of the more sophisticated built-in alias/expansions.

I did this quickly to validate my thoughts:

Go to Tools, Options, Languages, Application Languages, C/C++, Aliases.

Click 'New' and enter 'for' as the alias name.

In the expansion text box, enter:

for ( %\c; %\c; %\c )
{
%\i%\c
} /* for */


I also created one called 'if' like this:


if ( ( %\c ) )
{
%\i%\c
} /* ...if */
else
{
%\i%\c
} /* ...else */


There may be more sophisticated ways to patch the default aliases/expansions supplied with VS for C/C++, but I'm not that familiar with it yet.  :)

Dan112123

  • Community Member
  • Posts: 44
  • Hero Points: 2
Re: Comment for closing paren
« Reply #3 on: July 03, 2008, 07:07:50 pm »
It's fairly trivial to write an alias to do what you want...  The only problem is that it may over-ride or block some of the more sophisticated built-in alias/expansions.

Thanks Axe for your answer, but I'm not looking for an alias. I'm looking for a macro that would modify an existing code and add a comment after the code has been written already.
let's say I alreayd have code that looks like this

if ()
{
}

then I put my cursor on the last bracket

run my macro and get this

if ()
{
} // if ()

Does that make sense?

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Comment for closing paren
« Reply #4 on: July 03, 2008, 08:32:35 pm »
SourceInsight doesn't actually insert the " // if ()" into the file, it just paints it on the screen.

Personally, I do not want that kind of markup saved in the actual source code file.  But I would enjoy seeing one or more mechanisms to paint that type of thing.  Here are some of the recent suggestions for how to show similar information:

  • Paint "// if ()" (and etc) at the end of a block statement.
  • Paint (optionally colored/dotted) vertical lines to show indentation levels.  E.g. for Style 2 connect a vertical line between the { and }, or for Style 3 draw a vertical line from the beginning statement (if/else/while/do/switch/etc) to the last line of the block, or for Style 1 connect a vertical line from the beginning statement to the ending }.
  • Paint a vertical line (like above, but only one) to show the begin/end of the scope where the cursor currently is.  Could even paint "if"/"else"/"for"/etc in a smaller font near the top of the line (or the topmost visible part of the line) to show what kind of block it is.

AxeMaster

  • Community Member
  • Posts: 13
  • Hero Points: 0
Re: Comment for closing paren
« Reply #5 on: July 03, 2008, 09:16:12 pm »
Ah sorry, I didn't read close enough the first time.  Makes sense.

Is the macro language capable of doing things like "just painting?"  That would be a fun to write a little script like that. :)

Is there a browsable archive of user-submitted macros/programs for VS like they have on the VIM site?


at5dapa1

  • Senior Community Member
  • Posts: 166
  • Hero Points: 15
Re: Comment for closing paren
« Reply #6 on: July 13, 2008, 07:38:10 am »
Maybe other way to achieve similar result is if SlickEdit would show the opening brace in the Preview window, when the cursor is on closing brace. Then it would be very easy to see also the if, for, etc.
At the moment SlickEdit is already coloring the opening brace, so maybe frome there someone knows how to trigger Preview window too.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Comment for closing paren
« Reply #7 on: July 13, 2008, 06:41:34 pm »
That is interesting, and it might work for some folks.  It misses out on the key advantages of showing the "// if ()" inline, though:

1.  When you see the "// if ()" inline you have an easy visual cue without needing to move your eyes at all (it's not even so much moving the eyes away, it's the moving the eyes back).
2.  Some people have the Preview window normally auto-hidden (like me).

Also, the Preview window would not help show indentation level (the colored lines I mentioned help with that).

MindprisM

  • Senior Community Member
  • Posts: 127
  • Hero Points: 8
Re: Comment for closing paren
« Reply #8 on: July 14, 2008, 01:45:50 am »

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Comment for closing paren
« Reply #9 on: July 14, 2008, 04:40:31 am »
It's a partial solution, but involves invoking a macro to modify the source file (which in most multi-person teams is going to be a no-no).  The SourceInsight feature we were discussing does not modify the source files, it merely renders extra tags after closing braces (the tags are only rendered, they are not part of the source file).

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Comment for closing paren
« Reply #10 on: July 17, 2008, 01:01:17 am »
One possible short term solution could be to add a non-save line right below/above the ending/starting statement e.g. if the matching statement line is not visible. It might be neccesary to override the buffer props accordingly if adding a non-save line also sets 'p_modify'. This could be hooked into the hilite matching parens functionality. Just an idea...
HS2
« Last Edit: July 17, 2008, 01:05:52 am by hs2 »