Skip to content

Commit 886cfd6

Browse files
bors[bot]popzxc
andauthored
Merge #6276
6276: Extract call_info and completion into separate crates r=matklad a=popzxc As it was discussed in [zulip](https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Completion.20refactoring), we need to move `completions` into a separate crate. Unfortunately, the dependency on `call_info::ActiveParameter` doesn't look easy to get rid of, and it seems to be a topic for a separate PR, thus I also extracted `call_info` into a separate crate (on which both `ide` and `completion` crates depend). Additionally, a few `FIXME`s in doc-comments were resolved in order to make `tidy` happy. Co-authored-by: Igor Aleksanov <[email protected]>
2 parents 2067a41 + 9e7c952 commit 886cfd6

34 files changed

+337
-227
lines changed

Cargo.lock

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

crates/call_info/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "call_info"
3+
version = "0.0.0"
4+
description = "TBD"
5+
license = "MIT OR Apache-2.0"
6+
authors = ["rust-analyzer developers"]
7+
edition = "2018"
8+
9+
[lib]
10+
doctest = false
11+
12+
[dependencies]
13+
either = "1.5.3"
14+
15+
stdx = { path = "../stdx", version = "0.0.0" }
16+
syntax = { path = "../syntax", version = "0.0.0" }
17+
base_db = { path = "../base_db", version = "0.0.0" }
18+
ide_db = { path = "../ide_db", version = "0.0.0" }
19+
test_utils = { path = "../test_utils", version = "0.0.0" }
20+
21+
# call_info crate should depend only on the top-level `hir` package. if you need
22+
# something from some `hir_xxx` subpackage, reexport the API via `hir`.
23+
hir = { path = "../hir", version = "0.0.0" }
24+
25+
[dev-dependencies]
26+
expect-test = "1.0"

crates/ide/src/call_info.rs renamed to crates/call_info/src/lib.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! FIXME: write short doc here
1+
//! This crate provides primitives for tracking the information about a call site.
2+
use base_db::FilePosition;
23
use either::Either;
34
use hir::{HasAttrs, HirDisplay, Semantics, Type};
45
use ide_db::RootDatabase;
@@ -9,8 +10,6 @@ use syntax::{
910
};
1011
use test_utils::mark;
1112

12-
use crate::FilePosition;
13-
1413
/// Contains information about a call site. Specifically the
1514
/// `FunctionSignature`and current parameter.
1615
#[derive(Debug)]
@@ -40,7 +39,7 @@ impl CallInfo {
4039
}
4140

4241
/// Computes parameter information for the given call expression.
43-
pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
42+
pub fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
4443
let sema = Semantics::new(db);
4544
let file = sema.parse(position.file_id);
4645
let file = file.syntax();
@@ -141,13 +140,13 @@ fn call_info_impl(
141140
}
142141

