python: Add task for running modules (#26462)

Closes #26460

I am new to contributing to Zed (and pretty new to Rust in general). I'm
not too familiar with code style, guidelines etc. so please feel free to
suggest changes/improvements.

This PR adds a run icon to Python files that have a "main" function:
```python
if __name__ == "__main__":
    ...
```

In addition to the gutter icon, there is now also an extra task in the
command palette "run module".

Release Notes:

- Added detection for runnable Python modules
- Added Python-specific task to run a Python file as a module from
inside the project's scope

---------

Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
This commit is contained in:
Alex van de Griendt 2025-03-25 22:08:16 +01:00 committed by GitHub
parent 152432f1d9
commit 1574a3a2fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 1 deletions

View file

@ -82,3 +82,19 @@
)
)
)
; module main method
(
(module
(if_statement
condition: (comparison_operator
(identifier) @run @_lhs
operators: "=="
(string) @_rhs
)
(#eq? @_lhs "__name__")
(#match? @_rhs "^[\"']__main__[\"']$")
(#set! tag python-module-main-method)
)
)
)