-
-
Notifications
You must be signed in to change notification settings - Fork 193
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
Refactor Preserve end of line #390
Comments
I am bad at English and will have a hard Time to explain such complex things. So i will simplify and assert things for facts that probably are nether simple nor facts are. The fantomas remove Newlines/Spacing not for a Style reason but because of the a limitation in the implementation. The newlines/spacing should be preserved per default and removed/inserted by Style formatter. And not the other way around, removed and then inserted by formatter, like fantomas does it. To do so, one need both Lexer/Tokenizer and Parser Information. FSharpTokenInfo for spacing/eol information and AST Info to get symantics of a Token in code, because e.i. a "<" Token could be part of generics or a operator, a "X" Identifier could be a expresion or value, it could be anything. I tried to create such a structure but failed, as the amount of work needed is just to much for me. It may be not obvious but the TokenMatcher.fs -> "integrateComments" is just a hacky way to get parser and lexer information together, the formatted source part is based on parser and original source is tokenized. My preserve lines solution is build upon it. But i have seen multiple limitation in this approach and i think it is not possible to reverse all needed Information by diff on formatted and original source tokens. Sofar I do not have a good solution for preserve new lines in fantomas, the reason i wrote my own formatter. But i hope my thoughts about this Problem will help you, or at least entertain you. |
Hello @s-trooper , thanks for the information. Would you agree with the statement that the problem |
Sure, from the end user perspective eol disappears after formatting and that to prevent is the goal of "--preserve-eol". I am not sure if this is part of the question but, can "--preserve-eol" be handled in isolation, not as part of style formatters? I doubt it, spacing and most importend indentation (this is spacing after all too) are strong coupled to "preserve eol" problem space.
it can/will be formated to:
in this case one have to keep original spacing when eol should be preserved. |
Thanks for the sample code, this illustrates the part of eol I would consider not to preserve actually. What I do think the setting let a = 7
let b = 8 Both This is the only case where I should preserve the newlines. @jindraivanek what do you think? |
I would argue about "point of formatting", but i got your idea. You like to preserve blank lines only and that was the main issue anyway. What about statements? like:
But the idea is nice! the |
Yes, it should be more elaborate than just Question is if we should bound the proposed behavior to a setting? I'm considering no as I think it won't upset people that this blank lines are preserved. |
I think there is 3 separate things that preserve-eol try to do today:
Problem with 2) and 3) is that to do them correctly we need to know correct indentation and that need to be done in CodePrinter. I think 1) can be done in TokenMatcher like now. One more complexity with moving logic to CodePrinter is that we need to know I think main question is if we can change preserve-eol to do only 1), I'm not against it, but maybe there is some people that using 2) as workaround for:
Not sure if 3) have some good use. With all that in mind, I propose we:
|
As hinted before I think we should only be doing
to me that sounds like I believe people format code for consistency in their code-base. And if something is bothering people in a persistent way they should raise an issue. It may be discussed and perhaps the F# style guide could provide the necessary guidance. #386 is a good example here. I suggest we remove |
I agree, but I don't want to break existing functionality without providing alternative.
I think "don't care about empty lines" mode is still useful. |
Well I firmly believe that introducing settings is the root of all evil 😋. |
(*) i would vote for this as most flexible, but it would remove original amount of empty lines or use (-1) for that case? |
To clarify, I'm not for removing all empty lines, just for option where existence of empty line don't depend on source. I like Regarding breaking existing functionality, we can also start this as some beta of next major version and release when we have proper solution to newline placement. |
Ok, summarizing the outcome of this:
|
@nojaf I do like this one as well:
I usually place an empty line between top level let bindings and members and two lines between types. Removing extra lines would be handy. |
@auduchinok thanks for this input. We decided that we would focus on bug fixing other things first. On another note @jindraivanek maybe we should already indicate that |
Problem
The setting
--preserveEOL
will try and preserve the original end of lines.Meaning if multiple newlines were inserted somewhere in the code, these are preserved after the formatting.
before:
after:
Without this settings fantomas would place the two
let
bindings directly underneath each other.When the formatted code is constructed purely from the information stored in the AST (strict mode), fantomas will output:
In TokenMatcher.fs, the original tokens are being compared with the newly constructed tokens from AST.
When a newline is found in the old tokens it is being written
and the helper function indentWithNewSpacing will try and set the correct indentation of the next line.
However this helper function is somewhat confusing and does not cover all cases. It is difficult to solve #362 when other settings are also in place.
Proposed solution
Fixing the preservation of newlines in the TokenMatcher might not be the only place where we can fix this.
Given our sample code we can view in the ranges of the AST that there is an additional newline.
It would also be possible to add an additional newline in the CodePrinter.fs.
Drawbacks
F.ex
should we also check in the AST if it is ok to insert an extra newline after
z
? Or is this overkill?I mean formatting records and arrays/lists f.ex, should that not be in sync with the style guide instead of original newlines?
@s-trooper we (@jindraivanek and myself) would like your opinion as you made the first implementation of preserve newline.
Perhaps we also need some unification of newline break logic.
The text was updated successfully, but these errors were encountered: