mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-14 04:56:49 +00:00
Merge #5069
5069: DWIM introduce variable r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
5f08896424
@ -44,12 +44,26 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
|
||||
}
|
||||
let target = expr.syntax().text_range();
|
||||
acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| {
|
||||
let field_shorthand = match expr.syntax().parent().and_then(ast::RecordField::cast) {
|
||||
Some(field) => field.name_ref(),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let mut buf = String::new();
|
||||
|
||||
let var_name = match &field_shorthand {
|
||||
Some(it) => it.to_string(),
|
||||
None => "var_name".to_string(),
|
||||
};
|
||||
let expr_range = match &field_shorthand {
|
||||
Some(it) => it.syntax().text_range().cover(expr.syntax().text_range()),
|
||||
None => expr.syntax().text_range(),
|
||||
};
|
||||
|
||||
if wrap_in_block {
|
||||
buf.push_str("{ let var_name = ");
|
||||
format_to!(buf, "{{ let {} = ", var_name);
|
||||
} else {
|
||||
buf.push_str("let var_name = ");
|
||||
format_to!(buf, "let {} = ", var_name);
|
||||
};
|
||||
format_to!(buf, "{}", expr.syntax());
|
||||
|
||||
@ -64,13 +78,13 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
|
||||
if full_stmt.unwrap().semicolon_token().is_none() {
|
||||
buf.push_str(";");
|
||||
}
|
||||
let offset = expr.syntax().text_range();
|
||||
match ctx.config.snippet_cap {
|
||||
Some(cap) => {
|
||||
let snip = buf.replace("let var_name", "let $0var_name");
|
||||
edit.replace_snippet(cap, offset, snip)
|
||||
let snip =
|
||||
buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
|
||||
edit.replace_snippet(cap, expr_range, snip)
|
||||
}
|
||||
None => edit.replace(offset, buf),
|
||||
None => edit.replace(expr_range, buf),
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -88,11 +102,12 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
|
||||
buf.push_str(text);
|
||||
}
|
||||
|
||||
edit.replace(expr.syntax().text_range(), "var_name".to_string());
|
||||
edit.replace(expr_range, var_name.clone());
|
||||
let offset = anchor_stmt.text_range().start();
|
||||
match ctx.config.snippet_cap {
|
||||
Some(cap) => {
|
||||
let snip = buf.replace("let var_name", "let $0var_name");
|
||||
let snip =
|
||||
buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
|
||||
edit.insert_snippet(cap, offset, snip)
|
||||
}
|
||||
None => edit.insert(offset, buf),
|
||||
@ -503,6 +518,32 @@ fn main() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn introduce_var_field_shorthand() {
|
||||
check_assist(
|
||||
introduce_variable,
|
||||
r#"
|
||||
struct S {
|
||||
foo: i32
|
||||
}
|
||||
|
||||
fn main() {
|
||||
S { foo: <|>1 + 1<|> }
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
struct S {
|
||||
foo: i32
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let $0foo = 1 + 1;
|
||||
S { foo }
|
||||
}
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_introduce_var_for_return_not_applicable() {
|
||||
check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } ");
|
||||
|
@ -84,11 +84,7 @@ pub(crate) struct GlobalStateSnapshot {
|
||||
}
|
||||
|
||||
impl GlobalState {
|
||||
pub(crate) fn new(
|
||||
sender: Sender<lsp_server::Message>,
|
||||
lru_capacity: Option<usize>,
|
||||
config: Config,
|
||||
) -> GlobalState {
|
||||
pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
|
||||
let loader = {
|
||||
let (sender, receiver) = unbounded::<vfs::loader::Message>();
|
||||
let handle =
|
||||
@ -103,12 +99,13 @@ impl GlobalState {
|
||||
Handle { handle, receiver }
|
||||
};
|
||||
|
||||
let analysis_host = AnalysisHost::new(config.lru_capacity);
|
||||
GlobalState {
|
||||
sender,
|
||||
task_pool,
|
||||
loader,
|
||||
config,
|
||||
analysis_host: AnalysisHost::new(lru_capacity),
|
||||
analysis_host,
|
||||
flycheck: None,
|
||||
diagnostics: Default::default(),
|
||||
mem_docs: FxHashSet::default(),
|
||||
|
@ -44,8 +44,7 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
|
||||
SetThreadPriority(thread, thread_priority_above_normal);
|
||||
}
|
||||
|
||||
GlobalState::new(connection.sender.clone(), config.lru_capacity, config)
|
||||
.run(connection.receiver)
|
||||
GlobalState::new(connection.sender.clone(), config).run(connection.receiver)
|
||||
}
|
||||
|
||||
enum Event {
|
||||
|
Loading…
Reference in New Issue
Block a user