Skip to content

Commit db1cadd

Browse files
committed
In field patterns, don't highlight local binding as a field
1 parent 560b98b commit db1cadd

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@
8484
<span class="keyword">let</span> <span class="variable declaration mutable">y</span> = &<span class="keyword">mut</span> <span class="variable mutable">x</span>;
8585
<span class="keyword">let</span> <span class="variable declaration">z</span> = &<span class="variable mutable">y</span>;
8686

87-
<span class="variable mutable">y</span>;
87+
<span class="keyword">let</span> <span class="struct">Foo</span> { <span class="field">x</span>: <span class="variable declaration">z</span>, <span class="field">y</span> } = <span class="struct">Foo</span> { <span class="field">x</span>: <span class="variable">z</span>, <span class="field">y</span> };
88+
89+
<span class="variable">y</span>;
8890
}
8991

9092
<span class="keyword">enum</span> <span class="enum declaration">Option</span>&lt;<span class="type_param declaration">T</span>&gt; {

crates/ra_ide/src/syntax_highlighting/tests.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ use crate::{
77
FileRange, TextRange,
88
};
99

10-
/// Highlights the code given by the `ra_fixture` argument, renders the
11-
/// result as HTML, and compares it with the HTML file given as `snapshot`.
12-
/// Note that the `snapshot` file is overwritten by the rendered HTML.
13-
fn check_highlighting(ra_fixture: &str, snapshot: &str, rainbow: bool) {
14-
let (analysis, file_id) = single_file(ra_fixture);
15-
let dst_file = project_dir().join(snapshot);
16-
let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
17-
let expected_html = &read_text(&dst_file);
18-
fs::write(dst_file, &actual_html).unwrap();
19-
assert_eq_text!(expected_html, actual_html);
20-
}
21-
2210
#[test]
2311
fn test_highlighting() {
2412
check_highlighting(
@@ -77,6 +65,8 @@ fn main() {
7765
let y = &mut x;
7866
let z = &y;
7967
68+
let Foo { x: z, y } = Foo { x: z, y };
69+
8070
y;
8171
}
8272
@@ -334,3 +324,15 @@ impl Foo {
334324
false,
335325
)
336326
}
327+
328+
/// Highlights the code given by the `ra_fixture` argument, renders the
329+
/// result as HTML, and compares it with the HTML file given as `snapshot`.
330+
/// Note that the `snapshot` file is overwritten by the rendered HTML.
331+
fn check_highlighting(ra_fixture: &str, snapshot: &str, rainbow: bool) {
332+
let (analysis, file_id) = single_file(ra_fixture);
333+
let dst_file = project_dir().join(snapshot);
334+
let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
335+
let expected_html = &read_text(&dst_file);
336+
fs::write(dst_file, &actual_html).unwrap();
337+
assert_eq_text!(expected_html, actual_html);
338+
}

crates/ra_ide_db/src/defs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl Definition {
7878
}
7979
}
8080

81+
#[derive(Debug)]
8182
pub enum NameClass {
8283
Definition(Definition),
8384
/// `None` in `if let None = Some(82) {}`
@@ -131,9 +132,11 @@ pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option
131132
let local = sema.to_def(&it)?;
132133

133134
if let Some(record_field_pat) = it.syntax().parent().and_then(ast::RecordFieldPat::cast) {
134-
if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) {
135-
let field = Definition::Field(field);
136-
return Some(NameClass::FieldShorthand { local, field });
135+
if record_field_pat.name_ref().is_none() {
136+
if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) {
137+
let field = Definition::Field(field);
138+
return Some(NameClass::FieldShorthand { local, field });
139+
}
137140
}
138141
}
139142

0 commit comments

Comments
 (0)