2015-11-09 90 views
4

我正在做一个Visual Studio扩展,它包含编辑器边距,它显示每个代码行的注释和通过行号链接的数据。Visual Studio中的折叠区域

当我的代码有折叠区域(函数,代码块或区域)时,我得到不正确的行数。

如何计算折叠的线条?或者更简单地说,我如何展开所有折叠块并禁用折叠按钮?

+0

你确定你所有匹配的'#endregion'开始'#地区~~~'代码..? '〜'=您的地区名称 – MethodMan

+0

不仅在#区域中存在问题。功能也可能崩溃 –

+0

这要么不清楚,要么太宽泛。你可以扩大一点/给一些例子/显示你的尝试? – kebs

回答

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 

要求referenses到:microsoft.visualstudio.shell.immutable.10.0,Microsoft.VisualStudio.ComponentModelHost和,当然,Microsoft.VisualStudio.Text

相关问题