Pushing an edit requires leaving editor
I am using rlx edit to edit a file with vim. If there is an error (json syntax error for example), then it fails to push the json and the file is lost. It would be great if the file got pushed to couch on save. In addition to solving the lost file problem it would also let you keep your undo history in the editor. Not sure how this would work or if it is possible - but it's a problem I am running into as I try and use rlx.
Thanks for the feedback, I'll need to check this one out. I recall I had some linting logic in the initial implementation...
The edit function is a little odd because I wanted to know whether the file has actually changed or not so the file is hashed and then the comparison is performed on the hash of the edited document to determine whether the file should be sent.
I think maybe I should keep a copy of N file edits in the program settings directory and then maybe you could recall using a last-edit command or something like that.
Let me think on it a little but first I'll reproduce the issue here. Also I recommend getting a JSON lint tool integrated with vim - that will fix most of the problem by itself ;)
Thinking about your comment more, I could use fs.watchFile() compare the hashes and if they differ send the file to couch, could be quite neat but I think this would need to be enabled by a setting as I often run into low bandwidth connections on my travels so I was judicious about not using much bandwidth.
Note that on JSON error, couchdb would return an error and the file would not be saved, as the editor is still open you would not know this at the time. Seems like it could be quite confusing behaviour to me.
Is this kind of implementation something you would like me to look into more?
Ok then, another approach could be that I try to configure the vim environment such that you could send to couchdb from vim, something like:
:!rlx-save
Would send the current document to be saved? As it would just execute a program, errors should be reported within vim ...
When I run :! commands I get the 'Press ENTER to continue' after command execution but this seems like a better integration than something behind the scenes (the fs.watch idea) where it would be difficult to report errors.
Having just reproduced the issue:
rlx ⚡ ⌘ localhost/quotes> doc edit -i gone
rlx | unknown database error "{\n \"error\": \"bad_request\",\n \"reason\": \"invalid_json\"\n}"
rlx ⚡ ⌘ localhost/quotes> doc edit -i gone
rlx | no changes detected, aborting
rlx ⚡ ⌘ localhost/quotes>
Maybe it's as simple as storing the document sent in a cached file and when a non-2xx status is returned on save, presenting a question like: 'Save failed! Restore document? (y/n)'. I don't imagine that adding a history of edits would be that beneficial maybe just the last document edited?
Clearly I didn't implement the JSON lint logic that was in the original bash implementation too, I think this is even cleaner I do a JSON.parse() and if that fails show: 'Invalid JSON! Restore document? (y/n)'.
Any preference from your perspective, I think this last one is best, fairly easy to implement and prevents the call to the server to find out the JSON is invalid. Although, I would need to be sure it is a JSON document being edited, IIRC there is a generic edit command that may be used to edit any type of document. So this would need to be specific to doc edit.
I think the last option is a good workaround. We lose undo history, but at least we don't lose the doc. !rlx-save fixes this but is definitely a more advanced and un-intuitive command (that I would totally use).
On Fri, Feb 5, 2016 at 5:56 PM, muji [email protected] wrote:
Clearly I didn't implement the JSON lint logic that was in the original bash implementation too, I think this is even cleaner I do a JSON.parse() and if that fails show: 'Invalid JSON! Restore document? (y/n)'.
Any preference from your perspective, I think this last one is best, fairly easy to implement and prevents the call to the server to find out the JSON is invalid. Although, I would need to be sure it is a JSON document being edited, IIRC there is a generic edit command that may be used to edit any type of document. So this would need to be specific to doc edit.
— Reply to this email directly or view it on GitHub https://github.com/tmpfs/rlx/issues/8#issuecomment-180388925.