需要有关通过按键宏格式化 C 注释的方法的建议

发布于 2024-11-15 23:37:11 字数 769 浏览 4 评论 0原文

为了可视化,假设“_”实际上是一个空白。

考虑以下内容

/*!
____This_is_a_comment_about_a_function
____and_its_purpose
____and_arguments
____and_things_of_that_ilk
*/

我想做的是选择该文本块,按下按钮,然后哇哦! 就改成这样的形式了。

/*!____________________________________
____This_is_a_comment_about_a_function_
____and_its_purpose____________________
____and_arguments______________________
____and_things_of_that_ilk_____________
_____________________________________*/

为什么我需要这个?第二种形式可以缓解我在检查代码时的眼睛疲劳。对我来说,问题已经足够了,应该做点什么。

到目前为止我尝试过什么?主要是在谷歌上搜索宏示例。没有任何迹象表明可能有成效的方向。尝试理解宏IDE有点困难。有些人在这里搜索

我的问题是什么?

你能给我指向链接吗?给我一个提示吗?猜猜看?我需要研究什么才能弄清楚如何做到这一点。

感谢您的关注。

邪恶的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2024-11-22 23:37:11

您是否寻找过使用宏获取和替换选定文本的宏示例,以及相当于 字符串填充?我认为这三件事可以结合起来,在每行上循环来构建最终的字符串来替换原始选择。

伪代码:

selText = getSelectedText();
selTextSplit = selText.split("\n");
selTextPadded = "";
for (i = 0; i < selTextSplit.length; i++) {
    selTextPadded += selTextSplit[i].padRight(80, ' ') + "\n";
}
replaceSelectedText(selTextPadded);

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);
怀中猫帐中妖 2024-11-22 23:37:11

这就是我的成果。
感谢贾里德为我指明了富有成效的方向。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文