Skip to content

Commit

Permalink
feat(python): Scope type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Jul 30, 2024
1 parent 0fb549c commit 5dc106f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ Language scopes:
- globals: Global, i.e. module-level variables
- variable-identifiers: Identifiers for variables (left-hand side of
assignments)
- types: Types in type hints

--python-query <PYTHON_QUERY>
Scope Python code using a custom tree-sitter query.
Expand Down
3 changes: 3 additions & 0 deletions src/scoping/langs/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub enum PreparedPythonQuery {
Globals,
/// Identifiers for variables (left-hand side of assignments).
VariableIdentifiers,
/// Types in type hints.
Types,
}

impl From<PreparedPythonQuery> for TSQuery {
Expand Down Expand Up @@ -162,6 +164,7 @@ impl From<PreparedPythonQuery> for TSQuery {
PreparedPythonQuery::VariableIdentifiers => {
"(assignment left: (identifier) @identifier)"
}
PreparedPythonQuery::Types => "(type) @type",
},
)
.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 @@ -157,6 +157,11 @@ impl InScopeLinePart {
include_str!("python/base.py"),
Python::new(CodeQuery::Prepared(PreparedPythonQuery::VariableIdentifiers)),
)]
#[case(
"base.py_types",
include_str!("python/base.py"),
Python::new(CodeQuery::Prepared(PreparedPythonQuery::Types)),
)]
#[case(
"base.ts_strings",
include_str!("typescript/base.ts"),
Expand Down
19 changes: 19 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.py_types.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 16
l: "test_var: int = 10\n"
m: " ^^^ "
- n: 63
l: " def class_method(cls) -> None:\n"
m: " ^^^^ "
- n: 69
l: " def instance_method(self) -> None:\n"
m: " ^^^^ "
- n: 76
l: " def static_method() -> None:\n"
m: " ^^^^ "
- n: 112
l: "def exception_handling(x) -> None:\n"
m: " ^^^^ "

0 comments on commit 5dc106f

Please sign in to comment.