Skip to content

Commit 661961c

Browse files
Merge #9684
9684: Complete editable private items r=jonas-schievink a=jonas-schievink This checks if a private item's location is editable (local source root), and completes them anyways if that's the case. In order to test this, the `new_source_root` fixture command has been changed to take a `local` or `library` value, and to apply to all *following* files instead of the preceding ones (which would be hard to understand). bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 75d7da1 + 3efdf68 commit 661961c

File tree

6 files changed

+108
-39
lines changed

6 files changed

+108
-39
lines changed

crates/base_db/src/fixture.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl ChangeFixture {
9292
let mut default_cfg = CfgOptions::default();
9393

9494
let mut file_set = FileSet::default();
95+
let mut current_source_root_kind = SourceRootKind::Local;
9596
let source_root_prefix = "/".to_string();
9697
let mut file_id = FileId(0);
9798
let mut roots = Vec::new();
@@ -118,8 +119,13 @@ impl ChangeFixture {
118119
assert!(meta.krate.is_some(), "can't specify deps without naming the crate")
119120
}
120121

121-
if meta.introduce_new_source_root {
122-
roots.push(SourceRoot::new_local(mem::take(&mut file_set)));
122+
if let Some(kind) = &meta.introduce_new_source_root {
123+
let root = match current_source_root_kind {
124+
SourceRootKind::Local => SourceRoot::new_local(mem::take(&mut file_set)),
125+
SourceRootKind::Library => SourceRoot::new_library(mem::take(&mut file_set)),
126+
};
127+
roots.push(root);
128+
current_source_root_kind = *kind;
123129
}
124130

125131
if let Some(krate) = meta.krate {
@@ -197,14 +203,24 @@ impl ChangeFixture {
197203
crate_graph.add_dep(krate, CrateName::new("core").unwrap(), core_crate).unwrap();
198204
}
199205
}
200-
roots.push(SourceRoot::new_local(mem::take(&mut file_set)));
206+
let root = match current_source_root_kind {
207+
SourceRootKind::Local => SourceRoot::new_local(mem::take(&mut file_set)),
208+
SourceRootKind::Library => SourceRoot::new_library(mem::take(&mut file_set)),
209+
};
210+
roots.push(root);
201211
change.set_roots(roots);
202212
change.set_crate_graph(crate_graph);
203213

204214
ChangeFixture { file_position, files, change }
205215
}
206216
}
207217

218+
#[derive(Debug, Clone, Copy)]
219+
enum SourceRootKind {
220+
Local,
221+
Library,
222+
}
223+
208224
#[derive(Debug)]
209225
struct FileMeta {
210226
path: String,
@@ -213,7 +229,7 @@ struct FileMeta {
213229
cfg: CfgOptions,
214230
edition: Edition,
215231
env: Env,
216-
introduce_new_source_root: bool,
232+
introduce_new_source_root: Option<SourceRootKind>,
217233
}
218234

219235
impl From<Fixture> for FileMeta {
@@ -229,7 +245,11 @@ impl From<Fixture> for FileMeta {
229245
cfg,
230246
edition: f.edition.as_ref().map_or(Edition::CURRENT, |v| Edition::from_str(v).unwrap()),
231247
env: f.env.into_iter().collect(),
232-
introduce_new_source_root: f.introduce_new_source_root,
248+
introduce_new_source_root: f.introduce_new_source_root.map(|kind| match &*kind {
249+
"local" => SourceRootKind::Local,
250+
"library" => SourceRootKind::Library,
251+
invalid => panic!("invalid source root kind '{}'", invalid),
252+
}),
233253
}
234254
}
235255
}

