/* We need to fork the change tracker so that we can "mark" the moment
in history when we took the contents from the source doc, so that
if the document is edited while the save is in progress we don't
reset the true change tracker's state to a version we haven't
actually sent to the server. */
final ChangeTracker thisChangeTracker = changeTracker_.fork();
final String newContents = docDisplay_.getCode();
String oldContents = sourceDoc_.getContents();
final String hash = sourceDoc_.getHash();
final String foldSpec = Fold.encode(Fold.flatten(docDisplay_.getFolds()));
String oldFoldSpec = sourceDoc_.getFoldSpec();
//String patch = DiffMatchPatch.diff(oldContents, newContents);
SubstringDiff diff = new SubstringDiff(oldContents, newContents);
// Don't auto-save when there are no changes. In addition to being
// wasteful, it causes the server to think the document is dirty.
if (path == null && fileType == null && diff.isEmpty()
&& foldSpec.equals(oldFoldSpec))
{
changesPending_ = false;
return false;
}
if (path == null && fileType == null
&& oldContents.length() == 0
&& newContents.equals("\n"))
{
// This is necessary due to us adding an extra \n to empty
// documents, which we have to do or else CodeMirror starts
// acting funny. If we add the extra \n but don't do this
// check, then reloading the browser causes empty documents
// to appear dirty.
changesPending_ = false;
return false;
}
server_.saveDocumentDiff(
sourceDoc_.getId(),
path,
fileType,
encoding,
foldSpec,
diff.getReplacement(),
diff.getOffset(),
diff.getLength(),
hash,
new ServerRequestCallback<String>()
{
@Override
public void onError(ServerError error)
{
Debug.logError(error);
if (progress != null)
progress.onError(error.getUserMessage());
changesPending_ = false;
}
@Override
public void onResponseReceived(String newHash)
{
if (newHash != null)
{
// If the document hasn't changed further since the version
// we saved, then we know we're all synced up.
if (!thisChangeTracker.hasChanged())
changeTracker_.reset();
onSuccessfulUpdate(newContents,
newHash,
path,