@@ -7,7 +7,7 @@ use crate::model::declaration::Declaration;
77use crate :: model:: definitions:: Definition ;
88use crate :: model:: document:: Document ;
99use crate :: model:: identity_maps:: IdentityHashMap ;
10- use crate :: model:: ids:: { DeclarationId , DefinitionId , NameId , ReferenceId , StringId , UriId } ;
10+ use crate :: model:: ids:: { DeclarationId , DefinitionId , DiagnosticId , NameId , ReferenceId , StringId , UriId } ;
1111use crate :: model:: name:: { NameRef , ResolvedName } ;
1212// use crate::model::integrity::IntegrityChecker;
1313use crate :: model:: references:: { ConstantReference , MethodRef } ;
@@ -38,8 +38,7 @@ pub struct Graph {
3838 constant_references : IdentityHashMap < ReferenceId , ConstantReference > ,
3939 // Map of method references that still need to be resolved
4040 method_references : IdentityHashMap < ReferenceId , MethodRef > ,
41-
42- diagnostics : Vec < Diagnostic > ,
41+ diagnostics : IdentityHashMap < DiagnosticId , Diagnostic > ,
4342}
4443
4544impl Graph {
@@ -54,7 +53,7 @@ impl Graph {
5453 names : IdentityHashMap :: default ( ) ,
5554 constant_references : IdentityHashMap :: default ( ) ,
5655 method_references : IdentityHashMap :: default ( ) ,
57- diagnostics : Vec :: new ( ) ,
56+ diagnostics : IdentityHashMap :: default ( ) ,
5857 }
5958 }
6059
@@ -162,8 +161,8 @@ impl Graph {
162161 }
163162
164163 #[ must_use]
165- pub fn diagnostics ( & self ) -> & Vec < Diagnostic > {
166- & self . diagnostics
164+ pub fn diagnostics ( & self ) -> Vec < & Diagnostic > {
165+ self . diagnostics . values ( ) . collect ( )
167166 }
168167
169168 /// # Panics
@@ -266,16 +265,20 @@ impl Graph {
266265 /// Merges everything in `other` into this Graph. This method is meant to merge all graph representations from
267266 /// different threads, but not meant to handle updates to the existing global representation
268267 pub fn extend ( & mut self , local_graph : LocalGraph ) {
269- let ( uri_id, document, definitions, strings, names, constant_references, method_references, diagnostics) =
268+ let ( uri_id, mut document, definitions, strings, names, constant_references, method_references, diagnostics) =
270269 local_graph. into_parts ( ) ;
271270
271+ for diagnostic in diagnostics {
272+ document. add_diagnostic_id ( diagnostic. id ( ) ) ;
273+ self . diagnostics . insert ( diagnostic. id ( ) , diagnostic) ;
274+ }
275+
272276 self . documents . insert ( uri_id, document) ;
273277 self . definitions . extend ( definitions) ;
274278 self . strings . extend ( strings) ;
275279 self . names . extend ( names) ;
276280 self . constant_references . extend ( constant_references) ;
277281 self . method_references . extend ( method_references) ;
278- self . diagnostics . extend ( diagnostics) ;
279282 }
280283
281284 /// Updates the global representation with the information contained in `other`, handling deletions, insertions and
@@ -296,6 +299,10 @@ impl Graph {
296299 return ;
297300 } ;
298301
302+ for diagnostic_id in document. diagnostic_ids ( ) {
303+ self . diagnostics . remove ( diagnostic_id) ;
304+ }
305+
299306 let mut write_lock = self . declarations . write ( ) . unwrap ( ) ;
300307
301308 // TODO: Remove method references from method declarations once method inference is implemented
@@ -616,6 +623,21 @@ mod tests {
616623 context. graph ( ) . assert_integrity ( ) ;
617624 }
618625
626+ #[ test]
627+ fn updating_index_with_deleted_diagnostics ( ) {
628+ let mut context = GraphTest :: new ( ) ;
629+
630+ context. index_uri ( "file:///foo.rb" , "class Foo" ) ;
631+ assert ! ( !context. graph( ) . diagnostics( ) . is_empty( ) ) ;
632+ let document = context. graph ( ) . documents ( ) . get ( & UriId :: from ( "file:///foo.rb" ) ) . unwrap ( ) ;
633+ assert ! ( !document. diagnostic_ids( ) . is_empty( ) ) ;
634+
635+ context. index_uri ( "file:///foo.rb" , "class Foo; end" ) ;
636+ assert ! ( context. graph( ) . diagnostics( ) . is_empty( ) ) ;
637+ let document = context. graph ( ) . documents ( ) . get ( & UriId :: from ( "file:///foo.rb" ) ) . unwrap ( ) ;
638+ assert ! ( document. diagnostic_ids( ) . is_empty( ) ) ;
639+ }
640+
619641 #[ test]
620642 fn updating_index_with_new_definitions ( ) {
621643 let mut context = GraphTest :: new ( ) ;
0 commit comments