From 05839199190819bf835ba20442ecbc7b06471a8f Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Tue, 22 Mar 2022 12:00:20 -0700 Subject: [PATCH 1/2] LSIF: consolidate references into a single edge where possible. --- crates/rust-analyzer/src/cli/lsif.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/rust-analyzer/src/cli/lsif.rs b/crates/rust-analyzer/src/cli/lsif.rs index b9bb335b05..188f9b1380 100644 --- a/crates/rust-analyzer/src/cli/lsif.rs +++ b/crates/rust-analyzer/src/cli/lsif.rs @@ -212,19 +212,24 @@ impl LsifManager<'_> { in_v: result_id.into(), out_v: result_set_id.into(), })); - for x in token.references { - let vertex = *self.range_map.get(&x.range).unwrap(); + let edges = token.references.iter().fold( + HashMap::<_, Vec>::new(), + |mut edges, x| { + let entry = + edges.entry((x.range.file_id, x.is_definition)).or_insert_with(Vec::new); + entry.push((*self.range_map.get(&x.range).unwrap()).into()); + edges + }, + ); + for ((file_id, is_definition), vertices) in edges.into_iter() { self.add_edge(lsif::Edge::Item(lsif::Item { - document: (*self.file_map.get(&x.range.file_id).unwrap()).into(), - property: Some(if x.is_definition { + document: (*self.file_map.get(&file_id).unwrap()).into(), + property: Some(if is_definition { lsif::ItemKind::Definitions } else { lsif::ItemKind::References }), - edge_data: lsif::EdgeDataMultiIn { - in_vs: vec![vertex.into()], - out_v: result_id.into(), - }, + edge_data: lsif::EdgeDataMultiIn { in_vs: vertices, out_v: result_id.into() }, })); } } From af1aa86ac0df3470b379d21531e67fae0e9c942f Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Tue, 22 Mar 2022 12:38:59 -0700 Subject: [PATCH 2/2] Preserve order when grouping references. --- crates/rust-analyzer/src/cli/lsif.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/rust-analyzer/src/cli/lsif.rs b/crates/rust-analyzer/src/cli/lsif.rs index 188f9b1380..5e6d286987 100644 --- a/crates/rust-analyzer/src/cli/lsif.rs +++ b/crates/rust-analyzer/src/cli/lsif.rs @@ -212,7 +212,7 @@ impl LsifManager<'_> { in_v: result_id.into(), out_v: result_set_id.into(), })); - let edges = token.references.iter().fold( + let mut edges = token.references.iter().fold( HashMap::<_, Vec>::new(), |mut edges, x| { let entry = @@ -221,16 +221,21 @@ impl LsifManager<'_> { edges }, ); - for ((file_id, is_definition), vertices) in edges.into_iter() { - self.add_edge(lsif::Edge::Item(lsif::Item { - document: (*self.file_map.get(&file_id).unwrap()).into(), - property: Some(if is_definition { - lsif::ItemKind::Definitions - } else { - lsif::ItemKind::References - }), - edge_data: lsif::EdgeDataMultiIn { in_vs: vertices, out_v: result_id.into() }, - })); + for x in token.references { + if let Some(vertices) = edges.remove(&(x.range.file_id, x.is_definition)) { + self.add_edge(lsif::Edge::Item(lsif::Item { + document: (*self.file_map.get(&x.range.file_id).unwrap()).into(), + property: Some(if x.is_definition { + lsif::ItemKind::Definitions + } else { + lsif::ItemKind::References + }), + edge_data: lsif::EdgeDataMultiIn { + in_vs: vertices, + out_v: result_id.into(), + }, + })); + } } } }