Skip to content

Commit

Permalink
Implement g:nvim_ghost_keep_buffers
Browse files Browse the repository at this point in the history
When set to 1, buffers won't be deleted when the connection is closed
from the browser side. This avoids any data loss in case of browser
crashes.
  • Loading branch information
subnut committed Mar 29, 2023
1 parent 6489f0a commit b811a7b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 22 deletions.
14 changes: 7 additions & 7 deletions autoload/nvim_ghost.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ endfunction
function! s:start_server_or_request_focus()
" If we start the server, we are already focused, so we don't need to
" request_focus separately
if ! nvim_ghost#helper#is_running()
call nvim_ghost#helper#start_server()
if ! nvim_ghost#server#is_running()
call nvim_ghost#server#start_server()
else
call nvim_ghost#helper#request_focus()
call nvim_ghost#server#request_focus()
endif
endfunction

function! nvim_ghost#disable()
call nvim_ghost#helper#session_closed()
call nvim_ghost#server#session_closed()
autocmd! nvim_ghost
if !exists('g:_nvim_ghost_supports_focus')
autocmd! _nvim_ghost_does_not_support_focus
Expand All @@ -41,8 +41,8 @@ function! nvim_ghost#enable(defer = 0)
if a:defer
autocmd UIEnter * call s:start_server_or_request_focus()
endif
autocmd FocusGained * call nvim_ghost#helper#request_focus()
autocmd VimLeavePre * call nvim_ghost#helper#session_closed()
autocmd FocusGained * call nvim_ghost#server#request_focus()
autocmd VimLeavePre * call nvim_ghost#server#session_closed()
augroup END

" :doau causes error if augroup not defined
Expand Down Expand Up @@ -72,7 +72,7 @@ function! nvim_ghost#enable(defer = 0)
let s:focused = v:true
fun! s:focus_gained()
if !s:focused
call nvim_ghost#helper#request_focus()
call nvim_ghost#server#request_focus()
let s:focused = v:true
endif
endfun
Expand Down
11 changes: 11 additions & 0 deletions autoload/nvim_ghost/helpers.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if !has('nvim')
finish
endif

function! nvim_ghost#helpers#buf_del(bufnum) abort
if !g:nvim_ghost_keep_buffers
exe "bdelete " .. a:bufnum
return
endif
exe a:bufnum .. "bufdo setl buftype="
endfunction
4 changes: 2 additions & 2 deletions autoload/nvim_ghost/installer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function! s:report_result(exitcode) abort
endfunction

function! nvim_ghost#installer#install(callback) abort
if nvim_ghost#helper#is_running()
call nvim_ghost#helper#kill_server()
if nvim_ghost#server#is_running()
call nvim_ghost#server#kill_server()
endif

