mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +00:00
Merge #10846
10846: fix: qualify struct name in struct constructor completion r=Veykril a=andrewleverette * Alter `add_struct_literal` method signature to take an optional module path * Update `render_literal` method to use a qualified Relates to issue #10771 Side note: This is my first contribution. Any feedback is welcome! Co-authored-by: Andrew Leverette <andrewleverette@gmail.com>
This commit is contained in:
commit
3e4ac8a2c9
3 changed files with 56 additions and 5 deletions
|
@ -191,9 +191,10 @@ impl Completions {
|
|||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
strukt: hir::Struct,
|
||||
path: Option<hir::ModPath>,
|
||||
local_name: Option<hir::Name>,
|
||||
) {
|
||||
let item = render_struct_literal(RenderContext::new(ctx), strukt, local_name);
|
||||
let item = render_struct_literal(RenderContext::new(ctx), strukt, path, local_name);
|
||||
self.add_opt(item);
|
||||
}
|
||||
|
||||
|
|
|
@ -59,7 +59,12 @@ pub(crate) fn complete_record_literal(
|
|||
}
|
||||
|
||||
if let hir::Adt::Struct(strukt) = ctx.expected_type.as_ref()?.as_adt()? {
|
||||
acc.add_struct_literal(ctx, strukt, None);
|
||||
let module =
|
||||
if let Some(module) = ctx.scope.module() { module } else { strukt.module(ctx.db) };
|
||||
|
||||
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
|
||||
|
||||
acc.add_struct_literal(ctx, strukt, path, None);
|
||||
}
|
||||
|
||||
Some(())
|
||||
|
@ -94,6 +99,35 @@ fn baz() {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literal_struct_completion_from_sub_modules() {
|
||||
check_edit(
|
||||
"Struct {…}",
|
||||
r#"
|
||||
mod submod {
|
||||
pub struct Struct {
|
||||
pub a: u64,
|
||||
}
|
||||
}
|
||||
|
||||
fn f() -> submod::Struct {
|
||||
Stru$0
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
mod submod {
|
||||
pub struct Struct {
|
||||
pub a: u64,
|
||||
}
|
||||
}
|
||||
|
||||
fn f() -> submod::Struct {
|
||||
submod::Struct { a: ${1:()} }$0
|
||||
}
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literal_struct_complexion_module() {
|
||||
check_edit(
|
||||
|
|
|
@ -10,6 +10,7 @@ use crate::{render::RenderContext, CompletionItem, CompletionItemKind};
|
|||
pub(crate) fn render_struct_literal(
|
||||
ctx: RenderContext<'_>,
|
||||
strukt: hir::Struct,
|
||||
path: Option<hir::ModPath>,
|
||||
local_name: Option<Name>,
|
||||
) -> Option<CompletionItem> {
|
||||
let _p = profile::span("render_struct_literal");
|
||||
|
@ -23,7 +24,8 @@ pub(crate) fn render_struct_literal(
|
|||
}
|
||||
|
||||
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_smol_str();
|
||||
let literal = render_literal(&ctx, &name, strukt.kind(ctx.db()), &visible_fields)?;
|
||||
|
||||
let literal = render_literal(&ctx, path, &name, strukt.kind(ctx.db()), &visible_fields)?;
|
||||
|
||||
Some(build_completion(ctx, name, literal, strukt))
|
||||
}
|
||||
|
@ -49,13 +51,27 @@ fn build_completion(
|
|||
|
||||
fn render_literal(
|
||||
ctx: &RenderContext<'_>,
|
||||
path: Option<hir::ModPath>,
|
||||
name: &str,
|
||||
kind: StructKind,
|
||||
fields: &[hir::Field],
|
||||
) -> Option<String> {
|
||||
let path_string;
|
||||
|
||||
let qualified_name = if let Some(path) = path {
|
||||
path_string = path.to_string();
|
||||
&path_string
|
||||
} else {
|
||||
name
|
||||
};
|
||||
|
||||
let mut literal = match kind {
|
||||
StructKind::Tuple if ctx.snippet_cap().is_some() => render_tuple_as_literal(fields, name),
|
||||
StructKind::Record => render_record_as_literal(ctx.db(), ctx.snippet_cap(), fields, name),
|
||||
StructKind::Tuple if ctx.snippet_cap().is_some() => {
|
||||
render_tuple_as_literal(fields, qualified_name)
|
||||
}
|
||||
StructKind::Record => {
|
||||
render_record_as_literal(ctx.db(), ctx.snippet_cap(), fields, qualified_name)
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue