Omnisharp-vscode: Fold All Regions feature doesn't work

Created on 24 Oct 2018  路  24Comments  路  Source: OmniSharp/omnisharp-vscode

_From @suadev on October 18, 2018 8:42_

  • VSCode Version: 1.28.2
  • OS Version: Windows

"Fold All Regions" feature doesn't work neither by ctrl+k, ctrl+8 nor by F1 menu.

_Copied from original issue: Microsoft/vscode#61220_

Bug

Most helpful comment

@suadev Apologies I got a bit confused here. I am totally able to reproduce the problem.

All 24 comments

_From @vscodebot[bot] on October 18, 2018 8:42_

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

Please add a code sample. I tried with the folloing sample and it worked
foo.ts

//#region
Test
//#endregion
//#region
Test2
//#endregion
  • ctrl + k / ctrl + 8: folds all regions
  • ctrl + k / ctrl + 9: unfolds all regions

_From @suadev on October 23, 2018 10:43_

@aeschli sorry, i should have specified the language.

It looks, it's ok for .ts file.

But for .cs file, it doesn't work.

```
public class foo
{
#region newregion
int i = 0;
#endregion
}

Do you use an extension for c# ?

Out of the box you get indentation based folding with region markers sand your example works the same way as my example.
But maybe an extension adds language specific folding and doesn't mark the regions correctly.

_From @suadev on October 23, 2018 14:14_

Yes, i use c# extension that powered by omnisharp. My whole extension list;

image

The problem is with the C# extension's foldingRangeProvider: Ranges originating from #region/#endregion need be marked with Kind: FoldingRangeKind.Region.

@suadev What are the keys you are using to fold the region. I tried the combination specified here https://code.visualstudio.com/docs/editor/codebasics#_folding and it seems to work fine for me. Note that in using Fold Level X (Ctrl+K Ctrl+2 for level 2) folds all regions of level X, except the region at the current cursor position the region at the current cursor position will not be folded.

@akshita31 as i said in my first post;

"Fold All Regions" feature doesn't work neither by ctrl+k, ctrl+8 nor by F1 menu.

@suadev As per the vscode documentation here : https://code.visualstudio.com/docs/editor/codebasics#_folding, "Fold All" is Ctrl K+ Ctrl 0(zero) and that seems to work fine for me. Can you try that once ?

However I do agree that Fold All regions through F1 doesnt work here.

@akshita31

It's not "Fold All", The feature that i am talking about is "Fold all regions" :)

@suadev Apologies I got a bit confused here. I am totally able to reproduce the problem.

On further investigation, I realised that we are using the "BlockStructureService" from roslyn to provide the spans for CodeFolding. The "BlockType" defined in roslyn doesn't have an explicit type for a "Region" - http://source.roslyn.io/#Microsoft.CodeAnalysis.Features/Structure/BlockTypes.cs,863b31bd5e041d18. For regions it returns the base type called "NonStructural".

In omnisharp-roslyn - https://github.com/OmniSharp/omnisharp-roslyn/blob/94114978fdfa233c1391b6f630f092db5aa87053/src/OmniSharp.Roslyn.CSharp/Services/Structure/BlockStructureService.cs#L80 we are comparing the type and checking if the type == "Region" which is not returned by roslyn as of now, hence we return the null type to the extension and the given region is not marked as a "Region"

@rchande Thoughts here ?

@rchande: anything I can do to help you with the issue? Would be really great to have this working again :) Thanks in advance!

@zeroskyx The issue is that OmniSharp isn't properly marking "region" folding units as having the "region" kind. Thus, when you issue the command in VS Code, the C# extension doesn't think it has any regions to fold.

@rchande: thank you for the clarification -- what would be the steps to fix the issue? It would be really great to have it working again with .NET Core 3 coming up :)

Keep up the great work!

Have you given up on this issue? With region support in almost every language now (even Bash) it's sad to see that this feature only does not work in C# 馃槙

This issue is fixed in 1.21.17

@JoeRobich it doesn't work. Between, when i start vscode, i see this exception;

System.InvalidCastException: Specified cast is not valid.
at Roslyn.Utilities.EventMap.GetRegistries_NoLockTEventHandler
at Roslyn.Utilities.EventMap.AddEventHandlerTEventHandler
at lambda_method(Closure , LifetimeContext , CompositionOperation )
at System.Composition.Hosting.Core.LifetimeContext.GetOrCreate(Int32 sharingId, CompositionOperation operation, CompositeActivator creator)
at System.Composition.Hosting.Providers.ImportMany.ImportManyExportDescriptorProvider.<>c__DisplayClass3_21.<GetImportManyDescriptor>b__4(ExportDescriptor e) at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext()
at System.Linq.Buffer1..ctor(IEnumerable1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at lambda_method(Closure , LifetimeContext , CompositionOperation ) at System.Composition.Hosting.Core.LifetimeContext.GetOrCreate(Int32 sharingId, CompositionOperation operation, CompositeActivator creator) at System.Composition.Hosting.Providers.ImportMany.ImportManyExportDescriptorProvider.<>c__DisplayClass3_21.b__4(ExportDescriptor e)
at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)
at System.Composition.Hosting.Core.CompositionOperation.Run(LifetimeContext outermostLifetimeContext, CompositeActivator compositionRootActivator)
at System.Composition.Hosting.Core.LifetimeContext.TryGetExport(CompositionContract contract, Object& export)
at System.Composition.CompositionContext.GetExport(CompositionContract contract)
at System.Composition.CompositionContext.GetExports(Type exportType, String contractName)
at System.Composition.CompositionContext.GetExportsTExport
at OmniSharp.Stdio.Host.Initialize() in D:\a\1\s\src\OmniSharp.Stdio\Host.cs:line 57
at OmniSharp.Stdio.Host..ctor(TextReader input, ISharedTextWriter writer, IOmniSharpEnvironment environment, IServiceProvider serviceProvider, CompositionHostBuilder compositionHostBuilder, ILoggerFactory loggerFactory, CancellationTokenSource cancellationTokenSource) in D:\a\1\s\src\OmniSharp.Stdio\Host.cs:line 51
at OmniSharp.Stdio.Driver.Program.<>c__DisplayClass0_1.

b__1() in D:\a\1\s\src\OmniSharp.Stdio.Driver\Program.cs:line 66
at OmniSharp.HostHelpers.Start(Func`1 action) in D:\a\1\s\src\OmniSharp.Host\HostHelpers.cs:line 29

