Skip to content

Code formatting causes cursor to jump to EOF #1507

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

Closed
Cirzen opened this issue Aug 31, 2018 · 27 comments
Closed

Code formatting causes cursor to jump to EOF #1507

Cirzen opened this issue Aug 31, 2018 · 27 comments
Labels
Area-Code Formatting Issue-Bug A bug to squash. Resolution-Fixed Will close automatically.

Comments

@Cirzen
Copy link

Cirzen commented Aug 31, 2018

I have my formatting style set to Allman. If I start typing something, e.g. a function, or a process block and put the open-brace at the end of the line, when the formatter kicks in and moves it to the next line, the cursor also jumps to the last line of the file.
Here's a screen grab of what happens.
formatter

Disabling the add-in resolves the issue

System Details

  • Operating system name and version: Win 10 1709
  • VS Code version: 1.26.1
    493869ee8e8a846b0855873886fc79d480d342de
    x64
  • PowerShell extension version: 1.8.3
  • Output from $PSVersionTable:
    Name Value

PSVersion 6.1.0-preview.1
PSEdition Core
GitCommitId v6.1.0-preview.1
OS Microsoft Windows 10.0.16299
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

Issue Description

I am experiencing a problem with...

Attached Logs

EditorServices.zip
Follow the instructions in the [README](https://github.com/PowerShell/vscode-powershell#reporting-problems

)
about capturing and sending logs.

@Cirzen
Copy link
Author

Cirzen commented Sep 6, 2018

Cannot reproduce after a reboot. Closing as anomaly.

@Cirzen Cirzen closed this as completed Sep 6, 2018
@TylerLeonhardt
Copy link
Member

Thanks for the follow up!

@ThubLives
Copy link

I see this behaviour pretty consistently with larger scripts and using OTBS formatting style. Should I create a new issue with my info?

@rjmholt
Copy link
Contributor

rjmholt commented Oct 18, 2018

@ThubLives I think this issue is the best place. I will reopen.

Can you please follow the steps in our troubleshooting docs and post the info/logs/any extra info you think might be relevant here?

@ThubLives
Copy link

Some version info:

code -v
1.28.2
7f3ce96ff4729c91352ae6def877e59c561f4850
x64

code --list-extensions --show-versions
ms-vscode.PowerShell@1.9.0

I haven't been able to find anything useful in logs and I can't always reproduce it on demand, but here is what I can say with confidence:

This seems to affect my larger projects (over about 500 lines of code) and not always immediately, but once it's set in, any time formatting happens, be it triggered by "format on type" or manually with "Format Selection/Document" in the command pallette.

Often, manually choosing "Format Document" clears something and the cursor teleportation behaviour stops happening for a time. This does not always help, however. Restarting the application sometimes helps temporarily as well, but not always.

The behaviour that results is that a) appropriate formatting takes place, b) the cursor is moved to the end of the second to last line in the file, regardless of how many blank lines are at the bottom of the file, or whether the last line is even blank, and c) the view scrolls by about half the screen height, but is a large enough jump to make me lose my place and take a moment to reorient myself in order to put the cursor back to where I was typing. Obviously, if I continue typing, the view will jump to the bottom of the file where the cursor now is.

I've tried to create a simple example script that triggers this problem, but I haven't had much luck. However, every one of my real scripts (which happen to be over 600 lines of code) trigger this sooner or later.

@ThubLives
Copy link

Another quirk during cursor teleportation is that all folded code is left unfolded. Also, sometimes when I find my way back to where my cursor should be, I notice that the formatting was incomplete. For example, one remaining curly brace was not indented properly, though all the other lines were adjusted.

@gerane
Copy link

gerane commented Oct 30, 2018

@tylerl0706 this is one of the issues I see.

@andrewducker
Copy link

Yup. This happens to me intermittently. Usually when I hit "save" and it does an automatic format.

@ThubLives
Copy link

I now suspect that this is only happening in scripts that contain custom class definitions. Can anyone else who's experienced this problem confirm this with their own scripts? It may simply be that my larger scripts tend to use classes, but so far I haven't found any of my scripts without classes that trigger this behaviour.

@rjmholt
Copy link
Contributor

rjmholt commented Nov 28, 2018

If that's true, that's really great information!

@ThubLives
Copy link

Nope. My theory seems to be a bust. I found a 1690 line script that has no class definitions, but triggers this behaviour nonetheless.

@ThubLives
Copy link

ThubLives commented Dec 6, 2018

