使用 Visual Studio SDK,我将如何折叠一段代码?

我可以通过 TextSelection 选择文本。我可以突出显示特定部分。我将如何折叠所述部分,在 Visual Studio SDK 的文档中找不到有关折叠或隐藏部分的任何内容。

编辑 1 我要折叠的代码使用 c++ 语法

编辑 2 我正在尝试编写一个允许我折叠代码的扩展,但是我似乎无法从 Visual Studio SDK 文档中找到有关如何调用此类选项的参考。


青春有我
浏览 230回答 1
1回答

慕田峪9158850

IOutliningManager可能正是您要找的。它提供了允许您获取所有可折叠区域、折叠区域的方法,以及允许您展开或折叠给定区域的方法。例如,您可能会发现这很有用。尽管 OP 的代码存在未通过链接线程解决的问题,但提供的代码片段可能会让您朝着正确的方向前进。我已经包含了下面的片段:[Microsoft.VisualStudio.Utilities.ContentType("text")][Microsoft.VisualStudio.Text.Editor.TextViewRole(Microsoft.VisualStudio.Text.Editor.PredefinedTextViewRoles.Editable)][Export(typeof(IVsTextViewCreationListener))]public class Main : IVsTextViewCreationListener{&nbsp; &nbsp; private IOutliningManager _outliningManager;&nbsp; &nbsp; private IVsEditorAdaptersFactoryService _editorAdaptersFactoryService;&nbsp; &nbsp; public void VsTextViewCreated(IVsTextView textViewAdapter)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; IComponentModel componentModel = (IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel));&nbsp; &nbsp; &nbsp; &nbsp; if (componentModel != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IOutliningManagerService outliningManagerService = componentModel.GetService<IOutliningManagerService>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _editorAdaptersFactoryService = componentModel.GetService<IVsEditorAdaptersFactoryService>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (outliningManagerService != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (textViewAdapter != null && _editorAdaptersFactoryService != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var textView = _editorAdaptersFactoryService.GetWpfTextView(textViewAdapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var snapshot = textView.TextSnapshot;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var snapshotSpan = new Microsoft.VisualStudio.Text.SnapshotSpan(snapshot, new Microsoft.VisualStudio.Text.Span(0, snapshot.Length));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _outliningManager = outliningManagerService.GetOutliningManager(textView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var regions = _outliningManager.GetAllRegions(snapshotSpan);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var reg in regions)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _outliningManager.TryCollapse(reg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}祝你好运!
打开App,查看更多内容
随时随地看视频慕课网APP