rust-lang / rust-lang/rustfmt

[Feature Request] allow blank line as first line within functions

Open
#6,854 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-whitespace C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Feature Request

Summary

I love rust (and its formatting!). My single largest formatting pet peeve is the inability to have a blank line as the first line within a function contents. I often find this visually desirable to have that extra break when there are large function signatures with large comments.

More broadly this pattern seems to apply to most curly bracket situations (eg. if statements). Not allowing for this whitespace, to me, makes the code visually more cluttered/harder to read.

(Apologies if this is a dup issue, I couldn't find it directly in my review)

Example

Existing rustfmt only allows for:

/// Try to generate a suggestion using the data in the cache
/// Looks at the previous X characters to see if a completion is cached.
/// If one is found at (x,y) then it checks that the characters typed after (x,y)
/// match up with the cached completion result.
///
/// # Returns
///  - `Some(RenderedSuggestion)` - If a cached completion is found
///  - `None` - If no cached completion is found
pub fn fim_try_hint_inner(
    state: Arc<PluginState>,
    pos_x: usize,
    pos_y: usize,
    buffer_handle: u64,
    lines: Vec<String>,
) -> LttwResult<()> {
    // Get local context
    let ctx = get_local_context(&lines, pos_x, pos_y, None, &state.config.read());
    state.debug_manager.read().log("fim_try_hint_inner", "");

    // Compute primary hash
    let primary_hash = format!("{}{}{}{}", ctx.prefix, ctx.middle, "Î", ctx.suffix);
    let hash = sha256(&primary_hash);

...

Whereas it would be very nice to allow for:

/// Try to generate a suggestion using the data in the cache
/// Looks at the previous X characters to see if a completion is cached.
/// If one is found at (x,y) then it checks that the characters typed after (x,y)
/// match up with the cached completion result.
///
/// # Returns
///  - `Some(RenderedSuggestion)` - If a cached completion is found
///  - `None` - If no cached completion is found
pub fn fim_try_hint_inner(
    state: Arc<PluginState>,
    pos_x: usize,
    pos_y: usize,
    buffer_handle: u64,
    lines: Vec<String>,
) -> LttwResult<()> {

    // Get local context
    let ctx = get_local_context(&lines, pos_x, pos_y, None, &state.config.read());
    state.debug_manager.read().log("fim_try_hint_inner", "");

    // Compute primary hash
    let primary_hash = format!("{}{}{}{}", ctx.prefix, ctx.middle, "Î", ctx.suffix);
    let hash = sha256(&primary_hash);

...
Related configuration options

related https://github.com/rust-lang/rustfmt/issues/4930

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing rustfmt's handling of blank lines inside function and other curly-braced constructs, then read the related configuration discussion in issue 4930. Compare the existing and proposed formatting examples and define the supported scope; done means rustfmt preserves the requested leading blank line without introducing unwanted whitespace elsewhere.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.