简体   繁体   中英

Collapsed regions in Visual Studio

I'm doing a Visual Studio extension and it contains editor margin, which show comment for each code line and data linked by line number.

When I have code have collapsed regions (functions, code blocks or regions), I'm getting incorrect line numbers.

How can I calculate what lines collapsed? Or more simply, how can I expand all collapsed blocks and disable collapse buttons?

I found solution:

    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

Require referenses to: microsoft.visualstudio.shell.immutable.10.0, Microsoft.VisualStudio.ComponentModelHost and, sure, Microsoft.VisualStudio.Text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM