Visual Studio中的折叠区域

4

我正在开发一个Visual Studio扩展程序,其中包含编辑器边缘,它会显示每行代码的注释和与行号相关联的数据。

当我的代码有折叠区域(函数、代码块或区域)时,我得到了错误的行号。

如何计算哪些行被折叠?或者更简单地说,如何展开所有折叠的块并禁用折叠按钮?


你确定你的起始 #region ~~~ 代码有所有匹配的 #endregion 吗?其中 ~ 是你的区域名称。 - MethodMan
问题不仅在于 #regions。函数也可以被折叠。 - Pavel Hairullin
这个问题不够清晰,或者范围过于广泛。您能否再详细阐述一下,给出一些示例或展示一下您已经尝试的内容? - kebs
1个回答

1
我找到了解决方案:

    Dim _outliningManager As Outlining.IOutliningManager
Dim _oldCollapsedRegions As List(Of ICollapsed)

'Getting access to ServiceProvider
<Import>
Dim ServiceProvider As SVsServiceProvider

'Getting IOutliningManagerService object
 Dim componentModel As IComponentModel = ServiceProvider.GetService(GetType(SComponentModel))
 Dim outliningManagerService As IOutliningManagerService = componentModel.GetService(Of IOutliningManagerService)()

    'Creating IOutliningManager for current textView
    If outliningManagerService IsNot Nothing Then
        _outliningManager = outliningManagerService.GetOutliningManager(textView)
        If _outliningManager IsNot Nothing Then AddHandler _outliningManager.RegionsCollapsed, AddressOf RegionCollapsed

    End If

    'Getting all regions and expand them
    If _outliningManager IsNot Nothing Then
        Dim snapshot = _textView.TextSnapshot
        Dim snapshotSpan = New Microsoft.VisualStudio.Text.SnapshotSpan(snapshot, New Microsoft.VisualStudio.Text.Span(0, snapshot.Length))
        _oldCollapsedRegions = _outliningManager.GetCollapsedRegions(snapshotSpan).ToList()
        For Each reg In _oldCollapsedRegions
            _outliningManager.Expand(reg)
        Next
    End If


'Blocking regions collapsed
Sub RegionCollapsed(sender As Object, e As RegionsCollapsedEventArgs)
    If Me.Visibility = Windows.Visibility.Visible Then
        For Each reg In e.CollapsedRegions
            _outliningManager.Expand(reg)
        Next
    End If
End Sub

需要引用以下内容:microsoft.visualstudio.shell.immutable.10.0、Microsoft.VisualStudio.ComponentModelHost以及Microsoft.VisualStudio.Text。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接