Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/assists/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ stdx = { path = "../stdx", version = "0.0.0" }
syntax = { path = "../syntax", version = "0.0.0" }
text_edit = { path = "../text_edit", version = "0.0.0" }
profile = { path = "../profile", version = "0.0.0" }
base_db = { path = "../base_db", version = "0.0.0" }
ide_db = { path = "../ide_db", version = "0.0.0" }
hir = { path = "../hir", version = "0.0.0" }
test_utils = { path = "../test_utils", version = "0.0.0" }
2 changes: 1 addition & 1 deletion crates/assists/src/assist_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::mem;

use algo::find_covering_element;
use base_db::{FileId, FileRange};
use hir::Semantics;
use ide_db::base_db::{FileId, FileRange};
use ide_db::{
label::Label,
source_change::{SourceChange, SourceFileEdit},
Expand Down
3 changes: 2 additions & 1 deletion crates/assists/src/handlers/add_missing_impl_members.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use hir::HasSource;
use ide_db::traits::{get_missing_assoc_items, resolve_target_trait};
use syntax::{
ast::{
self,
Expand All @@ -11,7 +12,7 @@ use syntax::{
use crate::{
assist_context::{AssistContext, Assists},
ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
utils::{get_missing_assoc_items, render_snippet, resolve_target_trait, Cursor},
utils::{render_snippet, Cursor},
AssistId, AssistKind,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use base_db::FileId;
use hir::{EnumVariant, Module, ModuleDef, Name};
use ide_db::base_db::FileId;
use ide_db::{defs::Definition, search::Reference, RootDatabase};
use itertools::Itertools;
use rustc_hash::FxHashSet;
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/fix_visibility.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use base_db::FileId;
use hir::{db::HirDatabase, HasSource, HasVisibility, PathResolution};
use ide_db::base_db::FileId;
use syntax::{
ast::{self, VisibilityOwner},
AstNode, TextRange, TextSize,
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/generate_function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use base_db::FileId;
use hir::HirDisplay;
use ide_db::base_db::FileId;
use rustc_hash::{FxHashMap, FxHashSet};
use syntax::{
ast::{
Expand Down
6 changes: 2 additions & 4 deletions crates/assists/src/handlers/replace_if_let_with_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use syntax::{
AstNode,
};

use crate::{
utils::{unwrap_trivial_block, TryEnum},
AssistContext, AssistId, AssistKind, Assists,
};
use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists};
use ide_db::ty_filter::TryEnum;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor and is not required to be fixed, but we put crate:: and other_crate:: deps in different groups: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/style.md#order-of-imports


// Assist: replace_if_let_with_match
//
Expand Down
3 changes: 2 additions & 1 deletion crates/assists/src/handlers/replace_let_with_if_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use syntax::{
AstNode, T,
};

use crate::{utils::TryEnum, AssistContext, AssistId, AssistKind, Assists};
use crate::{AssistContext, AssistId, AssistKind, Assists};
use ide_db::ty_filter::TryEnum;

// Assist: replace_let_with_if_let
//
Expand Down
3 changes: 2 additions & 1 deletion crates/assists/src/handlers/replace_unwrap_with_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use syntax::{
};

use crate::{
utils::{render_snippet, Cursor, TryEnum},
utils::{render_snippet, Cursor},
AssistContext, AssistId, AssistKind, Assists,
};
use ide_db::ty_filter::TryEnum;

// Assist: replace_unwrap_with_match
//
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ mod tests;
pub mod utils;
pub mod ast_transform;

use base_db::FileRange;
use hir::Semantics;
use ide_db::base_db::FileRange;
use ide_db::{label::Label, source_change::SourceChange, RootDatabase};
use syntax::TextRange;

Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod generated;

use base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
use hir::Semantics;
use ide_db::base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
use ide_db::RootDatabase;
use syntax::TextRange;
use test_utils::{assert_eq_text, extract_offset, extract_range};
Expand Down
121 changes: 3 additions & 118 deletions crates/assists/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
pub(crate) mod insert_use;
pub(crate) mod import_assets;

use std::{iter, ops};
use std::ops;

use hir::{Adt, Crate, Enum, Module, ScopeDef, Semantics, Trait, Type};
use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait};
use ide_db::RootDatabase;
use itertools::Itertools;
use rustc_hash::FxHashSet;
use syntax::{
ast::{self, make, ArgListOwner, NameOwner},
ast::{self, make, ArgListOwner},
AstNode, Direction,
SyntaxKind::*,
SyntaxNode, TextSize, T,
Expand Down Expand Up @@ -115,72 +114,6 @@ pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor
}
}

pub fn get_missing_assoc_items(
sema: &Semantics<RootDatabase>,
impl_def: &ast::Impl,
) -> Vec<hir::AssocItem> {
// Names must be unique between constants and functions. However, type aliases
// may share the same name as a function or constant.
let mut impl_fns_consts = FxHashSet::default();
let mut impl_type = FxHashSet::default();

if let Some(item_list) = impl_def.assoc_item_list() {
for item in item_list.assoc_items() {
match item {
ast::AssocItem::Fn(f) => {
if let Some(n) = f.name() {
impl_fns_consts.insert(n.syntax().to_string());
}
}

ast::AssocItem::TypeAlias(t) => {
if let Some(n) = t.name() {
impl_type.insert(n.syntax().to_string());
}
}

ast::AssocItem::Const(c) => {
if let Some(n) = c.name() {
impl_fns_consts.insert(n.syntax().to_string());
}
}
ast::AssocItem::MacroCall(_) => (),
}
}
}

resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| {
target_trait
.items(sema.db)
.iter()
.filter(|i| match i {
hir::AssocItem::Function(f) => {
!impl_fns_consts.contains(&f.name(sema.db).to_string())
}
hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(sema.db).to_string()),
hir::AssocItem::Const(c) => c
.name(sema.db)
.map(|n| !impl_fns_consts.contains(&n.to_string()))
.unwrap_or_default(),
})
.cloned()
.collect()
})
}

pub(crate) fn resolve_target_trait(
sema: &Semantics<RootDatabase>,
impl_def: &ast::Impl,
) -> Option<hir::Trait> {
let ast_path =
impl_def.trait_().map(|it| it.syntax().clone()).and_then(ast::PathType::cast)?.path()?;

match sema.resolve_path(&ast_path) {
Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def),
_ => None,
}
}

pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
node.children_with_tokens()
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
Expand Down Expand Up @@ -223,54 +156,6 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
}
}