143142
#[derive(Debug)]
144-
pub(crate) struct ActiveParameter {
145-
pub(crate) ty: Type,
146-
pub(crate) name: String,
143+
pub struct ActiveParameter {
144+
pub ty: Type,
145+
pub name: String,
147146
}
148147

149148
impl ActiveParameter {
150-
pub(crate) fn at(db: &RootDatabase, position: FilePosition) -> Option<Self> {
149+
pub fn at(db: &RootDatabase, position: FilePosition) -> Option<Self> {
151150
let sema = Semantics::new(db);
152151
let file = sema.parse(position.file_id);
153152
let file = file.syntax();
@@ -156,7 +155,7 @@ impl ActiveParameter {
156155
Self::at_token(&sema, token)
157156
}
158157

159-
pub(crate) fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> {
158+
pub fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> {
160159
let (signature, active_parameter) = call_info_impl(&sema, token)?;
161160

162161
let idx = active_parameter?;
@@ -172,7 +171,7 @@ impl ActiveParameter {
172171
}
173172

174173
#[derive(Debug)]
175-
pub(crate) enum FnCallNode {
174+
pub enum FnCallNode {
176175
CallExpr(ast::CallExpr),
177176
MethodCallExpr(ast::MethodCallExpr),
178177
}
@@ -196,7 +195,7 @@ impl FnCallNode {
196195
})
197196
}
198197

199-
pub(crate) fn with_node_exact(node: &SyntaxNode) -> Option<FnCallNode> {
198+
pub fn with_node_exact(node: &SyntaxNode) -> Option<FnCallNode> {
200199
match_ast! {
201200
match node {
202201
ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)),
@@ -206,7 +205,7 @@ impl FnCallNode {
206205
}
207206
}
208207

209-
pub(crate) fn name_ref(&self) -> Option<ast::NameRef> {
208+
pub fn name_ref(&self) -> Option<ast::NameRef> {
210209
match self {
211210
FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()? {
212211
ast::Expr::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
@@ -229,14 +228,28 @@ impl FnCallNode {
229228

230229
#[cfg(test)]
231230
mod tests {
231+
use base_db::{fixture::ChangeFixture, FilePosition};
232232
use expect_test::{expect, Expect};
233-
use test_utils::mark;
234-
235-
use crate::fixture;
233+
use ide_db::RootDatabase;
234+
use test_utils::{mark, RangeOrOffset};
235+
236+
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
237+
pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
238+
let change_fixture = ChangeFixture::parse(ra_fixture);
239+
let mut database = RootDatabase::default();
240+
database.apply_change(change_fixture.change);
241+
let (file_id, range_or_offset) =
242+
change_fixture.file_position.expect("expected a marker (<|>)");
243+
let offset = match range_or_offset {
244+
RangeOrOffset::Range(_) => panic!(),
245+
RangeOrOffset::Offset(it) => it,
246+
};
247+
(database, FilePosition { file_id, offset })
248+
}
236249

237250
fn check(ra_fixture: &str, expect: Expect) {
238-
let (analysis, position) = fixture::position(ra_fixture);
239-
let call_info = analysis.call_info(position).unwrap();
251+
let (db, position) = position(ra_fixture);
252+
let call_info = crate::call_info(&db, position);
240253
let actual = match call_info {
241254
Some(call_info) => {
242255
let docs = match &call_info.doc {

crates/completion/Cargo.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[package]
2+
name = "completion"
3+
version = "0.0.0"
4+
description = "TBD"
5+
license = "MIT OR Apache-2.0"
6+
authors = ["rust-analyzer developers"]
7+
edition = "2018"
8+
9+
[lib]
10+
doctest = false
11+
12+
[dependencies]
13+
itertools = "0.9.0"
14+
log = "0.4.8"
15+
rustc-hash = "1.1.0"
16+
17+
syntax = { path = "../syntax", version = "0.0.0" }
18+
text_edit = { path = "../text_edit", version = "0.0.0" }
19+
base_db = { path = "../base_db", version = "0.0.0" }
20+
ide_db = { path = "../ide_db", version = "0.0.0" }
21+
profile = { path = "../profile", version = "0.0.0" }
22+
test_utils = { path = "../test_utils", version = "0.0.0" }
23+
assists = { path = "../assists", version = "0.0.0" }
24+
call_info = { path = "../call_info", version = "0.0.0" }
25+
26+
# completions crate should depend only on the top-level `hir` package. if you need
27+
# something from some `hir_xxx` subpackage, reexport the API via `hir`.
28+
hir = { path = "../hir", version = "0.0.0" }
29+
30+
[dev-dependencies]
31+
expect-test = "1.0"
32+
stdx = { path = "../stdx", version = "0.0.0" }

crates/ide/src/completion/complete_attribute.rs renamed to crates/completion/src/complete_attribute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use rustc_hash::FxHashSet;
77
use syntax::{ast, AstNode, SyntaxKind};
88

9-
use crate::completion::{
9+
use crate::{
1010
completion_context::CompletionContext,
1111
completion_item::{CompletionItem, CompletionItemKind, CompletionKind, Completions},
1212
generated_features::FEATURES,
@@ -389,7 +389,7 @@ const DEFAULT_LINT_COMPLETIONS: &[LintCompletion] = &[
389389
mod tests {
390390
use expect_test::{expect, Expect};
391391

392-
use crate::completion::{test_utils::completion_list, CompletionKind};
392+
use crate::{test_utils::completion_list, CompletionKind};
393393

394394
fn check(ra_fixture: &str, expect: Expect) {
395395
let actual = completion_list(ra_fixture, CompletionKind::Attribute);

crates/ide/src/completion/complete_dot.rs renamed to crates/completion/src/complete_dot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hir::{HasVisibility, Type};
44
use rustc_hash::FxHashSet;
55
use test_utils::mark;
66

7-
use crate::completion::{completion_context::CompletionContext, completion_item::Completions};
7+
use crate::{completion_context::CompletionContext, completion_item::Completions};
88

99
/// Complete dot accesses, i.e. fields or methods.
1010
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
@@ -64,7 +64,7 @@ mod tests {
6464
use expect_test::{expect, Expect};
6565
use test_utils::mark;
6666

67-
use crate::completion::{test_utils::completion_list, CompletionKind};
67+
use crate::{test_utils::completion_list, CompletionKind};
6868

6969
fn check(ra_fixture: &str, expect: Expect) {
7070
let actual = completion_list(ra_fixture, CompletionKind::Reference);

crates/ide/src/completion/complete_fn_param.rs renamed to crates/completion/src/complete_fn_param.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syntax::{
66
match_ast, AstNode,
77
};
88

9-
use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
9+
use crate::{CompletionContext, CompletionItem, CompletionKind, Completions};
1010

1111
/// Complete repeated parameters, both name and type. For example, if all
1212
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
@@ -68,7 +68,7 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
6868
mod tests {
6969
use expect_test::{expect, Expect};
7070

71-
use crate::completion::{test_utils::completion_list, CompletionKind};
71+
use crate::{test_utils::completion_list, CompletionKind};
7272

7373
fn check(ra_fixture: &str, expect: Expect) {
7474
let actual = completion_list(ra_fixture, CompletionKind::Magic);

crates/ide/src/completion/complete_keyword.rs renamed to crates/completion/src/complete_keyword.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
//! FIXME: write short doc here
1+
//! Completes keywords.
22
33
use syntax::{ast, SyntaxKind};
44
use test_utils::mark;
55

6-
use crate::completion::{
7-
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
8-
};
6+
use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
97

108
pub(super) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) {
119
// complete keyword "crate" in use stmt
@@ -177,7 +175,7 @@ fn complete_return(
177175
mod tests {
178176
use expect_test::{expect, Expect};
179177

180-
use crate::completion::{
178+
use crate::{
181179
test_utils::{check_edit, completion_list},
182180
CompletionKind,
183181
};

crates/ide/src/completion/complete_macro_in_item_position.rs renamed to crates/completion/src/complete_macro_in_item_position.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! FIXME: write short doc here
1+
//! Completes macro invocations used in item position.
22
3-
use crate::completion::{CompletionContext, Completions};
3+
use crate::{CompletionContext, Completions};
44

55
pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
66
// Show only macros in top level.
@@ -17,7 +17,7 @@ pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl
1717
mod tests {
1818
use expect_test::{expect, Expect};
1919

20-
use crate::completion::{test_utils::completion_list, CompletionKind};
20+
use crate::{test_utils::completion_list, CompletionKind};
2121

2222
fn check(ra_fixture: &str, expect: Expect) {
2323
let actual = completion_list(ra_fixture, CompletionKind::Reference);

crates/ide/src/completion/complete_mod.rs renamed to crates/completion/src/complete_mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn module_chain_to_containing_module_file(
150150

151151
#[cfg(test)]
152152
mod tests {
153-
use crate::completion::{test_utils::completion_list, CompletionKind};
153+
use crate::{test_utils::completion_list, CompletionKind};
154154
use expect_test::{expect, Expect};
155155

156156
fn check(ra_fixture: &str, expect: Expect) {

0 commit comments

Comments
 (0)