如果我有:
/// <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 SubWhen 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 Modulewhich 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!
更多推荐
发布评论