Rename compound things to variant things

This commit is contained in:
Lukas Wirth 2022-03-16 13:41:35 +01:00
parent 46008d4cf4
commit 4fe5f03c7f
10 changed files with 101 additions and 36 deletions

View file

@ -69,24 +69,17 @@ pub(crate) fn inlay_hints(
let mut hints = Vec::new(); let mut hints = Vec::new();
if let Some(range_limit) = range_limit { let get_hints = |node| get_hints(&mut hints, &sema, config, node);
let range_limit = range_limit.range; match range_limit {
match file.covering_element(range_limit) { Some(FileRange { range, .. }) => match file.covering_element(range) {
NodeOrToken::Token(_) => return hints, NodeOrToken::Token(_) => return hints,
NodeOrToken::Node(n) => { NodeOrToken::Node(n) => n
for node in n .descendants()
.descendants() .filter(|descendant| range.contains_range(descendant.text_range()))
.filter(|descendant| range_limit.contains_range(descendant.text_range())) .for_each(get_hints),
{ },
get_hints(&mut hints, &sema, config, node); None => file.descendants().for_each(get_hints),
} };
}
}
} else {
for node in file.descendants() {
get_hints(&mut hints, &sema, config, node);
}
}
hints hints
} }

View file

@ -58,7 +58,7 @@ pub(super) enum PathKind {
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct PathCompletionCtx { pub(crate) struct PathCompletionCtx {
/// If this is a call with () already there /// If this is a call with () already there (or {} in case of record patterns)
pub(super) has_call_parens: bool, pub(super) has_call_parens: bool,
/// Whether this path stars with a `::`. /// Whether this path stars with a `::`.
pub(super) is_absolute_path: bool, pub(super) is_absolute_path: bool,
@ -890,6 +890,7 @@ impl<'a> CompletionContext<'a> {
Some(PathKind::Pat) Some(PathKind::Pat)
}, },
ast::RecordPat(it) => { ast::RecordPat(it) => {
path_ctx.has_call_parens = true;
pat_ctx = Some(pattern_context_for(original_file, it.into())); pat_ctx = Some(pattern_context_for(original_file, it.into()));
Some(PathKind::Pat) Some(PathKind::Pat)
}, },

View file

@ -8,7 +8,7 @@ pub(crate) mod const_;
pub(crate) mod pattern; pub(crate) mod pattern;
pub(crate) mod type_alias; pub(crate) mod type_alias;
pub(crate) mod struct_literal; pub(crate) mod struct_literal;
pub(crate) mod compound; pub(crate) mod variant;
pub(crate) mod union_literal; pub(crate) mod union_literal;
use hir::{AsAssocItem, HasAttrs, HirDisplay, ScopeDef}; use hir::{AsAssocItem, HasAttrs, HirDisplay, ScopeDef};

View file

@ -7,8 +7,9 @@ use syntax::SmolStr;
use crate::{ use crate::{
item::{CompletionItem, ImportEdit}, item::{CompletionItem, ImportEdit},
render::{ render::{
compound::{format_literal_label, render_record, render_tuple, RenderedCompound}, compute_ref_match, compute_type_match,
compute_ref_match, compute_type_match, RenderContext, variant::{format_literal_label, render_record, render_tuple, RenderedLiteral},
RenderContext,
}, },
CompletionRelevance, CompletionRelevance,
}; };
@ -56,7 +57,7 @@ fn render(
render_record(db, ctx.snippet_cap(), &variant.fields(db), Some(&qualified_name)) render_record(db, ctx.snippet_cap(), &variant.fields(db), Some(&qualified_name))
} }
StructKind::Unit => { StructKind::Unit => {
RenderedCompound { literal: qualified_name.clone(), detail: qualified_name.clone() } RenderedLiteral { literal: qualified_name.clone(), detail: qualified_name.clone() }
} }
}; };

View file

@ -192,7 +192,7 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
Some(PathCompletionCtx { kind: Some(PathKind::Expr), has_call_parens: true, .. }) => { Some(PathCompletionCtx { kind: Some(PathKind::Expr), has_call_parens: true, .. }) => {
return false return false
} }
Some(PathCompletionCtx { kind: Some(PathKind::Use), .. }) => { Some(PathCompletionCtx { kind: Some(PathKind::Use | PathKind::Type), .. }) => {
cov_mark::hit!(no_parens_in_use_item); cov_mark::hit!(no_parens_in_use_item);
return false; return false;
} }

View file

@ -1,11 +1,12 @@
//! Renderer for `struct` literal. //! Renderer for `struct` literal.
use hir::{HasAttrs, Name, StructKind}; use hir::{HasAttrs, Name, StructKind};
use ide_db::SymbolKind;
use syntax::SmolStr; use syntax::SmolStr;
use crate::{ use crate::{
render::compound::{ render::variant::{
format_literal_label, render_record, render_tuple, visible_fields, RenderedCompound, format_literal_label, render_record, render_tuple, visible_fields, RenderedLiteral,
}, },
render::RenderContext, render::RenderContext,
CompletionItem, CompletionItemKind, CompletionItem, CompletionItemKind,
@ -37,12 +38,12 @@ pub(crate) fn render_struct_literal(
fn build_completion( fn build_completion(
ctx: &RenderContext<'_>, ctx: &RenderContext<'_>,
name: SmolStr, name: SmolStr,
rendered: RenderedCompound, rendered: RenderedLiteral,
kind: StructKind, kind: StructKind,
def: impl HasAttrs + Copy, def: impl HasAttrs + Copy,
) -> CompletionItem { ) -> CompletionItem {
let mut item = CompletionItem::new( let mut item = CompletionItem::new(
CompletionItemKind::Snippet, CompletionItemKind::SymbolKind(SymbolKind::Struct),
ctx.source_range(), ctx.source_range(),
format_literal_label(&name, kind), format_literal_label(&name, kind),
); );
@ -64,7 +65,7 @@ fn render_literal(
name: &str, name: &str,
kind: StructKind, kind: StructKind,
fields: &[hir::Field], fields: &[hir::Field],
) -> Option<RenderedCompound> { ) -> Option<RenderedLiteral> {
let path_string; let path_string;
let qualified_name = if let Some(path) = path { let qualified_name = if let Some(path) = path {

View file

@ -1,11 +1,12 @@
//! Renderer for `union` literals. //! Renderer for `union` literals.
use hir::{HirDisplay, Name, StructKind}; use hir::{HirDisplay, Name, StructKind};
use ide_db::SymbolKind;
use itertools::Itertools; use itertools::Itertools;
use crate::{ use crate::{
render::{ render::{
compound::{format_literal_label, visible_fields}, variant::{format_literal_label, visible_fields},
RenderContext, RenderContext,
}, },
CompletionItem, CompletionItemKind, CompletionItem, CompletionItemKind,
@ -25,7 +26,7 @@ pub(crate) fn render_union_literal(
}; };
let mut item = CompletionItem::new( let mut item = CompletionItem::new(
CompletionItemKind::Snippet, CompletionItemKind::SymbolKind(SymbolKind::Union),
ctx.source_range(), ctx.source_range(),
format_literal_label(&name, StructKind::Record), format_literal_label(&name, StructKind::Record),
); );

View file

@ -9,7 +9,7 @@ use syntax::SmolStr;
/// A rendered struct, union, or enum variant, split into fields for actual /// A rendered struct, union, or enum variant, split into fields for actual
/// auto-completion (`literal`, using `field: ()`) and display in the /// auto-completion (`literal`, using `field: ()`) and display in the
/// completions menu (`detail`, using `field: type`). /// completions menu (`detail`, using `field: type`).
pub(crate) struct RenderedCompound { pub(crate) struct RenderedLiteral {
pub(crate) literal: String, pub(crate) literal: String,
pub(crate) detail: String, pub(crate) detail: String,
} }
@ -21,7 +21,7 @@ pub(crate) fn render_record(
snippet_cap: Option<SnippetCap>, snippet_cap: Option<SnippetCap>,
fields: &[hir::Field], fields: &[hir::Field],
name: Option<&str>, name: Option<&str>,
) -> RenderedCompound { ) -> RenderedLiteral {
let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| { let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| {
if snippet_cap.is_some() { if snippet_cap.is_some() {
f(&format_args!("{}: ${{{}:()}}", field.name(db), idx + 1)) f(&format_args!("{}: ${{{}:()}}", field.name(db), idx + 1))
@ -34,7 +34,7 @@ pub(crate) fn render_record(
f(&format_args!("{}: {}", field.name(db), field.ty(db).display(db))) f(&format_args!("{}: {}", field.name(db), field.ty(db).display(db)))
}); });
RenderedCompound { RenderedLiteral {
literal: format!("{} {{ {} }}", name.unwrap_or(""), completions), literal: format!("{} {{ {} }}", name.unwrap_or(""), completions),
detail: format!("{} {{ {} }}", name.unwrap_or(""), types), detail: format!("{} {{ {} }}", name.unwrap_or(""), types),
} }
@ -47,7 +47,7 @@ pub(crate) fn render_tuple(
snippet_cap: Option<SnippetCap>, snippet_cap: Option<SnippetCap>,
fields: &[hir::Field], fields: &[hir::Field],
name: Option<&str>, name: Option<&str>,
) -> RenderedCompound { ) -> RenderedLiteral {
let completions = fields.iter().enumerate().format_with(", ", |(idx, _), f| { let completions = fields.iter().enumerate().format_with(", ", |(idx, _), f| {
if snippet_cap.is_some() { if snippet_cap.is_some() {
f(&format_args!("${{{}:()}}", idx + 1)) f(&format_args!("${{{}:()}}", idx + 1))
@ -58,7 +58,7 @@ pub(crate) fn render_tuple(
let types = fields.iter().format_with(", ", |field, f| f(&field.ty(db).display(db))); let types = fields.iter().format_with(", ", |field, f| f(&field.ty(db).display(db)));
RenderedCompound { RenderedLiteral {
literal: format!("{}({})", name.unwrap_or(""), completions), literal: format!("{}({})", name.unwrap_or(""), completions),
detail: format!("{}({})", name.unwrap_or(""), types), detail: format!("{}({})", name.unwrap_or(""), types),
} }

View file

@ -378,3 +378,71 @@ fn foo() {
"#]], "#]],
) )
} }
#[test]
fn completes_no_delims_if_existing() {
check_empty(
r#"
struct Bar(u32);
fn foo() {
match Bar(0) {
B$0(b) => {}
}
}
"#,
expect![[r#"
kw self::
kw super::
kw crate::
"#]],
);
check_empty(
r#"
struct Foo { bar: u32 }
fn foo() {
match Foo { bar: 0 } {
F$0 { bar } => {}
}
}
"#,
expect![[r#"
kw return
kw self
kw super
kw crate
st Foo
fn foo() fn()
bt u32
"#]],
);
check_empty(
r#"
enum Enum {
TupleVariant(u32)
}
fn foo() {
match Enum::TupleVariant(0) {
Enum::T$0(b) => {}
}
}
"#,
expect![[r#"
ev TupleVariant() TupleVariant(u32)
"#]],
);
check_empty(
r#"
enum Enum {
RecordVariant { field: u32 }
}
fn foo() {
match (Enum::RecordVariant { field: 0 }) {
Enum::RecordV$0 { field } => {}
}
}
"#,
expect![[r#"
ev RecordVariant {} RecordVariant { field: u32 }
"#]],
);
}

View file

@ -166,7 +166,7 @@ fn main() {
kw true kw true
kw false kw false
kw return kw return
sn Foo {} Foo { foo1: u32, foo2: u32 } st Foo {} Foo { foo1: u32, foo2: u32 }
fd ..Default::default() fd ..Default::default()
fd foo1 u32 fd foo1 u32
fd foo2 u32 fd foo2 u32