Skip to content

Commit

Permalink
feat(python): Scope staticmethods (@staticmethod def inside class)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Jul 30, 2024
1 parent 4779d69 commit 8f53aa5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,8 @@ Language scopes:
- methods: Function definitions inside `class` bodies
- class-methods: Function definitions decorated as `classmethod` (excl. the
decorator)
- static-methods: Function definitions decorated as `staticmethod` (excl. the
decorator)

--python-query <PYTHON_QUERY>
Scope Python code using a custom tree-sitter query.
Expand Down
17 changes: 17 additions & 0 deletions src/scoping/langs/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub enum PreparedPythonQuery {
Methods,
/// Function definitions decorated as `classmethod` (excl. the decorator).
ClassMethods,
/// Function definitions decorated as `staticmethod` (excl. the decorator).
StaticMethods,
}

impl From<PreparedPythonQuery> for TSQuery {
Expand Down Expand Up @@ -126,6 +128,21 @@ impl From<PreparedPythonQuery> for TSQuery {
IGNORE
)
}
PreparedPythonQuery::StaticMethods => {
formatcp!(
"
(class_definition
body: (block
(decorated_definition
(decorator (identifier) @{0})
definition: (function_definition) @method
(#eq? @{0} \"staticmethod\")
)
)
)",
IGNORE
)
}
},
)
.expect("Prepared queries to be valid")
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ impl InScopeLinePart {
include_str!("python/base.py"),
Python::new(CodeQuery::Prepared(PreparedPythonQuery::ClassMethods)),
)]
#[case(
"base.py_staticmethods",
include_str!("python/base.py"),
Python::new(CodeQuery::Prepared(PreparedPythonQuery::StaticMethods)),
)]
#[case(
"base.ts_strings",
include_str!("typescript/base.ts"),
Expand Down
37 changes: 37 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.py_staticmethods.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 52
l: " def static_decorator(func):\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 53
l: " \"\"\"Decorator for static methods.\"\"\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 54
l: "\n"
m: ^^
- n: 55
l: " def wrapper(*args, **kwargs):\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 56
l: " print(\"Static method decorator called\")\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 57
l: " return func(*args, **kwargs)\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 58
l: "\n"
m: ^^
- n: 59
l: " return wrapper\n"
m: "^^^^^^^^^^^^^^^^^^^^^^ "
- n: 76
l: " def static_method() -> None:\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 77
l: " \"\"\"Static method.\"\"\"\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 78
l: " print(\"Inside static method\")\n"
m: "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "

0 comments on commit 8f53aa5

Please sign in to comment.