1

如果我有:

/// <summary>
/// My summary
/// </summary>
/// <param name='args'></param>

在剪贴板上将其粘贴到方法的顶部static void Main(string[] args),它看起来像:

class Program
{
    /// <summary>
    /// This is my summary
    /// </summary>
    /// <param name='args'></param>
    static void Main(string[] args)
    { 
    }
}

注意:我在剪贴板上的文本没有缩进(左侧 4 个空格)。当我粘贴它时,Visual Studio 能够确定它需要缩进。

我想用宏做同样的事情。我不想使用剪贴板,因为我有想要在变量 ( myText) 中插入的文本。我有类似的东西:

Sub TemporaryMacro()

    Dim myText As String = "/// <summary>" _
    & vbCrLf & "/// My summary" _
    & vbCrLf & "/// </summary>" _
    & vbCrLf & "/// <param name='args'></param>"

    DTE.ActiveDocument.Selection.Text = myText

End Sub

当我运行该宏时,我最终得到:

class Program
{

    /// <summary>
    ///  <summary>
    ///  /// My summary
    ///  /// </summary>
    ///  /// <paramref name=" name='args'></param>"/></summary>
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {

    }
}

注意:我确实得到了不同的结果。


我也试过:

Public Module RecordingModule
    Sub TemporaryMacro()

        Dim myText As String = "/// <summary>" _
        & vbCrLf & "/// My summary" _
        & vbCrLf & "/// </summary>" _
        & vbCrLf & "/// <param name='args'></param>"

        DTE.ActiveDocument.Selection.Insert(myText)

    End Sub
End Module

这导致:

class Program
{

    /// <summary>
/// My summary
/// </summary>
/// <param name='args'></param>
    static void Main(string[] args)
    {

    }
}

我知道我可以放在myText剪贴板上然后粘贴。但这没有意义。如何实现与粘贴时相同的行为,myText而无需将其放置在剪贴板上?

4

2 回答 2

4

您是否尝试过(只需将制表符/缩进添加到您已经尝试过的代码中)?

Public Module RecordingModule
    Sub TemporaryMacro()

        Dim myText As String = "/// <summary>" _
        & vbCrLf & vbTab &  "/// My summary" _
        & vbCrLf & vbTab &  "/// </summary>" _
        & vbCrLf & vbTab &  "/// <param name='args'></param>"

        DTE.ActiveDocument.Selection.Insert(myText)

    End Sub
End Module

& vbTab &但是,除非您添加更多s ,否则此缩进不会按原样进行更深的嵌套。


此外,您是否尝试过将文本相对于特定文本行放置在正文中(static void Main(string[] args)在这种情况下),而不是使用当前选择(这意味着您必须在文档中放置一个光标)?

您在自己的回答中指出,并不总是需要这个特定的字符串,而是使用光标的位置。我只是想指出这是一种选择。如果您更改searchText下面的代码,您可以将插入的文本放在您想要的任何位置。这也可以更改为在光标处查找文本。

类似于此(未经测试)代码的内容:

Dim objTextDoc As TextDocument
Dim objEditPt As EditPoint
Dim objMovePt As EditPoint
Dim docText As String
Dim docNewText As String

Dim myText As String = "/// <summary>" _
    & vbCrLf & vbTab &  "/// My summary" _
    & vbCrLf & vbTab &  "/// </summary>" _
    & vbCrLf & vbTab &  "/// <param name='args'></param>"

Dim searchText As String = "static void Main(string[] args)"

objTextDoc = DTE.ActiveDocument.Object("TextDocument")
objEditPt = objTextDoc.StartPoint.CreateEditPoint
objMovePt = objTextDoc.StartPoint.CreateEditPoint

' Get all text of active document
docText = objEditPt.GetText(objTextDoc.EndPoint)

objEditPt.StartOfDocument()
objMovePt.EndOfDocument()

' Set string as the new intended body of the text, inserting myText just above the Main line
docNewText = docText.Substring(0, InStr(docText, searchText) - 1) & myText & docText.Substring(docText.Length() - InStr(docText, searchText) + searchText.Length())

objEditPt.ReplaceText(objMovePt, docNewText, vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)

我相信vsEPReplaceTextOptions.vsEPReplaceTextAutoformat(未确认)也会为您自动缩进您的代码。

有关更多信息/想法,请参见此处

于 2012-08-14T13:28:07.027 回答
0

终于找到了解决办法:

Dim myText As String = "/// <summary>" _
     & vbCrLf & "/// My summary" _
     & vbCrLf & "/// </summary>" _
     & vbCrLf & "/// <param name='args'></param>"

DTE.ActiveDocument.Selection.Insert(myText)

' how many characters where inserted?
' Trying myText.Length gives incorrect results because visual studio counts \r\n as one character so we 
' use the regex \r\n|. to try to find first \r\n as one match and any other character as another match
Dim insertLength As Integer = System.Text.RegularExpressions.Regex.Matches(myText, "(?>\r\n|.)").Count

' select the text that was just inserted
DTE.ActiveDocument.Selection.CharLeft(True, insertLength)

' here comes the magic!
DTE.ActiveDocument.Selection.SmartFormat()

编辑

我正在进行此编辑以询问为什么此答案保留了反对票。如果您参考它询问的问题标题:Visual studio macro to insert text like when pasting当您粘贴文本时,它与光标所在的位置有关!我不必搜索static void Main(string[] args)我也投票赞成其他答案,因为我感谢您的帮助。如果我早点知道答案,我会把这个问题变成赏金。

最后,其他解决方案无法正常工作...当我在页面顶部有评论并且它不起作用时运行该宏。这很有帮助,这就是我投票的原因!

于 2012-08-14T14:16:29.177 回答