It seems, OmniSharp doesn't start properly on my machine. The exception is added on above. Any idea? @aeschli @filipw

do you have a repro project?
also, can you post full Omnisharp log?

@filipw i am working on a dotnet core 3.1 web api project. Tried on another project and got same exception. The whole log:

Starting OmniSharp server at 4/14/2020, 9:31:30 AM
Target: c:\source\Pms.Backend\PMS.sln

OmniSharp server started.
Path: C:\Users\skose.vscode\extensions\ms-dotnettools.csharp-1.21.17.omnisharp\1.34.5-beta.28\OmniSharp.exe
PID: 15168

    Starting OmniSharp on Windows 6.2.9200.0 (x64)
    DotNetPath set to dotnet
    Located 2 MSBuild instance(s)
        1: Visual Studio Enterprise 2019 16.4.29806.167 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
        2: StandAlone 16.0 - "C:\Users\skose\.vscode\extensions\ms-dotnettools.csharp-1.21.17\.omnisharp\1.34.5-beta.28\.msbuild\Current\Bin"
    Registered MSBuild instance: Visual Studio Enterprise 2019 16.4.29806.167 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"

System.InvalidCastException: Specified cast is not valid.
at Roslyn.Utilities.EventMap.GetRegistries_NoLockTEventHandler
at Roslyn.Utilities.EventMap.AddEventHandlerTEventHandler
at lambda_method(Closure , LifetimeContext , CompositionOperation )
at System.Composition.Hosting.Core.LifetimeContext.GetOrCreate(Int32 sharingId, CompositionOperation operation, CompositeActivator creator)
at System.Composition.Hosting.Providers.ImportMany.ImportManyExportDescriptorProvider.<>c__DisplayClass3_21.<GetImportManyDescriptor>b__4(ExportDescriptor e) at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext()
at System.Linq.Buffer1..ctor(IEnumerable1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at lambda_method(Closure , LifetimeContext , CompositionOperation ) at System.Composition.Hosting.Core.LifetimeContext.GetOrCreate(Int32 sharingId, CompositionOperation operation, CompositeActivator creator) at System.Composition.Hosting.Providers.ImportMany.ImportManyExportDescriptorProvider.<>c__DisplayClass3_21.b__4(ExportDescriptor e)
at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)
at System.Composition.Hosting.Core.CompositionOperation.Run(LifetimeContext outermostLifetimeContext, CompositeActivator compositionRootActivator)
at System.Composition.Hosting.Core.LifetimeContext.TryGetExport(CompositionContract contract, Object& export)
at System.Composition.CompositionContext.GetExport(CompositionContract contract)
at System.Composition.CompositionContext.GetExports(Type exportType, String contractName)
at System.Composition.CompositionContext.GetExportsTExport
at OmniSharp.Stdio.Host.Initialize() in D:\a\1\s\src\OmniSharp.Stdio\Host.cs:line 57
at OmniSharp.Stdio.Host..ctor(TextReader input, ISharedTextWriter writer, IOmniSharpEnvironment environment, IServiceProvider serviceProvider, CompositionHostBuilder compositionHostBuilder, ILoggerFactory loggerFactory, CancellationTokenSource cancellationTokenSource) in D:\a\1\s\src\OmniSharp.Stdio\Host.cs:line 51
at OmniSharp.Stdio.Driver.Program.<>c__DisplayClass0_1.

b__1() in D:\a\1\s\src\OmniSharp.Stdio.Driver\Program.cs:line 66
at OmniSharp.HostHelpers.Start(Func`1 action) in D:\a\1\s\src\OmniSharp.Host\HostHelpers.cs:line 29

according to your log, you are on the old version of omnisharp
Path: C:\Users\skose.vscode\extensions\ms-dotnettools.csharp-1.21.17.omnisharp\1.34.5-beta.28\OmniSharp.exe

it should be 1.35.0.

check if you have any omnisharp.path setting in VS Code config and if you do, remove it.
Then try removing the extension and installing it again.

@filipw done :) Thank you !

Was this page helpful?
0 / 5 - 0 ratings