Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

toggling code commenting on/off

forall
Beginner
654 Views
A slightly non-Fortran topic but so closely related to Fortran work ... and the solution is probably v simple... I spent quite a bit on this unsucesfully and v frustratingly, so hope somebody can just point out whats going wrong...


Since I often toggle commented code ON/OFF, in CVF I used a macro to do this (linked to keyboard shortcut) - see the macro pasted below (trimmed for this post). It worked perfectly (still does in CVF)

In IVF commenting / uncommenting is provided automatically, but the problems are (1) it doesnt toggle, which is inconvenient (sorry.. yes, a bit picky..) and (2) I use 2-space comments, ie "! " rather than 1-space "!" by the autobutton, which stuffs up all my indentation..

So I just want to reuse my old VB macro (which still works perfectly in CVF).

But when using my old VB macro in IVF11+VS2008, it no longer works correctly when uncommenting indented code.

For example, applying the macro to uncommenting

! aaaaaa
! bbbbbbbbbbbb
! cccccccccccc
! cccccccccccc
! dddddddddddd

produces

aaaaaa
12bbbbbbbbbbbb
1234cccccccccccc
123456cccccccccccc
123456dddddddddddd

where I used the numbers to count blank spaces. Basically the macro doesnt preserve original indentation..

I think I identified the offending part of the macro (**PROBLEM HERE?** below), but have no idea how to fix it because I cant understand why exactly it doesnt work.

I suspect the problem/solution is v simple.. any hints? thanks in advance!

=====================================
Sub ToggleCommentOutFortran()
'DESCRIPTION: Toggles commenting out a selected block of text.
' [ by Adam Solesby -- http://solesby.com ]
' Comments by DK:
' 1. Modified by DK[17Jan2005] to implement F-90 comment toggling
'----------------------------------------------------------------------------------
' This will comment/uncomment out single lines or blocks. Single lines are commented
' with the same indention level. Blocks are commented at the beginning of the line.
' Assign this to a key (e.g. ctrl-/) and it will toggle the current line/block of code.
' This will handle both "//" and "'" style comments
'----------------------------------------------------------------------------------

CommentType = "! "
CommentWidth = 2

StartLine = ActiveDocument.Selection.TopLine
EndLine = ActiveDocument.Selection.BottomLine

' Added by DK 4/4/2009 AD
If VS_version = 2008 And EndLine - StartLine > 0 Then 'adjusted by DK for VS2008
EndLine = EndLine - 1 ' for some reason VS2008 counts the next line
End If

If EndLine < StartLine Then
Temp = StartLine
StartLine = EndLine
EndLine = Temp
End If

' Multi-line -- comment at start of line
'DK: this doesnt quite work well - gains extra 2 chars at beginning of line at every operation...
CommentLoc = dsFirstColumn ' or dsFirstText if you prefer

' check whether commenting on or off based on the _last_ line of selection
ActiveDocument.Selection.GoToLine(EndLine)
ActiveDocument.Selection.StartOfLine(CommentLoc)
ActiveDocument.Selection.CharRight(dsExtend, CommentWidth)
If ActiveDocument.Selection.Text = CommentType Then
bAddComment = False
Else
bAddComment = True
End If

' work with strings so that we can do a single undo in editor
ActiveDocument.Selection.MoveTo(StartLine, 1)
ActiveDocument.Selection.MoveTo(EndLine, dsEndOfLine, dsExtend)

s = ActiveDocument.Selection.Text

If bAddComment Then
s = CommentType & Replace(s, vbNewLine, vbNewLine & CommentType)
Else ' **PROBLEM HERE?**
s = Replace(s, vbNewLine & CommentType, vbNewLine)
s = Mid(s, Len(CommentType) + 1)
End If

ActiveDocument.Selection.Text = s
ActiveDocument.Selection.StartOfLine(dsFirstText) 'DK: move cursor to start of line
End Sub
0 Kudos
1 Reply
Paul_Curtis
Valued Contributor I
654 Views
Quoting - forall
...In IVF commenting / uncommenting is provided automatically, but the problems are (1) it doesnt toggle, which is inconvenient (sorry.. yes, a bit picky..) and (2) I use 2-space comments, ie "! " rather than 1-space "!" by the autobutton, which stuffs up all my indentation..

VS2003 and VS2008 toolbuttons provide one-click un/commenting for highlighted code blocks, couldn't be simpler:

  1. highlight the target code block
  2. shift-tab until the leftmost filled column is all the way leftwards (and stop at that point)
  3. un/comment the entire block; the ! is removed/added in column 1, un/bumping the entire block 1 char
  4. tab the modified block back to its original level of indent
This is very fast, and tab structure within the block is not affected at all. It's a really wonderful feature.
0 Kudos
Reply