Visual Studio宏在粘贴时插入文本(Visual studio macro to insert text like when pasting)

编程入门 行业动态 更新时间:2024-10-28 14:30:48
Visual Studio宏在粘贴时插入文本(Visual studio macro to insert text like when pasting)

如果我有:

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

If I was to have:

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

on the clipboard pasted it on top of the method static void Main(string[] args), it will look like:

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

Note: The text I had on the clipboard had no indentation (4 white spaces on the left). When I pasted it, Visual Studio was able to figure out that it needed indentation.

I would like to do the same thing with a macro. I do not want to use the clipboard as I have the text I want to insert in a variable (myText). I have something like:

Sub TemporaryMacro() Dim myText As String = "/// <summary>" _ & vbCrLf & "/// My summary" _ & vbCrLf & "/// </summary>" _ & vbCrLf & "/// <param name='args'></param>" DTE.ActiveDocument.Selection.Text = myText End Sub

When I run that macro I end up with:

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

Note: I do get a different result.


I have also tried:

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

which results in:

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

I know I can place myText on the clipboard and then paste it. That does not make sense though. How can I achieve the same behavior as if I where pasting myText without having it to place it on the clipboard?

最满意答案

您是否尝试过此操作(只需将标签/缩进添加到您已尝试过的代码中)?

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 (未确认)也会为您自动缩进代码。

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

Finally found the solution:

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()

Edit

I am making this edit to ask why this answer reserved down votes. If you refer to the question title it asks: Visual studio macro to insert text like when pasting when you paste text it maters where the cursor is located! I do not have to search for static void Main(string[] args) also I up voted the other answer cause I appreciate the help. If I would have known the answer earlier I would have turn this question into a bounty.

Lastly the other solution does not work correclty... run that macro when I have comments at the top of the page and it does not work. It is helpful that's why I up voted!

更多推荐

本文发布于:2023-08-03 01:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   studio   Visual   Studio   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!