-
-
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
Don't add newline to module where the first trivia is a newline #809
Don't add newline to module where the first trivia is a newline #809
Conversation
src/Fantomas/Context.fs
Outdated
let internal sepNlnConsideringTriviaContentBeforeAndAfter (moduleRange:range) ctx = | ||
match findTriviaMainNodeOrTokenOnStartFromRange ctx.Trivia moduleRange with | ||
| Some node -> | ||
if hasPrintableContent node.ContentBefore then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be simplified to
if hasPrintableContent node.ContentBefore || hasPrintableContent node.ContentAfter then
ctx
else
sepNln ctx
Because I think this won't work if you have
module foo
// bar
// meh
@ErikSchierboom you are in the right direction here. Please also fix this for signature files, as the bug appears there as well. You current fix will also not work for namespaces I think.
|
Alright thanks! I'll work on this some more. |
d558870
to
d6971e5
Compare
src/Fantomas.Tests/ModuleTests.fs
Outdated
""" | ||
|
||
[<TestCase(true)>] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: I'd prefer a not using the TestCase
attribute but duplicate the test to SignatureTests.fs.
To remain consistent with the rest of the codebase and to easier spot regressions.
This is clear now but it will be confusing that a test fails in ModuleTests while changing something in Signature AST for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!
5a731d7
to
cbd2cce
Compare
cbd2cce
to
9068795
Compare
src/Fantomas/Context.fs
Outdated
@@ -749,6 +749,12 @@ let private findTriviaMainNodeOrTokenOnEndFromRange nodes (range:range) = | |||
|> List.tryFind(fun n -> | |||
Trivia.isMainNode n && RangeHelpers.rangeEq n.Range range | |||
|| Trivia.isToken n && RangeHelpers.rangeEndEq n.Range range) | |||
|
|||
let private findTriviaMainNodeOrTokenContainedInRange nodes (range:range) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this function to TriviaHelpers.fs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I thought it should be added here due to Context.fs
already having findTriviaMainNodeOrTokenOnStartFromRange
and findTriviaMainNodeFromRange
functions. But I can move it to TriviaHelpers.fs
. Would you like it to be renamed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah TriviaHelpers came after we already have some stuff in Context.fs
Not everything was perfectly moved to TriviaHelpers.
Please move and rename your function, we can move the other functions in a cleanup PR.
TriviaNodeType can only be two things so the Trivia.isMainNode
and Trivia.isToken
can also be left out I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, done! I've renamed the function to findInRange
to better fit with the existing findByRange
function in TriviaHelpers.fs
. Let me know if that is okay or if you' prefer a different name.
Hey @ErikSchierboom! Very readable code, many thanks for this PR. |
Thanks for the review! I'll apply your suggestions (tomorrow). |
9068795
to
ef3e519
Compare
ef3e519
to
2123819
Compare
I've taken a stab at fixing #784, but I'd like to check if I'm heading in the right direction first. The problem is that the
sepModuleAndFirstDecl
checks for any declaration. If there is no such declaration, two newlines are added after the module declaration. As comments are no declarations, this means that the comment will also receive two newlines. The main problem is that running format one more time after that, will add another newlines (there will be three at this point), and so on.Let me know if I'm heading in the right direction with my solution.