Skip to content

Commit 159c5c3

Browse files
Karman-singh15meta-codesync[bot]
authored andcommitted
fix: 'Go to symbol' now includes methods inside a class (#2109)
Summary: Logic was added to post-process symbols inside a ClassDef. it allows Stmt::FunctionDef to remain simple (always creating a FUNCTION), and shifts the responsibility of context-awareness (knowing it is a method) to the parent ClassDef. Any child symbol with SymbolKind::FUNCTION is now converted to SymbolKind::METHOD Fixes #2051 Pull Request resolved: #2109 Test Plan: I updated the existing test test_document_symbols_normal_file in pyrefly/lib/test/lsp/lsp_interaction/document_symbols.rs The test uses an existing file normal.py which contains NormalClass and normal_method. I updated the test assertion to verify that normal_method inside NormalClass has SymbolKind::METHOD. Reviewed By: grievejia Differential Revision: D90689263 Pulled By: stroxler fbshipit-source-id: ac0e6f8702e4bbc0d7b646f084615152668dcea5
1 parent 12cd1d0 commit 159c5c3

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

pyrefly/lib/lsp/non_wasm/document_symbols.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ fn recurse_stmt_adding_symbols<'a>(
6161
Stmt::ClassDef(stmt_class_def) => {
6262
let mut children = Vec::new();
6363
children.append(&mut recursed_symbols);
64+
65+
// Functions defined inside a class are methods.
66+
for child in &mut children {
67+
if child.kind == lsp_types::SymbolKind::FUNCTION {
68+
child.kind = lsp_types::SymbolKind::METHOD;
69+
}
70+
}
71+
6472
let name = match stmt_class_def.name.as_str() {
6573
"" => "unknown".to_owned(),
6674
name => name.to_owned(),

pyrefly/lib/test/lsp/document_symbols.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class MyClass:
189189
"children": [
190190
{
191191
"name": "__init__",
192-
"kind": 12,
192+
"kind": 6,
193193
"range": {
194194
"start": {
195195
"line": 4,
@@ -214,7 +214,7 @@ class MyClass:
214214
},
215215
{
216216
"name": "method1",
217-
"kind": 12,
217+
"kind": 6,
218218
"range": {
219219
"start": {
220220
"line": 7,
@@ -239,7 +239,7 @@ class MyClass:
239239
},
240240
{
241241
"name": "method2",
242-
"kind": 12,
242+
"kind": 6,
243243
"range": {
244244
"start": {
245245
"line": 10,
@@ -396,7 +396,7 @@ result = y.method()
396396
},
397397
{
398398
"name": "method",
399-
"kind": 12,
399+
"kind": 6,
400400
"range": {
401401
"start": {
402402
"line": 12,
@@ -689,7 +689,7 @@ result = y.method()
689689
},
690690
{
691691
"name": "method",
692-
"kind": 12,
692+
"kind": 6,
693693
"range": {
694694
"start": {
695695
"line": 12,
@@ -976,7 +976,7 @@ items: List[str] = ["a", "b", "c"]
976976
},
977977
{
978978
"name": "method",
979-
"kind": 12,
979+
"kind": 6,
980980
"range": {
981981
"start": {
982982
"line": 14,

pyrefly/lib/test/lsp/lsp_interaction/document_symbols.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,20 @@ fn test_document_symbols_normal_file() {
101101
.iter()
102102
.any(|s| s.name == "normal_function" && s.kind == lsp_types::SymbolKind::FUNCTION);
103103

104-
let has_class = symbols
104+
let class_symbol = symbols
105105
.iter()
106-
.any(|s| s.name == "NormalClass" && s.kind == lsp_types::SymbolKind::CLASS);
106+
.find(|s| s.name == "NormalClass" && s.kind == lsp_types::SymbolKind::CLASS);
107+
108+
let has_class_and_method = match class_symbol {
109+
Some(c) => c.children.as_ref().is_some_and(|children| {
110+
children.iter().any(|s| {
111+
s.name == "normal_method" && s.kind == lsp_types::SymbolKind::METHOD
112+
})
113+
}),
114+
None => false,
115+
};
107116

108-
has_function && has_class
117+
has_function && has_class_and_method
109118
})
110119
.unwrap();
111120

0 commit comments

Comments
 (0)