Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 312893

Summary: Flushing content merge viewer resets dirty flag for a Saveable that is not being saved
Product: [Eclipse Project] Platform Reporter: Szymon Ptaszkiewicz <sptaszkiewicz>
Component: CompareAssignee: Malgorzata Janczarska <malgorzata.tomczyk>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, georg.kallidis, Szymon.Brandys, tomasz.zarna
Version: 3.5   
Target Milestone: 3.8 M4   
Hardware: All   
OS: Windows XP   
Whiteboard:
Bug Depends on: 340666    
Bug Blocks: 275153    
Attachments:
Description Flags
Fix setting LocalResourceSaveableComparison dirty.
none
Fixing refreshing in ContentMergeViewer
none
Fixing refreshing ContentMergeViewer with test
none
mylyn/context/zip none

Description Szymon Ptaszkiewicz CLA 2010-05-14 07:41:00 EDT
Current implementation of org.eclipse.compare.CompareEditorInput and org.eclipse.compare.contentmergeviewer.ContentMergeViewer blocks further improvements.

There is inconsistency in dealing with editing two files in compare editor and dirty states of those files. Currently when saving the compare editor on closing the editor both sides are saved. When we improve it to save only selected side on closing, after saving only one side, dirty flag of the ContentMergeViewer is set to false and the other side will not be saved - we lose changed content of the other side.
Comment 1 Tomasz Zarna CLA 2010-05-14 10:12:44 EDT
(In reply to comment #0)

> Currently when saving the compare editor on
> closing the editor both sides are saved. When we improve it to save only
> selected side on closing, after saving only one side, dirty flag of the
> ContentMergeViewer is set to false and the other side will not be saved - we
> lose changed content of the other side.

This has surfaced while working on bug 273450 (see patch attached to bug 273450, comment 11). 

It's caused by the update[1] that takes place when viewers are flushed. It resets the dirty flags for both sides, even when only one of them is being saved. So far I can see two possible solutions for that:
1) Make the update respect the dirty flag.
2) Update selectively, refreshing only the part that has been saved.

[1] org.eclipse.compare.contentmergeviewer.ContentMergeViewer.updateContent(Object, Object, Object)
Comment 2 Malgorzata Janczarska CLA 2011-03-31 10:17:13 EDT
Created attachment 192276 [details]
Fix setting LocalResourceSaveableComparison dirty.

This patch works only when patch attached to bug 340666 is applied.

SavablesCompareEditorInput has references to two savables representing left and right side of the comparison. It keeps dirty flag for each of them separately, but both savables are notified about dirty property changed each time any site has changed.
This results in false dirty status changes, when both savables are dirty and then:
a) one of the savables is saved (the main subject of this bug)
b) one of the savables dirty flag is changed to "false", for instance by "undo" (ctrl+Z)

This fix makes savables conscious if the "change dirty" notification applies to them or to the other savable. Then SavablesCompareEditorInput sets its dirty state depending on witch savable has changed.
Comment 3 Malgorzata Janczarska CLA 2011-04-12 12:33:53 EDT
Comment on attachment 192276 [details]
Fix setting LocalResourceSaveableComparison dirty.

Unfortunately after adding this patch the bug can be still reproduced. But it gets much better if fix for bug 340666 is applied.
Comment 4 Tomasz Zarna CLA 2011-04-13 12:02:52 EDT
(In reply to comment #3)
> But it gets much better if fix for bug 340666 is applied.

What cases does the fix not cover? Writing them down sounds like a good idea in case we go back to this bug in the future.
Comment 5 Malgorzata Janczarska CLA 2011-04-14 09:51:37 EDT
It's type-dependent: html, plain text, xml files still get the other site reset; java, properties, manifest files don't. Generally when StructureDiffViewer applies to this compare saving works fine.

From short debugging I discovered that while setting up the compare TextFileDocumentProvider.connect is called twice for each site when we deal with StructureDiffViewer and this prevents the right site from resetting later. For other DiffViewers connect is called only once for each site. Each connect/disconnect increases/decreases some counter on file info.
The actual reset is made when after saving one site we call SaveablesCompareEditorInput$InternalResourceSaveableComparison.fireInputChange, after saving the left site. It does a pair of disconnect/connect on each site. If a counter on file info goes to 1 this operation resets the file in view.
Comment 6 Malgorzata Janczarska CLA 2011-06-27 09:37:22 EDT
Moving this bug to 3.8 because it blocks another 3.8 bug.
Comment 7 Malgorzata Janczarska CLA 2011-10-10 08:41:57 EDT
*** Bug 360429 has been marked as a duplicate of this bug. ***
Comment 8 Malgorzata Janczarska CLA 2011-10-26 10:42:35 EDT
Created attachment 205988 [details]
Fixing refreshing in ContentMergeViewer

I found the problem.
When the first file is saved CompareInputChange is propagated, ContenMergeViewer refreshes on input change, this resets the second side.
ContentMesgeViewer#handleCompareInputChange already had some logic based on dirty state of both sides and isSaving (true if we are in the middle of saving), so I added another condition: if we are in the middle of saving and one of the sides still wasn't saved than we don't refresh, we'll do it when second side is saved.
It works for both: when StructureDiffViewer is used and when it's not, before that comparisons with StructureDiffViewer where resistant to this problem, see comment 5.
Comment 9 Malgorzata Janczarska CLA 2011-10-27 06:24:13 EDT
I suppose I should add some tests.
Comment 10 Tomasz Zarna CLA 2011-10-27 06:43:20 EDT
(In reply to comment #9)
> I suppose I should add some tests.

Tests are always welcome with open arms :)
Comment 11 Malgorzata Janczarska CLA 2011-10-31 07:04:04 EDT
Created attachment 206199 [details]
Fixing refreshing ContentMergeViewer with test

I added tests that verify saving both sides of Compare Editor for various file types. As said in comment 5 the problem didn't occur for some file types so some of the tests don't fail without the fix, but of course we should test them too.
I added separate test for each file type I considered as relevant, because if one of them fails we still know status of the others.
Comment 12 Malgorzata Janczarska CLA 2011-10-31 07:04:10 EDT
Created attachment 206200 [details]
mylyn/context/zip
Comment 13 Tomasz Zarna CLA 2011-11-23 12:05:43 EST
Fixed with 1bbe39d33b1ed5e57003e7e16b483d62633463f2. Available in builds >= N20111123-2000.