Skip to content

Commit

Permalink
feat(python): Scope global aka module-level variable (assignments)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Jul 30, 2024
1 parent 94894c0 commit fc5c027
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,7 @@ Language scopes:
- with: `with` blocks (in their entirety)
- try: `try` blocks (in their entirety)
- lambda: `lambda` statements (in their entirety)
- globals: Global, i.e. module-level variables

--python-query <PYTHON_QUERY>
Scope Python code using a custom tree-sitter query.
Expand Down
5 changes: 5 additions & 0 deletions src/scoping/langs/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub enum PreparedPythonQuery {
Try,
/// `lambda` statements (in their entirety).
Lambda,
/// Global, i.e. module-level variables.
Globals,
}

impl From<PreparedPythonQuery> for TSQuery {
Expand Down Expand Up @@ -152,6 +154,9 @@ impl From<PreparedPythonQuery> for TSQuery {
PreparedPythonQuery::With => "(with_statement) @with",
PreparedPythonQuery::Try => "(try_statement) @try",
PreparedPythonQuery::Lambda => "(lambda) @lambda",
PreparedPythonQuery::Globals => {
"(module (expression_statement (assignment left: (identifier) @global)))"
}
},
)
.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 @@ -147,6 +147,11 @@ impl InScopeLinePart {
include_str!("python/base.py"),
Python::new(CodeQuery::Prepared(PreparedPythonQuery::Lambda)),
)]
#[case(
"base.py_globals",
include_str!("python/base.py"),
Python::new(CodeQuery::Prepared(PreparedPythonQuery::Globals)),
)]
#[case(
"base.ts_strings",
include_str!("typescript/base.ts"),
Expand Down
34 changes: 34 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.py_globals.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 16
l: "test_var: int = 10\n"
m: "^^^^^^^^ "
- n: 82
l: "square = lambda x: x * x\n"
m: "^^^^^^ "
- n: 85
l: "multi_line_str = \"\"\"\n"
m: "^^^^^^^^^^^^^^ "
- n: 91
l: "multiline_f_string = f\"\"\"This is a\n"
m: "^^^^^^^^^^^^^^^^^^ "
- n: 96
l: "raw_string = r\"This is a raw string with no special treatment for \\n\"\n"
m: "^^^^^^^^^^ "
- n: 97
l: "bytes_string = b\"This is a bytes string\"\n"
m: "^^^^^^^^^^^^ "
- n: 98
l: "bytes_string = rf\"This is a raw f-string with {raw_string}\"\n"
m: "^^^^^^^^^^^^ "
- n: 102
l: "squared_numbers = [\"x\" + square(x) for x in range(10)]\n"
m: "^^^^^^^^^^^^^^^ "
- n: 105
l: "unique_squares = {square(x) for x in range(10)}\n"
m: "^^^^^^^^^^^^^^ "
- n: 108
l: "squares_dict = {x: square(x) for x in range(10)}\n"
m: "^^^^^^^^^^^^ "

0 comments on commit fc5c027

Please sign in to comment.