场景

让我们以这个随机代码为例,它在 VB.Net 中,但只是为了演示我假装要做的 VB.Net 扩展行为:

Dim regEx As New Regex("Dog", RegexOptions.IgnoreCase)

Dim text As String = "One Dog!, Two Dogs!, three Dogs!"

Dim matchesPos As IEnumerable(Of MatchPosition) = GetMatchesPositions(regEx, text, 0)

使用Visual Studio中的 Ctrl + K + C 热键,我可以对获得此结果的整个代码块进行注释:

'Dim regEx As New Regex("Dog", RegexOptions.IgnoreCase)

'Dim text As String = "One Dog!, Two Dogs!, three Dogs!"

'Dim matchesPos As IEnumerable(Of MatchPosition) = GetMatchesPositions(regEx, text, 0)

问题

我假装开发这样的扩展,但预期的结果应该具有这种格式,所有代码都包含在 <Example> 标记中,用于XML文档:

''' <example>
''' Dim regEx As New Regex("Dog", RegexOptions.IgnoreCase)
'''
''' Dim text As String = "One Dog!, Two Dogs!, three Dogs!"
'''
''' Dim matchesPos As IEnumerable(Of MatchPosition) = GetMatchesPositions(regEx, text, 0)
''' </example>

在第一个视图看起来很容易做到,所有逻辑只是用三个引号 ''' (甚至空行)注释行并添加 <Example> 标记,但我不确定什么工具,项目类型,指南,或代码示例我需要开始开发这个 .

我需要特定的信息,帮助或代码示例 . 只是......任何事情 .


研究

MSDN 上的 Visual Studio Extensibility Samples 引用将我发送到 MSDN Code Gallery 站点,但是在这里,我只能找到 VSX Project SubType 样本(我正在寻找),但是,当我尝试运行由三个项目组成的解决方案时,项目无法加载,因为我的IDE, VS 2013 ,说:

此项目与当前版本的Visual Studio不兼容 .

这让我觉得我的IDE可能缺少必需的组件?

我也在谷歌搜索了很多..当然没有成功,我下载并解压缩了 Copy As HTML 扩展的MSI安装程序,这似乎是一个简单的扩展,可以帮助我理解如何开始这样做,当解压缩时我预计会遇到一个源代码,但我遇到了一个已编译的dll程序集,所以,我想MSDN代码库上的所有其他扩展都没有任何源代码,这意味着我没有任何源代码引用来查看和了解如何完成这项任务 .

我还在 StackOverflow 标签中分析了很多问题为 visual-studio-extensions ,但我没有找到解决方案,只是特定代码或 VSIX 安装程序的具体问题 .


更新

感谢这个问题get the selected text of the editor window..visual studio extension我能够理解检索文本编辑器实例的步骤,并且在对象检查器中进行了一些研究,我找到了替换文本的方法,这是我的VS包的基本代码:

''' <summary>
''' This function is the callback used to execute a command when the a menu item is clicked.
''' See the Initialize method to see how the menu item is associated to this function using
''' the OleMenuCommandService service and the MenuCommand class.
''' </summary>
Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs)
    ' Show a Message Box to prove we were here
    Dim uiShell As IVsUIShell = TryCast(GetService(GetType(SVsUIShell)), IVsUIShell)
    Dim clsid As Guid = Guid.Empty
    Dim result As Integer
    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, clsid, "VSPackage2", String.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", Me.GetType().Name), String.Empty, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_INFO, 0, result))

    Me.ModifySelectedText()

End Sub

Private Sub ModifySelectedText()

    Dim selectionSpan As VirtualSnapshotSpan = Me.GetSelection(Me.GetCurrentViewHost).StreamSelectionSpan
    Dim selectedText As String = selectionSpan.SnapshotSpan.GetText

    Dim sb As New StringBuilder

    sb.AppendLine("''' <example>")
    For Each line As String In selectedText.Split({Environment.NewLine}, StringSplitOptions.None)
        sb.AppendLine(String.Format("''' {0}", line))
    Next
    sb.AppendLine("''' </example>")

    selectionSpan.Snapshot.TextBuffer.Replace(selectionSpan.SnapshotSpan, sb.ToString)

End Sub

Private Function GetCurrentViewHost() As IWpfTextViewHost
    ' code to get access to the editor's currently selected text cribbed from
    ' http://msdn.microsoft.com/en-us/library/dd884850.aspx
    Dim txtMgr As IVsTextManager = DirectCast(GetService(GetType(SVsTextManager)), IVsTextManager)
    Dim vTextView As IVsTextView = Nothing
    Dim mustHaveFocus As Integer = 1
    txtMgr.GetActiveView(mustHaveFocus, Nothing, vTextView)
    Dim userData As IVsUserData = TryCast(vTextView, IVsUserData)
    If userData Is Nothing Then
        Return Nothing
    Else
        Dim viewHost As IWpfTextViewHost
        Dim holder As Object = Nothing
        Dim guidViewHost As Guid = DefGuidList.guidIWpfTextViewHost
        userData.GetData(guidViewHost, holder)
        viewHost = DirectCast(holder, IWpfTextViewHost)
        Return viewHost
    End If
End Function

''' Given an IWpfTextViewHost representing the currently selected editor pane,
''' return the ITextDocument for that view. That's useful for learning things 
''' like the filename of the document, its creation date, and so on.
Private Function GetTextDocumentForView(viewHost As IWpfTextViewHost) As ITextDocument
    Dim document As ITextDocument = Nothing
    viewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(GetType(ITextDocument), document)
    Return document
End Function

''' Get the current editor selection
Private Function GetSelection(viewHost As IWpfTextViewHost) As ITextSelection
    Return viewHost.TextView.Selection
End Function

由于我之前没有收到我的问题的答案,我会问一个随之而来的问题,其实质上是同一个问题:

我的问题是,开发一个简单的文本修饰扩展的正确方法是什么?, Span 和虚拟 Span 和流 Span 以及很多这样的成员真的让我感到困惑,我正在对所选的进行修改是安全的文本? .