You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should not move the starting point of a single-line comment #1233
changed the title [-]Should not move the starting point of a single-line comment (stylistic)[/-][+]Should not move the starting point of a single-line comment[/+]on Nov 10, 2020
So I analyzed this example using the fantomas tool,
the Trivia containing the comment is linked to SynModuleDecl_Types, this causes it to use the same range as the whole declaration type, ignoring the old position.
I see two possible solutions for this particular case:
Make Trivia use the SynExp_TryWith node, or another node closer to the comment, instead of the SynModuleDecl_Types node.
When creating the Trivia with the tokens in the collectTrivia function, add in the range parameter the length of the whitespace token before it, thus maintaining the initial position.
What do you think @nojaf ?
For me these two solutions would impact other parts of the code, especially the 2.
Hello Mike, this is known problem and appears in multiple places.
Trying to solve this will impact the entire unit test suite. And that is the tricky part, you really need to nail it in many scenarios.
Option 1 is the only that would work in my opinion. You'll need to do something clever in
To select the better trivia node to attach the comment to. In the past I did some attempts myself and well, let's say it is really challenging to get all cases right.
Next to that please keep performance in mind, this part is now already one of the slowest parts of Fantomas.
Doing more checks here could really have a bad impact.
I take your idea of adding the missing spaces in the comment and print the new string at the right indentation,
but I'm having a double indentation, see should keep comment position - replicate of ( attribute, line comment, attribute, new line, record definition field ) test in CommentTests.fs for example.
atIndentLevel true 0 <string> should write <string> from the start of the line without adding any indentation right or I'm missing something?
Hey Mike, the thing I suggested in #1393 is a solution for that problem, not this one.
That solution really talks about adding missing spaces within a BlockComment, it does not touch the subject of adding spaces before (or after) a comment.
This information cannot reliably come from the source code. Example: if the indentation in the source code is 2 spaces and the configuration to format is 4 spaces this idea will never work.
I described how this problem should be solve in my comment above in this thread. Using a solution for another problem doesn't really fit the bill here.
Allow me to again re-elaborate the rational:
Imagine the following code:
letfoo a =
someLongFunctionCall parameterOne parameterTwo parameterThree
// bar
The line comment // bar is found on line 3.
Now that comment needs to be assigned to some AST node that will be processed in CodePrinter.
Currently it is assigned as ContentAfter for the SynModuleDecl_Let node. That is why it is formatted on the start of the line 3:
letfoo a =
someLongFunctionCall parameterOne parameterTwo parameterThree
// bar
The better candidate node in this case was SynExpr_App (2,4) (2,65). So the trivia assignment needs a better algorithm to make the correct choice.
Activity
Add fantomas configuration file
[-]Should not move the starting point of a single-line comment (stylistic)[/-][+]Should not move the starting point of a single-line comment[/+]Add fantomas configuration file
btzo commentedon Jan 27, 2021
So I analyzed this example using the fantomas tool,
the
Triviacontaining the comment is linked toSynModuleDecl_Types, this causes it to use the same range as the whole declaration type, ignoring the old position.I see two possible solutions for this particular case:
Make
Triviause theSynExp_TryWithnode, or another node closer to the comment, instead of theSynModuleDecl_Typesnode.When creating the
Triviawith the tokens in thecollectTriviafunction, add in the range parameter the length of the whitespace token before it, thus maintaining the initial position.What do you think @nojaf ?
For me these two solutions would impact other parts of the code, especially the 2.
nojaf commentedon Jan 27, 2021
Hello Mike, this is known problem and appears in multiple places.
Trying to solve this will impact the entire unit test suite. And that is the tricky part, you really need to nail it in many scenarios.
Option 1 is the only that would work in my opinion. You'll need to do something clever in
fantomas/src/Fantomas/Trivia.fs
Lines 327 to 339 in 7b6419a
To select the better trivia node to attach the comment to. In the past I did some attempts myself and well, let's say it is really challenging to get all cases right.
Next to that please keep performance in mind, this part is now already one of the slowest parts of Fantomas.
Doing more checks here could really have a bad impact.
btzo commentedon Feb 10, 2021
Hey @nojaf , I'm trying to solve the single-line comments problem based on #1393 discussion.
I added the initial column position of the comment in the
LineCommentOnSingleLinetype.You can see all that in this branch.
I take your idea of adding the missing spaces in the comment and print the new string at the right indentation,
but I'm having a double indentation, see
should keep comment position - replicate of ( attribute, line comment, attribute, new line, record definition field )test inCommentTests.fsfor example.atIndentLevel true 0 <string>should write<string>from the start of the line without adding any indentation right or I'm missing something?nojaf commentedon Feb 10, 2021
Hey Mike, the thing I suggested in #1393 is a solution for that problem, not this one.
That solution really talks about adding missing spaces within a
BlockComment, it does not touch the subject of adding spaces before (or after) a comment.This information cannot reliably come from the source code. Example: if the indentation in the source code is 2 spaces and the configuration to format is 4 spaces this idea will never work.
I described how this problem should be solve in my comment above in this thread. Using a solution for another problem doesn't really fit the bill here.
Allow me to again re-elaborate the rational:
Imagine the following code:
The line comment
// baris found on line 3.Now that comment needs to be assigned to some AST node that will be processed in
CodePrinter.Currently it is assigned as ContentAfter for the
SynModuleDecl_Letnode. That is why it is formatted on the start of the line 3:The better candidate node in this case was
SynExpr_App (2,4) (2,65). So the trivia assignment needs a better algorithm to make the correct choice.Backend: format with fantomas v4.4.0-beta-008
Backend: format with fantomas v4.5.3
Backend: format with fantomas v4.5.3
Should not move the starting point of a single-line comment
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
79 remaining items
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
Respect column info for single line comments
Bump the dotnet-sidecars group with 6 updates (#257)
Bump the packages group with 5 updates (#106)
Bump the packages group with 6 updates (#84)