echom '[nvim-ghost] Downloading binary'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ endif
let s:saved_updatetime = &updatetime
let s:can_use_cursorhold = v:false
let s:joblog_arguments = !g:nvim_ghost_logging_enabled ? {} : {
\'on_stdout':{id,data,type->nvim_ghost#helper#joboutput_logger(data,type)},
\'on_stderr':{id,data,type->nvim_ghost#helper#joboutput_logger(data,type)},
\'on_stdout':{id,data,type->nvim_ghost#server#joboutput_logger(data,type)},
\'on_stderr':{id,data,type->nvim_ghost#server#joboutput_logger(data,type)},
\}
let s:joblog_arguments_nokill = extend(copy(s:joblog_arguments), {
\'detach': v:true,
\'cwd': g:nvim_ghost_installation_dir,
\})

function! nvim_ghost#helper#is_running() abort " {{{1
function! nvim_ghost#server#is_running() abort " {{{1
let v:errmsg = ''
let l:url = s:localhost .. ':' .. $GHOSTTEXT_SERVER_PORT
let l:opts = #{ data_buffered: v:true }
Expand Down Expand Up @@ -76,10 +76,10 @@ function! s:send_GET_request(url) abort "{{{1
call chansend(l:connection, "\r\n")

" We're done, log what we sent
call nvim_ghost#helper#joboutput_logger(['Sent ' . a:url], '')
call nvim_ghost#server#joboutput_logger(['Sent ' . a:url], '')
endfunction

function! nvim_ghost#helper#start_server() abort " {{{1
function! nvim_ghost#server#start_server() abort " {{{1
if has('win32')
call jobstart(['cscript.exe',
\g:nvim_ghost_scripts_dir . 'start_server.vbs'])
Expand All @@ -95,19 +95,19 @@ function! nvim_ghost#helper#start_server() abort " {{{1
endif
endfunction

function! nvim_ghost#helper#kill_server() abort " {{{1
function! nvim_ghost#server#kill_server() abort " {{{1
call s:send_GET_request('/exit')
endfunction

function! nvim_ghost#helper#request_focus() abort " {{{1
function! nvim_ghost#server#request_focus() abort " {{{1
call s:send_GET_request('/focus?focus=' . v:servername)
endfunction

function! nvim_ghost#helper#session_closed() abort " {{{1
function! nvim_ghost#server#session_closed() abort " {{{1
call s:send_GET_request('/session-closed?session=' . v:servername)
call rpcnotify(0, "nvim_ghost_exit_event")
endfunction
function! nvim_ghost#helper#joboutput_logger(data,type) abort " {{{1
function! nvim_ghost#server#joboutput_logger(data,type) abort " {{{1
if !g:nvim_ghost_logging_enabled || g:nvim_ghost_super_quiet
return
endif
Expand Down
6 changes: 4 additions & 2 deletions binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from simple_websocket_server import WebSocket
from simple_websocket_server import WebSocketServer

BUILD_VERSION: str = "v0.5.1"
BUILD_VERSION: str = "v0.5.1-keepbuf.1"

WINDOWS: bool = os.name == "nt"
LOCALHOST: str = "127.0.0.1" if WINDOWS else "localhost"
Expand Down Expand Up @@ -370,7 +370,9 @@ def handle_close(self):
)

# Delete buffer and stop event loop
self.neovim_handle.command(f"bdelete {self.buffer_handle.number}")
self.neovim_handle.command(
f"call nvim_ghost#helpers#buf_del({self.buffer_handle.number})"
)
self.neovim_handle.close()
self.loop_neovim_handle.stop_loop()
self.loop_neovim_handle.close()
Expand Down
2 changes: 1 addition & 1 deletion binary_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.5.1
v0.5.1-keepbuf.1
1 change: 1 addition & 0 deletions plugin/nvim_ghost.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ endif
let g:nvim_ghost_autostart = get(g:,'nvim_ghost_autostart', 1)
let g:nvim_ghost_use_script = get(g:,'nvim_ghost_use_script', 0)
let g:nvim_ghost_super_quiet = get(g:,'nvim_ghost_super_quiet', 0)
let g:nvim_ghost_keep_buffers = get(g:,'nvim_ghost_keep_buffers', 0)
let g:nvim_ghost_logging_enabled = get(g:,'nvim_ghost_logging_enabled', 0)
let g:nvim_ghost_server_port = get(g:,'nvim_ghost_server_port', get(environ(),'GHOSTTEXT_SERVER_PORT', 4001))

Expand Down
2 changes: 1 addition & 1 deletion scripts/install_binary.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Remove-Item -ErrorAction:Ignore "$binary.version"

if (Test-Path "$binary") {
Write-Output "Binary still running"
Write-Output "Please run ':call nvim_ghost#helper#kill_server()' in neovim"
Write-Output "Please run ':call nvim_ghost#server#kill_server()' in neovim"
while (Test-Path "$binary") {
try { Remove-Item -ErrorAction:Stop "$binary" }
catch { Start-Sleep -Seconds 1 }
Expand Down

0 comments on commit b811a7b

Please sign in to comment.