Skip to content
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

Merged
merged 2 commits into from
May 12, 2020

Conversation

ErikSchierboom
Copy link
Contributor

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.

let internal sepNlnConsideringTriviaContentBeforeAndAfter (moduleRange:range) ctx =
match findTriviaMainNodeOrTokenOnStartFromRange ctx.Trivia moduleRange with
| Some node ->
if hasPrintableContent node.ContentBefore then
Copy link
Contributor

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

@nojaf
Copy link
Contributor

nojaf commented May 4, 2020

@ErikSchierboom you are in the right direction here.
Maybe change sepNlnConsideringTriviaContentBeforeAndAfter to sepNlnForEmptyModule because I don't think this edge case can happen anywhere else at first glance.

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.
Consider this example, notice that the namespace has a weird range in the AST.

ImplFile
  (ParsedImplFileInput
     ("tmp.fsx",true,QualifiedNameOfFile Tmp$fsx,[],[],
      [SynModuleOrNamespace
         ([Foo],false,DeclaredNamespace,[],
          PreXmlDoc ((1,9),FSharp.Compiler.XmlDoc+XmlDocCollector),[],None,
          tmp.fsx (3,6--3,6) IsSynthetic=false)],(true, true)))

The comment is linked after the ident instead there:
image

@ErikSchierboom
Copy link
Contributor Author

Alright thanks! I'll work on this some more.

@ErikSchierboom ErikSchierboom force-pushed the module-comment-newline branch 3 times, most recently from d558870 to d6971e5 Compare May 5, 2020 16:19
"""

[<TestCase(true)>]
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

@ErikSchierboom ErikSchierboom force-pushed the module-comment-newline branch from 5a731d7 to cbd2cce Compare May 10, 2020 13:32
@ErikSchierboom ErikSchierboom marked this pull request as ready for review May 10, 2020 13:41
@ErikSchierboom ErikSchierboom force-pushed the module-comment-newline branch from cbd2cce to 9068795 Compare May 10, 2020 13:42
@ErikSchierboom ErikSchierboom requested a review from nojaf May 10, 2020 13:43
@@ -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) =
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@nojaf
Copy link
Contributor

nojaf commented May 10, 2020

Hey @ErikSchierboom! Very readable code, many thanks for this PR.

@ErikSchierboom
Copy link
Contributor Author

Thanks for the review! I'll apply your suggestions (tomorrow).

@ErikSchierboom ErikSchierboom force-pushed the module-comment-newline branch from 9068795 to ef3e519 Compare May 11, 2020 12:26
@ErikSchierboom ErikSchierboom force-pushed the module-comment-newline branch from ef3e519 to 2123819 Compare May 12, 2020 07:06
@nojaf nojaf merged commit c64f61e into fsprojects:master May 12, 2020
@ErikSchierboom ErikSchierboom deleted the module-comment-newline branch May 12, 2020 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants