@@ -99,76 +99,66 @@ pub(crate) fn json_in_items(
9999 && node. last_token ( ) . map ( |x| x. kind ( ) ) == Some ( SyntaxKind :: R_CURLY )
100100 {
101101 let node_string = node. to_string ( ) ;
102- if let Ok ( it) = serde_json:: from_str ( & node_string) {
103- if let serde_json:: Value :: Object ( it) = it {
104- let import_scope = ImportScope :: find_insert_use_container ( node, sema) ?;
105- let range = node. text_range ( ) ;
106- let mut edit = TextEdit :: builder ( ) ;
107- edit. delete ( range) ;
108- let mut state = State :: default ( ) ;
109- let semantics_scope = sema. scope ( node) ?;
110- let scope_resolve =
111- |it| semantics_scope. speculative_resolve ( & make:: path_from_text ( it) ) ;
112- let scope_has = |it| scope_resolve ( it) . is_some ( ) ;
113- let deserialize_resolved = scope_resolve ( "::serde::Deserialize" ) ;
114- let serialize_resolved = scope_resolve ( "::serde::Serialize" ) ;
115- state. has_deserialize = deserialize_resolved. is_some ( ) ;
116- state. has_serialize = serialize_resolved. is_some ( ) ;
117- state. build_struct ( & it) ;
118- edit. insert ( range. start ( ) , state. result ) ;
119- acc. push (
120- Diagnostic :: new (
121- "json-is-not-rust" ,
122- "JSON syntax is not valid as a Rust item" ,
123- range,
124- )
125- . severity ( Severity :: WeakWarning )
126- . with_fixes ( Some ( vec ! [ {
127- let mut scb = SourceChangeBuilder :: new( file_id) ;
128- let scope = match import_scope {
129- ImportScope :: File ( it) => ImportScope :: File ( scb. make_mut( it) ) ,
130- ImportScope :: Module ( it) => ImportScope :: Module ( scb. make_mut( it) ) ,
131- ImportScope :: Block ( it) => ImportScope :: Block ( scb. make_mut( it) ) ,
132- } ;
133- let current_module = semantics_scope. module( ) ;
134- if !scope_has( "Serialize" ) {
135- if let Some ( PathResolution :: Def ( it) ) = serialize_resolved {
136- if let Some ( it) = current_module. find_use_path_prefixed(
137- sema. db,
138- it,
139- config. insert_use. prefix_kind,
140- config. prefer_no_std,
141- ) {
142- insert_use(
143- & scope,
144- mod_path_to_ast( & it) ,
145- & config. insert_use,
146- ) ;
147- }
102+ if let Ok ( serde_json:: Value :: Object ( it) ) = serde_json:: from_str ( & node_string) {
103+ let import_scope = ImportScope :: find_insert_use_container ( node, sema) ?;
104+ let range = node. text_range ( ) ;
105+ let mut edit = TextEdit :: builder ( ) ;
106+ edit. delete ( range) ;
107+ let mut state = State :: default ( ) ;
108+ let semantics_scope = sema. scope ( node) ?;
109+ let scope_resolve =
110+ |it| semantics_scope. speculative_resolve ( & make:: path_from_text ( it) ) ;
111+ let scope_has = |it| scope_resolve ( it) . is_some ( ) ;
112+ let deserialize_resolved = scope_resolve ( "::serde::Deserialize" ) ;
113+ let serialize_resolved = scope_resolve ( "::serde::Serialize" ) ;
114+ state. has_deserialize = deserialize_resolved. is_some ( ) ;
115+ state. has_serialize = serialize_resolved. is_some ( ) ;
116+ state. build_struct ( & it) ;
117+ edit. insert ( range. start ( ) , state. result ) ;
118+ acc. push (
119+ Diagnostic :: new (
120+ "json-is-not-rust" ,
121+ "JSON syntax is not valid as a Rust item" ,
122+ range,
123+ )
124+ . severity ( Severity :: WeakWarning )
125+ . with_fixes ( Some ( vec ! [ {
126+ let mut scb = SourceChangeBuilder :: new( file_id) ;
127+ let scope = match import_scope {
128+ ImportScope :: File ( it) => ImportScope :: File ( scb. make_mut( it) ) ,
129+ ImportScope :: Module ( it) => ImportScope :: Module ( scb. make_mut( it) ) ,
130+ ImportScope :: Block ( it) => ImportScope :: Block ( scb. make_mut( it) ) ,
131+ } ;
132+ let current_module = semantics_scope. module( ) ;
133+ if !scope_has( "Serialize" ) {
134+ if let Some ( PathResolution :: Def ( it) ) = serialize_resolved {
135+ if let Some ( it) = current_module. find_use_path_prefixed(
136+ sema. db,
137+ it,
138+ config. insert_use. prefix_kind,
139+ config. prefer_no_std,
140+ ) {
141+ insert_use( & scope, mod_path_to_ast( & it) , & config. insert_use) ;
148142 }
149143 }
150- if !scope_has( "Deserialize" ) {
151- if let Some ( PathResolution :: Def ( it) ) = deserialize_resolved {
152- if let Some ( it) = current_module. find_use_path_prefixed(
153- sema. db,
154- it,
155- config. insert_use. prefix_kind,
156- config. prefer_no_std,
157- ) {
158- insert_use(
159- & scope,
160- mod_path_to_ast( & it) ,
161- & config. insert_use,
162- ) ;
163- }
144+ }
145+ if !scope_has( "Deserialize" ) {
146+ if let Some ( PathResolution :: Def ( it) ) = deserialize_resolved {
147+ if let Some ( it) = current_module. find_use_path_prefixed(
148+ sema. db,
149+ it,
150+ config. insert_use. prefix_kind,
151+ config. prefer_no_std,
152+ ) {
153+ insert_use( & scope, mod_path_to_ast( & it) , & config. insert_use) ;
164154 }
165155 }
166- let mut sc = scb . finish ( ) ;
167- sc . insert_source_edit ( file_id , edit . finish( ) ) ;
168- fix ( "convert_json_to_struct" , "Convert JSON to struct" , sc , range )
169- } ] ) ) ,
170- ) ;
171- }
156+ }
157+ let mut sc = scb . finish( ) ;
158+ sc . insert_source_edit ( file_id , edit . finish ( ) ) ;
159+ fix ( "convert_json_to_struct" , "Convert JSON to struct" , sc , range )
160+ } ] ) ) ,
161+ ) ;
172162 }
173163 }
174164 Some ( ( ) )
0 commit comments