VS 2013 SDK:如何开发简单的选定文本装饰扩展?

时间:2015-07-01 10:04:33

标签: c# .net vb.net visual-studio visual-studio-extensions

方案

让我们以这个随机代码为例,它在 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

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

我的问题是,是开发简单文本装饰扩展的正确方法吗?跨度和虚拟跨度和流跨度以及很多这样的成员真的让我感到困惑,这是我安全的修改。 m对所选文本执行?。

0 个答案:

没有答案