QodeAssist icon indicating copy to clipboard operation
QodeAssist copied to clipboard

Replace existing code with suggesed

Open OneMoreGres opened this issue 8 months ago • 9 comments

Is your feature request related to a problem? Please describe. Models can produce suggestions, affecting already existing code Image Currently suggestion can only be inserted at cursor position.

Describe the solution you'd like It would be great if suggestion could also change existing code (like in cursor IDE). Something like checking suggested lines for similarity to existing ones and if result is above threshold then treat it as replacement, if not - insertion. For replacement, show suggested code changes near the code being changed.

Describe alternatives you've considered

Additional context

Tested on zed-industries/zeta quants

Sample code

void do_some_work(int);

struct Foo {
    int bar;
};

int foo(Foo first, Foo second) {
    int firstBar = 
    if (first.bar == second.bar)
        return false;

    if (first.bar > second.bar)
        do_some_work(first.bar);
    else
        do_some_work(second.bar);
    return true;
}

OneMoreGres avatar Mar 30 '25 09:03 OneMoreGres

So, looks you are using FIM here via llama.cpp? If you will run like Instruct model for code completion in QodeAssist you can fix this just with correct system prompt, I made some by default. Indeed I don't see now any sense to use clear FIM model. To much problem need to fix by manual

Palm1r avatar Apr 01 '25 10:04 Palm1r

Yes, it's llama.cpp. But there is not error in the suggestion. It's simply doesn't not add new code, but does some refactoring. In the example it suggests to assign class members to separate variables and replace their following usages. That's what I intended to do in a similar case. But if I simply press tab to apply it, there will still be an old version of changed code, which I had to remove manually. The idea of the issue is to add support of refactoring suggestions, i.e. replacing existing code with the one provided by LLM, without manually deleting the old code.

OneMoreGres avatar Apr 01 '25 18:04 OneMoreGres

I meant not about error, but about how FIM models works. You will have bigger output answer and pay for that bigger counting of tokens. In case with instruct models in code completion, you can fix it by system prompt and count output of output tokens will be small. I just try to help spent less. Anyway I will add merge-things for code completion suggestions, it is not problem.

Palm1r avatar Apr 03 '25 16:04 Palm1r

Okay, merging code with suggest and show is not simple in QtC like I thought. So now I don't have idea for that

Palm1r avatar Apr 21 '25 09:04 Palm1r

Sad to hear. Maybe show it as usual, but insert or merge when user starts applying, depending on the suggestion and the existing code similarity? Maybe also add separate button/shortcut for this mode (like apply with merge)?

OneMoreGres avatar Apr 23 '25 06:04 OneMoreGres

I improved code suggestion and merging with existing code in 0.5.10. Please try, should be better. It is not absolutely what did you want. Maybe need new algorithm for that. PR welcome

Palm1r avatar Apr 24 '25 01:04 Palm1r

I couldn't get it to override existing code. It always inserts the new one. I'll make a PR if I get any results

OneMoreGres avatar Apr 24 '25 17:04 OneMoreGres

For example Image Image

Palm1r avatar Apr 24 '25 17:04 Palm1r

I've made a PR. Have no issues so far, but I'm still testing.

Btw, looks like single word apply was was affected by changes made for this issue. It drops suggestion after first apply.

Changing if (!subText.contains('\n')) { to if (!subText.contains('\n') && part == Line) { here fixed it for me

OneMoreGres avatar May 18 '25 18:05 OneMoreGres

I'm used to the current behavior, so there's no issue anymore. Closing the PR too

OneMoreGres avatar Oct 07 '25 09:10 OneMoreGres