在Visual Studio中,当我触发自动注释功能(通过输入“ ”或“///”),大部分XML注释细节都会显示出来。然而,我通常会将历史标签添加到文档中,以便我可以跟踪并记录随时间变化的方法更改。
我可以定制自动评论功能以添加历史标记和可能的通用名称 - 日期 - 更改占位符文本吗?
在Visual Studio中,当我触发自动注释功能(通过输入“ ”或“///”),大部分XML注释细节都会显示出来。然而,我通常会将历史标签添加到文档中,以便我可以跟踪并记录随时间变化的方法更改。
我可以定制自动评论功能以添加历史标记和可能的通用名称 - 日期 - 更改占位符文本吗?
我建议使用GhostDoc。它可以根据您的方法名称和参数基于 /// 生成非常智能的注释。此外,它是完全可定制的。
作为对Olivier评论的跟进。现在是宏的副本,请查找 “Do History” 部分以查看我插入代码的位置。
// InsertDocComments goes through the current document using the VS Code Model
// to add documentation style comments to each function.
Sub InsertDocComments()
Dim projectItem As ProjectItem
Dim fileCodeModel As FileCodeModel
Dim codeElement As CodeElement
Dim codeElementType As CodeType
Dim editPoint As EditPoint
Dim commentStart As String
projectItem = DTE.ActiveDocument.ProjectItem
fileCodeModel = projectItem.FileCodeModel
codeElement = fileCodeModel.CodeElements.Item(1)
// For the sample, don t bother recursively descending all code like
// the OutlineCode sample does. Just get a first CodeType in the
// file.
If (TypeOf codeElement Is CodeNamespace) Then
codeElement = codeElement.members.item(1)
End If
If (TypeOf codeElement Is CodeType) Then
codeElementType = CType(codeElement, CodeType)
Else
Throw New Exception("Didn t find a type definition as first thing in file or find a namespace as the first thing with a type inside the namespace.")
End If
editPoint = codeElementType.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint()
// Make doc comment start.
commentStart = LineOrientedCommentStart()
If (commentStart.Length = 2) Then
commentStart = commentStart & commentStart.Chars(1) & " "
ElseIf (commentStart.Length = 1) Then
commentStart = commentStart & commentStart.Chars(0) & commentStart.Chars(0) & " "
End If
// Make this atomically undo able. Use Try...Finally to ensure Undo
// Context is close.
Try
DTE.UndoContext.Open("Insert Doc Comments")
// Iterate over code elements emitting doc comments for functions.
For Each codeElement In codeElementType.Members
If (codeElement.Kind = vsCMElement.vsCMElementFunction) Then
// Get Params.
Dim parameters As CodeElements
Dim codeFunction As CodeFunction
Dim codeElement2 As CodeElement
Dim codeParameter As CodeParameter
codeFunction = codeElement
editPoint.MoveToPoint(codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader))
//editPoint.LineUp()
parameters = codeFunction.Parameters
// Do comment.
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.LineUp()
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<summary>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "Summary of " & codeElement.Name & ".")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</summary>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
For Each codeElement2 In parameters
codeParameter = codeElement2
editPoint.Insert("<param name=" & codeParameter.Name & "></param>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
Next //param
// Do history tag.
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.LineUp()
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<history>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "Name MM/DD/YYYY [Created]")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</history>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
End If //we have a function
Next //code elt member
Finally
DTE.UndoContext.Close()
End Try
End Sub
由于某种原因,在保存、重建和重新启动Visual Studio后,我没有得到历史标记。有人能看到我缺少了什么吗?
VB使用XML文件来加载默认设置。这是VBXMLDoc.xml文件,它与您运行的版本有关,文件的位置取决于版本。