Programmatically collapse source code blocks from a Visual Studio extension

29 views Asked by At

I've written a VS extension which modifies C# source files. The extension applies changes to the source code file which is currently open and visible in the IDE. One aspect of the extension is to insert #region and #endregion directives surrounding the inserted source lines. What I'd like to do from the extension is collapse that region of code in the visible editor window.

I've been searching around for days, and can't find any previous examples of how this can be done.

Any guidance would be greatly appreciated.

1

There are 1 answers

0
Sergey Vlasov On

You can import Microsoft.VisualStudio.Text.Outlining.IOutliningManagerService, call GetOutliningManager and then use TryCollapse.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.text.outlining.ioutliningmanager.trycollapse?view=visualstudiosdk-2022