Skip to content

Commit 8d3d509

Browse files
committed
Remove dependency on 'assists' from 'completion' crate
1 parent 4105378 commit 8d3d509

File tree

12 files changed

+151
-129
lines changed

12 files changed

+151
-129
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/assists/src/handlers/add_missing_impl_members.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use hir::HasSource;
2+
use ide_db::traits::{get_missing_assoc_items, resolve_target_trait};
23
use syntax::{
34
ast::{
45
self,
@@ -11,7 +12,7 @@ use syntax::{
1112
use crate::{
1213
assist_context::{AssistContext, Assists},
1314
ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
14-
utils::{get_missing_assoc_items, render_snippet, resolve_target_trait, Cursor},
15+
utils::{render_snippet, Cursor},
1516
AssistId, AssistKind,
1617
};
1718

crates/assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ use syntax::{
77
AstNode,
88
};
99

10-
use crate::{
11-
utils::{unwrap_trivial_block, TryEnum},
12-
AssistContext, AssistId, AssistKind, Assists,
13-
};
10+
use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists};
11+
use ide_db::ty_filter::TryEnum;
1412

1513
// Assist: replace_if_let_with_match
1614
//

crates/assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use syntax::{
99
AstNode, T,
1010
};
1111

12-
use crate::{utils::TryEnum, AssistContext, AssistId, AssistKind, Assists};
12+
use crate::{AssistContext, AssistId, AssistKind, Assists};
13+
use ide_db::ty_filter::TryEnum;
1314

1415
// Assist: replace_let_with_if_let
1516
//

crates/assists/src/handlers/replace_unwrap_with_match.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use syntax::{
1010
};
1111

1212
use crate::{
13-
utils::{render_snippet, Cursor, TryEnum},
13+
utils::{render_snippet, Cursor},
1414
AssistContext, AssistId, AssistKind, Assists,
1515
};
16+
use ide_db::ty_filter::TryEnum;
1617

1718
// Assist: replace_unwrap_with_match
1819
//

crates/assists/src/utils.rs

Lines changed: 3 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
pub(crate) mod insert_use;
33
pub(crate) mod import_assets;
44

5-
use std::{iter, ops};
5+
use std::ops;
66

7-
use hir::{Adt, Crate, Enum, Module, ScopeDef, Semantics, Trait, Type};
7+
use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait};
88
use ide_db::RootDatabase;
99
use itertools::Itertools;
10-
use rustc_hash::FxHashSet;
1110
use syntax::{
12-
ast::{self, make, ArgListOwner, NameOwner},
11+
ast::{self, make, ArgListOwner},
1312
AstNode, Direction,
1413
SyntaxKind::*,
1514
SyntaxNode, TextSize, T,
@@ -115,72 +114,6 @@ pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor
115114
}
116115
}
117116

118-
pub fn get_missing_assoc_items(
119-
sema: &Semantics<RootDatabase>,
120-
impl_def: &ast::Impl,
121-
) -> Vec<hir::AssocItem> {
122-
// Names must be unique between constants and functions. However, type aliases
123-
// may share the same name as a function or constant.
124-
let mut impl_fns_consts = FxHashSet::default();
125-
let mut impl_type = FxHashSet::default();
126-
127-
if let Some(item_list) = impl_def.assoc_item_list() {
128-
for item in item_list.assoc_items() {
129-
match item {
130-
ast::AssocItem::Fn(f) => {
131-
if let Some(n) = f.name() {
132-
impl_fns_consts.insert(n.syntax().to_string());
133-
}
134-
}
135-
136-
ast::AssocItem::TypeAlias(t) => {
137-
if let Some(n) = t.name() {
138-
impl_type.insert(n.syntax().to_string());
139-
}
140-
}
141-
142-
ast::AssocItem::Const(c) => {
143-
if let Some(n) = c.name() {
144-
impl_fns_consts.insert(n.syntax().to_string());
145-
}
146-
}
147-
ast::AssocItem::MacroCall(_) => (),
148-
}
149-
}
150-
}
151-
152-
resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| {
153-
target_trait
154-
.items(sema.db)
155-
.iter()
156-
.filter(|i| match i {
157-
hir::AssocItem::Function(f) => {
158-
!impl_fns_consts.contains(&f.name(sema.db).to_string())
159-
}
160-
hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(sema.db).to_string()),
161-
hir::AssocItem::Const(c) => c
162-
.name(sema.db)
163-
.map(|n| !impl_fns_consts.contains(&n.to_string()))
164-
.unwrap_or_default(),
165-
})
166-
.cloned()
167-
.collect()
168-
})
169-
}
170-
171-
pub(crate) fn resolve_target_trait(
172-
sema: &Semantics<RootDatabase>,
173-
impl_def: &ast::Impl,
174-
) -> Option<hir::Trait> {
175-
let ast_path =
176-
impl_def.trait_().map(|it| it.syntax().clone()).and_then(ast::PathType::cast)?.path()?;
177-
178-
match sema.resolve_path(&ast_path) {
179-
Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def),
180-
_ => None,
181-
}
182-
}
183-
184117
pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
185118
node.children_with_tokens()
186119
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
@@ -223,54 +156,6 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
223156
}
224157
}
225158

