We have integrated the editor in our WinForms application and have some issues with indentation wrt Blocks in both C# method bodies and classes.
Our expectation is to have something similar to what is available in Visual Studio.
Please help us with suggestions/settings to achieve the following:
Auto-indent upon hitting Enter for single line if-else and for loops
When curly brackets are used, upon hitting enter, the opening bracket is always on newline, auto-indent is established between the braces and is kept until the loop is exited. Indentation is restored to original after exiting the block.
So far, we have tried to use IndentOptions on SyntaxEdit control and also SyntaxOptions on the SyntaxParser itself. But, we have not had any success.
It looks like VS not only indents a code when you press Enter inside {} after the method declaration, but it also formats the code (inserts additional line break).
We will look at how to support this behaviour.
As for pressing Enter after if or for statement, on our tests it jumps to the correct indent - please refer to the attached screenshot. Tested with version 8.1
Thanks for your response. We would really be interested in the VS like behavior for indentation when using braces. Please do let us know when there are advancements in this area.
“As for pressing Enter after if or for statement, on our tests it jumps to the correct indent - please refer to the attached screenshot. Tested with version 8.1” – After having a look at the Demo for Roslyn Parser, we do not see any differences in SyntaxOptions on the Roslyn parser(also version 8.1) at our end. We also took over the
protected override bool UseIndentService
{
get { return false; }
}
We see strange behavior when this Override is removed. Is there a special reason for this? Nevertheless, removing this still does not solve our problem. Are there any other settings that need to be respected additionally to make this work?
UseIndentService is true by default for .NET 4.61 or later. If it’s true, indentation is obtained using Roslyn API. Otherwise, we calculate it based on the syntax tree. Could you elaborate on the strange behavior you’re experiencing?
Here are some examples:
In a method context, after the if block, the Indent does not reset. Also the same in case of the single line else statement.
However, what is strange is it works in a class context
Are there some other options that need to be set for the method context?
Just wondering if you could reproduce this behavior with our RoslynSyntaxParsing quick start project, as an indentation after if and else seems to work fine for me.
Alternatively, if you could send me (dmitry.medvedev@alternetsoft.com) a cut-down version of your project which we can reproduce this problem with, we will be able to look further at it.