fix: do not add use stmt when use stmt is selected in extract_module

This commit is contained in:
roife 2024-03-14 19:50:36 +08:00
parent 02214a6d12
commit 6248b45340

View File

@ -1,12 +1,10 @@
use std::iter; use std::iter;
use hir::{HirFileIdExt, ModuleSource}; use hir::{HasSource, HirFileIdExt, ModuleSource};
use ide_db::{ use ide_db::{
assists::{AssistId, AssistKind}, assists::{AssistId, AssistKind},
base_db::FileId, base_db::FileId,
defs::{Definition, NameClass, NameRefClass}, defs::{Definition, NameClass, NameRefClass},
helpers::item_name,
items_locator::items_with_name,
search::{FileReference, SearchScope}, search::{FileReference, SearchScope},
FxHashMap, FxHashSet, FxHashMap, FxHashSet,
}; };
@ -473,6 +471,9 @@ fn process_def_in_sel(
.filter(|(use_file_id, _)| *use_file_id == file_id) .filter(|(use_file_id, _)| *use_file_id == file_id)
.flat_map(|(_, refs)| refs.into_iter().rev()) .flat_map(|(_, refs)| refs.into_iter().rev())
.find_map(|fref| find_node_at_range(file.syntax(), fref.range)); .find_map(|fref| find_node_at_range(file.syntax(), fref.range));
let use_stmt_not_in_sel = use_stmt.as_ref().is_some_and(|use_stmt| {
!selection_range.contains_range(use_stmt.syntax().text_range())
});
let mut use_tree_str_opt: Option<Vec<ast::Path>> = None; let mut use_tree_str_opt: Option<Vec<ast::Path>> = None;
//Exists inside and outside selection //Exists inside and outside selection
@ -565,7 +566,7 @@ fn process_def_in_sel(
_ => false, _ => false,
}; };
if def_out_sel || !is_item { if (def_out_sel || !is_item) && use_stmt_not_in_sel {
self.use_items.insert(0, item.clone()); self.use_items.insert(0, item.clone());
} }
} }