misogi / vscode-ruby-rubocop

Rubocop extension for Visual Studio Code
https://marketplace.visualstudio.com/items/misogi.ruby-rubocop
MIT License
137 stars 68 forks source link

Autocorrect destroys undo history #48

Closed AndrewRayCode closed 6 years ago

AndrewRayCode commented 7 years ago
  1. Make changes to a file
  2. Run autocorrect
  3. Try to undo

Undo history is lost by using the autocorrect feature. This plugin is dangerous!

damien commented 7 years ago

@AndrewRayCode For my own reference, could you give a more detailed accounting of your use-case? In particular:

Thanks!


As an aside, I'd strongly recommend using source control and committing your work immediately before running scripts that automatically change your code. It's not a fix for this bug, but it'll take the sting out of it and similar issues you may run into with other extensions/CLI tools.

AndrewRayCode commented 7 years ago

I'm in a single file, hitting command-shift-P, and typing "autocorrect" and selecting "Rubocop > autocorrect", which as I understand is provided by this plugin

I'm using the latest version of this plugin as far as I know. No upgrade icon. Latest vscode.

rubocop --version
    0.47.1

I version control my files. That shouldn't be related to this. I'm not going to make a commit "pre-code-linting save place", which still won't solve the undo history problem. After writing brand new code, I want to auto-format it. Of course I want to keep undo history here because I've just written brand new code. No other plugins work like this, in vscode or any other editor, for example the JS "prettier" plugin or eslint plugin autofix feature keep undo history.

damien commented 7 years ago

@AndrewRayCode It's a fair criticism, I'm just trying to understand the context and expectations.

Based on your feedback, inline autocorrecting clearly needs more work (undo example being a great example of a pain point). If other plugins are supporting undos, we probably can as well.

I'm currently busy adding Bundler support, but this seems like a great feature to pull in. Would you be interested in contributing a fix to your problem @AndrewRayCode? I'm not the maintainer, but I'd be surprised if @misogi turned down any willing contributors.

misogi commented 7 years ago

Thanks for your report.

You are right. I edit a code and execute autocorrect. Then I can't undo(Cmd+Z). I reproduced same issue. But I don't understand why breaking undo history.

@sergey-kintsel Can you please help us?

sergey-kintsel commented 7 years ago

That's true, it can't be reverted ( at least for now). As I understand, this is because autocorrect changes the file directly using file system. At the moment I do not know how to make it better

damien commented 7 years ago

@misogi @sergey-kintsel I did a bit of digging, the VSCode extension API seems have an abstraction for file changes, though I'm not certain off the top of my head how we'd integrate them. See:

Looks like we'd need to figure out how to get rubocop autocorrect output without altering the underlying files and convert them into file/location/text-range changes VSCode could implement via the workspace. I'm hoping there's an easier way to get VSCode to recognize side-effecting changes, because that sounds like a bit of a PITA to implement. I'll update this thread if I can fine some examples of how other extensions handle this behavior.

damien commented 7 years ago

Quick update: Looking at the ESLint extension, it seems they're using DocumentFormattingEditProvider under the VSCode languages namespace to do this sort of thing. We should probably look in there for ways to wrap rubocop's autocorrect/formatting functionality.

esalter commented 7 years ago

I'd like to second this, I stumbled on this while I was hacking in support for autocorrecting on save. Losing undo history is a big deal.

AndrewRayCode commented 7 years ago

The updated flow (specifically for manually calling this action in vscode) should not save the file ever. It should replace the contents (or selected contents) with the autocorrected text, and show the file as unsaved. This is also consistent with how other plugins work. I'm not sure how that will interact with saving a file, I agree that should be a config option.

pauloschilling commented 7 years ago

Would be really great if we can achieve this! 👏

Right now I'm trying to move from Atom to vscode, and this kind of feature is really something I want to keep.

Glavin001/atom-beautify works exactly the way we want by not saving the file, and allowing the undo operation. It also works when you want to autocorrect only the selected lines.