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

Properly trim spaces using the new TextEditor.edit() callback #26

Closed
wants to merge 1 commit into from

Conversation

ameily
Copy link

@ameily ameily commented Mar 27, 2019

This should fix #24 and #21

It looks like the vscode API changed to where vscode wants all document edits to be performed on the TextEditorEdit object passed to the TextEdit.edit() callback. This PR adds support for trimming trailing spaces on both TextEditorEdit and WorkspaceEdit objects. The changes should not affect on-demand trimming operations.

@shardulm94
Copy link
Owner

Thanks @ameily for digging into this. Back when I wrote this extension, the VSCode extension API did not have a hook to modify the file before save. Hence, I relied on the after save hook to achieve this functionality. Over these years, the vscode extension API and the execution environment has changed significantly. This extension needs a major refactor to ensure that it follows current VSCode standards. Hence I am a little hesitant to push out a minor or a patch version now. I will try to find some time next week to see if I can refactor and also include this change.

@ameily
Copy link
Author

ameily commented Mar 27, 2019

@shardulm94 based on my research, I think the other parts of the extension look fine. You could switch to a pre-save hook, willSaveTextDocument(), however vscode only allows a total of 1.5 seconds for this hook to execute and the documentation isn't clear on how to delay the save operation while any hooks run. So, I think keeping the hook in didSaveTextDocument() makes sense, in my opinion.

Other trailing spaces extensions don't come close to the features in trailing-spaces, so I think you'd be ok performing a minor release. Plus, that'd mean I wouldn't need to run a local version of the extension, 👍

@shardulm94
Copy link
Owner

@ameily I think 1.5 seconds should be good enough. I will time some executions and see how they perform. I had already switched to the onWillSaveTextDocument() in the develop branch. And this PR fixes the issues I have been facing to get the unit tests running locally. I made some good progress last night (mostly cleaning up my terrible code from 3 years ago), so I am feeling more confident now.

shardulm94 added a commit that referenced this pull request Mar 28, 2019
@shardulm94
Copy link
Owner

I ran some performance tests around 1.5 second limit.

  • Was able to delete 30k trailing space regions in the file within the limit with deleteModifiedLinesOnly set to false
  • The diff logic for deleteModifiedLinesOnly is slow and hence only able to delete 1k trailing space regions within the limit

Given the above numbers, I think moving to onWillSaveTextDocument() is relatively safe and it is better to adhere to the API provided by VSCode for the pre-save edits purpose. I incorporated parts of your PR (applying edits on TextEditorEdit) in the develop branch and initial tests suggest that things look good. You can also give it a try using the VSIX file here https://github.com/shardulm94/vscode-trailingspaces/releases/tag/v0.3.0-rc1

Note: This does not fix #21 entirely, because it seems like the deleteModifiedLinesOnly logic has always been incorrect and broken.

@ameily
Copy link
Author

ameily commented Mar 30, 2019

@shardulm94 I tested v0.3.0-rc1 and verified that the problem is fixed! I literally cannot develop without this extension so I'm excited for the next official release.

@shardulm94
Copy link
Owner

Closing as parts of this PR were incorporated in d74fdef

@shardulm94 shardulm94 closed this Apr 3, 2019
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

Successfully merging this pull request may close these issues.

trimOnSave trims _after_ save instead of before
2 participants