Revert "[ty] Fix completion in decorator without class or function definition" (#22176)

This commit is contained in:
Micha Reiser
2025-12-24 17:27:21 +01:00
committed by GitHub
parent adef89eb7c
commit eef403f6cf
6 changed files with 22 additions and 165 deletions

View File

@@ -2914,27 +2914,21 @@ impl<'src> Parser<'src> {
self.current_token_range(),
);
let range = self.node_range(start);
ast::StmtFunctionDef {
node_index: Default::default(),
range,
is_async: false,
decorator_list: decorators,
name: ast::Identifier {
id: Name::empty(),
range: self.missing_node_range(),
node_index: AtomicNodeIndex::NONE,
},
type_params: None,
parameters: Box::new(ast::Parameters {
range: self.missing_node_range(),
..ast::Parameters::default()
}),
returns: None,
body: vec![],
}
.into()
// TODO(dhruvmanila): It seems that this recovery drops all the parsed
// decorators. Maybe we could convert them into statement expression
// with a flag indicating that this expression is part of a decorator.
// It's only possible to keep them if it's a function or class definition.
// We could possibly keep them if there's indentation error:
//
// ```python
// @decorator
// @decorator
// def foo(): ...
// ```
//
// Or, parse it as a binary expression where the left side is missing.
// We would need to convert each decorator into a binary expression.
self.parse_statement()
}
}
}

View File

@@ -1,5 +1,6 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/decorator_missing_expression.py
---
## AST
@@ -9,44 +10,6 @@ Module(
node_index: NodeIndex(None),
range: 0..51,
body: [
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 0..4,
is_async: false,
decorator_list: [
Decorator {
range: 0..4,
node_index: NodeIndex(None),
expression: Name(
ExprName {
node_index: NodeIndex(None),
range: 1..4,
id: Name("def"),
ctx: Load,
},
),
},
],
name: Identifier {
id: Name(""),
range: 4..4,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 4..4,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [],
},
),
AnnAssign(
StmtAnnAssign {
node_index: NodeIndex(None),

View File

@@ -1,5 +1,6 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/decorator_unexpected_token.py
---
## AST
@@ -9,44 +10,6 @@ Module(
node_index: NodeIndex(None),
range: 0..34,
body: [
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 0..4,
is_async: false,
decorator_list: [
Decorator {
range: 0..4,
node_index: NodeIndex(None),
expression: Name(
ExprName {
node_index: NodeIndex(None),
range: 1..4,
id: Name("foo"),
ctx: Load,
},
),
},
],
name: Identifier {
id: Name(""),
range: 4..4,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 4..4,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [],
},
),
With(
StmtWith {
node_index: NodeIndex(None),
@@ -83,44 +46,6 @@ Module(
],
},
),
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 23..27,
is_async: false,
decorator_list: [
Decorator {
range: 23..27,
node_index: NodeIndex(None),
expression: Name(
ExprName {
node_index: NodeIndex(None),
range: 24..27,
id: Name("foo"),
ctx: Load,
},
),
},
],
name: Identifier {
id: Name(""),
range: 27..27,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 27..27,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [],
},
),
Assign(
StmtAssign {
node_index: NodeIndex(None),

View File

@@ -48,7 +48,6 @@ pub fn completion<'db>(
}
ContextKind::NonImport(ref non_import) => {
let model = SemanticModel::new(db, file);
dbg!(&non_import.target);
let (semantic_completions, scoped) = match non_import.target {
CompletionTargetAst::ObjectDot { expr } => {
(model.attribute_completions(expr), None)
@@ -521,7 +520,7 @@ impl<'m> Context<'m> {
ContextKind::Import(import)
} else {
let target_token = CompletionTargetTokens::find(&cursor)?;
let target = dbg!(target_token.ast(&cursor)?);
let target = target_token.ast(&cursor)?;
ContextKind::NonImport(ContextNonImport { target })
};
@@ -560,7 +559,6 @@ impl<'m> Context<'m> {
/// The lifetime parameter `'m` refers to the shorter of the following
/// lifetimes: the parsed module the cursor is in and the actual bytes
/// making up the source file containing the cursor.
struct ContextCursor<'m> {
/// The parsed module containing the cursor.
parsed: &'m ParsedModuleRef,
@@ -1463,7 +1461,7 @@ impl<'t> CompletionTargetTokens<'t> {
}
}
CompletionTargetTokens::Generic { token } => {
let node = dbg!(cursor.covering_node(dbg!(token.range()))).node();
let node = cursor.covering_node(token.range()).node();
Some(CompletionTargetAst::Scoped(ScopedTarget { node }))
}
CompletionTargetTokens::Unknown => {
@@ -6688,19 +6686,6 @@ def func():
.not_contains("False");
}
#[test]
fn decorator_without_class_or_function() {
completion_test_builder(
"\
from dataclasses import dataclass
@dataclass(froz<CURSOR>
",
)
.build()
.contains("frozen");
}
#[test]
fn statement_keywords_in_if_body() {
completion_test_builder(

View File

@@ -442,18 +442,8 @@ impl Workspace {
new_text: edit.content().map(ToString::to_string).unwrap_or_default(),
}
});
let name = comp.insert.as_deref().unwrap_or(&comp.name).to_string();
let import_suffix = comp
.module_name
.and_then(|name| import_edit.is_some().then(|| format!(" (import {name})")));
let label = import_suffix
.map(|suffix| format!("{name}{suffix}"))
.unwrap_or_else(|| name);
Completion {
label,
name: comp.name.into(),
kind,
detail: type_display,
module_name: comp.module_name.map(ToString::to_string),
@@ -1080,7 +1070,7 @@ pub struct Hover {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Completion {
#[wasm_bindgen(getter_with_clone)]
pub label: String,
pub name: String,
pub kind: Option<CompletionKind>,
#[wasm_bindgen(getter_with_clone)]
pub insert_text: Option<String>,

View File

@@ -336,7 +336,7 @@ class PlaygroundServer
return {
suggestions: completions.map((completion, i) => ({
label: {
label: completion.label,
label: completion.name,
detail:
completion.module_name == null
? undefined