From d52ee59a712932bc381d8c690dc2f681598760fe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 21 Jul 2019 13:28:58 +0300 Subject: streamline API --- crates/ra_ide_api/src/completion/completion_context.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 2f78d5409..968f5694b 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -1,6 +1,6 @@ use hir::source_binder; use ra_syntax::{ - algo::{find_covering_element, find_node_at_offset, find_token_at_offset}, + algo::{find_covering_element, find_node_at_offset}, ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TextUnit, @@ -48,7 +48,7 @@ impl<'a> CompletionContext<'a> { ) -> Option> { let module = source_binder::module_from_position(db, position); let token = - find_token_at_offset(original_parse.tree().syntax(), position.offset).left_biased()?; + original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?; let analyzer = hir::SourceAnalyzer::new(db, position.file_id, &token.parent(), Some(position.offset)); let mut ctx = CompletionContext { -- cgit v1.2.3 From 5fe19d2fbd2daa05b2cd3b1ebb6fa926e9d86c36 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Sun, 21 Jul 2019 14:11:45 +0300 Subject: provide completion in struct patterns --- .../src/completion/complete_struct_literal.rs | 15 ++-- .../src/completion/complete_struct_pattern.rs | 94 ++++++++++++++++++++++ .../src/completion/completion_context.rs | 11 ++- 3 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 crates/ra_ide_api/src/completion/complete_struct_pattern.rs (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs index 9410f740f..6aa41f498 100644 --- a/crates/ra_ide_api/src/completion/complete_struct_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs @@ -1,23 +1,22 @@ -use hir::{Substs, Ty}; +use hir::Substs; use crate::completion::{CompletionContext, Completions}; /// Complete fields in fields literals. pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { let (ty, variant) = match ctx.struct_lit_syntax.as_ref().and_then(|it| { - Some((ctx.analyzer.type_of(ctx.db, &it.clone().into())?, ctx.analyzer.resolve_variant(it)?)) + Some(( + ctx.analyzer.type_of(ctx.db, &it.clone().into())?, + ctx.analyzer.resolve_struct_literal(it)?, + )) }) { Some(it) => it, _ => return, }; - - let ty_substs = match ty { - Ty::Apply(it) => it.parameters, - _ => Substs::empty(), - }; + let substs = &ty.substs().unwrap_or_else(Substs::empty); for field in variant.fields(ctx.db) { - acc.add_field(ctx, field, &ty_substs); + acc.add_field(ctx, field, substs); } } diff --git a/crates/ra_ide_api/src/completion/complete_struct_pattern.rs b/crates/ra_ide_api/src/completion/complete_struct_pattern.rs new file mode 100644 index 000000000..d0dde5930 --- /dev/null +++ b/crates/ra_ide_api/src/completion/complete_struct_pattern.rs @@ -0,0 +1,94 @@ +use hir::Substs; + +use crate::completion::{CompletionContext, Completions}; + +pub(super) fn complete_struct_pattern(acc: &mut Completions, ctx: &CompletionContext) { + let (ty, variant) = match ctx.struct_lit_pat.as_ref().and_then(|it| { + Some(( + ctx.analyzer.type_of_pat(ctx.db, &it.clone().into())?, + ctx.analyzer.resolve_struct_pattern(it)?, + )) + }) { + Some(it) => it, + _ => return, + }; + let substs = &ty.substs().unwrap_or_else(Substs::empty); + + for field in variant.fields(ctx.db) { + acc.add_field(ctx, field, substs); + } +} + +#[cfg(test)] +mod tests { + use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use insta::assert_debug_snapshot_matches; + + fn complete(code: &str) -> Vec { + do_completion(code, CompletionKind::Reference) + } + + #[test] + fn test_struct_pattern_field() { + let completions = complete( + r" + struct S { foo: u32 } + + fn process(f: S) { + match f { + S { f<|>: 92 } => (), + } + } + ", + ); + assert_debug_snapshot_matches!(completions, @r###" + ⋮[ + ⋮ CompletionItem { + ⋮ label: "foo", + ⋮ source_range: [117; 118), + ⋮ delete: [117; 118), + ⋮ insert: "foo", + ⋮ kind: Field, + ⋮ detail: "u32", + ⋮ }, + ⋮] + "###); + } + + #[test] + fn test_struct_pattern_enum_variant() { + let completions = complete( + r" + enum E { + S { foo: u32, bar: () } + } + + fn process(e: E) { + match e { + E::S { <|> } => (), + } + } + ", + ); + assert_debug_snapshot_matches!(completions, @r###" + ⋮[ + ⋮ CompletionItem { + ⋮ label: "bar", + ⋮ source_range: [161; 161), + ⋮ delete: [161; 161), + ⋮ insert: "bar", + ⋮ kind: Field, + ⋮ detail: "()", + ⋮ }, + ⋮ CompletionItem { + ⋮ label: "foo", + ⋮ source_range: [161; 161), + ⋮ delete: [161; 161), + ⋮ insert: "foo", + ⋮ kind: Field, + ⋮ detail: "u32", + ⋮ }, + ⋮] + "###); + } +} diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 2f78d5409..6fee7b5be 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -21,6 +21,7 @@ pub(crate) struct CompletionContext<'a> { pub(super) function_syntax: Option, pub(super) use_item_syntax: Option, pub(super) struct_lit_syntax: Option, + pub(super) struct_lit_pat: Option, pub(super) is_param: bool, /// If a name-binding or reference to a const in a pattern. /// Irrefutable patterns (like let) are excluded. @@ -60,6 +61,7 @@ impl<'a> CompletionContext<'a> { function_syntax: None, use_item_syntax: None, struct_lit_syntax: None, + struct_lit_pat: None, is_param: false, is_pat_binding: false, is_trivial_path: false, @@ -106,8 +108,7 @@ impl<'a> CompletionContext<'a> { // Otherwise, see if this is a declaration. We can use heuristics to // suggest declaration names, see `CompletionKind::Magic`. if let Some(name) = find_node_at_offset::(file.syntax(), offset) { - if is_node::(name.syntax()) { - let bind_pat = name.syntax().ancestors().find_map(ast::BindPat::cast).unwrap(); + if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) { let parent = bind_pat.syntax().parent(); if parent.clone().and_then(ast::MatchArm::cast).is_some() || parent.and_then(ast::Condition::cast).is_some() @@ -119,6 +120,10 @@ impl<'a> CompletionContext<'a> { self.is_param = true; return; } + if name.syntax().ancestors().find_map(ast::FieldPatList::cast).is_some() { + self.struct_lit_pat = + find_node_at_offset(original_parse.tree().syntax(), self.offset); + } } } @@ -235,7 +240,7 @@ fn find_node_with_range(syntax: &SyntaxNode, range: TextRange) -> Op } fn is_node(node: &SyntaxNode) -> bool { - match node.ancestors().filter_map(N::cast).next() { + match node.ancestors().find_map(N::cast) { None => false, Some(n) => n.syntax().text_range() == node.text_range(), } -- cgit v1.2.3