rust-lang / rust-lang/rust-analyzer

Feature Request: Sort multi-line items

Open
#12,255 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-ide C-feature
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

VSCode provides Sort Lines Ascending.
However when rust-analyzer's formatting breaks lines, the items cannot be sorted by vscode any longer.
Sorting multi-line items can be only done by rust-analyzer, which understands the AST.

Example:

let a = &[
  MyObject {
    a: 4,
    b: 2,
  },
  MyObject {
    a: 3,
    b: 1,
  },
];

is currently wrongly sorted to

let a = &[
    a: 3,
    a: 4,
    b: 1,
    b: 2,
  },
  },
  MyObject {
  MyObject {
];

The desired result is to swap two MyObjects.

Example2

// sorting should swap these top-level let statements
let b = {
  ..
};
let a = {
  ..
};

Example3

// when line 2~4 are selected, sorting should rearrange them to a,b,c.
let a = My Object {
  c_field: {   // line 2
    ..
  },
  b_field: ..  // line 3
  a_field: ..  // line 4
};

Example4

I haven't thought about desired behavior in more complex cases, like items with different nested fields. scope label, items with different types, etc.

MyObject {
  c_field: ..
  b_field: ..
  ..Default::default()
},
'a: MyObject {
  a_field: ..
  c_field: ..
  ..Default::default()
},
macro_to_generate_MyObejct!(..),
MyObject<TypeParam=..>,

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

No file or test is named. Start by examining rust-analyzer's existing formatting and sorting support, then clarify how AST items, selections, nested fields, differing types, macros, and generic items should be ordered; done means multi-line selections are rearranged without the corruption shown in the examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.