#[derive(Clone, Copy)]
pub enum TryEnum {
Result,
Option,
}

impl TryEnum {
const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];

pub fn from_ty(sema: &Semantics<RootDatabase>, ty: &Type) -> Option<TryEnum> {
let enum_ = match ty.as_adt() {
Some(Adt::Enum(it)) => it,
_ => return None,
};
TryEnum::ALL.iter().find_map(|&var| {
if &enum_.name(sema.db).to_string() == var.type_name() {
return Some(var);
}
None
})
}

pub(crate) fn happy_case(self) -> &'static str {
match self {
TryEnum::Result => "Ok",
TryEnum::Option => "Some",
}
}

pub(crate) fn sad_pattern(self) -> ast::Pat {
match self {
TryEnum::Result => make::tuple_struct_pat(
make::path_unqualified(make::path_segment(make::name_ref("Err"))),
iter::once(make::wildcard_pat().into()),
)
.into(),
TryEnum::Option => make::ident_pat(make::name("None")).into(),
}
}

fn type_name(self) -> &'static str {
match self {
TryEnum::Result => "Result",
TryEnum::Option => "Option",
}
}
}

/// Helps with finding well-know things inside the standard library. This is
/// somewhat similar to the known paths infra inside hir, but it different; We
/// want to make sure that IDE specific paths don't become interesting inside
Expand Down
26 changes: 0 additions & 26 deletions crates/call_info/Cargo.toml

This file was deleted.

2 changes: 0 additions & 2 deletions crates/completion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ base_db = { path = "../base_db", version = "0.0.0" }
ide_db = { path = "../ide_db", version = "0.0.0" }
profile = { path = "../profile", version = "0.0.0" }
test_utils = { path = "../test_utils", version = "0.0.0" }
assists = { path = "../assists", version = "0.0.0" }
call_info = { path = "../call_info", version = "0.0.0" }

# completions crate should depend only on the top-level `hir` package. if you need
# something from some `hir_xxx` subpackage, reexport the API via `hir`.
Expand Down
2 changes: 1 addition & 1 deletion crates/completion/src/complete_mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Completes mod declarations.

use base_db::{SourceDatabaseExt, VfsPath};
use hir::{Module, ModuleSource};
use ide_db::base_db::{SourceDatabaseExt, VfsPath};
use ide_db::RootDatabase;
use rustc_hash::FxHashSet;

Expand Down
2 changes: 1 addition & 1 deletion crates/completion/src/complete_postfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

mod format_like;

use assists::utils::TryEnum;
use ide_db::ty_filter::TryEnum;
use syntax::{
ast::{self, AstNode, AstToken},
TextRange, TextSize,
Expand Down
2 changes: 1 addition & 1 deletion crates/completion/src/complete_trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
//! }
//! ```

use assists::utils::get_missing_assoc_items;
use hir::{self, HasAttrs, HasSource};
use ide_db::traits::get_missing_assoc_items;
use syntax::{
ast::{self, edit, Impl},
display::function_declaration,
Expand Down
Loading