Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions pyrefly/lib/state/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,47 @@ impl<'a> Transaction<'a> {
}
}

/// Suggest Literal values when completing inside a `match` value pattern.
///
/// We can't reuse the call-argument literal completion path here because
/// `case <value>:` isn't a call site, so we never get parameter types to
/// infer literals from. Instead, we look for a match value/singleton
/// pattern at the cursor and pull the `match` subject's type to surface
/// its Literal members.
fn add_match_literal_completions(
&self,
handle: &Handle,
covering_nodes: &[AnyNodeRef],
completions: &mut Vec<CompletionItem>,
in_string_literal: bool,
) {
let mut is_match_value_pattern = false;
let mut subject = None;
for node in covering_nodes {
match node {
AnyNodeRef::PatternMatchValue(_) | AnyNodeRef::PatternMatchSingleton(_) => {
is_match_value_pattern = true;
}
AnyNodeRef::StmtMatch(stmt_match) => {
subject = Some(stmt_match.subject.as_ref());
}
_ => {}
}
if is_match_value_pattern && subject.is_some() {
break;
}
}
if !is_match_value_pattern {
return;
}
let Some(subject) = subject else {
return;
};
if let Some(subject_type) = self.get_type_trace(handle, subject.range()) {
Self::add_literal_completions_from_type(&subject_type, completions, in_string_literal);
}
}

// Kept for backwards compatibility - used by external callers (lsp/server.rs, playground.rs)
// who don't need the is_incomplete flag
pub fn completion(
Expand Down Expand Up @@ -3061,6 +3102,12 @@ impl<'a> Transaction<'a> {
let in_string_literal = nodes
.iter()
.any(|node| matches!(node, AnyNodeRef::ExprStringLiteral(_)));
self.add_match_literal_completions(
handle,
&nodes,
&mut result,
in_string_literal,
);
self.add_literal_completions(handle, position, &mut result, in_string_literal);
self.add_dict_key_completions(
handle,
Expand Down
25 changes: 25 additions & 0 deletions pyrefly/lib/test/lsp/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,31 @@ Completion Results:
);
}

#[test]
fn completion_literal_match_value() {
let code = r#"
from typing import Literal
x: Literal['a', 'b'] = 'a'
match x:
case ':
# ^
"#;
let report =
get_batched_lsp_operations_report_allow_error(&[("main", code)], get_default_test_report());
assert_eq!(
r#"
# main.py
5 | case ':
^
Completion Results:
- (Value) 'a': Literal['a'] inserting `a`
- (Value) 'b': Literal['b'] inserting `b`
"#
.trim(),
report.trim(),
);
}

#[test]
fn completion_literal_union_alias() {
let code = r#"
Expand Down
Loading