Please preserve comments when applying changes

Most of the times while applying changes Cursor removes existing comments from the code

It would be nice if it didn’t do that

I understand that it’s not an easy thing to deal with but preserving comments would make applying changes using the different modes quicker and more convenient.

7 Likes

I have also had an evening of getting suggested code changes in Composer (Ctrl + I) and clicking on the Editor window to Approve/Reject each diff.

A lot of the diffs were to ‘correct’:

  • Line spacing and other irregularities I might have when developing

  • Comments (it just wants to remove single and multi-line comments)

So it was time-consuming to have to manually Approve/Reject each diff rather than just click ‘Accept All’.

I also understand it is probably not an easy thing to deal with, but just reporting it is happening, in case it helps.

Another thing I noticed in the process is that when I had finished manually Accepting and Rejecting all diffs, the ‘Accept All’ button did not get greyed out and disabled (which I think it should if all the diffs have been accepted or rejected).

3 Likes

Same. If apply only edited the relevant code and didn’t try to refactor/edit other things, the process would become more efficient.

Currently:

  1. It removes the last line in the file if it’s present.
  2. It corrects occasional spacing issues.
  3. Removes comments.

While by itself this doesn’t seem like a problem, the problem arises when one is relying heavily on git diff/gitlens diff/source control diff to remember what they are working on, what already has been edited and review their own changes.

I’m relying heavily on diff to understand what I’ve already changed and what else needs changing, to quickly navigate to the edited parts and continue editing them or to quickly revert the edits.

If there are unrelated edits, such as the last line removed/commented code removed/minor spacing edits, this makes it more time consuming to navigate to only things I’m working on and trying to be focused on, especially in a huge huge code base.

4 Likes

Agreed with all of the points. I’d be happy if it was spacing agnostic in general and adhered to whatever was existing in the current file at least.

I tried putting this into .cursorrules but it’s being ignored somehow

3 Likes

The worst part is that it’ll remove comments and old functionality even when I ask it not to. I actually have in my system prompt ‘do not remove comments’ and it still does so. It must be something deeply-ingrained in the model at this point.

5 Likes

I experienced similar behaviour in Composer when I included in my prompt something like ‘don’t suggest changes related to coding style’, but it still suggested removing line spaces and other intentional formatting.

3 Likes

It might be a good idea to allow for some post processing filters of the proposed code changes.

In most proper programming languages you can simply delete all white space without changing the functionality.

If there would be a language specific filter that compares the proposals to the original code and rejects it if it is pure formatting, that would be great.
And a similar filter when it wants to delete comments.

Or even just a scriptable post processing filter hook, I could easily write something for Java and Dart for myself that would eliminate 99% of its code change “noise”.

That would reduce the time to go through all that crap manually and in turn increase the usability by quite a bit.

1 Like

Yeah, I am certain the system prompt for cursor contains something like “Preserve all comments”, and I don’t think that’s the key issue here. I think there’s a much more difficult problem to solve for because it’s not necessarily a problem of how the AI model generates code, it is also a problem of how to interpret and apply the diff after the AI generates code.

Imagine that you’re an AI model and are prompted into providing only the parts that need to be changed. So naturally you send back a portion of relevant code and maybe add new comments to highlight what should be changed. How does a diff then determine which parts were meant to be deleted (i.e. comments, EOF, etc), and which parts were meant to be kept, based merely on some code’s absence. If a portion of code doesn’t exist, does that mean delete it? Or skip it because it’s not relevant to the prompt?

Guess I’m not really offering any suggestions here but just recognizing it’s a challenge.

1 Like

I see that therefore I would like to have a post processing hook/script that I can write on my own that works on the generated diff in plain text so I can filter out unnecessary changes.

Very simple solution

I’ve just started using Cursor in the past week, so I’m kinda new to these hangups. But I did have a big issue where instead of correctly updating whatever I was working on, it would update the entire file with only the newly added section. Deleting all other code.

I’ve now begun pre-prompting with something like “please return the fixes for the following in their full context and with previous code/comments so that current functionality can be preserved.”

It’s pretty tedious.