mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 13:33:31 +00:00
a number of code simplifications
This commit is contained in:
parent
3987c835f2
commit
ac3844a0bb
13 changed files with 99 additions and 124 deletions
|
@ -80,7 +80,7 @@ impl<'a> Write for Printer<'a> {
|
|||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
for line in s.split_inclusive('\n') {
|
||||
if self.needs_indent {
|
||||
match self.buf.chars().rev().skip_while(|ch| *ch == ' ').next() {
|
||||
match self.buf.chars().rev().find(|ch| *ch != ' ') {
|
||||
Some('\n') | None => {}
|
||||
_ => self.buf.push('\n'),
|
||||
}
|
||||
|
|
|
@ -1600,17 +1600,15 @@ impl ModCollector<'_, '_> {
|
|||
FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
|
||||
|
||||
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
|
||||
if self.def_collector.is_proc_macro {
|
||||
if self.module_id == def_map.root {
|
||||
if let Some(proc_macro) = attrs.parse_proc_macro_decl(&it.name) {
|
||||
let crate_root = def_map.module_id(def_map.root);
|
||||
self.def_collector.export_proc_macro(
|
||||
proc_macro,
|
||||
ItemTreeId::new(self.tree_id, id),
|
||||
fn_id,
|
||||
crate_root,
|
||||
);
|
||||
}
|
||||
if self.def_collector.is_proc_macro && self.module_id == def_map.root {
|
||||
if let Some(proc_macro) = attrs.parse_proc_macro_decl(&it.name) {
|
||||
let crate_root = def_map.module_id(def_map.root);
|
||||
self.def_collector.export_proc_macro(
|
||||
proc_macro,
|
||||
ItemTreeId::new(self.tree_id, id),
|
||||
fn_id,
|
||||
crate_root,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -472,8 +472,8 @@ impl Module {
|
|||
let def_map = self.id.def_map(db.upcast());
|
||||
let children = def_map[self.id.local_id]
|
||||
.children
|
||||
.iter()
|
||||
.map(|(_, module_id)| Module { id: def_map.module_id(*module_id) })
|
||||
.values()
|
||||
.map(|module_id| Module { id: def_map.module_id(*module_id) })
|
||||
.collect::<Vec<_>>();
|
||||
children.into_iter()
|
||||
}
|
||||
|
|
|
@ -82,18 +82,18 @@ fn generate_trait_impl_text_from_impl(impl_: &ast::Impl, trait_text: &str, code:
|
|||
let generic_params = impl_.generic_param_list().map(|generic_params| {
|
||||
let lifetime_params =
|
||||
generic_params.lifetime_params().map(ast::GenericParam::LifetimeParam);
|
||||
let ty_or_const_params = generic_params.type_or_const_params().filter_map(|param| {
|
||||
let ty_or_const_params = generic_params.type_or_const_params().map(|param| {
|
||||
// remove defaults since they can't be specified in impls
|
||||
match param {
|
||||
ast::TypeOrConstParam::Type(param) => {
|
||||
let param = param.clone_for_update();
|
||||
param.remove_default();
|
||||
Some(ast::GenericParam::TypeParam(param))
|
||||
ast::GenericParam::TypeParam(param)
|
||||
}
|
||||
ast::TypeOrConstParam::Const(param) => {
|
||||
let param = param.clone_for_update();
|
||||
param.remove_default();
|
||||
Some(ast::GenericParam::ConstParam(param))
|
||||
ast::GenericParam::ConstParam(param)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -92,7 +92,7 @@ trait Merge: AstNode + Clone {
|
|||
fn try_merge_from(self, items: &mut dyn Iterator<Item = Self>) -> Option<Vec<Edit>> {
|
||||
let mut edits = Vec::new();
|
||||
let mut merged = self.clone();
|
||||
while let Some(item) = items.next() {
|
||||
for item in items {
|
||||
merged = merged.try_merge(&item)?;
|
||||
edits.push(Edit::Remove(item.into_either()));
|
||||
}
|
||||
|
|
|
@ -86,8 +86,7 @@ pub(crate) fn unmerge_match_arm(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
|
|||
it.prev_sibling_or_token()
|
||||
})
|
||||
.map(|it| it.kind())
|
||||
.skip_while(|it| it.is_trivia())
|
||||
.next()
|
||||
.find(|it| !it.is_trivia())
|
||||
== Some(T![,]);
|
||||
let has_arms_after = neighbor(&match_arm, Direction::Next).is_some();
|
||||
if !has_comma_after && !has_arms_after {
|
||||
|
|
|
@ -334,11 +334,9 @@ pub fn source_edit_from_references(
|
|||
}
|
||||
_ => false,
|
||||
};
|
||||
if !has_emitted_edit {
|
||||
if !edited_ranges.contains(&range.start()) {
|
||||
edit.replace(range, new_name.to_string());
|
||||
edited_ranges.push(range.start());
|
||||
}
|
||||
if !has_emitted_edit && !edited_ranges.contains(&range.start()) {
|
||||
edit.replace(range, new_name.to_string());
|
||||
edited_ranges.push(range.start());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -494,20 +494,28 @@ impl<'a> FindUsages<'a> {
|
|||
}
|
||||
|
||||
// Search for `super` and `crate` resolving to our module
|
||||
match self.def {
|
||||
Definition::Module(module) => {
|
||||
let scope = search_scope
|
||||
.intersection(&SearchScope::module_and_children(self.sema.db, module));
|
||||
if let Definition::Module(module) = self.def {
|
||||
let scope =
|
||||
search_scope.intersection(&SearchScope::module_and_children(self.sema.db, module));
|
||||
|
||||
let is_crate_root =
|
||||
module.is_crate_root(self.sema.db).then(|| Finder::new("crate"));
|
||||
let finder = &Finder::new("super");
|
||||
let is_crate_root = module.is_crate_root(self.sema.db).then(|| Finder::new("crate"));
|
||||
let finder = &Finder::new("super");
|
||||
|
||||
for (text, file_id, search_range) in scope_files(sema, &scope) {
|
||||
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
|
||||
for (text, file_id, search_range) in scope_files(sema, &scope) {
|
||||
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
|
||||
|
||||
for offset in match_indices(&text, finder, search_range) {
|
||||
if let Some(iter) = find_nodes("super", &tree, offset) {
|
||||
for name_ref in iter.filter_map(ast::NameRef::cast) {
|
||||
if self.found_name_ref(&name_ref, sink) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(finder) = &is_crate_root {
|
||||
for offset in match_indices(&text, finder, search_range) {
|
||||
if let Some(iter) = find_nodes("super", &tree, offset) {
|
||||
if let Some(iter) = find_nodes("crate", &tree, offset) {
|
||||
for name_ref in iter.filter_map(ast::NameRef::cast) {
|
||||
if self.found_name_ref(&name_ref, sink) {
|
||||
return;
|
||||
|
@ -515,20 +523,8 @@ impl<'a> FindUsages<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
if let Some(finder) = &is_crate_root {
|
||||
for offset in match_indices(&text, finder, search_range) {
|
||||
if let Some(iter) = find_nodes("crate", &tree, offset) {
|
||||
for name_ref in iter.filter_map(ast::NameRef::cast) {
|
||||
if self.found_name_ref(&name_ref, sink) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// search for module `self` references in our module's definition source
|
||||
|
|
|
@ -99,76 +99,66 @@ pub(crate) fn json_in_items(
|
|||
&& node.last_token().map(|x| x.kind()) == Some(SyntaxKind::R_CURLY)
|
||||
{
|
||||
let node_string = node.to_string();
|
||||
if let Ok(it) = serde_json::from_str(&node_string) {
|
||||
if let serde_json::Value::Object(it) = it {
|
||||
let import_scope = ImportScope::find_insert_use_container(node, sema)?;
|
||||
let range = node.text_range();
|
||||
let mut edit = TextEdit::builder();
|
||||
edit.delete(range);
|
||||
let mut state = State::default();
|
||||
let semantics_scope = sema.scope(node)?;
|
||||
let scope_resolve =
|
||||
|it| semantics_scope.speculative_resolve(&make::path_from_text(it));
|
||||
let scope_has = |it| scope_resolve(it).is_some();
|
||||
let deserialize_resolved = scope_resolve("::serde::Deserialize");
|
||||
let serialize_resolved = scope_resolve("::serde::Serialize");
|
||||
state.has_deserialize = deserialize_resolved.is_some();
|
||||
state.has_serialize = serialize_resolved.is_some();
|
||||
state.build_struct(&it);
|
||||
edit.insert(range.start(), state.result);
|
||||
acc.push(
|
||||
Diagnostic::new(
|
||||
"json-is-not-rust",
|
||||
"JSON syntax is not valid as a Rust item",
|
||||
range,
|
||||
)
|
||||
.severity(Severity::WeakWarning)
|
||||
.with_fixes(Some(vec![{
|
||||
let mut scb = SourceChangeBuilder::new(file_id);
|
||||
let scope = match import_scope {
|
||||
ImportScope::File(it) => ImportScope::File(scb.make_mut(it)),
|
||||
ImportScope::Module(it) => ImportScope::Module(scb.make_mut(it)),
|
||||
ImportScope::Block(it) => ImportScope::Block(scb.make_mut(it)),
|
||||
};
|
||||
let current_module = semantics_scope.module();
|
||||
if !scope_has("Serialize") {
|
||||
if let Some(PathResolution::Def(it)) = serialize_resolved {
|
||||
if let Some(it) = current_module.find_use_path_prefixed(
|
||||
sema.db,
|
||||
it,
|
||||
config.insert_use.prefix_kind,
|
||||
config.prefer_no_std,
|
||||
) {
|
||||
insert_use(
|
||||
&scope,
|
||||
mod_path_to_ast(&it),
|
||||
&config.insert_use,
|
||||
);
|
||||
}
|
||||
if let Ok(serde_json::Value::Object(it)) = serde_json::from_str(&node_string) {
|
||||
let import_scope = ImportScope::find_insert_use_container(node, sema)?;
|
||||
let range = node.text_range();
|
||||
let mut edit = TextEdit::builder();
|
||||
edit.delete(range);
|
||||
let mut state = State::default();
|
||||
let semantics_scope = sema.scope(node)?;
|
||||
let scope_resolve =
|
||||
|it| semantics_scope.speculative_resolve(&make::path_from_text(it));
|
||||
let scope_has = |it| scope_resolve(it).is_some();
|
||||
let deserialize_resolved = scope_resolve("::serde::Deserialize");
|
||||
let serialize_resolved = scope_resolve("::serde::Serialize");
|
||||
state.has_deserialize = deserialize_resolved.is_some();
|
||||
state.has_serialize = serialize_resolved.is_some();
|
||||
state.build_struct(&it);
|
||||
edit.insert(range.start(), state.result);
|
||||
acc.push(
|
||||
Diagnostic::new(
|
||||
"json-is-not-rust",
|
||||
"JSON syntax is not valid as a Rust item",
|
||||
range,
|
||||
)
|
||||
.severity(Severity::WeakWarning)
|
||||
.with_fixes(Some(vec![{
|
||||
let mut scb = SourceChangeBuilder::new(file_id);
|
||||
let scope = match import_scope {
|
||||
ImportScope::File(it) => ImportScope::File(scb.make_mut(it)),
|
||||
ImportScope::Module(it) => ImportScope::Module(scb.make_mut(it)),
|
||||
ImportScope::Block(it) => ImportScope::Block(scb.make_mut(it)),
|
||||
};
|
||||
let current_module = semantics_scope.module();
|
||||
if !scope_has("Serialize") {
|
||||
if let Some(PathResolution::Def(it)) = serialize_resolved {
|
||||
if let Some(it) = current_module.find_use_path_prefixed(
|
||||
sema.db,
|
||||
it,
|
||||
config.insert_use.prefix_kind,
|
||||
config.prefer_no_std,
|
||||
) {
|
||||
insert_use(&scope, mod_path_to_ast(&it), &config.insert_use);
|
||||
}
|
||||
}
|
||||
if !scope_has("Deserialize") {
|
||||
if let Some(PathResolution::Def(it)) = deserialize_resolved {
|
||||
if let Some(it) = current_module.find_use_path_prefixed(
|
||||
sema.db,
|
||||
it,
|
||||
config.insert_use.prefix_kind,
|
||||
config.prefer_no_std,
|
||||
) {
|
||||
insert_use(
|
||||
&scope,
|
||||
mod_path_to_ast(&it),
|
||||
&config.insert_use,
|
||||
);
|
||||
}
|
||||
}
|
||||
if !scope_has("Deserialize") {
|
||||
if let Some(PathResolution::Def(it)) = deserialize_resolved {
|
||||
if let Some(it) = current_module.find_use_path_prefixed(
|
||||
sema.db,
|
||||
it,
|
||||
config.insert_use.prefix_kind,
|
||||
config.prefer_no_std,
|
||||
) {
|
||||
insert_use(&scope, mod_path_to_ast(&it), &config.insert_use);
|
||||
}
|
||||
}
|
||||
let mut sc = scb.finish();
|
||||
sc.insert_source_edit(file_id, edit.finish());
|
||||
fix("convert_json_to_struct", "Convert JSON to struct", sc, range)
|
||||
}])),
|
||||
);
|
||||
}
|
||||
}
|
||||
let mut sc = scb.finish();
|
||||
sc.insert_source_edit(file_id, edit.finish());
|
||||
fix("convert_json_to_struct", "Convert JSON to struct", sc, range)
|
||||
}])),
|
||||
);
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
|
|
|
@ -11,10 +11,7 @@ pub(crate) fn private_assoc_item(
|
|||
d: &hir::PrivateAssocItem,
|
||||
) -> Diagnostic {
|
||||
// FIXME: add quickfix
|
||||
let name = match d.item.name(ctx.sema.db) {
|
||||
Some(name) => format!("`{}` ", name),
|
||||
None => String::new(),
|
||||
};
|
||||
let name = d.item.name(ctx.sema.db).map(|name| format!("`{name}` ")).unwrap_or_default();
|
||||
Diagnostic::new(
|
||||
"private-assoc-item",
|
||||
format!(
|
||||
|
|
|
@ -34,10 +34,7 @@ pub(crate) fn unresolved_proc_macro(
|
|||
let message = format!(
|
||||
"{message}: {}",
|
||||
if config_enabled {
|
||||
match def_map.proc_macro_loading_error() {
|
||||
Some(e) => e,
|
||||
None => "proc macro not found in the built dylib",
|
||||
}
|
||||
def_map.proc_macro_loading_error().unwrap_or("proc macro not found in the built dylib")
|
||||
} else {
|
||||
match d.kind {
|
||||
hir::MacroKind::Attr if proc_macros_enabled => {
|
||||
|
|
|
@ -212,7 +212,7 @@ fn expand_var(ctx: &mut ExpandCtx<'_>, v: &SmolStr, id: tt::TokenId) -> ExpandRe
|
|||
} else {
|
||||
ctx.bindings.get(v, &mut ctx.nesting).map_or_else(
|
||||
|e| ExpandResult { value: Fragment::Tokens(tt::TokenTree::empty()), err: Some(e) },
|
||||
|it| ExpandResult::ok(it),
|
||||
ExpandResult::ok,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -366,11 +366,11 @@ impl ProjectWorkspace {
|
|||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let ref mut outputs = match WorkspaceBuildScripts::run_once(config, &cargo_ws, progress) {
|
||||
let outputs = &mut (match WorkspaceBuildScripts::run_once(config, &cargo_ws, progress) {
|
||||
Ok(it) => Ok(it.into_iter()),
|
||||
// io::Error is not Clone?
|
||||
Err(e) => Err(Arc::new(e)),
|
||||
};
|
||||
});
|
||||
|
||||
workspaces
|
||||
.iter()
|
||||
|
|
Loading…
Reference in a new issue