8142: temp disable broken ref match completions for struct fields/methods r=matklad a=JoshMcguigan

This PR implements a temporary workaround for #8058 by disabling ref match completions for struct fields and methods. Disabling this doesn't break any existing functionality (that I am aware of) since these completions were broken.

I plan to keep working on a real fix for the underlying issue here, but I think a proper fix could take some time, so I'd prefer to quickly fix the bug to buy some more time to implement a better solution (which would ultimately allow re-enabling ref matches for struct fields and methods). 

Co-authored-by: Josh Mcguigan <joshmcg88@gmail.com>
This commit is contained in:
bors[bot] 2021-03-23 09:24:26 +00:00 committed by GitHub
commit bf3a8eb40a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 7 deletions

View File

@ -26,7 +26,7 @@ use crate::{
render::{
const_::render_const,
enum_variant::render_variant,
function::render_fn,
function::{render_fn, render_method},
macro_::render_macro,
pattern::{render_struct_pat, render_variant_pat},
render_field, render_resolution, render_tuple_field,
@ -123,6 +123,17 @@ impl Completions {
}
}
pub(crate) fn add_method(
&mut self,
ctx: &CompletionContext,
func: hir::Function,
local_name: Option<String>,
) {
if let Some(item) = render_method(RenderContext::new(ctx), None, local_name, func) {
self.add(item)
}
}
pub(crate) fn add_variant_pat(
&mut self,
ctx: &CompletionContext,

View File

@ -51,7 +51,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &T
&& ctx.scope.module().map_or(true, |m| func.is_visible_from(ctx.db, m))
&& seen_methods.insert(func.name(ctx.db))
{
acc.add_function(ctx, func, None);
acc.add_method(ctx, func, None);
}
None::<()>
});

View File

@ -148,8 +148,10 @@ impl<'a> Render<'a> {
..CompletionRelevance::default()
});
if let Some(ref_match) = compute_ref_match(self.ctx.completion, ty) {
item.ref_match(ref_match);
if let Some(_ref_match) = compute_ref_match(self.ctx.completion, ty) {
// FIXME
// For now we don't properly calculate the edits for ref match
// completions on struct fields, so we've disabled them. See #8058.
}
item.build()
@ -1313,4 +1315,42 @@ fn main() {
"#]],
)
}
#[test]
fn struct_field_method_ref() {
check(
r#"
struct Foo { bar: u32 }
impl Foo { fn baz(&self) -> u32 { 0 } }
fn foo(f: Foo) { let _: &u32 = f.b$0 }
"#,
// FIXME
// Ideally we'd also suggest &f.bar and &f.baz() as exact
// type matches. See #8058.
expect![[r#"
[
CompletionItem {
label: "bar",
source_range: 98..99,
delete: 98..99,
insert: "bar",
kind: SymbolKind(
Field,
),
detail: "u32",
},
CompletionItem {
label: "baz()",
source_range: 98..99,
delete: 98..99,
insert: "baz()$0",
kind: Method,
lookup: "baz",
detail: "fn(&self) -> u32",
},
]
"#]],
);
}
}

View File

@ -20,7 +20,17 @@ pub(crate) fn render_fn<'a>(
fn_: hir::Function,
) -> Option<CompletionItem> {
let _p = profile::span("render_fn");
Some(FunctionRender::new(ctx, local_name, fn_)?.render(import_to_add))
Some(FunctionRender::new(ctx, local_name, fn_, false)?.render(import_to_add))
}
pub(crate) fn render_method<'a>(
ctx: RenderContext<'a>,
import_to_add: Option<ImportEdit>,
local_name: Option<String>,
fn_: hir::Function,
) -> Option<CompletionItem> {
let _p = profile::span("render_method");
Some(FunctionRender::new(ctx, local_name, fn_, true)?.render(import_to_add))
}
#[derive(Debug)]
@ -29,6 +39,7 @@ struct FunctionRender<'a> {
name: String,
func: hir::Function,
ast_node: Fn,
is_method: bool,
}
impl<'a> FunctionRender<'a> {
@ -36,11 +47,12 @@ impl<'a> FunctionRender<'a> {
ctx: RenderContext<'a>,
local_name: Option<String>,
fn_: hir::Function,
is_method: bool,
) -> Option<FunctionRender<'a>> {
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string());
let ast_node = fn_.source(ctx.db())?.value;
Some(FunctionRender { ctx, name, func: fn_, ast_node })
Some(FunctionRender { ctx, name, func: fn_, ast_node, is_method })
}
fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
@ -67,8 +79,13 @@ impl<'a> FunctionRender<'a> {
});
if let Some(ref_match) = compute_ref_match(self.ctx.completion, &ret_type) {
// FIXME
// For now we don't properly calculate the edits for ref match
// completions on methods, so we've disabled them. See #8058.
if !self.is_method {
item.ref_match(ref_match);
}
}
item.build()
}