forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#15374 - jmintb:extern_crate, r=Veykril
feat: Implement extern crate completion Hi, this is a draft PR for rust-lang#13002. I have basic completion working as well as a filter for existing extern crate imports in the same file. This is based on the tests, I have not actually tried this in an editor. Before going further I think this is a good point to stop and get feedback on the structure and approach I have taken so far. Let me know what you think :) I will make sure to add more tests, rebase commits and align with the code style guidelines before submitting a final version. A few specific questions : 1. Is there a better way to check for matching suggestions? right now I just test if an extern crate name starts with the current user input. 2. Am I creating the `CompletionItem` correctly? I noticed that `use_.rs` invokes a builder where as I do not. 3. When checking for existing extern crate imports the current implementation only looks at the current source file, is that sufficient?
- Loading branch information
Showing
6 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//! Completion for extern crates | ||
|
||
use hir::{HasAttrs, Name}; | ||
use ide_db::SymbolKind; | ||
|
||
use crate::{context::CompletionContext, CompletionItem, CompletionItemKind}; | ||
|
||
use super::Completions; | ||
|
||
pub(crate) fn complete_extern_crate(acc: &mut Completions, ctx: &CompletionContext<'_>) { | ||
let imported_extern_crates: Vec<Name> = ctx.scope.extern_crate_decls().collect(); | ||
|
||
for (name, module) in ctx.scope.extern_crates() { | ||
if imported_extern_crates.contains(&name) { | ||
continue; | ||
} | ||
|
||
let mut item = CompletionItem::new( | ||
CompletionItemKind::SymbolKind(SymbolKind::Module), | ||
ctx.source_range(), | ||
name.to_smol_str(), | ||
); | ||
item.set_documentation(module.docs(ctx.db)); | ||
|
||
item.add_to(acc, ctx.db); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::tests::completion_list_no_kw; | ||
|
||
#[test] | ||
fn can_complete_extern_crate() { | ||
let case = r#" | ||
//- /lib.rs crate:other_crate_a | ||
// nothing here | ||
//- /other_crate_b.rs crate:other_crate_b | ||
pub mod good_mod{} | ||
//- /lib.rs crate:crate_c | ||
// nothing here | ||
//- /lib.rs crate:lib deps:other_crate_a,other_crate_b,crate_c extern-prelude:other_crate_a | ||
extern crate oth$0 | ||
mod other_mod {} | ||
"#; | ||
|
||
let completion_list = completion_list_no_kw(case); | ||
|
||
assert_eq!("md other_crate_a\n".to_string(), completion_list); | ||
} | ||
|
||
#[test] | ||
fn will_not_complete_existing_import() { | ||
let case = r#" | ||
//- /lib.rs crate:other_crate_a | ||
// nothing here | ||
//- /lib.rs crate:crate_c | ||
// nothing here | ||
//- /lib.rs crate:other_crate_b | ||
// | ||
//- /lib.rs crate:lib deps:other_crate_a,other_crate_b,crate_c extern-prelude:other_crate_a,other_crate_b | ||
extern crate other_crate_b; | ||
extern crate oth$0 | ||
mod other_mod {} | ||
"#; | ||
|
||
let completion_list = completion_list_no_kw(case); | ||
|
||
assert_eq!("md other_crate_a\n".to_string(), completion_list); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters