Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to save file asynchronously? #56

Open
qsdrqs opened this issue Nov 23, 2020 · 5 comments
Open

Any way to save file asynchronously? #56

qsdrqs opened this issue Nov 23, 2020 · 5 comments

Comments

@qsdrqs
Copy link

qsdrqs commented Nov 23, 2020

Hello, I use InsertLeave to trigger the autosave function. However, the cursor always stuck when I leave insert mode.

Since vim 8 and neovim use job_start() and jobstart() to run command in asynchronous way, so I'm wondering is there any way in asynchronous can help for this problem?

@907th
Copy link
Owner

907th commented Nov 23, 2020

@qsdrqs Hello! Thank you for your question! Unfortunately, I'm not able to support this project anymore. But the source code of the plugin is very simple. It would be great if you try to implement and test the feature you asked by yourself. Please, create a PR if it will work. Have a good day!

@qsdrqs
Copy link
Author

qsdrqs commented Nov 23, 2020

Ok, I will have a try soon.

@Frydac
Copy link

Frydac commented Dec 1, 2020

Hey,

I was having some issues with this with respect to 2 usecases:

  • plugin https://github.com/terryma/vim-multiple-cursors, which seems to trigger a change event each character entered, which especially when using many cursors in a bigger file, make it quite slow. Also because writing the file would trigger my completion engine to reparse the file, and I have semantic highlighting that would get updated after each write.
  • another issue I had was with one particular file that was specifying a database of values in a big C source file (40k+ lines), and my completion engine would reparse it all the time even when I was editing another file while this file was in another buffer open (not sure why it was doing that, but still), which uses a lot of cpu, and I'm speculating it would trigger some communication of a huge completion database (I think) between the completer executable and vim, which seemed to be blocking (is vim really multithreaded? could be something else though).

A little bit of research seems to indicate that job_start/jobstart is not the correct tool to asynchronously trigger a vim command such as the saving of a buffer, it seems to be only for triggering external commands/executables asynchronously.
Also in my case I was thinking if there was a maximum frequency of saving the file, of say 5 seconds or so, that would lessen the vim-multiple-cursors issue.

One way I could think of, based on very little vimscript knowledge and not very well researched atm though, to accomplish this asynchronous write, is to use vim's timer_start() functionality to start some 'worker thread' that checks if an event has occurred for a particular buffer, and then saves that buffer (or all buffers depending on the autosave settings).
The autosave events could, for example, add the name of the buffer they are triggered for to a queue that is shared with and checked in the 'worker thread' every so often.
Although I'm not sure how this timer works, is it really in another thread or would it be blocking anyway? And if it is in another thread, is it even possible to have a shared queue that is 'threadsafe'? Also this could introduce a number of issues that should be handled.. for example: when you switch to a shell you'd expect the files to be saved before compiling/interpreting the source code you just edited, and not have to wait a second or 2 (depending on the setting), so maybe also force writing on FocusLost event or something (though iirc that this FocusLost only works in gvim, not in the shell).
But yeah, just a thought that might inspire someone (maybe me later, I have a some holidays coming up).

@907th
Copy link
Owner

907th commented Dec 2, 2020

@Frydac great comment!

@qsdrqs
Copy link
Author

qsdrqs commented Dec 18, 2020

Hey,

I was having some issues with this with respect to 2 usecases:

* plugin https://github.com/terryma/vim-multiple-cursors, which seems to trigger a change event each character entered, which especially when using many cursors in a bigger file, make it quite slow. Also because writing the file would trigger my completion engine to reparse the file, and I have semantic highlighting that would get updated after each write.

* another issue I had was with one particular file that was specifying a database of values in a big C source file (40k+ lines), and my completion engine would reparse it all the time even when I was editing another file while this file was in another buffer open (not sure why it was doing that, but still), which uses a lot of cpu, and I'm speculating it would trigger some communication of a huge completion database (I think) between the completer executable and vim, which seemed to be blocking (is vim really multithreaded? could be something else though).

A little bit of research seems to indicate that job_start/jobstart is not the correct tool to asynchronously trigger a vim command such as the saving of a buffer, it seems to be only for triggering external commands/executables asynchronously.
Also in my case I was thinking if there was a maximum frequency of saving the file, of say 5 seconds or so, that would lessen the vim-multiple-cursors issue.

One way I could think of, based on very little vimscript knowledge and not very well researched atm though, to accomplish this asynchronous write, is to use vim's timer_start() functionality to start some 'worker thread' that checks if an event has occurred for a particular buffer, and then saves that buffer (or all buffers depending on the autosave settings).
The autosave events could, for example, add the name of the buffer they are triggered for to a queue that is shared with and checked in the 'worker thread' every so often.
Although I'm not sure how this timer works, is it really in another thread or would it be blocking anyway? And if it is in another thread, is it even possible to have a shared queue that is 'threadsafe'? Also this could introduce a number of issues that should be handled.. for example: when you switch to a shell you'd expect the files to be saved before compiling/interpreting the source code you just edited, and not have to wait a second or 2 (depending on the setting), so maybe also force writing on FocusLost event or something (though iirc that this FocusLost only works in gvim, not in the shell).
But yeah, just a thought that might inspire someone (maybe me later, I have a some holidays coming up).

Hello Frydac! I have made some experiments about timer_start(). Unfortunately, it only wait for several time asynchronously and still call the function in sync way. You can add sleep 5 in Do_Save() function then you can find out it still run in sync way.

I asked for a vim community about this question. However, They told me that there is no way to save file in async way because it would be unsafe if people change their file when saving. Thus there may be not any way to implement it. Sorry about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants