Skip to content

Commit b811a7b

Browse files
committed
Implement g:nvim_ghost_keep_buffers
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.
1 parent 6489f0a commit b811a7b

File tree

8 files changed

+36
-22
lines changed

8 files changed

+36
-22
lines changed

autoload/nvim_ghost.vim

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ endfunction
1616
function! s:start_server_or_request_focus()
1717
" If we start the server, we are already focused, so we don't need to
1818
" request_focus separately
19-
if ! nvim_ghost#helper#is_running()
20-
call nvim_ghost#helper#start_server()
19+
if ! nvim_ghost#server#is_running()
20+
call nvim_ghost#server#start_server()
2121
else
22-
call nvim_ghost#helper#request_focus()
22+
call nvim_ghost#server#request_focus()
2323
endif
2424
endfunction
2525

2626
function! nvim_ghost#disable()
27-
call nvim_ghost#helper#session_closed()
27+
call nvim_ghost#server#session_closed()
2828
autocmd! nvim_ghost
2929
if !exists('g:_nvim_ghost_supports_focus')
3030
autocmd! _nvim_ghost_does_not_support_focus
@@ -41,8 +41,8 @@ function! nvim_ghost#enable(defer = 0)
4141
if a:defer
4242
autocmd UIEnter * call s:start_server_or_request_focus()
4343
endif
44-
autocmd FocusGained * call nvim_ghost#helper#request_focus()
45-
autocmd VimLeavePre * call nvim_ghost#helper#session_closed()
44+
autocmd FocusGained * call nvim_ghost#server#request_focus()
45+
autocmd VimLeavePre * call nvim_ghost#server#session_closed()
4646
augroup END
4747

4848
" :doau causes error if augroup not defined
@@ -72,7 +72,7 @@ function! nvim_ghost#enable(defer = 0)
7272
let s:focused = v:true
7373
fun! s:focus_gained()
7474
if !s:focused
75-
call nvim_ghost#helper#request_focus()
75+
call nvim_ghost#server#request_focus()
7676
let s:focused = v:true
7777
endif
7878
endfun

autoload/nvim_ghost/helpers.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if !has('nvim')
2+
finish
3+
endif
4+
5+
function! nvim_ghost#helpers#buf_del(bufnum) abort
6+
if !g:nvim_ghost_keep_buffers
7+
exe "bdelete " .. a:bufnum
8+
return
9+
endif
10+
exe a:bufnum .. "bufdo setl buftype="
11+
endfunction

autoload/nvim_ghost/installer.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function! s:report_result(exitcode) abort
1313
endfunction
1414

1515
function! nvim_ghost#installer#install(callback) abort
16-
if nvim_ghost#helper#is_running()
17-
call nvim_ghost#helper#kill_server()
16+
if nvim_ghost#server#is_running()
17+
call nvim_ghost#server#kill_server()
1818
endif
1919

2020
echom '[nvim-ghost] Downloading binary'
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ endif
1111
let s:saved_updatetime = &updatetime
1212
let s:can_use_cursorhold = v:false
1313
let s:joblog_arguments = !g:nvim_ghost_logging_enabled ? {} : {
14-
\'on_stdout':{id,data,type->nvim_ghost#helper#joboutput_logger(data,type)},
15-
\'on_stderr':{id,data,type->nvim_ghost#helper#joboutput_logger(data,type)},
14+
\'on_stdout':{id,data,type->nvim_ghost#server#joboutput_logger(data,type)},
15+
\'on_stderr':{id,data,type->nvim_ghost#server#joboutput_logger(data,type)},
1616
\}
1717
let s:joblog_arguments_nokill = extend(copy(s:joblog_arguments), {
1818
\'detach': v:true,
1919
\'cwd': g:nvim_ghost_installation_dir,
2020
\})
2121

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

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

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

98-
function! nvim_ghost#helper#kill_server() abort " {{{1
98+
function! nvim_ghost#server#kill_server() abort " {{{1
9999
call s:send_GET_request('/exit')
100100
endfunction
101101

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

106-
function! nvim_ghost#helper#session_closed() abort " {{{1
106+
function! nvim_ghost#server#session_closed() abort " {{{1
107107
call s:send_GET_request('/session-closed?session=' . v:servername)
108108
call rpcnotify(0, "nvim_ghost_exit_event")
109109
endfunction
110-
function! nvim_ghost#helper#joboutput_logger(data,type) abort " {{{1
110+
function! nvim_ghost#server#joboutput_logger(data,type) abort " {{{1
111111
if !g:nvim_ghost_logging_enabled || g:nvim_ghost_super_quiet
112112
return
113113
endif

binary.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from simple_websocket_server import WebSocket
1919
from simple_websocket_server import WebSocketServer
2020

21-
BUILD_VERSION: str = "v0.5.1"
21+
BUILD_VERSION: str = "v0.5.1-keepbuf.1"
2222

2323
WINDOWS: bool = os.name == "nt"
2424
LOCALHOST: str = "127.0.0.1" if WINDOWS else "localhost"
@@ -370,7 +370,9 @@ def handle_close(self):
370370
)
371371

372372
# Delete buffer and stop event loop
373-
self.neovim_handle.command(f"bdelete {self.buffer_handle.number}")
373+
self.neovim_handle.command(
374+
f"call nvim_ghost#helpers#buf_del({self.buffer_handle.number})"
375+
)
374376
self.neovim_handle.close()
375377
self.loop_neovim_handle.stop_loop()
376378
self.loop_neovim_handle.close()

binary_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.5.1
1+
v0.5.1-keepbuf.1

plugin/nvim_ghost.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ endif
1010
let g:nvim_ghost_autostart = get(g:,'nvim_ghost_autostart', 1)
1111
let g:nvim_ghost_use_script = get(g:,'nvim_ghost_use_script', 0)
1212
let g:nvim_ghost_super_quiet = get(g:,'nvim_ghost_super_quiet', 0)
13+
let g:nvim_ghost_keep_buffers = get(g:,'nvim_ghost_keep_buffers', 0)
1314
let g:nvim_ghost_logging_enabled = get(g:,'nvim_ghost_logging_enabled', 0)
1415
let g:nvim_ghost_server_port = get(g:,'nvim_ghost_server_port', get(environ(),'GHOSTTEXT_SERVER_PORT', 4001))
1516

scripts/install_binary.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Remove-Item -ErrorAction:Ignore "$binary.version"
1111

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

0 commit comments

Comments
 (0)