-
-
Notifications
You must be signed in to change notification settings - Fork 101
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
Code in IfDirective can't currently be formatted #15
Comments
PreprocessorSymbols can be passed like so var syntaxTree = CSharpSyntaxTree.ParseText(
code,
new CSharpParseOptions(
LanguageVersion.CSharp9,
DocumentationMode.Diagnose,
preprocessorSymbols: new[] { "DEBUG" }
),
cancellationToken: cancellationToken
); We also have cases where we can't just send in one symbol. In this case we'd need to parse with and without DEBUG
The problems are
The idea starting to take shape in my head
This will probably be complicated quite a bit by the situation in #121 |
What might be helpful as a workaround for now is for csharpier to support being passed the list of defines to use. This would unblock those of us like myself that have a lot of code inside preprocessor blocks that are currently skipped, since we could just run csharpier multiple times for all the different preprocessor flags we want covered |
I think your workaround won't be too difficult to implement... and actually csharpier may just be able to reformat the file itself multiple times. First without any defines and then once for each define it finds. That will be way easier than my original idea if it works. |
What about complex conditionals like Either that or read which different combinations of flags are actually used. Does Roslyn expose that information? |
doh, you are right. |
My current plan
preprocessorSymbols: [
["FOO", "BAR"],
["QUX"],
["DEBUG"]
] Long term we can hopefully handle the more complicated conditions automatically without resorting to formatting a file with every possible combination. With just 6 symbols there would be 720 combinations. Nested directives have me a little worried though. This is valid #if DEBUG1
#if OTHER
public string ShortPropertyName;
#endif
#endif Supplying the symbol sets per file is another idea I had (if we can't figure the sets out ourselves). It would be more efficient for large code bases where only a few files need them. |
I wonder if the order of format operations for the different defines could affect the result? If so it might be important to make that order consistent |
Adding some basic validation support for disabled text closes #15
Adding some basic validation support for disabled text closes #15
I just tested this and you were right. Good call! If I went with my original plan of combining doc trees, then I think that could be avoided. But I think that approach is just not viable. For example how would you possibly combine the doc tree with code like this? #if LOOP
do
{
#endif
#if TRY
try
{
#endif
#if LOOP
do
{
for (int i = 0; i < 10; i++)
{
#endif This is also a good example of a case where the order it is formatted in affects the indentation level of the code after the I think for now just keeping the order consistent like you suggested will avoid the problem. |
Adding some basic validation support for disabled text closes #15
* Support for basic if/elif directives Adding some basic validation support for disabled text closes #15 * Fixing unit tests * Making some progress on edge cases * Self code review * Change format of PreprocessorSymbols * Self code review * Cleanup after rebase * Cleaning up some edge cases around new lines
Given the following IfDirective, roslyn does not parse the contents because DEBUG is not defined
The contents are just a block of text.
Can I determine some way to parse those contents and format them? Currently they are just written out as is.
The text was updated successfully, but these errors were encountered: