0

for visualization, pretend the '_' is actually a blank.

Consider the following

/*!
____This_is_a_comment_about_a_function
____and_its_purpose
____and_arguments
____and_things_of_that_ilk
*/

What I would like to do is select that chunk of text, press a button, and whammo! It is changed to this form.

/*!____________________________________
____This_is_a_comment_about_a_function_
____and_its_purpose____________________
____and_arguments______________________
____and_things_of_that_ilk_____________
_____________________________________*/

Why do I want this? The second form easies my eye strain when reviewing code. For me, it is enough of a problem that something should be done.

What have I tried so far? Mostly Googling for macro examples. Nothing has popped up which suggests a direction that might be productive. A bit of struggling to try to understand the macro ide. Some searching here on SO

What is my question?

Can you point me to links? Give me a hint? Take a guess? on what I need to study to figure out how to do this.

Thank you for your attention.

Evil.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
  • Can you "pre-process" the file before opening it in your editor of choice? You could create something like [`indent`](http://www.gnu.org/software/indent/) and pass the source through that just to add trailing spaces to comments ... – pmg Jun 18 '11 at 16:21
  • @delnan. Yes... read the first line @pmg. Yes... I could do that. I am looking for a cheaper solution. – EvilTeach Jun 18 '11 at 16:30
  • Hmmm ... in [`vim`](http://www.vim.org/) you can run a filter for selected lines: switch to "visual mode", select the lines, hit `:!filter` and whammo! -- just tested it with a simpler filter :) – pmg Jun 18 '11 at 16:43
  • Have you looked for macro examples of getting and replacing selected text using a macro, and the macro equivalent of strpad? I would think those three things could be combined, with a loop over each line to construct the final string to replace the original selection. – Jared Farrish Jun 18 '11 at 16:45
  • @jared thank you. I will look into that. Pop that in as an answer and i will up vote it. – EvilTeach Jun 18 '11 at 17:03

2 Answers2

1

Have you looked for macro examples of getting and replacing selected text using a macro, and the macro equivalent of string padding? I would think those three things could be combined, with a loop over each line to construct the final string to replace the original selection.

Psuedocode:

selText = getSelectedText();
selTextSplit = selText.split("\n");
selTextPadded = "";
for (i = 0; i < selTextSplit.length; i++) {
    selTextPadded += selTextSplit[i].padRight(80, ' ') + "\n";
}
replaceSelectedText(selTextPadded);
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • Yes. I haven't seen the string padding before. That does look like a viable approach. Thank you. – EvilTeach Jun 18 '11 at 17:10
  • @EvilTeach - Something you'll need to consider is how you will determine the pad amount. If it's fixed (like 80 columns), then you will also have to detect if the current line is over that value, and figure out what to do with a line that has that attribute. Push to the next line? Offset? Add a line? Leave it alone (and let it go over)? Just something to consider. – Jared Farrish Jun 18 '11 at 17:13
  • Ya, a first pass over it to get the max length, maybe round that up to a multiple of my tab size, then apply it. – EvilTeach Jun 18 '11 at 17:30
  • @EvilTeach - Can I ask why you're right-padding the comments? – Jared Farrish Jun 18 '11 at 17:32
  • Yes, if it is ok for me to refer you to the text up top following "Why do I want this?" – EvilTeach Jun 18 '11 at 17:38
  • @EvilTeach - I guess I don't understand how that helps (putting white space to the right of the text), since it won't be visible. It doesn't matter, it just seems like editing comments may become an issue with lots of white space for presentation only. – Jared Farrish Jun 18 '11 at 17:43
  • @EvilTeach - In fact, when you setup your macro, you'll probably want to detect the last character to be detected in the string, not the total length of the string, as a space will probably give you a false reading if you're edited a line. – Jared Farrish Jun 18 '11 at 17:50
0

This is what I worked out. Thanks to Jared for pointing me in a productive direction.

Sub FormatEvil()

Try
    DTE.UndoContext.Open("Evil Style C Comment Padder")

    Dim txtSel As TextSelection = DTE.ActiveDocument.Selection

    Dim currText As String = txtSel.Text

    If currText.Trim.StartsWith("/*") AndAlso currText.Trim.EndsWith("*/") Then

        Dim splitText() As String = Split(currText, vbCrLf)

        ' Trim all of the lines down
        For z = 0 To UBound(splitText) - 1
            splitText(z) = Trim(splitText(z))
        Next

        ' How long should the block be?
        Dim longestLine As Integer = 0
        For z = 0 To UBound(splitText) - 1
            If splitText(z).Length > longestLine Then
                longestLine = splitText(z).Length
            End If
        Next

        longestLine += 4

        ' build the value to replace the selection with.
        Dim selTextPadded As String = "/*    "
        selTextPadded = selTextPadded.PadRight(longestLine + 2) + vbCrLf

        For z = 1 To UBound(splitText) - 2
            splitText(z) = splitText(z).PadRight(longestLine - 2)
            selTextPadded += "    " + splitText(z).ToString() + vbCrLf
        Next

        splitText(UBound(splitText) - 1) = splitText(UBound(splitText) - 1).PadRight(longestLine - 2)
        selTextPadded += "    " + splitText(UBound(splitText) - 1).ToString() + vbCrLf

        Dim tmp As String = "*/"
        selTextPadded += tmp.PadLeft(longestLine + 2) + vbCrLf

        txtSel.Delete() 'This is to help keep formatting correct when multiline
        txtSel.Insert(selTextPadded, vsInsertFlags.vsInsertFlagsContainNewText)
    Else
        ' This is not a c comment
    End If
Finally
    DTE.UndoContext.Close()
End Try
End Sub
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
  • I learned a bit. No surprise there. object.split, only applies to 1 character, which was causing the split on the CR, leaving rogue LFs in the text. Replacing it with split(object) didn't have that problem. – EvilTeach Jun 21 '11 at 02:57
  • Suggestions for improving the code will gleefully be welcomed. I am not experienced in vb, but am willing to learn. – EvilTeach Jun 21 '11 at 02:58