Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatao Li committed Nov 16, 2020
1 parent e0e645c commit 01ce6de
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ let startMainWindow app serveropts =
0

let startCrashReportWindow app ex =
FVim.log.trace "main" "%s" "displaying crash dialog"
FVim.log.trace "main" "displaying crash dialog"
FVim.log.trace "main" "exception: %O" ex
let code, msgs = States.get_crash_info()
let crash = new CrashReportViewModel(ex, code, msgs)
Expand Down
4 changes: 2 additions & 2 deletions ViewModels/EditorViewModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ type EditorViewModel(_gridid: int, ?parent: EditorViewModel, ?_gridsize: GridSiz
m_mouse_en <- en

let closeGrid() =
trace _gridid "%s" "closeGrid"
trace _gridid "closeGrid"
if this.IsFocused then
this.IsFocused <- false
match parent with
Expand Down Expand Up @@ -399,7 +399,7 @@ type EditorViewModel(_gridid: int, ?parent: EditorViewModel, ?_gridsize: GridSiz
MouseButton.None

do
trace _gridid "%s" "ctor"
trace _gridid "ctor"
fontConfig()
setCursorEnabled theme.cursor_enabled
clearBuffer false
Expand Down
8 changes: 4 additions & 4 deletions Views/Editor.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ type Editor() as this =

let onViewModelConnected(vm: EditorViewModel) =
grid_vm <- vm
trace grid_vm "%s" "viewmodel connected"
trace grid_vm "viewmodel connected"
resizeFrameBuffer()
vm.Watch
[ Observable.merge (vm.ObservableForProperty(fun x -> x.BufferWidth))
Expand All @@ -214,7 +214,7 @@ type Editor() as this =
vm.ObservableForProperty(fun x -> x.IsFocused)
|> Observable.subscribe(fun focused ->
if focused.Value && not this.IsFocused then
trace grid_vm "%s" "viewmodel ask to focus"
trace grid_vm "viewmodel ask to focus"
this.Focus())

this.GotFocus.Subscribe(fun _ -> vm.IsFocused <- true)
Expand Down Expand Up @@ -266,7 +266,7 @@ type Editor() as this =
dc.DrawLine(Media.Pen(Media.Brushes.Red, 1.0), Point(0.0, this.Bounds.Height), Point(this.Bounds.Width, 0.0))

let drawDirty () =
trace grid_vm "%s" "drawing whole grid"
trace grid_vm "drawing whole grid"
for row = 0 to grid_vm.Rows - 1 do
drawBufferLine grid_dc row 0 grid_vm.Cols
true
Expand Down Expand Up @@ -355,7 +355,7 @@ type Editor() as this =

override this.Render ctx =
if isNull grid_fb then
trace grid_vm "%s" "grid_fb is null"
trace grid_vm "grid_fb is null"
else
grid_dc.PushClip(Rect this.Bounds.Size)
let timer = System.Diagnostics.Stopwatch.StartNew()
Expand Down
2 changes: 1 addition & 1 deletion Views/PopupMenu.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type PopupMenu() as this =

let relayToParent (e: #Avalonia.Interactivity.RoutedEventArgs) =
if this.Parent <> null then
trace "PopupMenu" "%s" "relay to parent"
trace "PopupMenu" "relay to parent"
this.Parent.Focus()

do
Expand Down
58 changes: 35 additions & 23 deletions daemon.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,53 @@ open System.Runtime.InteropServices
open System.Diagnostics
open FVim.common
open System
open System.IO.Pipes
open FSharp.Control.Tasks.V2.ContextSensitive

let inline private trace x = trace "daemon" x

let FVimPipeAddress = sprintf "fvr-%s"
let pipeaddr x =
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
then @"\\.\pipe\" + x
else "/tmp/" + x

let pipename = sprintf "fvr-%s"

let daemon (pname: string option) (nvim: string) =
trace "Running as daemon."
let pname = pname |> Option.defaultValue (pipename "main")
let paddr = pipeaddr pname
trace "FVR server address is '%s'" paddr

let daemon (pipe: string option) (nvim: string) =
trace "%s" "Running as daemon."
let pipe = pipe |> Option.defaultValue (FVimPipeAddress "server")
trace "FVimServerName = %s" pipe
let pipeArgs =
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then ["--listen"; @"\\.\pipe\" + pipe]
else ["--listen"; pipe]
while true do
(task {
let svrpipe =
new NamedPipeServerStream(pname, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Byte, PipeOptions.Asynchronous)
do! svrpipe.WaitForConnectionAsync()
}).Wait()
(*
try
try
let pipe = new System.IO.Pipes.NamedPipeClientStream(".", FVim.Shell.FVimServerAddress, IO.Pipes.PipeDirection.InOut, IO.Pipes.PipeOptions.Asynchronous, TokenImpersonationLevel.Impersonation)
pipe.Connect(timeout=50)
RemoteSession pipe
with :? TimeoutException ->
// transition from TryDamon to StartNew, add "--embed"
this.createIO {opts with serveropts = StartNew; args = ["--embed"] @ args}
*)
let psi = ProcessStartInfo(nvim, join("--headless" :: pipeArgs))
psi.CreateNoWindow <- true
psi.ErrorDialog <- false
psi.RedirectStandardError <- true
psi.RedirectStandardInput <- true
psi.RedirectStandardOutput <- true
psi.UseShellExecute <- false
psi.WindowStyle <- ProcessWindowStyle.Hidden
psi.WorkingDirectory <- Environment.CurrentDirectory
(*let psi = ProcessStartInfo(nvim, join ("--headless" :: pipeArgs))*)
(*psi.CreateNoWindow <- true*)
(*psi.ErrorDialog <- false*)
(*psi.RedirectStandardError <- true*)
(*psi.RedirectStandardInput <- true*)
(*psi.RedirectStandardOutput <- true*)
(*psi.UseShellExecute <- false*)
(*psi.WindowStyle <- ProcessWindowStyle.Hidden*)
(*psi.WorkingDirectory <- Environment.CurrentDirectory*)

use proc = Process.Start(psi)
use __sub = AppDomain.CurrentDomain.ProcessExit.Subscribe (fun _ -> proc.Kill(true))
trace "Neovim process started. Pid = %d" proc.Id
proc.WaitForExit()
trace "Neovim process terminated. ExitCode = %d" proc.ExitCode
(*use proc = Process.Start(psi)*)
(*use __sub = AppDomain.CurrentDomain.ProcessExit.Subscribe(fun _ -> proc.Kill(true))*)
(*trace "Neovim process started. Pid = %d" proc.Id*)
(*proc.WaitForExit()*)
(*trace "Neovim process terminated. ExitCode = %d" proc.ExitCode*)
0
10 changes: 5 additions & 5 deletions model.fs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ let UpdateUICapabilities() =
/// Call this once at initialization.
/// </summary>
let Start (serveropts, norc, debugMultigrid) =
trace "%s" "starting neovim instance..."
trace "starting neovim instance..."
trace "opts = %A" serveropts
States.ui_multigrid <- debugMultigrid
nvim.start serveropts
Expand Down Expand Up @@ -518,13 +518,13 @@ let Start (serveropts, norc, debugMultigrid) =
trace "get-clipboard: match, using clipboard lines with regtype %s" States.clipboard_regtype
States.clipboard_lines, States.clipboard_regtype
else
trace "%s" "get-clipboard: mismatch, using system clipboard"
trace "get-clipboard: mismatch, using system clipboard"
sysClipboardLines, "v"

return { result = Ok(box [| box lines; box regtype |])}
})

trace "%s" "commencing early initialization..."
trace "commencing early initialization..."

async {
let! api_info = Async.AwaitTask(nvim.call { method = "nvim_get_api_info"; parameters = [||] })
Expand Down Expand Up @@ -730,12 +730,12 @@ let OnFocusGained() =
} |> run

let OnTerminated () =
trace "%s" "terminating nvim..."
trace "terminating nvim..."
nvim.stop 1

let OnTerminating(args: CancelEventArgs) =
args.Cancel <- true
trace "%s" "window is closing"
trace "window is closing"
task {
if nvim.isRemote then
Detach()
Expand Down
4 changes: 2 additions & 2 deletions neovim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type Nvim() =

let read (ob: IObserver<obj>) (cancel: CancellationToken) =
Task.Factory.StartNew(fun () ->
trace "%s" "begin read loop"
trace "begin read loop"
let mutable ex = false
while not ex && not cancel.IsCancellationRequested do
try
Expand All @@ -134,7 +134,7 @@ type Nvim() =
else
ob.OnNext([|box Exit|])
else
trace "%s" "end read loop."
trace "end read loop."
ob.OnNext([|box Exit|])
ob.OnCompleted()

Expand Down
8 changes: 4 additions & 4 deletions shell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trace "Discovered %d file icons" FVimIcons.Length

let private win32CheckUAC() =
if not(UACHelper.IsElevated) then
trace "%s" "Starting setup in elevated environment..."
trace "Starting setup in elevated environment..."
let exe = Path.Combine(FVimDir, "FVim.exe")
let psi = ProcessStartInfo(exe, String.Join(" ", Environment.GetCommandLineArgs() |> Array.skip 1))

Expand All @@ -45,7 +45,7 @@ let private win32CheckUAC() =
proc.WaitForExit()
false
else
trace "%s" "FVim is elevated"
trace "FVim is elevated"
true

let private noextkey ext =
Expand All @@ -54,7 +54,7 @@ let private noextkey ext =

let private win32RegisterFileAssociation() =

trace "%s" "registering file associations..."
trace "registering file associations..."

let HKCR = Registry.ClassesRoot
let HKLM = Registry.LocalMachine
Expand Down Expand Up @@ -109,7 +109,7 @@ let private win32RegisterFileAssociation() =
extKey.SetValue("", progId)

let private win32UnregisterFileAssociation() =
trace "%s" "unregistering file associations..."
trace "unregistering file associations..."
let HKCR = Registry.ClassesRoot
let HKLM = Registry.LocalMachine

Expand Down
2 changes: 1 addition & 1 deletion states.fs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ let msg_dispatch =
error "rpc" "neovim: %s" err
_errormsgs.Add err
| Exit ->
trace "rpc" "%s" "shutting down application lifetime"
trace "rpc" "shutting down application lifetime"
_appLifetime.Shutdown()
| Crash code ->
trace "rpc" "neovim crashed with code %d" code
Expand Down

0 comments on commit 01ce6de

Please sign in to comment.