crates/ide/src/references.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,13 +1375,13 @@ fn foo(_: bool) -> bo$0ol { true }
13751375
fn test_transitive() {
13761376
check(
13771377
r#"
1378-
//- /level3.rs new_source_root: crate:level3
1378+
//- /level3.rs new_source_root:local crate:level3
13791379
pub struct Fo$0o;
1380-
//- /level2.rs new_source_root: crate:level2 deps:level3
1380+
//- /level2.rs new_source_root:local crate:level2 deps:level3
13811381
pub use level3::Foo;
1382-
//- /level1.rs new_source_root: crate:level1 deps:level2
1382+
//- /level1.rs new_source_root:local crate:level1 deps:level2
13831383
pub use level2::Foo;
1384-
//- /level0.rs new_source_root: crate:level0 deps:level1
1384+
//- /level0.rs new_source_root:local crate:level0 deps:level1
13851385
pub use level1::Foo;
13861386
"#,
13871387
expect![[r#"
@@ -1411,7 +1411,7 @@ macro_rules! foo$0 {
14111411
}
14121412
//- /bar.rs
14131413
foo!();
1414-
//- /other.rs crate:other deps:lib new_source_root:
1414+
//- /other.rs crate:other deps:lib new_source_root:local
14151415
lib::foo!();
14161416
"#,
14171417
expect![[r#"

crates/ide_completion/src/completions/dot.rs

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,36 +174,81 @@ fn foo(a: A) { a.$0() }
174174
fn test_visibility_filtering() {
175175
check(
176176
r#"
177-
mod inner {
177+
//- /lib.rs crate:lib new_source_root:local
178+
pub mod m {
178179
pub struct A {
179180
private_field: u32,
180181
pub pub_field: u32,
181182
pub(crate) crate_field: u32,
182-
pub(crate) super_field: u32,
183+
pub(super) super_field: u32,
183184
}
184185
}
185-
fn foo(a: inner::A) { a.$0 }
186+
//- /main.rs crate:main deps:lib new_source_root:local
187+
fn foo(a: lib::m::A) { a.$0 }
186188
"#,
187189
expect![[r#"
188-
fd pub_field u32
189-
fd crate_field u32
190-
fd super_field u32
190+
fd private_field u32
191+
fd pub_field u32
192+
fd crate_field u32
193+
fd super_field u32
191194
"#]],
192195
);
193196

194197
check(
195198
r#"
196-
struct A {}
199+
//- /lib.rs crate:lib new_source_root:library
200+
pub mod m {
201+
pub struct A {
202+
private_field: u32,
203+
pub pub_field: u32,
204+
pub(crate) crate_field: u32,
205+
pub(super) super_field: u32,
206+
}
207+
}
208+
//- /main.rs crate:main deps:lib new_source_root:local
209+
fn foo(a: lib::m::A) { a.$0 }
210+
"#,
211+
expect![[r#"
212+
fd pub_field u32
213+
"#]],
214+
);
215+
216+
check(
217+
r#"
218+
//- /lib.rs crate:lib new_source_root:local
219+
pub struct A {}
197220
mod m {
198221
impl super::A {
199222
fn private_method(&self) {}
200-
pub(crate) fn the_method(&self) {}
223+
pub(crate) fn crate_method(&self) {}
224+
pub fn pub_method(&self) {}
201225
}
202226
}
203-
fn foo(a: A) { a.$0 }
227+
//- /main.rs crate:main deps:lib new_source_root:local
228+
fn foo(a: lib::A) { a.$0 }
204229
"#,
205230
expect![[r#"
206-
me the_method() fn(&self)
231+
me private_method() fn(&self)
232+
me crate_method() fn(&self)
233+
me pub_method() fn(&self)
234+
"#]],
235+
);
236+
check(
237+
r#"
238+
//- /lib.rs crate:lib new_source_root:library
239+
pub struct A {}
240+
mod m {
241+
impl super::A {
242+
fn private_method(&self) {}
243+
pub(crate) fn crate_method(&self) {}
244+
pub fn pub_method(&self) {}
245+
}
246+
}
247+
//- /main.rs crate:main deps:lib new_source_root:local
248+
fn foo(a: lib::A) { a.$0 }
249+
"#,
250+
expect![[r#"
251+
me pub_method() fn(&self)
207252
"#]],
208253
);
209254
}

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,25 +259,25 @@ mod tests {
259259
fn associated_item_visibility() {
260260
check(
261261
r#"
262-
struct S;
263-
264-
mod m {
265-
impl super::S {
266-
pub(crate) fn public_method() { }
267-
fn private_method() { }
268-
pub(crate) type PublicType = u32;
269-
type PrivateType = u32;
270-
pub(crate) const PUBLIC_CONST: u32 = 1;
271-
const PRIVATE_CONST: u32 = 1;
272-
}
262+
//- /lib.rs crate:lib new_source_root:library
263+
pub struct S;
264+
265+
impl S {
266+
pub fn public_method() { }
267+
fn private_method() { }
268+
pub type PublicType = u32;
269+
type PrivateType = u32;
270+
pub const PUBLIC_CONST: u32 = 1;
271+
const PRIVATE_CONST: u32 = 1;
273272
}
274273
275-
fn foo() { let _ = S::$0 }
274+
//- /main.rs crate:main deps:lib new_source_root:local
275+
fn foo() { let _ = lib::S::$0 }
276276
"#,
277277
expect![[r#"
278278
fn public_method() fn()
279-
ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1;
280-
ta PublicType pub(crate) type PublicType = u32;
279+
ct PUBLIC_CONST pub const PUBLIC_CONST: u32 = 1;
280+
ta PublicType pub type PublicType = u32;
281281
"#]],
282282
);
283283
}

crates/ide_completion/src/context.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! See `CompletionContext` structure.
22
3+
use base_db::SourceDatabaseExt;
34
use hir::{Local, ScopeDef, Semantics, SemanticsScope, Type};
45
use ide_db::{
56
base_db::{FilePosition, SourceDatabase},
@@ -380,8 +381,11 @@ impl<'a> CompletionContext<'a> {
380381
None => return false,
381382
};
382383
if !vis.is_visible_from(self.db, module.into()) {
383-
// FIXME: if the definition location is editable, also show private items
384-
return false;
384+
// If the definition location is editable, also show private items
385+
let root_file = defining_crate.root_file(self.db);
386+
let source_root_id = self.db.file_source_root(root_file);
387+
let is_editable = !self.db.source_root(source_root_id).is_library;
388+
return is_editable;
385389
}
386390

387391
if module.krate() != defining_crate && attrs.has_doc_hidden() {

crates/test_utils/src/fixture.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct Fixture {
7474
pub cfg_key_values: Vec<(String, String)>,
7575
pub edition: Option<String>,
7676
pub env: FxHashMap<String, String>,
77-
pub introduce_new_source_root: bool,
77+
pub introduce_new_source_root: Option<String>,
7878
}
7979

8080
pub struct MiniCore {
@@ -162,7 +162,7 @@ impl Fixture {
162162
let mut cfg_atoms = Vec::new();
163163
let mut cfg_key_values = Vec::new();
164164
let mut env = FxHashMap::default();
165-
let mut introduce_new_source_root = false;
165+
let mut introduce_new_source_root = None;
166166
for component in components[1..].iter() {
167167
let (key, value) = component
168168
.split_once(':')
@@ -186,7 +186,7 @@ impl Fixture {
186186
}
187187
}
188188
}
189-
"new_source_root" => introduce_new_source_root = true,
189+
"new_source_root" => introduce_new_source_root = Some(value.to_string()),
190190
_ => panic!("bad component: {:?}", component),
191191
}
192192
}

0 commit comments

Comments
 (0)