Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to generate dosctring after defining a function? #1494

Open
castroavila opened this issue Aug 29, 2023 · 1 comment
Open

Is it possible to generate dosctring after defining a function? #1494

castroavila opened this issue Aug 29, 2023 · 1 comment

Comments

@castroavila
Copy link

Hi,

Is it possible to run some command to create the docstring after a function has been defined? Let's suppose I already have defined the function:

def function(a, b=2, c=3):
    """


    """

    .....

And I want to generate the docstring to have:

def function(a, b=2, c=3):
    """Function

    Parameters
    ----------
    a : TODO
    b : TODO, optional
    c : TODO, optional

    Returns
    -------
    TODO

    """
    pass

I have checked other options to generate the docstring, however the style does match the one followed by this plugin.

Best regards,
Manuel

@PhilippFeO
Copy link

Out of curiosity: Have you already defined your own snippet?

Probably, this repo is not the right address for your issue since it's (in my understanding) rather a collection of templates than dealing with the expansion mechanism itself–this is done with a snippet engine, fi. UltiSnips.

You wish to generate code based on existing one, ie 'Generate X based on code I have already typed/saved'. A snippet is more like 'Generate X based on this recently typed character sequence (while still being in insert mode)'. You stop the snippet expansion algorithm each time quitting insert mode. Having a snippet working on an already defined function is (based on my understanding) not possible (or at least not sensible since the trigger would be utterly complex).

Nevertheless, your wish can be fullfilled. You can either use Treesitter and, especially, Treesitter Playground to filter for the function node, ie. getting it's signature. Then you parse the signature generating your docstring and insert it via :h nvim_buf_set_lines(). You will find video on Treesitter Playground on YouTube.
Another option involves moving to the line of function signature (anywhere), read the line with :h nvim_get_current_line(), parse (as above) and insert (as above). This approach has more manual labor but is a bit easier to implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants