Skip to content

Removal of newlines before commented top-level statements #150

@asakura

Description

@asakura

When a top-level statement is preceded by a comment, nufmt removes the empty line separating it from the previous block.

Minimal reproducer

# module doc

# next line comment
use b.nu [colors]
use a.nu # inline comment
use d.nu [thing]

# nex line comment
const PRIORITY_ERROR: int = 3
const PRIORITY_INFO: int  = 6
const PRIORITY_INFO: int = 6
const PRIORITY_DEBUG: int = 7

# next multi
# line comment
const VOLATILE_FIELDS: list<string> = [ EXCEPTION EXCEPTION_DEBUG ]

# func comment
def level-to-max-priority [level: string]: nothing -> int {
  match ($level | str upcase) {
    ERROR => 3
    WARNING => 4
    INFO => 6
    _ => 7
  }
}

# func comment
def format-timestamp [ts_us: string]: nothing -> nothing { }

Output:

# module doc

# next line comment
use b.nu [colors]
use a.nu # inline comment
use d.nu [thing]
# nex line comment
const PRIORITY_ERROR: int = 3
const PRIORITY_INFO: int = 6
const PRIORITY_INFO: int = 6
const PRIORITY_DEBUG: int = 7
# next multi
# line comment
const VOLATILE_FIELDS: list<string> = [EXCEPTION EXCEPTION_DEBUG]
# func comment
def level-to-max-priority [level: string]: nothing -> int {
  match ($level | str upcase) {
    ERROR => 3
    WARNING => 4
    INFO => 6
    _ => 7
  }
}
# func comment
def format-timestamp [ts_us: string]: nothing -> nothing { }

Excepted:

# module doc

# next line comment
use b.nu [colors]
use a.nu # inline comment
use d.nu [thing]

# nex line comment
const PRIORITY_ERROR: int = 3
const PRIORITY_INFO: int  = 6
const PRIORITY_INFO: int = 6
const PRIORITY_DEBUG: int = 7

# next multi
# line comment
const VOLATILE_FIELDS: list<string> = [ EXCEPTION EXCEPTION_DEBUG ]

# func comment
def level-to-max-priority [level: string]: nothing -> int {
  match ($level | str upcase) {
    ERROR => 3
    WARNING => 4
    INFO => 6
    _ => 7
  }
}

# func comment
def format-timestamp [ts_us: string]: nothing -> nothing { }

Environment

  • nushell 0.111.0
  • nufmt f2f4020
  • rustc 1.93.0 (254b59607 2026-01-19) (built from a source tarball)
{
  indent: 2
  line_length: 80
  margin: 1
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions