From db1cadd4447f7c580c5436a4368a4de3c67fa37a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 10 Jun 2020 12:34:23 +0200 Subject: [PATCH] In field patterns, don't highlight local binding as a field --- crates/ra_ide/src/snapshots/highlighting.html | 4 ++- .../ra_ide/src/syntax_highlighting/tests.rs | 26 ++++++++++--------- crates/ra_ide_db/src/defs.rs | 9 ++++--- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index e34ff5a7da..33548d43cc 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -84,7 +84,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd let y = &mut x; let z = &y; - y; + let Foo { x: z, y } = Foo { x: z, y }; + + y; } enum Option<T> { diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index 021f8e7e2d..949bf59a0d 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs @@ -7,18 +7,6 @@ use crate::{ FileRange, TextRange, }; -/// Highlights the code given by the `ra_fixture` argument, renders the -/// result as HTML, and compares it with the HTML file given as `snapshot`. -/// Note that the `snapshot` file is overwritten by the rendered HTML. -fn check_highlighting(ra_fixture: &str, snapshot: &str, rainbow: bool) { - let (analysis, file_id) = single_file(ra_fixture); - let dst_file = project_dir().join(snapshot); - let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap(); - let expected_html = &read_text(&dst_file); - fs::write(dst_file, &actual_html).unwrap(); - assert_eq_text!(expected_html, actual_html); -} - #[test] fn test_highlighting() { check_highlighting( @@ -77,6 +65,8 @@ fn main() { let y = &mut x; let z = &y; + let Foo { x: z, y } = Foo { x: z, y }; + y; } @@ -334,3 +324,15 @@ impl Foo { false, ) } + +/// Highlights the code given by the `ra_fixture` argument, renders the +/// result as HTML, and compares it with the HTML file given as `snapshot`. +/// Note that the `snapshot` file is overwritten by the rendered HTML. +fn check_highlighting(ra_fixture: &str, snapshot: &str, rainbow: bool) { + let (analysis, file_id) = single_file(ra_fixture); + let dst_file = project_dir().join(snapshot); + let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap(); + let expected_html = &read_text(&dst_file); + fs::write(dst_file, &actual_html).unwrap(); + assert_eq_text!(expected_html, actual_html); +} diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index 1826f3ac65..3ef5e74b69 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -78,6 +78,7 @@ impl Definition { } } +#[derive(Debug)] pub enum NameClass { Definition(Definition), /// `None` in `if let None = Some(82) {}` @@ -131,9 +132,11 @@ pub fn classify_name(sema: &Semantics, name: &ast::Name) -> Option let local = sema.to_def(&it)?; if let Some(record_field_pat) = it.syntax().parent().and_then(ast::RecordFieldPat::cast) { - if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) { - let field = Definition::Field(field); - return Some(NameClass::FieldShorthand { local, field }); + if record_field_pat.name_ref().is_none() { + if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) { + let field = Definition::Field(field); + return Some(NameClass::FieldShorthand { local, field }); + } } }