Skip to content

Commit

Permalink
update init mechanism to support remote sessions. use lua to write th…
Browse files Browse the repository at this point in the history
…e init script to a remote temp file and then source it.
  • Loading branch information
Yatao Li committed Sep 13, 2021
1 parent fc875b0 commit e72abf8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
6 changes: 3 additions & 3 deletions Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ let buildAvaloniaApp() =
//.With(new X11PlatformOptions(UseDeferredRendering=false))
//.With(new MacOSPlatformOptions(ShowInDock=true))

let startMainWindow app serveropts =
let startMainWindow app opts =
let app = app()
model.Start serveropts
model.Start opts

let cfg = config.load()
let cwd = Environment.CurrentDirectory |> Path.GetFullPath
Expand Down Expand Up @@ -127,6 +127,6 @@ let main(args: string[]) =
| Setup -> setup()
| Uninstall -> uninstall()
| Daemon(pipe, nvim, enc) -> daemon pipe nvim enc
| Start(a,b) -> startMainWindow app (a,b)
| Start(opts,norc,remote) -> startMainWindow app (opts,norc,remote)
with
| ex -> startCrashReportWindow app ex
23 changes: 8 additions & 15 deletions fvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,17 @@ function! s:fvim_on_vim_enter()
if exists("g:gui_widgets")
call GuiWidgetClientAttach(g:fvim_channel)
endif
delcommand FVimOnVimEnter
let g:fvim_ginit_complete = v:true
endfunction

function FVimTestGuiWidget()
" :put =string(nvim_get_namespaces())
" {'lsp_cxx_hl_symbols_11_1': 11, 'coc-color': 13, 'vim_lsp_references': 3,
" 'coc-codelens': 12, 'vim_lsp_diagnostics': 4, 'GuiWidget': 1,
" 'lsp_cxx_hl_symbols_11_0': 10, 'lsp_cxx_hl_skipped_11_0': 8,
" 'lsp_cxx_hl_skipped_11_1': 9, 'vim_lsp_signs': 5, 'hlyank': 2,
" 'treesitter/highlighter': 6, 'coc-diagnosticlua': 7}
"
" :put =string(nvim_buf_get_extmarks(11,12,0,-1,{})
" [[258, 62, 0], [261, 64, 0], [254, 67, 0], [256, 72, 0], [262, 74, 0],
" [257, 75, 0], [253, 76, 0], [264, 77, 0], [260, 78, 0], [263, 79, 0],
" [255, 80, 0], [259, 84, 0]]

let w1 = GuiWidgetPut("F:/test/1.png","image/png")
let w2 = GuiWidgetPut("F:/test/2.png","image/png")
call GuiWidgetPlace(w1, 0, 2, 0, 20, 5)
call GuiWidgetPlace(w2, 0, 7, 0, 20, 5)
call GuiWidgetUpdateView(0)
endfunction

command! -nargs=0 FVimOnVimEnter call s:fvim_on_vim_enter()

augroup FVim
autocmd BufWinEnter * call s:fvim_on_bufwinenter()
autocmd WinEnter * call s:fvim_on_winenter()
Expand All @@ -107,3 +93,10 @@ augroup FVim
autocmd CursorHold * call s:fvim_on_cursorhold()
autocmd CursorHoldI * call s:fvim_on_cursorhold()
augroup END

" trigger upon VimEnter
if v:vim_did_enter
call s:fvim_on_vim_enter()
else
autocmd VimEnter * call s:fvim_on_vim_enter()
endif
22 changes: 11 additions & 11 deletions getopt.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ServerOptions =
| FVimRemote of serverName: string option * transport: FVimRemoteTransport * verb: FVimRemoteVerb * files: string list

