Visual Studio宏在粘贴时插入文本

时间:2012-07-13 01:55:01

标签: visual-studio vba visual-studio-macros

如果我有:

/// <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而不将其放在剪贴板上的情况下实现相同的行为?

2 个答案:

答案 0 :(得分: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 &,否则此缩进不会按原样用于更深层次的嵌套。


此外,不是使用当前选择(意味着您必须在文档中放置光标),而是尝试将文本放在相对于特定文本行的正文中(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(未确认)也会为您自动缩进代码。

See here for more info/ideas

答案 1 :(得分: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)我也投了另一个答案,因为我很感激帮助。如果我早点知道答案,我会把这个问题变成赏金。

最后,另一个解决方案无法正常运行...当我在页面顶部有注释时运行该宏并且它不起作用。我投票的原因很有帮助!