Skip to content

Commit

Permalink
Merge pull request #8 from bhakta0007/reload-from-remote
Browse files Browse the repository at this point in the history
Ability to re-load a file from remote
  • Loading branch information
vukbgit authored Jul 7, 2020
2 parents de86924 + 4da19bd commit 1620856
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
90 changes: 90 additions & 0 deletions lib/ftp-remote-edit-plus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -394,6 +395,95 @@ 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 reload, 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("Pane Closed status = ", destroyed);
}
if (destroyed) {
// Pane has been destroyed - go ahead and open the file again
if (self.debug) {
console.log("pane closed - open the file again");
}
file.open();
} else {
console.log("error- window pane could not be destroyed..");
}
})
.catch(function (err) {
console.log("Error closing file", err);
});
} else {
// Not sure if there is a case where the pane would be absent
// Irrespective, open the file
file.open();
}
}
}
}
}
};

rename() {
const self = this;
const selected = self.treeView.list.find('.selected');
Expand Down
4 changes: 4 additions & 0 deletions menus/ftp-remote-edit.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 1620856

Please sign in to comment.