rust-lang / rust-lang/rust-analyzer

Feature request: move constant away from a function to the top of the module.

Open
#13,701 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

There should be a way to extract constant without scrolling viewport up, then down again.

Rust analyzer already prodives "extract variable" and "promote varianbe to constant" code actions. The missing step is moving the constant away from the function (maybe also replacing occurrences of the same value with it on the way).

Code:

fn second() {
   let a = 555;
}
fn main() {
    ┃const VAR_NAME : i32 = 555;
    println!("{}", VAR_NAME);
}

After "Move constant to the top":

const VAR_NAME : i32 = 555;

fn second() {
   let a = 555;
}
fn main() {
    ┃println!("{}", VAR_NAME);
}

Note that viewport and cursor should stay in original location, not where the constant ended up.

After "Move constant to the top, also replace occurrences":

const VAR_NAME : i32 = 555;

fn second() {
   let a = VAR_NAME;
}
fn main() {
    ┃println!("{}", VAR_NAME);
}

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 repository files or tests are named. Start by reading rust-analyzer’s existing “extract variable” and “promote variable to constant” code-action entry points; done should cover moving the constant to the module top, preserving the original viewport and cursor location, and determining whether occurrence replacement is included.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.