neogen icon indicating copy to clipboard operation
neogen copied to clipboard

Neogen sync/update [feature request]

Open smackesey opened this issue 2 years ago • 12 comments

Sometimes the source material for a generated annotation might be changed after the annotation has been generated. In these cases, it would be very useful to have a command (neogen.sync()?) to update an existing annotation. For example:

def foo(bar):
     return bar

Run neogen.generate():

def foo(bar):
    """

    Args:
        bar (): 

    Returns:
        
    """
    return bar

But now suppose I add type annotations so the top line reads:

def foo(bar: int):

Then the hypothetical neogen.sync() would do this:

def foo(bar: int):
    """

    Args:
        bar (int): 

    Returns:
        
    """
    return bar

smackesey avatar Feb 10 '22 02:02 smackesey

~~what about just re-doing Neogen command and it does it automatically ? So no need of a sync function?~~

This will not work because it'll override the annotation

danymat avatar Feb 10 '22 08:02 danymat

Hmm it seems difficult to implement, as how we could retrieve easily the annotation and see if it has been changed

Do you have any ideas ? Maybe with some regex

danymat avatar Feb 10 '22 08:02 danymat

Really what's needed is a treesitter grammar for the annotations themselves-- so it would be a feature that relatively few languages support, but IMO definitely worth it for popular ones.

I could implement this for Python google-style docstrings.

smackesey avatar Feb 10 '22 15:02 smackesey

Really what's needed is a treesitter grammar for the annotations themselves-- so it would be a feature that relatively few languages support, but IMO definitely worth it for popular ones.

You mean creating a grammar for annotations ? That could work indeed, but as of right now, we don't have any.

danymat avatar Feb 11 '22 11:02 danymat

you could retrieve the old annotation, create the new one, compare the "fields", see where values are set, set the im the new annotation -> profit :)

max397574 avatar Feb 23 '22 21:02 max397574

you could retrieve the old annotation

the problem is: how do you know something is the old annotation ? That's why having a parser for annotations could be a huge benefit, but I don't think it's something easily feasible

danymat avatar Feb 24 '22 21:02 danymat

you just get the comment about the function and check if it's kinda like the annotations format e.g. ---in lua

max397574 avatar Feb 24 '22 21:02 max397574

you just get the comment about the function and check if it's kinda like the annotations format e.g. ---in lua

Even though, this check is not sufficient, take a look:

--- this is not meant to be an annotation
function test()
end

If we allow this type of behavior, this will mean that everything on top of the function, that has --- will be deleted and replaced with new annotation. I don't think this is a good idea, because the user should be expected to clearly know the result of the command before typing it out.

danymat avatar Feb 24 '22 21:02 danymat

hmmm

max397574 avatar Feb 24 '22 21:02 max397574

One way to handle this, would be to keep the Information about created annotation (with marks) But that would mean that exiting neovim will lose the informations

danymat avatar Feb 24 '22 21:02 danymat

For reference, it seems like this is possible with bare luasnip (at least for the active session, exiting neovim still poses a problem). LuaSnip's README shows a GIF of this working, but the source code for the demo even admits that it is ugly and fragile. I'm not sure what the existing neogen+luasnip integration looks like, but I'm wondering if some combination of setting history = true in luasnip and using restoreNode would get close?

sputnick1124 avatar May 18 '22 13:05 sputnick1124

It would be a great feature, but I think it is not that easy to implement. To implement this, a better way is to parse the generated comments before, and then we can do a sync, which means additional parser should be implemented. A special mark could be a quick (but dirty) way...

tan-wei avatar Aug 26 '23 13:08 tan-wei