New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose CSharpCodeStyleOption in API #29612
Comments
The expected form here is to not try to do this manually, but to just put teh appropriate annotations on things and allow the formatter to format the document after you've made your changes. This happens automatically if you use the Analyzer/CodeAction infrastructure. Specifically, it happens here: roslyn/src/Workspaces/Core/Portable/CodeActions/CodeAction.cs Lines 264 to 281 in e086a1d
Trying to do this yourself is actually quite complex as there is a lot involved with any formatting rule. If you try to do it yourself you will effectively just be trying to replicate all the logic already baked into the formatting engine and the existing formatting rules. |
How I can achieve that? |
By calling
Simplifying is unnecessary here. As mentioned, you need to use the formatter to ensure proper formatting. |
Oh... i got confused about your title. You said "CSharpFormattingOptions". But 'PreferBraces' is a CSharpCodeStyleOption. I agree we should have some mechanism for the right thing to be done here. Note that 'PreferBraces' is poorly named. It's really more about 'Require Braces'. So regardless of the setting, we always put in braces. It's just htat if the value is 'false' the user isn't told there's a problem if they're missing. So, for your fix, just always add the braces. |
Have filed #29621 over hte confusion about the name 'PreferBraces'. |
Sorry, copy-pasted wrong class name into the title. I'll change it to make more descriptive. |
Considering that nothing in VS supports that option or tries to enforce that... it would be hard to say it's a problem for you to behave entirely like the rest of VS :) |
So, just to clarify - there are no plans to expose CSharpCodeStyleOption right now, all settings should be applied automatically? |
Is there a reason you need CSharpCodeStyleOption exposed? Could you clarify what you would use it for? |
Practically, any fixers that touching something affected by code style settings. Or something like "prefer expression body for methods/constructors/operators". Some fixers could have one-liners and it good to know if it is a preferred way to have expression body or fixer should generate more "old school" code. So, practically, it is a question of being able to create more convenient fixers, that could do exactly what user wants to have, not only some default (or "safe") behavior. |
As mentioned, there is no IDE preference controlling that. Roslyn behavior is to always use braces. There is only an option stating how severe a problem it is if no braces are used. But there is no preference to say "do not use braces". The preference is always "use braces" :)
The expectation is that fixers should not have to be aware of this. They should just generate the new code and the Roslyn code fix infrastructure will fix things up to match user preference. Note that we do this now with the 'var vs explicit-type' preference. The plumbing has not gone through to support expression-bodies yet, but it's on list to do at some point. The benefit here is that it's now no longer necessary for each feature to have to figure this out. They should just be able to do something like make a method, and if it's got the right annotation on it, the code-fix system will just convert it to match user style.
As mentioned, if you create code with types in it now (like a local-declaration), the IDE will attempt to make it match the user's code-style preference. I believe this happens automatically if you use SyntaxGEnerator, and you can opt-into it by adding the Simplification annotation on a local-declaration if you create one manually. |
I strongly agree with that. However, i don't think the right approach here is to expose options. Options aren't that helpful here because now the fix author has to still do all the work necessary to figure out what to do with teh varying options. For example, say the "use var" option is on, how does a fix-author still find out if/when 'var' would be legal in tihs case? Or say the option is set to "use var when type is apparent". How does a fix-author know if the type is apparent in this case? The right way IMO, is to have the fix author opt-in (or potentially opt-out) and have the Roslyn system handle this itself taking into account the options as well as all the accumulated smarts it has built up in these areas over its lifetime. -- Note: say we did finally have an option to "do not have braces if not necessary". Then i would want Roslyn's fix infrastructure to apply that to any annotated node. That way people could just make fixers that created if-statements, and they would all come out according to the user's preferences, without every fix-author having to read and support this option. |
It would be great to have automatically applied style settings. Or, there are plans to changes settings in a way to allow Roslyn change any code to the desired style? I have feeling that this is not a simple task, and probably not possible to have backward compatibility with current settings set (like "Require braces" is not compatible with "prefer no braces for one-liners"). UPDATE |
The idea is this: as the code-fixer, you generate the code in either style (usually the more verbose style, since you can think about less stuff). i.e. if you working with Note: this is already what we do today. If you use |
Note: It migt be good to consider a different annotation as well. i.e. CodeStyle.Annotation instead of Simplifer.Annotation. The reason for this (for me at least) is that the 'Simplifier' annotation has the semantics of applying to all nodes under an annotated node. It's unclear to me if we'd want that for CodeStyle. i.e. if you wrapped some existing code with an 'if', it's fine to say "the 'if' should match the user's code style", but it doesn't mean you want all nested code to be updated. |
Interesting! Sounds like something worth doing. I'll see if i have time over the next couple of weeks to do this. Prefer-Braces is small enough and nicely contained enough to likely make this a good test candidate for this design. |
Thank you! Please, update this thread so I'll be able to try it |
At this moment it seems to be impossible to get style options for C# in code fixer.
For example, I can get few generic CodeStyleOptions or CSharpFormattingOptions, but can't get CSharpCodeStyleOptions.
This behavior complicates the development of some extensions. For example:
or
It could be really useful if analyzer automatically detects PreferBraces option and decide how to build a statement. I see some usage examples inside of Roslyn, but it is not an option when trying to do that outside.
I understand that there could be implications to do that, or maybe even another way to achieve my goal. If yes, could you please elaborate it?
The text was updated successfully, but these errors were encountered: