-
Notifications
You must be signed in to change notification settings - Fork 287
fix Autocomplete parameters of class pattern in match-case situation #2830 #2848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,6 +48,7 @@ use crate::state::lsp::IdentifierContext; | |||||||||||||||||||||
| use crate::state::lsp::IdentifierWithContext; | ||||||||||||||||||||||
| use crate::state::lsp::ImportFormat; | ||||||||||||||||||||||
| use crate::state::lsp::MIN_CHARACTERS_TYPED_AUTOIMPORT; | ||||||||||||||||||||||
| use crate::state::lsp::PatternMatchParameterKind; | ||||||||||||||||||||||
| use crate::state::state::Transaction; | ||||||||||||||||||||||
| use crate::types::callable::Param; | ||||||||||||||||||||||
| use crate::types::types::Type; | ||||||||||||||||||||||
|
|
@@ -836,6 +837,52 @@ impl Transaction<'_> { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// Suggest `attr=` completions inside a class pattern like `case Point(x=...)`. | ||||||||||||||||||||||
| pub(crate) fn add_match_class_keyword_completions( | ||||||||||||||||||||||
| &self, | ||||||||||||||||||||||
| handle: &Handle, | ||||||||||||||||||||||
| covering_nodes: &[AnyNodeRef], | ||||||||||||||||||||||
| completions: &mut Vec<RankedCompletion>, | ||||||||||||||||||||||
| ) { | ||||||||||||||||||||||
| let Some(pattern_class) = covering_nodes.iter().find_map(|node| match node { | ||||||||||||||||||||||
| AnyNodeRef::PatternMatchClass(pattern_class) => Some(pattern_class), | ||||||||||||||||||||||
| _ => None, | ||||||||||||||||||||||
| }) else { | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| let Some(class_ty) = self.get_type_trace(handle, pattern_class.cls.range()) else { | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| let Some(items) = self.ad_hoc_solve(handle, "completion_match_class_keywords", |solver| { | ||||||||||||||||||||||
| let instance_ty = match class_ty { | ||||||||||||||||||||||
| Type::ClassDef(cls) => solver.instantiate(&cls), | ||||||||||||||||||||||
| Type::ClassType(cls) => Type::ClassType(cls), | ||||||||||||||||||||||
| Type::Type(box Type::ClassType(cls)) => Type::ClassType(cls), | ||||||||||||||||||||||
| _ => return Vec::new(), | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| solver | ||||||||||||||||||||||
| .completions(instance_ty, None, true) | ||||||||||||||||||||||
| .into_iter() | ||||||||||||||||||||||
| .map(|attr| { | ||||||||||||||||||||||
| RankedCompletion::new(CompletionItem { | ||||||||||||||||||||||
| label: format!("{}=", attr.name.as_str()), | ||||||||||||||||||||||
| detail: attr.ty.map(|ty| ty.to_string()), | ||||||||||||||||||||||
| kind: Some(CompletionItemKind::VARIABLE), | ||||||||||||||||||||||
| tags: if attr.is_deprecated { | ||||||||||||||||||||||
| Some(vec![CompletionItemTag::DEPRECATED]) | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| None | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| ..Default::default() | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| .collect::<Vec<_>>() | ||||||||||||||||||||||
| }) else { | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| completions.extend(items); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// Core completion implementation returning items and incomplete flag. | ||||||||||||||||||||||
| pub(crate) fn completion_sorted_opt_with_incomplete<F>( | ||||||||||||||||||||||
| &self, | ||||||||||||||||||||||
|
|
@@ -1045,6 +1092,14 @@ impl Transaction<'_> { | |||||||||||||||||||||
| if matches!(context, IdentifierContext::MethodDef { .. }) { | ||||||||||||||||||||||
| Self::add_magic_method_completions(&identifier, &mut result); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if matches!( | ||||||||||||||||||||||
| context, | ||||||||||||||||||||||
| IdentifierContext::PatternMatch(PatternMatchParameterKind::KeywordArgName) | ||||||||||||||||||||||
| ) && let Some(mod_module) = self.get_ast(handle) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| let nodes = Ast::locate_node(&mod_module, position); | ||||||||||||||||||||||
| self.add_match_class_keyword_completions(handle, &nodes, &mut result); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| self.add_kwargs_completions(handle, position, &mut result); | ||||||||||||||||||||||
| Self::add_keyword_completions(handle, &mut result); | ||||||||||||||||||||||
| let has_local_completions = self.add_local_variable_completions( | ||||||||||||||||||||||
|
|
@@ -1104,6 +1159,7 @@ impl Transaction<'_> { | |||||||||||||||||||||
| &mut result, | ||||||||||||||||||||||
| in_string_literal, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| self.add_match_class_keyword_completions(handle, &nodes, &mut result); | ||||||||||||||||||||||
|
||||||||||||||||||||||
| self.add_match_class_keyword_completions(handle, &nodes, &mut result); | |
| // Only offer match class keyword completions when the innermost | |
| // node at the cursor is a PatternMatchClass. This avoids | |
| // suggesting `attr=` in value positions such as `case A(a=|)`, | |
| // where a new keyword cannot legally start. | |
| if let Some(first) = nodes.first() | |
| && matches!(first, AnyNodeRef::PatternMatchClass(_)) | |
| { | |
| self.add_match_class_keyword_completions(handle, &nodes, &mut result); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The completion
detailhere usesType::to_string(), while other completion paths in this file format types withas_lsp_string(LspDisplayMode::Hover)for consistency and readability. Consider using the same formatting helper so match-class keyword completions display types consistently with attribute completions.