type Intent =
| Start of serveropts: ServerOptions * norc: bool
| Start of serveropts: ServerOptions * norc: bool * remoteinit: bool
| Setup
| Uninstall
| Daemon of pipe: string option * nvim: string * stderrenc: System.Text.Encoding
Expand Down Expand Up @@ -114,7 +114,7 @@ let parseOptions (args: string[]) =
elif uninstall then Uninstall
elif daemon then Daemon(pipe, nvim, enc)
else
let serveropts =
let serveropts, remoteinit =
match fvr, nvr with
| Some fvrVerb, _ ->
let transport =
Expand All @@ -131,23 +131,23 @@ let parseOptions (args: string[]) =
match verb with
| NewSession _ -> []
| _ -> argsL
FVimRemote(pipe, transport, verb, files)
FVimRemote(pipe, transport, verb, files),true
| _, Some nvrAddr ->
match nvrAddr.Split(":") with
| [| ParseIp ipaddr; ParseUInt16 port |] -> NeovimRemote(Tcp <| IPEndPoint(ipaddr, int port), argsL)
| _ -> NeovimRemote(NamedPipe nvrAddr, argsL)
| [| ParseIp ipaddr; ParseUInt16 port |] -> NeovimRemote(Tcp <| IPEndPoint(ipaddr, int port), argsL),true
| _ -> NeovimRemote(NamedPipe nvrAddr, argsL),true
| None, None ->
let prog, args =
let prog, args, r =
if wsl then
"wsl", ["bash"; "-l"; "-c"; sprintf "nvim --embed %s" (args |> escapeArgs |> join)]
"wsl", ["bash"; "-l"; "-c"; sprintf "nvim --embed %s" (args |> escapeArgs |> join)], true
elif ssh.IsSome then
"ssh", [ssh.Value; nvim; "--embed"] @ argsL
"ssh", [ssh.Value; nvim; "--embed"] @ argsL, true
else
nvim, ["--embed"] @ argsL
nvim, ["--embed"] @ argsL, false
in
Embedded(prog, args, enc)
Embedded(prog, args, enc),r
in
Start(serveropts, norc)
Start(serveropts, norc, remoteinit)

{
logToStdout = trace_to_stdout
Expand Down
35 changes: 30 additions & 5 deletions model.fs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ let private UpdateUICapabilities() =
/// <summary>
/// Call this once at initialization.
/// </summary>
let Start (serveropts, norc) =
let Start (serveropts, norc, remote) =
trace "starting neovim instance..."
trace "opts = %A" serveropts
nvim.start serveropts
Expand Down Expand Up @@ -504,15 +504,40 @@ let Start (serveropts, norc) =

if not norc then
let! _ = nvim.set_var "fvim_channel" myChannel
// !!! breaks remote execution
let fvimPath =
Assembly.GetExecutingAssembly().Location
|> Path.GetDirectoryName
let scriptPath = Path.Combine(fvimPath, "fvim.vim")
trace "script path: %A" scriptPath
let! _ = nvim.command ("source " + scriptPath)
// trigger upon VimEnter
let! _ = nvim.command "if v:vim_did_enter | execute \"FVimOnVimEnter\" | else | execute \"autocmd VimEnter * execute \\\"FVimOnVimEnter\\\"\" | endif"
if remote then
let! scriptText = File.ReadAllTextAsync(scriptPath) |> Async.AwaitTask
let luaText = $"""local uv = vim.loop
local txt = [[{scriptText}]]
local template = "fvim-init.vim-XXXXXX"
local temp_env = os.getenv("TEMP")
if temp_env == nil then
template = "/tmp/"..template
else
template = temp_env.."\\"..template
end
uv.fs_mkstemp(template, function(err,fd,path)
assert(not err, err)
uv.fs_write(fd,txt,nil,function(err,n)
assert(not err, err)
uv.fs_close(fd, vim.schedule_wrap(function(err)
assert(not err, err)
vim.cmd("source "..path)
uv.fs_unlink(path)
vim.g.fvim_init_complete = true
end))
end)
end)
"""
let! _ = nvim.exec_lua (luaText.Replace("\r\n", "\n")) [||]
()
else
let! _ = nvim.command ("source " + scriptPath)
()
()

// initialization complete. no more messages will be sent from this thread.
Expand Down
3 changes: 3 additions & 0 deletions neovim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ type Nvim() as nvim =
member __.exec (src: string) (output: bool) =
nvim.call { method = "nvim_exec"; parameters = mkparams2 src output }

member __.exec_lua (src: string) (args: obj[]) =
nvim.call { method = "nvim_exec_lua"; parameters = mkparams2 src args }

member __.edit (file: string) =
nvim.command ("edit " + file)

Expand Down

0 comments on commit e72abf8

Please sign in to comment.