Skip to content

Comment without code after it in the current scope should retain their "parent" indentation #2286

Closed

Description

@MangelMaxime

Issue created from fantomas-online

Code

module Test

let generateBinding () =
    if true then
        // Update the Femto metadata
        ()

    // Otherwise, do nothing

Result

module Test

let generateBinding () =
    if true then
        // Update the Femto metadata
        ()

// Otherwise, do nothing

Problem description

Please describe here the Fantomas problem you encountered.
Check out our Contribution Guidelines.

Extra information

  • The formatted result breaks my code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.

Options

Fantomas master branch at 2022-05-30T18:11:41Z - 5e92752

Default Fantomas configuration

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

Activity

  1. nojaf commented on Jun 3, 2022

    @nojaf
    Contributor

    Hello, this is a known problem.
    I would most welcome a fix for this, alas it is not an easy problem to tackle.
    Are you invested in submitting a PR for this?

  2. MangelMaxime commented on Jun 9, 2022

    @MangelMaxime
    ContributorAuthor

    Hello @nojaf,
    I can't really invest the time needed for submitting a PR, I don't have the time for that right now.

  3. github-actions commented on Mar 30, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    I investigated this with the current Fantomas main branch and the bug is still present. Here's the root cause.

    Root Cause

    The problem is in how Fantomas assigns comment trivia to Oak nodes.

    Consider the input:

    module Test
    
    let generateBinding () =
        if true then
            // Update the Femto metadata
            ()
    
        // Otherwise, do nothing
    ```
    
    When Fantomas builds the Oak tree, the `BindingNode` for `generateBinding` has range `(4,0)–(7,10)` — it ends where the `()` expression ends (line 7, column 10). The `// Otherwise, do nothing` comment is at line 9, column 4, which is **outside** this range.
    
    Running `scripts/oak.fsx` reveals:
    ```
    Oak((2,0--9,28)
      ModuleOrNamespaceNode((2,0--7,10)
        BindingNode((4,0--7,10)
          ExprIfThenNode((5,4--7,10)
            ...
          )
        )
        Newline(range: (8,0--8,0))
        CommentOnSingleLine(range: (9,4--9,28), "// Otherwise, do nothing")
      )
    )

    The comment ends up as ContentAfter of the ModuleOrNamespaceNode (the smallest Oak node whose range contains the comment's range is the Oak root, which has range (2,0)–(9,28); simpleTriviaToTriviaInstruction then places it after the module's last child). When printed, the ModuleOrNamespaceNode is at indent level 0, so the comment is emitted at column 0 instead of column 4.

    When a following declaration exists (e.g., let anotherFunc () = ()), the comment is placed as ContentBefore of that next binding — still at module-level column 0.

    Why it's Hard

    The F# parser does not extend binding ranges to include trailing comments. The comment is at column 4, which visually "belongs" to the generateBinding function body, but the AST has no way to express this. Fantomas' trivia attribution in Trivia.fs uses AST range containment, not column indentation heuristics.

    Potential Fix Direction

    In Trivia.fs → addToTree, after finding smallestNodeThatContainsTrivia, one could check whether there is a descendant node whose range ends before the comment but whose column indentation matches the comment's column. If such a node exists, the comment could be attributed as ContentAfter of that node instead of the parent.

    This is non-trivial because:

    • Column matching alone is not a reliable heuristic
    • findNodeWhereRangeFitsIn is pure range-containment, with no column check
    • It would need careful handling to avoid regressions in cases where trailing-comment-at-column-0 is correct (e.g., a comment really is at module level)

    This issue is related to #932 and the general challenge of indentation-based scope attribution for trivia.

    Generated by 🌈 Repo Assist at {run-started}. Learn more.

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@1f672aef974f4246124860fc532f82fe8a93a57e
    
  4. added a commit that references this issue on Apr 3, 2026
    ecde417
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions