From acb9da7c9309d942353748e28e2163122464d185 Mon Sep 17 00:00:00 2001 From: Tarek Date: Wed, 13 Nov 2024 15:02:08 +0200 Subject: [PATCH 1/3] feat: migrate `reorder_fields` assist to use `SyntaxFactory` Signed-off-by: Tarek --- .../src/handlers/reorder_fields.rs | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_fields.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_fields.rs index df7a5112f12..4d3e85ab1b2 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_fields.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_fields.rs @@ -1,7 +1,7 @@ use either::Either; use ide_db::FxHashMap; use itertools::Itertools; -use syntax::{ast, ted, AstNode, SmolStr, ToSmolStr}; +use syntax::{ast, syntax_editor::SyntaxEditor, AstNode, SmolStr, SyntaxElement, ToSmolStr}; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -24,6 +24,11 @@ pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti let record = path.syntax().parent().and_then(>::cast)?; + let parent_node = match ctx.covering_element() { + SyntaxElement::Node(n) => n, + SyntaxElement::Token(t) => t.parent()?, + }; + let ranks = compute_fields_ranks(&path, ctx)?; let get_rank_of_field = |of: Option| { *ranks.get(of.unwrap_or_default().trim_start_matches("r#")).unwrap_or(&usize::MAX) @@ -65,23 +70,31 @@ pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti AssistId("reorder_fields", AssistKind::RefactorRewrite), "Reorder record fields", target, - |builder| match fields { - Either::Left((sorted, field_list)) => { - replace(builder.make_mut(field_list).fields(), sorted) - } - Either::Right((sorted, field_list)) => { - replace(builder.make_mut(field_list).fields(), sorted) + |builder| { + let mut editor = builder.make_editor(&parent_node); + + match fields { + Either::Left((sorted, field_list)) => { + replace(&mut editor, field_list.fields(), sorted) + } + Either::Right((sorted, field_list)) => { + replace(&mut editor, field_list.fields(), sorted) + } } + + builder.add_file_edits(ctx.file_id(), editor); }, ) } fn replace( + editor: &mut SyntaxEditor, fields: impl Iterator, sorted_fields: impl IntoIterator, ) { fields.zip(sorted_fields).for_each(|(field, sorted_field)| { - ted::replace(field.syntax(), sorted_field.syntax().clone_for_update()) + // FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us + editor.replace(field.syntax(), sorted_field.syntax().clone_for_update()) }); } From 9f4f903bcfcc67efb53d8f4807062a88dfb5bbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 16 Nov 2024 18:02:44 +0200 Subject: [PATCH 2/3] Bump xshell --- src/tools/rust-analyzer/Cargo.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tools/rust-analyzer/Cargo.lock b/src/tools/rust-analyzer/Cargo.lock index 55705de9b2c..019609e6a5b 100644 --- a/src/tools/rust-analyzer/Cargo.lock +++ b/src/tools/rust-analyzer/Cargo.lock @@ -2625,18 +2625,18 @@ checksum = "672423d4fea7ffa2f6c25ba60031ea13dc6258070556f125cc4d790007d4a155" [[package]] name = "xshell" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db0ab86eae739efd1b054a8d3d16041914030ac4e01cd1dca0cf252fd8b6437" +checksum = "9e7290c623014758632efe00737145b6867b66292c42167f2ec381eb566a373d" dependencies = [ "xshell-macros", ] [[package]] name = "xshell-macros" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d422e8e38ec76e2f06ee439ccc765e9c6a9638b9e7c9f2e8255e4d41e8bd852" +checksum = "32ac00cd3f8ec9c1d33fb3e7958a82df6989c42d747bd326c822b1d625283547" [[package]] name = "xtask" From 744e8e870c4109c4bcd74d6c1283866bf2a20cda Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Sat, 16 Nov 2024 21:17:15 +0200 Subject: [PATCH 3/3] Fix related documents diagnostics They were converted to LSP position using the current file's line index, which is obviously wrong. --- .../crates/rust-analyzer/src/handlers/request.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs index a5c9d2823e0..4975467ece9 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs @@ -511,12 +511,16 @@ pub(crate) fn handle_document_diagnostics( .into_iter() .filter_map(|d| { let file = d.range.file_id; - let diagnostic = convert_diagnostic(&line_index, d); if file == file_id { + let diagnostic = convert_diagnostic(&line_index, d); return Some(diagnostic); } if supports_related { - related_documents.entry(file).or_insert_with(Vec::new).push(diagnostic); + let (diagnostics, line_index) = related_documents + .entry(file) + .or_insert_with(|| (Vec::new(), snap.file_line_index(file).ok())); + let diagnostic = convert_diagnostic(line_index.as_mut()?, d); + diagnostics.push(diagnostic); } None }); @@ -529,7 +533,7 @@ pub(crate) fn handle_document_diagnostics( related_documents: related_documents.is_empty().not().then(|| { related_documents .into_iter() - .map(|(id, items)| { + .map(|(id, (items, _))| { ( to_proto::url(&snap, id), lsp_types::DocumentDiagnosticReportKind::Full(