With the release of vscode-powershell 1.10.0, the code-folding code has been removed from the client-side to be re-implemented server-side. Also with this version, whether related or not, I can no longer reproduce the cursor teleportation behaviour during automatic formatting, which has been a boon for my fraying sanity. Even the unfolding of all code doesn't seem to be happening anymore, though this problem has been replaced with more subtle code folding bugs, deserving their own issue(s).

tl;dr: No more unexpected cursor teleportation in v1.10.0 for me. Code folding misbehaviour is different, but deserves a new issue. Should this issue be closed?

@TylerLeonhardt
Copy link
Member

Cool! Can anyone else repro this? @Cirzen @gerane @andrewducker

@rjmholt
Copy link
Contributor

rjmholt commented Dec 6, 2018

@ThubLives We're working on a fix for the folding issues here: PowerShell/PowerShellEditorServices#805

@Cirzen
Copy link
Author

Cirzen commented Dec 7, 2018

@TylerLeonhardt I've not noticed this issue since updating this morning, but then to be honest, I haven't noticed the issue on the previous version for a while either. Happy to close if that's the consensus.

Edit: hold fire, I did just experience something that looked like it when manually reformatting code. I'll try to repro

Edit2: Yes, can repro if I'm making amendments as the formatter is still running. I'm not jumping to EOF, but getting duplicated code at the end of the module:
cursorjump

@andrewducker
Copy link

I've not seen the issue today.

(I had been thinking that it was a clash with GitLens, which I started recently, but I turned that back on, and I'm still not seeing the issue.)

@andrewducker
Copy link

Aha! That duplicated code issue is one I've seen repeatedly! And once it happens, if I delete it then hitting "Save" (and triggering the automatic formatting) makes the code reappear. Sometimes at the end of the file, sometimes randomly in the middle (possibly where the cursor was).

@andrewducker
Copy link

So presumably it only happens in files large (and/or complex) enough for the reformatting to take longer than it takes to update the file again?

@ThubLives
Copy link

@andrewducker I'm thinking you're right, this seems like some kind of race condition. I have been largely free of these issues since updating to v1.10.0 (code folding has its own bugs since it's been moved to PS editor service), but I thought I would try GitLens on my current 2200 line script project. I was almost immediately greeted with a familiar disorientation when Format on Type was triggered.

The cursor did not move and I didn't see any duplicate code, but all code was unfolded, which meant the text under the current view port moved and I was left looking at a much earlier portion of the script. Fortunately, since the cursor was still at the same line number and column as before, I could just move the cursor with the arrow keys and reset the view port to continue working on the code.

I'm guessing that during formatting, the code is effectively unfolded until certain operations are completed, then re-folded at the user's selected points before the view port is updated. It would seem that by installing GitLens, I have increased the duration of some operation triggered by formatting that happens between unfolding and re-folding code and is causing a race condition with some other operation that is interrupting the re-folding and possibly other operations.

In my case, only code folding is affected, but if the above is true, it would stand to reason that the cursor re-positioning might also be affected if the cursor is temporarily moved and is supposed to return to the appropriate line before the view port is updated, but is similarly interrupted.

As for the duplicated code, I'm having a hard time fitting that into my little theory. I haven't seen that in my testing, so I'm not sure if there is any correlation to cursor position — other than where the duplicate code appears.

@lokwaniyash
Copy link

My html file is doing the same thing, It happens whether there are many lines or not, my 3000 line and 30 line files act the same. Any fixes cuz it's very annoying to click the cursor where the I'm typing after every save. Thanks.

@andrewducker
Copy link

If you've got a reproducible situation where it happens every time then that sounds incredibly useful!

@lokwaniyash
Copy link

lokwaniyash commented Jun 15, 2019

As a matter of fact, it's happening right now as I'm playing with some stuff.
Should I share the recording?

@andrewducker
Copy link

Does it still do it if you disable the PowerShell extension? Because if it's HTML then I'd expect it's not the extension doing it. Worth a test!

@lokwaniyash
Copy link

I don't even have it installed.

@andrewducker
Copy link

In that case, presumably not a plugin issue, but a VS Code issue.

@AshuTheDoughnut
Copy link

AshuTheDoughnut commented Nov 26, 2022

Have the same issue on one of my JS files. It has in total 4430 lines of code

@andyleejordan
Copy link
Member

This issue was for a bug in the PowerShell extension, solved a good while ago. Issues with HTML and JS files need to be filed with their respective extensions, thank you!

@andyleejordan andyleejordan added the Resolution-Fixed Will close automatically. label Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Code Formatting Issue-Bug A bug to squash. Resolution-Fixed Will close automatically.
Projects
None yet
Development

No branches or pull requests

10 participants