Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Sequential navigation in edit history #13418

Merged
merged 15 commits into from
Jun 28, 2017
Prev Previous commit
Next Next commit
Additional checks to validate the navigation frames
  • Loading branch information
swmitra committed Jun 27, 2017
commit 167e4dd029335834f86bc93bae4249cc647b4156
39 changes: 23 additions & 16 deletions src/extensions/default/NavigationAndHistory/NavigationProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ define(function (require, exports, module) {
}

/**
* Function to check existence of a file entry
* Function to check existence of a file entry, validity of markers
* @private
*/
function _checkIfExist(entry) {
function _validateFrame(entry) {
var deferred = new $.Deferred(),
fileEntry = FileSystem.getFileForPath(entry.file);

Expand All @@ -110,7 +110,14 @@ define(function (require, exports, module) {
} else {
fileEntry.exists(function (err, exists) {
if (!err && exists) {
deferred.resolve();
// Additional check to handle external modification and mutation of the doc text affecting markers
if (fileEntry._stat !== entry.fileStat) {
deferred.reject();
} else if (!entry._validateMarkers()) {
deferred.reject();
} else {
deferred.resolve();
}
} else {
deferred.reject();
}
Expand All @@ -128,6 +135,7 @@ define(function (require, exports, module) {
this.file = editor.document.file._path;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might call this filepath or just path to make it clear that this is not a file handle.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. "filePath" would be more logical.

this.inMem = editor.document.file.constructor.name === "InMemoryFile";
this.paneId = editor._paneId;
this.fileStat = editor.document.file._stat;
this.uId = (new Date()).getTime();
this.selections = [];
this.bookMarkIds = [];
Expand Down Expand Up @@ -345,17 +353,15 @@ define(function (require, exports, module) {
// if true, jump again
if (navFrame && navFrame === currentEditPos) {
jumpedPosStack.push(navFrame);
CommandManager.execute(NAVIGATION_JUMP_BACK);
return;
} else if (navFrame && !navFrame._validateMarkers()) {
_validateNavigationCmds();
CommandManager.execute(NAVIGATION_JUMP_BACK);
return;
}

if (navFrame) {
// We will check for the file existence now, if it doesn't exist we will jump back again
// but discard the popped frame as invalid.
_checkIfExist(navFrame).done(function () {
_validateFrame(navFrame).done(function () {
jumpedPosStack.push(navFrame);
navFrame.goTo();
currentEditPos = navFrame;
Expand All @@ -377,17 +383,15 @@ define(function (require, exports, module) {
// if true, jump again
if (navFrame && navFrame === currentEditPos) {
jumpToPosStack.push(navFrame);
CommandManager.execute(NAVIGATION_JUMP_FWD);
return;
} else if (navFrame && !navFrame._validateMarkers()) {
_validateNavigationCmds();
CommandManager.execute(NAVIGATION_JUMP_FWD);
return;
}

if (navFrame) {
// We will check for the file existence now, if it doesn't exist we will jump back again
// but discard the popped frame as invalid.
_checkIfExist(navFrame).done(function () {
_validateFrame(navFrame).done(function () {
jumpToPosStack.push(navFrame);
navFrame.goTo();
currentEditPos = navFrame;
Expand Down Expand Up @@ -461,22 +465,22 @@ define(function (require, exports, module) {
}

/**
* Removes all frames from backward navigation stack for the given file.
* Removes all frames from backward navigation stack for the given file only if the file is changed on disk.
* @private
*/
function _removeBackwardFramesForFile(file) {
jumpToPosStack = jumpToPosStack.filter(function (frame) {
return frame.file !== file._path;
return frame.file !== file._path && frame.stat !== file._stat;
});
}

/**
* Removes all frames from forward navigation stack for the given file.
* Removes all frames from forward navigation stack for the given file only if the file is changed on disk.
* @private
*/
function _removeForwardFramesForFile(file) {
jumpedPosStack = jumpedPosStack.filter(function (frame) {
return frame.file !== file._path;
return frame.file !== file._path && frame.stat !== file._stat;
});
}

Expand Down Expand Up @@ -534,14 +538,17 @@ define(function (require, exports, module) {
function _initHandlers() {
EditorManager.on("activeEditorChange", _handleActiveEditorChange);
ProjectManager.on("projectOpen", _handleProjectOpen);
Document.on("_documentRefreshed", _handleExternalChange);
EditorManager.on("_fullEditorCreatedForDocument", function (event, document, editor) {
_handleExternalChange(event, {file: document.file});
_reinstateMarkers(editor, jumpToPosStack);
_reinstateMarkers(editor, jumpedPosStack);
});
FileSystem.on("change", function (event, entry) {
_handleExternalChange(event, {file: entry});
});
Document.on("_documentRefreshed", function (event, doc) {
_handleExternalChange(event, {file: doc.file});
});
}

function init() {
Expand Down