226-
#[derive(Clone, Copy)]
227-
pub enum TryEnum {
228-
Result,
229-
Option,
230-
}
231-
232-
impl TryEnum {
233-
const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];
234-
235-
pub fn from_ty(sema: &Semantics<RootDatabase>, ty: &Type) -> Option<TryEnum> {
236-
let enum_ = match ty.as_adt() {
237-
Some(Adt::Enum(it)) => it,
238-
_ => return None,
239-
};
240-
TryEnum::ALL.iter().find_map(|&var| {
241-
if &enum_.name(sema.db).to_string() == var.type_name() {
242-
return Some(var);
243-
}
244-
None
245-
})
246-
}
247-
248-
pub(crate) fn happy_case(self) -> &'static str {
249-
match self {
250-
TryEnum::Result => "Ok",
251-
TryEnum::Option => "Some",
252-
}
253-
}
254-
255-
pub(crate) fn sad_pattern(self) -> ast::Pat {
256-
match self {
257-
TryEnum::Result => make::tuple_struct_pat(
258-
make::path_unqualified(make::path_segment(make::name_ref("Err"))),
259-
iter::once(make::wildcard_pat().into()),
260-
)
261-
.into(),
262-
TryEnum::Option => make::ident_pat(make::name("None")).into(),
263-
}
264-
}
265-
266-
fn type_name(self) -> &'static str {
267-
match self {
268-
TryEnum::Result => "Result",
269-
TryEnum::Option => "Option",
270-
}
271-
}
272-
}
273-
274159
/// Helps with finding well-know things inside the standard library. This is
275160
/// somewhat similar to the known paths infra inside hir, but it different; We
276161
/// want to make sure that IDE specific paths don't become interesting inside

crates/completion/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ base_db = { path = "../base_db", version = "0.0.0" }
2121
ide_db = { path = "../ide_db", version = "0.0.0" }
2222
profile = { path = "../profile", version = "0.0.0" }
2323
test_utils = { path = "../test_utils", version = "0.0.0" }
24-
assists = { path = "../assists", version = "0.0.0" }
2524
call_info = { path = "../call_info", version = "0.0.0" }
2625

2726
# completions crate should depend only on the top-level `hir` package. if you need

crates/completion/src/complete_postfix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod format_like;
44

5-
use assists::utils::TryEnum;
5+
use ide_db::ty_filter::TryEnum;
66
use syntax::{
77
ast::{self, AstNode, AstToken},
88
TextRange, TextSize,

crates/completion/src/complete_trait_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
//! }
3232
//! ```
3333
34-
use assists::utils::get_missing_assoc_items;
3534
use hir::{self, HasAttrs, HasSource};
35+
use ide_db::traits::get_missing_assoc_items;
3636
use syntax::{
3737
ast::{self, edit, Impl},
3838
display::function_declaration,

crates/ide_db/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub mod defs;
1010
pub mod search;
1111
pub mod imports_locator;
1212
pub mod source_change;
13+
pub mod ty_filter;
14+
pub mod traits;
1315

1416
use std::{fmt, sync::Arc};
1517

0 commit comments

Comments
 (0)