fsprojects / fantomas

FSharp source code formatter
https://fsprojects.github.io/fantomas
Other
772 stars 194 forks source link

Indentation removed when combining comments and compiler directives #382

Closed alfonsogarciacaro closed 5 years ago

alfonsogarciacaro commented 5 years ago

Issue created from fantomas-ui

This is a very weird combination of circumstances so not sure if it's fixable. Please look at the example below, after formatting, private loses indentation so the code doesn't compile (and in this case, it only showed up in Fable compilation with apparently unrelated errors so it was a bit difficult to spot it). The interesting part is this only happens when using both the comments between the type name and private keyword and the compiler directive. If you remove any of the two, the formatting is done correctly. Does it ring a bell? If it doesn't, don't worry too much as this will happen very rarely (though in cases like this it may be a good idea to leave the formatting of code between compiler directives untouched).

Code

type Currency =
    // TODO: Temporary fix until a new Thoth.Json.Net package is released
    // See https://github.com/MangelMaxime/Thoth/pull/70

#if FABLE_COMPILER
    private
#endif
    | Code of string

Error

type Currency = // TODO: Temporary fix until a new Thoth.Json.Net package is released
                // See https://github.com/MangelMaxime/Thoth/pull/70

#if FABLE_COMPILER
private
#endif
    Code of string

Options

Fantomas 2.9.1

Name Value
IndentSpaceNum 4
PageWidth 80
PreserveEndOfLine false
SemicolonAtEndOfLine false
SpaceBeforeArgument true
SpaceBeforeColon true
SpaceAfterComma true
SpaceAfterSemicolon true
IndentOnTryWith false
ReorderOpenDeclaration false
SpaceAroundDelimiter true
StrictMode false
nojaf commented 5 years ago

I'm guessing something is wrong in TokenMatcher code. Perhaps setting PreserveEndOfLine to true helps as workaround, but that might lead to other surprises 🙃