Skip to content

Commit 59f7596

Browse files
committed
Add is_type_or_error_thereof to make some things simpler
1 parent 1cd3a35 commit 59f7596

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

crates/parsa/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ macro_rules! __create_node {
409409
self.internal_node.type_ == type_.to_internal()
410410
}
411411

412+
pub fn is_type_or_error_thereof(&self, type_: $NodeType) -> bool {
413+
self.internal_node.type_.remove_error_recovery_bit()== type_.to_internal()
414+
}
415+
416+
412417
pub fn type_str(&self) -> String {
413418
// Not a fast API, should probably only be used for tests.
414419
match self.type_() {

crates/parsa_python_cst/src/completion.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ impl Tree {
9494
rest,
9595
);
9696
} else if before_dot.is_type(Nonterminal(name_def))
97-
&& matches!(
98-
before_dot.parent().unwrap().type_(),
99-
Nonterminal(dotted_as_name) | ErrorNonterminal(dotted_as_name)
100-
)
97+
&& before_dot
98+
.parent()
99+
.unwrap()
100+
.is_type_or_error_thereof(Nonterminal(dotted_as_name))
101101
{
102102
return (
103103
scope,
@@ -106,10 +106,11 @@ impl Tree {
106106
},
107107
rest,
108108
);
109-
} else if matches!(
110-
previous.parent().unwrap().type_(),
111-
Nonterminal(import_from) | ErrorNonterminal(import_from)
112-
) {
109+
} else if previous
110+
.parent()
111+
.unwrap()
112+
.is_type_or_error_thereof(Nonterminal(import_from))
113+
{
113114
return (
114115
scope,
115116
CompletionNode::ImportFromFirstPart {
@@ -125,10 +126,11 @@ impl Tree {
125126
}
126127
}
127128
"from" | "..." => {
128-
if matches!(
129-
previous.parent().unwrap().type_(),
130-
Nonterminal(import_from) | ErrorNonterminal(import_from)
131-
) {
129+
if previous
130+
.parent()
131+
.unwrap()
132+
.is_type_or_error_thereof(Nonterminal(import_from))
133+
{
132134
return (
133135
scope,
134136
CompletionNode::ImportFromFirstPart {
@@ -238,12 +240,9 @@ fn context(node: PyNode) -> Option<CompletionContext> {
238240
"(" => node.parent()?,
239241
"," => {
240242
let parent = node.parent()?;
241-
if parent.is_type(Nonterminal(arguments)) || parent.is_type(ErrorNonterminal(arguments))
242-
{
243+
if parent.is_type_or_error_thereof(Nonterminal(arguments)) {
243244
parent.parent()?
244-
} else if parent.is_type(Nonterminal(kwargs))
245-
|| parent.is_type(ErrorNonterminal(kwargs))
246-
{
245+
} else if parent.is_type_or_error_thereof(Nonterminal(kwargs)) {
247246
parent.parent()?.parent()?
248247
} else {
249248
return None;
@@ -301,7 +300,7 @@ fn call_args(node: PyNode) -> Option<CallArgs> {
301300
Nonterminal(stmt),
302301
ErrorNonterminal(stmt),
303302
])?;
304-
(args.is_type(Nonterminal(arguments)) || args.is_type(ErrorNonterminal(arguments)))
303+
args.is_type_or_error_thereof(Nonterminal(arguments))
305304
.then_some(CallArgs(args))
306305
}
307306

@@ -318,8 +317,7 @@ impl<'db> CallArgs<'db> {
318317
}
319318
if child.is_type(Nonterminal(named_expression)) {
320319
positional_args += 1;
321-
} else if child.is_type(Nonterminal(kwargs)) || child.is_type(ErrorNonterminal(kwargs))
322-
{
320+
} else if child.is_type_or_error_thereof(Nonterminal(kwargs)) {
323321
for in_kwargs in child.iter_children() {
324322
if in_kwargs.start() >= until.node.start() {
325323
break 'outer;
@@ -355,10 +353,7 @@ fn from_import_dots_before_node(leaf: PyNode) -> usize {
355353

356354
fn import_from_target_node(node: PyNode) -> CompletionNode {
357355
debug_assert!(
358-
matches!(
359-
node.type_(),
360-
Nonterminal(import_from) | ErrorNonterminal(import_from),
361-
),
356+
node.is_type_or_error_thereof(Nonterminal(import_from)),
362357
"{:?}",
363358
node.type_()
364359
);

0 commit comments

Comments
 (0)