Bug 1169 - 'Format document on save' does really bad things to source code
Summary: 'Format document on save' does really bad things to source code
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Text Editor ()
Version: Trunk
Hardware: PC Linux
: --- critical
Target Milestone: ---
Assignee: Mike Krüger
Depends on:
Reported: 2011-09-30 18:09 UTC by Stephen Shaw
Modified: 2011-10-27 06:48 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

pre save code (74.08 KB, image/png)
2011-09-30 18:13 UTC, Stephen Shaw
post save code (69.13 KB, image/png)
2011-09-30 18:15 UTC, Stephen Shaw

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Stephen Shaw 2011-09-30 18:09:56 UTC
If you enable the 'Format document on save' feature...

Modifying a file even with something as simple as adding a newline somewhere in the file and hit save it will completely mess up the text.  I'm not sure if maybe there is something in the file that the formatter doesn't like since in this particular case it doesn't happen in the same place as the commit.

Steps to Reproduce:
1.  Enable 'Format document on save'
    Edit -> Preferences... -> (under Text Editor) Behavior
2.  Add a new line anywhere in the code
3.  Hit save.
4.  Code gets all messed up :(

I'll add a couple screenshots as well.
Comment 1 Stephen Shaw 2011-09-30 18:13:59 UTC
Created attachment 547 [details]
pre save code

This is before adding a new line and hitting save.  What the code should look like.
Comment 2 Stephen Shaw 2011-09-30 18:15:04 UTC
Created attachment 548 [details]
post save code

This is the code after I have added a new line and hit save.  Note, the new line was towards the beginning of the file.  So, not near where the code started to mess up.
Comment 3 Alan McGovern 2011-09-30 18:19:30 UTC
Just to clarify, if you open the file in another text editor it is just as corrupt, so it's not just the texteditor getting confused and displaying incorrectly.
Comment 5 Stephen Shaw 2011-09-30 18:25:48 UTC
It looks like it might be that if statement:

if (ext == extension)
    return true;

Is it trying to add {} and getting it wrong which causes all of the formatting after that point to get messed up?
Comment 7 Mike Krüger 2011-10-04 05:46:18 UTC
Can I have the source file where that happens ?

I think I've fixed the issue, format on save missed a parse error check at the beginning (The formatter can't work properly on files with parse errors) & I now check if the formatting produces code errors, but I need the file to be 100% sure it's fixed.
Comment 8 Mike Krüger 2011-10-27 06:48:04 UTC
regarding this one as fixed.