From 283bc9e13713a8321e4c33360565856da7be16a7 Mon Sep 17 00:00:00 2001 From: Bhakta Raghavan Date: Mon, 6 Jul 2020 23:11:53 +0530 Subject: [PATCH 1/3] Ability to re-load a file from remote This pull request adds an option called "Reload From Remote" to the file context menu. If the buffer is open, it will be closed (appropriate warning displayed if there are local changes). Once close, the file will be opened again Test-cases: Test-case-1: Open remote tree view (but file is not yet opened), right click on file and "Reload From Remote" Expected outcome: Since file is not open, nothing happens (i.e this will not result in opening the file) Result: Pass Test-case-2a: Open a remote file, then, without making any local change - do "Reload From Remote" Expected outcome: File should reload Result: Pass Test-case-2b: Open a remote file, then make a change on remote. Don't make any local change - do "Reload From Remote" Expected outcome: File should reload and the remote changed content should be visible Result: Pass Test-case-4a: Open a remote file, Make a local change - do "Reload From Remote". A dialog box will appear indicating that "you will lose your local changes if you reload from remote". Press No Expected outcome: Nothing happens, the file is not reloaded the local buffer remains Result: Pass Test-case-4b: Open a remote file, Make a local change - do "Reload From Remote". A dialog box will appear indicating that "you will lose your local changes if you reload from remote". Press Yes Expected outcome: Local buffer is closed (lost) and remote content is loaded. Result: Pass --- lib/ftp-remote-edit-plus.js | 88 +++++++++++++++++++++++++++++++++++++ menus/ftp-remote-edit.json | 4 ++ 2 files changed, 92 insertions(+) diff --git a/lib/ftp-remote-edit-plus.js b/lib/ftp-remote-edit-plus.js index 1d37157..9225888 100755 --- a/lib/ftp-remote-edit-plus.js +++ b/lib/ftp-remote-edit-plus.js @@ -60,6 +60,7 @@ class FtpRemoteEdit { 'ftp-remote-edit:new-directory': () => self.newDirectory(), 'ftp-remote-edit:delete': () => self.delete(), 'ftp-remote-edit:rename': () => self.rename(), + 'ftp-remote-edit:reload': () => self.reload(), 'ftp-remote-edit:copy': () => self.copy(), 'ftp-remote-edit:cut': () => self.cut(), 'ftp-remote-edit:paste': () => self.paste(), @@ -394,6 +395,93 @@ class FtpRemoteEdit { } } + // Reload From Remote + // This function will attempt to re-load the file from remote + // This is particularly useful if you are editing the file on remote + // but you want local copy to refresh. Closing and opening is definitely + // a way but reload makes it a bit easier. + reload() { + const self = this; + const selected = self.treeView.list.find('.selected'); + + if (selected.length === 0) return; + + if (selected) { + // We care about files only. The same cam be implemented at the folder level - later + if (!selected.view().is('.directory')) { + let file = selected.view(); + if (file) { + // get the textEditor object, if the file is open in a pane, we will + // get a textEditor object, else null + const textEditor = file.getTextEditor(file.getLocalPath(true) + file.name); + + if (self.debug) { + console.log("Reload file ", file.getLocalPath(true) + file.name, "textEditor = ", textEditor); + } + + if (textEditor) { + // We want to reaload, unless user says No + let doReload = true; + + // Find if the buffer is modified (i.e. there is a local change) + // If there is no change, we can simply proceed with reload + // If there is change, warn user that the local changes would + // be lost + const modified = textEditor.isModified(); + if (modified) { + doReload = atom.confirm({ + message: 'Do you want to Reload from Remote with local changes?', + detailedMessage: `You will lose your local changes if you choose Yes`, + buttons: { + Yes: () => true, + No: () => false, + }, + }); + } + if (!doReload) { + // User says don't reload - so just go away.. + if (self.debug) { + console.log("User cancelled reload"); + } + return; + } + + // Now that we want to reload, get hold of the window pane. + pane = atom.workspace.paneForItem(textEditor); + if (pane) { + if (self.debug) { + console.log("Go ahead and destroy the pane ", pane); + } + // We have user's approval to nuke the pane (without having it + // ask for confirmation). + const forceDestroyWithoutConfirmation = true; + pane.destroyItem(textEditor, forceDestroyWithoutConfirmation) + .then((destroyed) => { + if (self.debug) { + console.log("File Closed status = ", destroyed); + } + if (destroyed) { + // Pane has been destroyed - go ahead and open the file again + if (self.debug) { + console.log("file Closed - open it again"); + } + file.open(); + } + }) + .catch(function (err) { + console.log("Error closing file", err); + }); + } else { + // Not sure if there is a case where the pane would be absent + // Irrespective, opeh the file + file.open(); + } + } + } + } + } + }; + rename() { const self = this; const selected = self.treeView.list.find('.selected'); diff --git a/menus/ftp-remote-edit.json b/menus/ftp-remote-edit.json index 594c580..b505c4c 100755 --- a/menus/ftp-remote-edit.json +++ b/menus/ftp-remote-edit.json @@ -113,6 +113,10 @@ "label": "Rename", "command": "ftp-remote-edit:rename" }, + { + "label": "Reload From Remote", + "command": "ftp-remote-edit:reload" + }, { "label": "Delete", "command": "ftp-remote-edit:delete" From 3f0501a92bd4b9e6ad9ceaa8d8d115ba82fa4bd8 Mon Sep 17 00:00:00 2001 From: Bhakta Raghavan Date: Mon, 6 Jul 2020 23:16:02 +0530 Subject: [PATCH 2/3] fix typo and add a console log in error case --- lib/ftp-remote-edit-plus.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ftp-remote-edit-plus.js b/lib/ftp-remote-edit-plus.js index 9225888..68fbd51 100755 --- a/lib/ftp-remote-edit-plus.js +++ b/lib/ftp-remote-edit-plus.js @@ -466,6 +466,8 @@ class FtpRemoteEdit { console.log("file Closed - open it again"); } file.open(); + } else { + console.log("error- window pane could not be destroyed.."); } }) .catch(function (err) { @@ -473,7 +475,7 @@ class FtpRemoteEdit { }); } else { // Not sure if there is a case where the pane would be absent - // Irrespective, opeh the file + // Irrespective, open the file file.open(); } } From 4da19bd372194afa36f060937f528a6b666c709a Mon Sep 17 00:00:00 2001 From: Bhakta Raghavan Date: Tue, 7 Jul 2020 11:33:16 +0530 Subject: [PATCH 3/3] fix comments and typos --- lib/ftp-remote-edit-plus.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ftp-remote-edit-plus.js b/lib/ftp-remote-edit-plus.js index 68fbd51..3dacbb6 100755 --- a/lib/ftp-remote-edit-plus.js +++ b/lib/ftp-remote-edit-plus.js @@ -420,7 +420,7 @@ class FtpRemoteEdit { } if (textEditor) { - // We want to reaload, unless user says No + // We want to reload, unless user says No let doReload = true; // Find if the buffer is modified (i.e. there is a local change) @@ -458,12 +458,12 @@ class FtpRemoteEdit { pane.destroyItem(textEditor, forceDestroyWithoutConfirmation) .then((destroyed) => { if (self.debug) { - console.log("File Closed status = ", destroyed); + console.log("Pane Closed status = ", destroyed); } if (destroyed) { // Pane has been destroyed - go ahead and open the file again if (self.debug) { - console.log("file Closed - open it again"); + console.log("pane closed - open the file again"); } file.open(); } else {