about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2024-11-18 02:24:36 -0500
committerGitHub <noreply@github.com>2024-11-18 02:24:36 -0500
commit194c76ef0a6db5f83d7bf6eef6b67a0e1f73dcc0 (patch)
tree73295395b6893ac54decf359c783dc53347fa839
parent19c145d816fc03741dadc884765e90b256759108 (diff)
parent647749aa32a8b1830c33bb8b778ff8ebca6daeea (diff)
downloadrust-194c76ef0a6db5f83d7bf6eef6b67a0e1f73dcc0.tar.gz
rust-194c76ef0a6db5f83d7bf6eef6b67a0e1f73dcc0.zip
Rollup merge of #133158 - lnicola:sync-from-ra, r=lnicola
Subtree update of `rust-analyzer`

r? `@ghost`
-rw-r--r--src/tools/rust-analyzer/Cargo.lock8
-rw-r--r--src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_fields.rs29
-rw-r--r--src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs10
3 files changed, 32 insertions, 15 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"
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(<Either<ast::RecordExpr, ast::RecordPat>>::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<SmolStr>| {
         *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<T: AstNode + PartialEq>(
+    editor: &mut SyntaxEditor,
     fields: impl Iterator<Item = T>,
     sorted_fields: impl IntoIterator<Item = T>,
 ) {
     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())
     });
 }
 
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(