aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock40
-rw-r--r--crates/assists/src/assist_context.rs5
-rw-r--r--crates/completion/src/context.rs8
-rw-r--r--crates/hir_expand/src/db.rs2
-rw-r--r--crates/hir_expand/src/lib.rs8
-rw-r--r--crates/ide/src/extend_selection.rs6
-rw-r--r--crates/ide/src/join_lines.rs4
-rw-r--r--crates/ide/src/syntax_tree.rs4
-rw-r--r--crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt47
-rw-r--r--crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt47
-rw-r--r--crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt47
-rw-r--r--crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt120
-rw-r--r--crates/rust-analyzer/src/diagnostics/to_proto.rs53
-rw-r--r--crates/syntax/Cargo.toml2
-rw-r--r--crates/syntax/src/algo.rs6
-rw-r--r--crates/syntax/src/parsing/reparsing.rs5
16 files changed, 83 insertions, 321 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1c893cd5c..db8b49122 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -323,7 +323,7 @@ dependencies = [
323 "const_fn", 323 "const_fn",
324 "crossbeam-utils 0.8.1", 324 "crossbeam-utils 0.8.1",
325 "lazy_static", 325 "lazy_static",
326 "memoffset", 326 "memoffset 0.6.1",
327 "scopeguard", 327 "scopeguard",
328] 328]
329 329
@@ -910,6 +910,15 @@ dependencies = [
910 910
911[[package]] 911[[package]]
912name = "memoffset" 912name = "memoffset"
913version = "0.5.6"
914source = "registry+https://github.com/rust-lang/crates.io-index"
915checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa"
916dependencies = [
917 "autocfg",
918]
919
920[[package]]
921name = "memoffset"
913version = "0.6.1" 922version = "0.6.1"
914source = "registry+https://github.com/rust-lang/crates.io-index" 923source = "registry+https://github.com/rust-lang/crates.io-index"
915checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" 924checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87"
@@ -1316,14 +1325,14 @@ checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
1316 1325
1317[[package]] 1326[[package]]
1318name = "rowan" 1327name = "rowan"
1319version = "0.10.0" 1328version = "0.10.1"
1320source = "registry+https://github.com/rust-lang/crates.io-index" 1329source = "registry+https://github.com/rust-lang/crates.io-index"
1321checksum = "1e081ed6eacce09e243b619ab90f069c27b0cff8a6d0eb8ad2ec935b65853798" 1330checksum = "ca9516377b32367ade2fbe1846c75afa4827c8976f1c7d05d5473573d18a1a04"
1322dependencies = [ 1331dependencies = [
1323 "rustc-hash", 1332 "rustc-hash",
1324 "smol_str", 1333 "smol_str",
1325 "text-size", 1334 "text-size",
1326 "thin-dst", 1335 "triomphe",
1327] 1336]
1328 1337
1329[[package]] 1338[[package]]
@@ -1569,6 +1578,12 @@ dependencies = [
1569] 1578]
1570 1579
1571[[package]] 1580[[package]]
1581name = "stable_deref_trait"
1582version = "1.2.0"
1583source = "registry+https://github.com/rust-lang/crates.io-index"
1584checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
1585
1586[[package]]
1572name = "stdx" 1587name = "stdx"
1573version = "0.0.0" 1588version = "0.0.0"
1574dependencies = [ 1589dependencies = [
@@ -1655,12 +1670,6 @@ dependencies = [
1655] 1670]
1656 1671
1657[[package]] 1672[[package]]
1658name = "thin-dst"
1659version = "1.1.0"
1660source = "registry+https://github.com/rust-lang/crates.io-index"
1661checksum = "db3c46be180f1af9673ebb27bc1235396f61ef6965b3fe0dbb2e624deb604f0e"
1662
1663[[package]]
1664name = "thread_local" 1673name = "thread_local"
1665version = "1.0.1" 1674version = "1.0.1"
1666source = "registry+https://github.com/rust-lang/crates.io-index" 1675source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1802,6 +1811,17 @@ dependencies = [
1802] 1811]
1803 1812
1804[[package]] 1813[[package]]
1814name = "triomphe"
1815version = "0.1.2"
1816source = "registry+https://github.com/rust-lang/crates.io-index"
1817checksum = "6e9d872053cf9e5a833d8c1dd772cdc38ab66a908129d6f73c049c986161d07c"
1818dependencies = [
1819 "memoffset 0.5.6",
1820 "serde",
1821 "stable_deref_trait",
1822]
1823
1824[[package]]
1805name = "tt" 1825name = "tt"
1806version = "0.0.0" 1826version = "0.0.0"
1807dependencies = [ 1827dependencies = [
diff --git a/crates/assists/src/assist_context.rs b/crates/assists/src/assist_context.rs
index 321fe77f3..8d93edba2 100644
--- a/crates/assists/src/assist_context.rs
+++ b/crates/assists/src/assist_context.rs
@@ -2,7 +2,6 @@
2 2
3use std::mem; 3use std::mem;
4 4
5use algo::find_covering_element;
6use hir::Semantics; 5use hir::Semantics;
7use ide_db::{ 6use ide_db::{
8 base_db::{AnchoredPathBuf, FileId, FileRange}, 7 base_db::{AnchoredPathBuf, FileId, FileRange},
@@ -94,11 +93,11 @@ impl<'a> AssistContext<'a> {
94 self.sema.find_node_at_offset_with_descend(self.source_file.syntax(), self.offset()) 93 self.sema.find_node_at_offset_with_descend(self.source_file.syntax(), self.offset())
95 } 94 }
96 pub(crate) fn covering_element(&self) -> SyntaxElement { 95 pub(crate) fn covering_element(&self) -> SyntaxElement {
97 find_covering_element(self.source_file.syntax(), self.frange.range) 96 self.source_file.syntax().covering_element(self.frange.range)
98 } 97 }
99 // FIXME: remove 98 // FIXME: remove
100 pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement { 99 pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement {
101 find_covering_element(self.source_file.syntax(), range) 100 self.source_file.syntax().covering_element(range)
102 } 101 }
103} 102}
104 103
diff --git a/crates/completion/src/context.rs b/crates/completion/src/context.rs
index d809460e2..b1e8eba85 100644
--- a/crates/completion/src/context.rs
+++ b/crates/completion/src/context.rs
@@ -4,10 +4,8 @@ use hir::{Local, ScopeDef, Semantics, SemanticsScope, Type};
4use ide_db::base_db::{FilePosition, SourceDatabase}; 4use ide_db::base_db::{FilePosition, SourceDatabase};
5use ide_db::{call_info::ActiveParameter, RootDatabase}; 5use ide_db::{call_info::ActiveParameter, RootDatabase};
6use syntax::{ 6use syntax::{
7 algo::{find_covering_element, find_node_at_offset}, 7 algo::find_node_at_offset, ast, match_ast, AstNode, NodeOrToken, SyntaxKind::*, SyntaxNode,
8 ast, match_ast, AstNode, NodeOrToken, 8 SyntaxToken, TextRange, TextSize,
9 SyntaxKind::*,
10 SyntaxNode, SyntaxToken, TextRange, TextSize,
11}; 9};
12use test_utils::mark; 10use test_utils::mark;
13use text_edit::Indel; 11use text_edit::Indel;
@@ -513,7 +511,7 @@ impl<'a> CompletionContext<'a> {
513} 511}
514 512
515fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> { 513fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
516 find_covering_element(syntax, range).ancestors().find_map(N::cast) 514 syntax.covering_element(range).ancestors().find_map(N::cast)
517} 515}
518 516
519fn is_node<N: AstNode>(node: &SyntaxNode) -> bool { 517fn is_node<N: AstNode>(node: &SyntaxNode) -> bool {
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index c62086390..467516eb7 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -118,7 +118,7 @@ pub fn expand_hypothetical(
118 parse_macro_with_arg(db, macro_file, Some(std::sync::Arc::new((tt, tmap_1)))).value?; 118 parse_macro_with_arg(db, macro_file, Some(std::sync::Arc::new((tt, tmap_1)))).value?;
119 let token_id = macro_def.0.map_id_down(token_id); 119 let token_id = macro_def.0.map_id_down(token_id);
120 let range = tmap_2.range_by_token(token_id)?.by_kind(token_to_map.kind())?; 120 let range = tmap_2.range_by_token(token_id)?.by_kind(token_to_map.kind())?;
121 let token = syntax::algo::find_covering_element(&node.syntax_node(), range).into_token()?; 121 let token = node.syntax_node().covering_element(range).into_token()?;
122 Some((node.syntax_node(), token)) 122 Some((node.syntax_node(), token))
123} 123}
124 124
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 3fa1b1d77..e388ddacc 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -22,7 +22,7 @@ use std::sync::Arc;
22 22
23use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange}; 23use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
24use syntax::{ 24use syntax::{
25 algo::{self, skip_trivia_token}, 25 algo::skip_trivia_token,
26 ast::{self, AstNode}, 26 ast::{self, AstNode},
27 Direction, SyntaxNode, SyntaxToken, TextRange, TextSize, 27 Direction, SyntaxNode, SyntaxToken, TextRange, TextSize,
28}; 28};
@@ -335,7 +335,7 @@ impl ExpansionInfo {
335 335
336 let range = self.exp_map.range_by_token(token_id)?.by_kind(token.value.kind())?; 336 let range = self.exp_map.range_by_token(token_id)?.by_kind(token.value.kind())?;
337 337
338 let token = algo::find_covering_element(&self.expanded.value, range).into_token()?; 338 let token = self.expanded.value.covering_element(range).into_token()?;
339 339
340 Some(self.expanded.with_value(token)) 340 Some(self.expanded.with_value(token))
341 } 341 }
@@ -360,8 +360,8 @@ impl ExpansionInfo {
360 }; 360 };
361 361
362 let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?; 362 let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?;
363 let token = algo::find_covering_element(&tt.value, range + tt.value.text_range().start()) 363 let token =
364 .into_token()?; 364 tt.value.covering_element(range + tt.value.text_range().start()).into_token()?;
365 Some((tt.with_value(token), origin)) 365 Some((tt.with_value(token), origin))
366 } 366 }
367} 367}
diff --git a/crates/ide/src/extend_selection.rs b/crates/ide/src/extend_selection.rs
index 56418c960..17a540972 100644
--- a/crates/ide/src/extend_selection.rs
+++ b/crates/ide/src/extend_selection.rs
@@ -3,7 +3,7 @@ use std::iter::successors;
3use hir::Semantics; 3use hir::Semantics;
4use ide_db::RootDatabase; 4use ide_db::RootDatabase;
5use syntax::{ 5use syntax::{
6 algo::{self, find_covering_element, skip_trivia_token}, 6 algo::{self, skip_trivia_token},
7 ast::{self, AstNode, AstToken}, 7 ast::{self, AstNode, AstToken},
8 Direction, NodeOrToken, 8 Direction, NodeOrToken,
9 SyntaxKind::{self, *}, 9 SyntaxKind::{self, *},
@@ -76,7 +76,7 @@ fn try_extend_selection(
76 }; 76 };
77 return Some(leaf_range); 77 return Some(leaf_range);
78 }; 78 };
79 let node = match find_covering_element(root, range) { 79 let node = match root.covering_element(range) {
80 NodeOrToken::Token(token) => { 80 NodeOrToken::Token(token) => {
81 if token.text_range() != range { 81 if token.text_range() != range {
82 return Some(token.text_range()); 82 return Some(token.text_range());
@@ -120,7 +120,7 @@ fn extend_tokens_from_range(
120 macro_call: ast::MacroCall, 120 macro_call: ast::MacroCall,
121 original_range: TextRange, 121 original_range: TextRange,
122) -> Option<TextRange> { 122) -> Option<TextRange> {
123 let src = find_covering_element(&macro_call.syntax(), original_range); 123 let src = macro_call.syntax().covering_element(original_range);
124 let (first_token, last_token) = match src { 124 let (first_token, last_token) = match src {
125 NodeOrToken::Node(it) => (it.first_token()?, it.last_token()?), 125 NodeOrToken::Node(it) => (it.first_token()?, it.last_token()?),
126 NodeOrToken::Token(it) => (it.clone(), it), 126 NodeOrToken::Token(it) => (it.clone(), it),
diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs
index 05380f2a1..981467c8d 100644
--- a/crates/ide/src/join_lines.rs
+++ b/crates/ide/src/join_lines.rs
@@ -1,7 +1,7 @@
1use assists::utils::extract_trivial_expression; 1use assists::utils::extract_trivial_expression;
2use itertools::Itertools; 2use itertools::Itertools;
3use syntax::{ 3use syntax::{
4 algo::{find_covering_element, non_trivia_sibling}, 4 algo::non_trivia_sibling,
5 ast::{self, AstNode, AstToken}, 5 ast::{self, AstNode, AstToken},
6 Direction, NodeOrToken, SourceFile, 6 Direction, NodeOrToken, SourceFile,
7 SyntaxKind::{self, USE_TREE, WHITESPACE}, 7 SyntaxKind::{self, USE_TREE, WHITESPACE},
@@ -31,7 +31,7 @@ pub(crate) fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
31 range 31 range
32 }; 32 };
33 33
34 let node = match find_covering_element(file.syntax(), range) { 34 let node = match file.syntax().covering_element(range) {
35 NodeOrToken::Node(node) => node, 35 NodeOrToken::Node(node) => node,
36 NodeOrToken::Token(token) => token.parent(), 36 NodeOrToken::Token(token) => token.parent(),
37 }; 37 };
diff --git a/crates/ide/src/syntax_tree.rs b/crates/ide/src/syntax_tree.rs
index 1f26f8043..1d4bac7ad 100644
--- a/crates/ide/src/syntax_tree.rs
+++ b/crates/ide/src/syntax_tree.rs
@@ -1,7 +1,7 @@
1use ide_db::base_db::{FileId, SourceDatabase}; 1use ide_db::base_db::{FileId, SourceDatabase};
2use ide_db::RootDatabase; 2use ide_db::RootDatabase;
3use syntax::{ 3use syntax::{
4 algo, AstNode, NodeOrToken, SourceFile, SyntaxKind::STRING, SyntaxToken, TextRange, TextSize, 4 AstNode, NodeOrToken, SourceFile, SyntaxKind::STRING, SyntaxToken, TextRange, TextSize,
5}; 5};
6 6
7// Feature: Show Syntax Tree 7// Feature: Show Syntax Tree
@@ -21,7 +21,7 @@ pub(crate) fn syntax_tree(
21) -> String { 21) -> String {
22 let parse = db.parse(file_id); 22 let parse = db.parse(file_id);
23 if let Some(text_range) = text_range { 23 if let Some(text_range) = text_range {
24 let node = match algo::find_covering_element(parse.tree().syntax(), text_range) { 24 let node = match parse.tree().syntax().covering_element(text_range) {
25 NodeOrToken::Node(node) => node, 25 NodeOrToken::Node(node) => node,
26 NodeOrToken::Token(token) => { 26 NodeOrToken::Token(token) => {
27 if let Some(tree) = syntax_tree_for_string(&token, text_range) { 27 if let Some(tree) = syntax_tree_for_string(&token, text_range) {
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt
index 23d42b4d0..5c282fe67 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt
@@ -66,52 +66,7 @@
66 ), 66 ),
67 data: None, 67 data: None,
68 }, 68 },
69 fixes: [ 69 fixes: [],
70 CodeAction {
71 title: "consider prefixing with an underscore",
72 group: None,
73 kind: Some(
74 CodeActionKind(
75 "quickfix",
76 ),
77 ),
78 edit: Some(
79 SnippetWorkspaceEdit {
80 changes: Some(
81 {
82 Url {
83 scheme: "file",
84 host: None,
85 port: None,
86 path: "/test/driver/subcommand/repl.rs",
87 query: None,
88 fragment: None,
89 }: [
90 TextEdit {
91 range: Range {
92 start: Position {
93 line: 290,
94 character: 8,
95 },
96 end: Position {
97 line: 290,
98 character: 11,
99 },
100 },
101 new_text: "_foo",
102 },
103 ],
104 },
105 ),
106 document_changes: None,
107 },
108 ),
109 is_preferred: Some(
110 true,
111 ),
112 data: None,
113 },
114 ],
115 }, 70 },
116 MappedRustDiagnostic { 71 MappedRustDiagnostic {
117 url: Url { 72 url: Url {
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt
index 4e428bedc..d36d7693d 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt
@@ -66,52 +66,7 @@
66 ), 66 ),
67 data: None, 67 data: None,
68 }, 68 },
69 fixes: [ 69 fixes: [],
70 CodeAction {
71 title: "consider prefixing with an underscore",
72 group: None,
73 kind: Some(
74 CodeActionKind(
75 "quickfix",
76 ),
77 ),
78 edit: Some(
79 SnippetWorkspaceEdit {
80 changes: Some(
81 {
82 Url {
83 scheme: "file",
84 host: None,
85 port: None,
86 path: "/test/driver/subcommand/repl.rs",
87 query: None,
88 fragment: None,
89 }: [
90 TextEdit {
91 range: Range {
92 start: Position {
93 line: 290,
94 character: 8,
95 },
96 end: Position {
97 line: 290,
98 character: 11,
99 },
100 },
101 new_text: "_foo",
102 },
103 ],
104 },
105 ),
106 document_changes: None,
107 },
108 ),
109 is_preferred: Some(
110 true,
111 ),
112 data: None,
113 },
114 ],
115 }, 70 },
116 MappedRustDiagnostic { 71 MappedRustDiagnostic {
117 url: Url { 72 url: Url {
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt
index 4ddd7efae..17845b711 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt
@@ -66,52 +66,7 @@
66 ), 66 ),
67 data: None, 67 data: None,
68 }, 68 },
69 fixes: [ 69 fixes: [],
70 CodeAction {
71 title: "consider prefixing with an underscore",
72 group: None,
73 kind: Some(
74 CodeActionKind(
75 "quickfix",
76 ),
77 ),
78 edit: Some(
79 SnippetWorkspaceEdit {
80 changes: Some(
81 {
82 Url {
83 scheme: "file",
84 host: None,
85 port: None,
86 path: "/test/driver/subcommand/repl.rs",
87 query: None,
88 fragment: None,
89 }: [
90 TextEdit {
91 range: Range {
92 start: Position {
93 line: 290,
94 character: 8,
95 },
96 end: Position {
97 line: 290,
98 character: 11,
99 },
100 },
101 new_text: "_foo",
102 },
103 ],
104 },
105 ),
106 document_changes: None,
107 },
108 ),
109 is_preferred: Some(
110 true,
111 ),
112 data: None,
113 },
114 ],
115 }, 70 },
116 MappedRustDiagnostic { 71 MappedRustDiagnostic {
117 url: Url { 72 url: Url {
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt
index 4cbdb3b92..a19962167 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt
@@ -102,65 +102,7 @@
102 tags: None, 102 tags: None,
103 data: None, 103 data: None,
104 }, 104 },
105 fixes: [ 105 fixes: [],
106 CodeAction {
107 title: "return the expression directly",
108 group: None,
109 kind: Some(
110 CodeActionKind(
111 "quickfix",
112 ),
113 ),
114 edit: Some(
115 SnippetWorkspaceEdit {
116 changes: Some(
117 {
118 Url {
119 scheme: "file",
120 host: None,
121 port: None,
122 path: "/test/src/main.rs",
123 query: None,
124 fragment: None,
125 }: [
126 TextEdit {
127 range: Range {
128 start: Position {
129 line: 2,
130 character: 4,
131 },
132 end: Position {
133 line: 2,
134 character: 30,
135 },
136 },
137 new_text: "",
138 },
139 TextEdit {
140 range: Range {
141 start: Position {
142 line: 3,
143 character: 4,
144 },
145 end: Position {
146 line: 3,
147 character: 5,
148 },
149 },
150 new_text: "(0..10).collect()",
151 },
152 ],
153 },
154 ),
155 document_changes: None,
156 },
157 ),
158 is_preferred: Some(
159 true,
160 ),
161 data: None,
162 },
163 ],
164 }, 106 },
165 MappedRustDiagnostic { 107 MappedRustDiagnostic {
166 url: Url { 108 url: Url {
@@ -242,65 +184,7 @@
242 tags: None, 184 tags: None,
243 data: None, 185 data: None,
244 }, 186 },
245 fixes: [ 187 fixes: [],
246 CodeAction {
247 title: "return the expression directly",
248 group: None,
249 kind: Some(
250 CodeActionKind(
251 "quickfix",
252 ),
253 ),
254 edit: Some(
255 SnippetWorkspaceEdit {
256 changes: Some(
257 {
258 Url {
259 scheme: "file",
260 host: None,
261 port: None,
262 path: "/test/src/main.rs",
263 query: None,
264 fragment: None,
265 }: [
266 TextEdit {
267 range: Range {
268 start: Position {
269 line: 2,
270 character: 4,
271 },
272 end: Position {
273 line: 2,
274 character: 30,
275 },
276 },
277 new_text: "",
278 },
279 TextEdit {
280 range: Range {
281 start: Position {
282 line: 3,
283 character: 4,
284 },
285 end: Position {
286 line: 3,
287 character: 5,
288 },
289 },
290 new_text: "(0..10).collect()",
291 },
292 ],
293 },
294 ),
295 document_changes: None,
296 },
297 ),
298 is_preferred: Some(
299 true,
300 ),
301 data: None,
302 },
303 ],
304 }, 188 },
305 MappedRustDiagnostic { 189 MappedRustDiagnostic {
306 url: Url { 190 url: Url {
diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs
index 540759198..757899484 100644
--- a/crates/rust-analyzer/src/diagnostics/to_proto.rs
+++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs
@@ -74,11 +74,13 @@ fn diagnostic_related_information(
74 Some(lsp_types::DiagnosticRelatedInformation { location, message }) 74 Some(lsp_types::DiagnosticRelatedInformation { location, message })
75} 75}
76 76
77struct SubDiagnostic {
78 related: lsp_types::DiagnosticRelatedInformation,
79 suggested_fix: Option<lsp_ext::CodeAction>,
80}
81
77enum MappedRustChildDiagnostic { 82enum MappedRustChildDiagnostic {
78 Related { 83 SubDiagnostic(SubDiagnostic),
79 related: lsp_types::DiagnosticRelatedInformation,
80 suggested_fix: Option<lsp_ext::CodeAction>,
81 },
82 MessageLine(String), 84 MessageLine(String),
83} 85}
84 86
@@ -105,15 +107,15 @@ fn map_rust_child_diagnostic(
105 } 107 }
106 108
107 if edit_map.is_empty() { 109 if edit_map.is_empty() {
108 MappedRustChildDiagnostic::Related { 110 MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic {
109 related: lsp_types::DiagnosticRelatedInformation { 111 related: lsp_types::DiagnosticRelatedInformation {
110 location: location(workspace_root, spans[0]), 112 location: location(workspace_root, spans[0]),
111 message: rd.message.clone(), 113 message: rd.message.clone(),
112 }, 114 },
113 suggested_fix: None, 115 suggested_fix: None,
114 } 116 })
115 } else { 117 } else {
116 MappedRustChildDiagnostic::Related { 118 MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic {
117 related: lsp_types::DiagnosticRelatedInformation { 119 related: lsp_types::DiagnosticRelatedInformation {
118 location: location(workspace_root, spans[0]), 120 location: location(workspace_root, spans[0]),
119 message: rd.message.clone(), 121 message: rd.message.clone(),
@@ -130,7 +132,7 @@ fn map_rust_child_diagnostic(
130 is_preferred: Some(true), 132 is_preferred: Some(true),
131 data: None, 133 data: None,
132 }), 134 }),
133 } 135 })
134 } 136 }
135} 137}
136 138
@@ -175,26 +177,22 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
175 } 177 }
176 178
177 let mut needs_primary_span_label = true; 179 let mut needs_primary_span_label = true;
178 let mut related_information = Vec::new(); 180 let mut subdiagnostics = Vec::new();
179 let mut tags = Vec::new(); 181 let mut tags = Vec::new();
180 182
181 for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) { 183 for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) {
182 let related = diagnostic_related_information(workspace_root, secondary_span); 184 let related = diagnostic_related_information(workspace_root, secondary_span);
183 if let Some(related) = related { 185 if let Some(related) = related {
184 related_information.push(related); 186 subdiagnostics.push(SubDiagnostic { related, suggested_fix: None });
185 } 187 }
186 } 188 }
187 189
188 let mut fixes = Vec::new();
189 let mut message = rd.message.clone(); 190 let mut message = rd.message.clone();
190 for child in &rd.children { 191 for child in &rd.children {
191 let child = map_rust_child_diagnostic(workspace_root, &child); 192 let child = map_rust_child_diagnostic(workspace_root, &child);
192 match child { 193 match child {
193 MappedRustChildDiagnostic::Related { related, suggested_fix } => { 194 MappedRustChildDiagnostic::SubDiagnostic(sub) => {
194 related_information.push(related); 195 subdiagnostics.push(sub);
195 if let Some(code_action) = suggested_fix {
196 fixes.push(code_action);
197 }
198 } 196 }
199 MappedRustChildDiagnostic::MessageLine(message_line) => { 197 MappedRustChildDiagnostic::MessageLine(message_line) => {
200 format_to!(message, "\n{}", message_line); 198 format_to!(message, "\n{}", message_line);
@@ -284,7 +282,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
284 diagnostics.push(MappedRustDiagnostic { 282 diagnostics.push(MappedRustDiagnostic {
285 url: in_macro_location.uri, 283 url: in_macro_location.uri,
286 diagnostic, 284 diagnostic,
287 fixes: fixes.clone(), 285 fixes: Vec::new(),
288 }); 286 });
289 } 287 }
290 288
@@ -298,17 +296,20 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
298 code_description: code_description.clone(), 296 code_description: code_description.clone(),
299 source: Some(source.clone()), 297 source: Some(source.clone()),
300 message, 298 message,
301 related_information: if related_information.is_empty() { 299 related_information: if subdiagnostics.is_empty() {
302 None 300 None
303 } else { 301 } else {
304 let mut related = related_information.clone(); 302 let mut related = subdiagnostics
303 .iter()
304 .map(|sub| sub.related.clone())
305 .collect::<Vec<_>>();
305 related.extend(related_macro_info); 306 related.extend(related_macro_info);
306 Some(related) 307 Some(related)
307 }, 308 },
308 tags: if tags.is_empty() { None } else { Some(tags.clone()) }, 309 tags: if tags.is_empty() { None } else { Some(tags.clone()) },
309 data: None, 310 data: None,
310 }, 311 },
311 fixes: fixes.clone(), 312 fixes: Vec::new(),
312 }); 313 });
313 314
314 // Emit hint-level diagnostics for all `related_information` entries such as "help"s. 315 // Emit hint-level diagnostics for all `related_information` entries such as "help"s.
@@ -318,21 +319,21 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
318 location, 319 location,
319 message: "original diagnostic".to_string(), 320 message: "original diagnostic".to_string(),
320 }; 321 };
321 for info in &related_information { 322 for sub in &subdiagnostics {
322 // Filter out empty/non-existent messages, as they greatly confuse VS Code. 323 // Filter out empty/non-existent messages, as they greatly confuse VS Code.
323 if info.message.is_empty() { 324 if sub.related.message.is_empty() {
324 continue; 325 continue;
325 } 326 }
326 diagnostics.push(MappedRustDiagnostic { 327 diagnostics.push(MappedRustDiagnostic {
327 url: info.location.uri.clone(), 328 url: sub.related.location.uri.clone(),
328 fixes: fixes.clone(), // share fixes to make them easier to apply 329 fixes: sub.suggested_fix.iter().cloned().collect(),
329 diagnostic: lsp_types::Diagnostic { 330 diagnostic: lsp_types::Diagnostic {
330 range: info.location.range, 331 range: sub.related.location.range,
331 severity: Some(lsp_types::DiagnosticSeverity::Hint), 332 severity: Some(lsp_types::DiagnosticSeverity::Hint),
332 code: code.clone().map(lsp_types::NumberOrString::String), 333 code: code.clone().map(lsp_types::NumberOrString::String),
333 code_description: code_description.clone(), 334 code_description: code_description.clone(),
334 source: Some(source.clone()), 335 source: Some(source.clone()),
335 message: info.message.clone(), 336 message: sub.related.message.clone(),
336 related_information: Some(vec![back_ref.clone()]), 337 related_information: Some(vec![back_ref.clone()]),
337 tags: None, // don't apply modifiers again 338 tags: None, // don't apply modifiers again
338 data: None, 339 data: None,
diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml
index cfeaed9e6..ad8b797fe 100644
--- a/crates/syntax/Cargo.toml
+++ b/crates/syntax/Cargo.toml
@@ -12,7 +12,7 @@ doctest = false
12 12
13[dependencies] 13[dependencies]
14itertools = "0.10.0" 14itertools = "0.10.0"
15rowan = "0.10.0" 15rowan = "0.10.1"
16rustc_lexer = { version = "697.0.0", package = "rustc-ap-rustc_lexer" } 16rustc_lexer = { version = "697.0.0", package = "rustc-ap-rustc_lexer" }
17rustc-hash = "1.1.0" 17rustc-hash = "1.1.0"
18arrayvec = "0.5.1" 18arrayvec = "0.5.1"
diff --git a/crates/syntax/src/algo.rs b/crates/syntax/src/algo.rs
index 39da45cc0..827ae78f9 100644
--- a/crates/syntax/src/algo.rs
+++ b/crates/syntax/src/algo.rs
@@ -45,7 +45,7 @@ pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextSize) ->
45} 45}
46 46
47pub fn find_node_at_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> { 47pub fn find_node_at_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
48 find_covering_element(syntax, range).ancestors().find_map(N::cast) 48 syntax.covering_element(range).ancestors().find_map(N::cast)
49} 49}
50 50
51/// Skip to next non `trivia` token 51/// Skip to next non `trivia` token
@@ -74,10 +74,6 @@ pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Optio
74 } 74 }
75} 75}
76 76
77pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement {
78 root.covering_element(range)
79}
80
81pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNode> { 77pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNode> {
82 if u == v { 78 if u == v {
83 return Some(u.clone()); 79 return Some(u.clone());
diff --git a/crates/syntax/src/parsing/reparsing.rs b/crates/syntax/src/parsing/reparsing.rs
index 78eaf3410..76f01084c 100644
--- a/crates/syntax/src/parsing/reparsing.rs
+++ b/crates/syntax/src/parsing/reparsing.rs
@@ -10,7 +10,6 @@ use parser::Reparser;
10use text_edit::Indel; 10use text_edit::Indel;
11 11
12use crate::{ 12use crate::{
13 algo,
14 parsing::{ 13 parsing::{
15 lexer::{lex_single_syntax_kind, tokenize, Token}, 14 lexer::{lex_single_syntax_kind, tokenize, Token},
16 text_token_source::TextTokenSource, 15 text_token_source::TextTokenSource,
@@ -41,7 +40,7 @@ fn reparse_token<'node>(
41 root: &'node SyntaxNode, 40 root: &'node SyntaxNode,
42 edit: &Indel, 41 edit: &Indel,
43) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> { 42) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
44 let prev_token = algo::find_covering_element(root, edit.delete).as_token()?.clone(); 43 let prev_token = root.covering_element(edit.delete).as_token()?.clone();
45 let prev_token_kind = prev_token.kind(); 44 let prev_token_kind = prev_token.kind();
46 match prev_token_kind { 45 match prev_token_kind {
47 WHITESPACE | COMMENT | IDENT | STRING => { 46 WHITESPACE | COMMENT | IDENT | STRING => {
@@ -124,7 +123,7 @@ fn is_contextual_kw(text: &str) -> bool {
124} 123}
125 124
126fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(SyntaxNode, Reparser)> { 125fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(SyntaxNode, Reparser)> {
127 let node = algo::find_covering_element(node, range); 126 let node = node.covering_element(range);
128 127
129 let mut ancestors = match node { 128 let mut ancestors = match node {
130 NodeOrToken::Token(it) => it.parent().ancestors(), 129 NodeOrToken::Token(it) => it.parent().ancestors(),