C# 使用VisualStudioSDK,如何折叠一段代码?
我可以通过TextSelection获得文本选择。 我可以突出显示特定部分。我将如何折叠该部分,在VisualStudioSDK的文档中找不到任何关于折叠或隐藏部分的内容 编辑1 我要崩溃的代码使用C++语法 编辑2 我正在尝试编写一个允许我折叠代码的扩展,但是我似乎无法从VisualStudioSDK文档中找到如何调用此类选项的参考 可能就是你要找的 它提供了允许您获取所有可折叠区域、折叠区域的方法,以及允许您展开或折叠给定区域的方法 举个例子,你可能会发现这很有用。虽然OP的代码存在问题,但这些问题无法通过链接线程解决,但提供的代码片段可能会让您朝着正确的方向前进。我已将片段包括在下面:C# 使用VisualStudioSDK,如何折叠一段代码?,c#,visual-studio-2015,visual-studio-sdk,C#,Visual Studio 2015,Visual Studio Sdk,我可以通过TextSelection获得文本选择。 我可以突出显示特定部分。我将如何折叠该部分,在VisualStudioSDK的文档中找不到任何关于折叠或隐藏部分的内容 编辑1 我要崩溃的代码使用C++语法 编辑2 我正在尝试编写一个允许我折叠代码的扩展,但是我似乎无法从VisualStudioSDK文档中找到如何调用此类选项的参考 可能就是你要找的 它提供了允许您获取所有可折叠区域、折叠区域的方法,以及允许您展开或折叠给定区域的方法 举个例子,你可能会发现这很有用。虽然OP的代码存在问题,但
[Microsoft.VisualStudio.Utilities.ContentType("text")]
[Microsoft.VisualStudio.Text.Editor.TextViewRole(Microsoft.VisualStudio.Text.Editor.PredefinedTextViewRoles.Editable)]
[Export(typeof(IVsTextViewCreationListener))]
public class Main : IVsTextViewCreationListener
{
private IOutliningManager _outliningManager;
private IVsEditorAdaptersFactoryService _editorAdaptersFactoryService;
public void VsTextViewCreated(IVsTextView textViewAdapter)
{
IComponentModel componentModel = (IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel));
if (componentModel != null)
{
IOutliningManagerService outliningManagerService = componentModel.GetService<IOutliningManagerService>();
_editorAdaptersFactoryService = componentModel.GetService<IVsEditorAdaptersFactoryService>();
if (outliningManagerService != null)
{
if (textViewAdapter != null && _editorAdaptersFactoryService != null)
{
var textView = _editorAdaptersFactoryService.GetWpfTextView(textViewAdapter);
var snapshot = textView.TextSnapshot;
var snapshotSpan = new Microsoft.VisualStudio.Text.SnapshotSpan(snapshot, new Microsoft.VisualStudio.Text.Span(0, snapshot.Length));
_outliningManager = outliningManagerService.GetOutliningManager(textView);
var regions = _outliningManager.GetAllRegions(snapshotSpan);
foreach (var reg in regions)
{
_outliningManager.TryCollapse(reg);
}
}
}
}
}
}
[Microsoft.VisualStudio.Utilities.ContentType(“文本”)]
[Microsoft.VisualStudio.Text.Editor.TextViewRole(Microsoft.VisualStudio.Text.Editor.PredefinedTextViewRoles.Editable)]
[导出(类型(IVsTextViewCreationListener))]
公共类Main:IVsTextViewCreationListener
{
私人IOUTLINING经理_大纲经理;
私人IVSEditor Adapters工厂服务(U Editor Adapters工厂服务);
已创建公共无效VSTEXTVIEW(IVsTextView textViewAdapter)
{
IComponentModel componentModel=(IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel));
if(componentModel!=null)
{
IOOutliningManagerService OutlineingManagerService=componentModel.GetService();
_editorAdaptersFactoryService=componentModel.GetService();
if(OutlineingManagerService!=null)
{
if(textViewAdapter!=null&&U EditorAdapterFactoryService!=null)
{
var textView=_editorAdaptersFactoryService.GetWpfTextView(textViewAdapter);
var snapshot=textView.TextSnapshot;
var snapshotSpan=new Microsoft.VisualStudio.Text.snapshotSpan(快照,new Microsoft.VisualStudio.Text.Span(0,snapshot.Length));
_OutlineingManager=OutlineingManagerService.GetOutlineingManager(textView);
var regions=\u OutlineingManager.GetAllRegions(snapshotSpan);
foreach(区域中的var reg)
{
_大纲经理。TryCollapse(注册);
}
}
}
}
}
}
祝你好运 诸如名称空间、类和方法之类的块通常在其左侧有一个小的“减号”或“加号”,以便您可以轻松地上下滚动它们。如果您想要比这更大的灵活性,并且您正在使用C#,请参阅@ADyson我想OP是在问如何使用VS SDK以编程方式完成它。我想通过扩展来完成。因此,这对我不起作用。