kylebarron / vscode-jupyter-python

Run automatically-inferred Python code blocks in the VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=kylebarron.vscode-jupyter-python
MIT License
37 stars 3 forks source link

Identifying decorator #7

Open vavrines opened 1 year ago

vavrines commented 1 year ago

First thanks for creating this package! It works perfectly most of the time, but I found a problem in identifying decorators for functions. An illustrative example would be like

@ti.kernel
def solver_ti(u):
    pass

The package would recognize @ti.kernel as a single sentence instead of the decorator of the function. This particular decorator is from taichi, but I guess the other ones like Numba will face the same issue.

kylebarron commented 1 year ago

Yeah this is mentioned in the readme as a future TODO.

For most code we can expand the selection downward either looking for lines with additional indentation or for specific keywords. That naive approach won't work here, because if we added def as a keyword, then any selection would propagate downwards for any sequence of function definitions.

The solution https://github.com/nikitakit/hydrogen-python chose is to implement expanding selections upward as well as downward. So if your cursor is on the def line, it'll search upwards for any lines at the same indentation starting with @.

A PR is welcome for this. Otherwise I'll probably implement it next time I use a decorator 😄