Skip to content

Commit

Permalink
interpret raw text message from neovim
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatao Li committed Dec 16, 2020
1 parent f0602fd commit 345b1b9
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let buildAvaloniaApp() =
.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.With(new Win32PlatformOptions(UseDeferredRendering=false))
.With(new Win32PlatformOptions(UseDeferredRendering=false, UseWgl=true))
.With(new AvaloniaNativePlatformOptions(UseDeferredRendering=false))
.With(new X11PlatformOptions(UseDeferredRendering=false))
.With(new MacOSPlatformOptions(ShowInDock=true))
Expand Down
3 changes: 1 addition & 2 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"profiles": {
"fvim": {
"commandName": "Project",
"commandLineArgs": "--daemon"
"commandName": "Project"
},
"norc": {
"commandName": "Project",
Expand Down
5 changes: 3 additions & 2 deletions ViewModels/CrashReportViewModel.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace FVim

open ui
open log
open common
open Avalonia.Media

type CrashReportViewModel(ex: exn, code: int, msgs: ResizeArray<string>) =
inherit ViewModelBase()
Expand All @@ -25,3 +24,5 @@ type CrashReportViewModel(ex: exn, code: int, msgs: ResizeArray<string>) =
"Feel free to create new issues, and I'll help to triage and fix the problem."
tip + generic_message

member __.Font =
FontFamily(ui.DefaultFont)
6 changes: 3 additions & 3 deletions Views/CrashReport.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition Width="400" />
<ColumnDefinition Width="600" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="FVim exited unexpectedly." Grid.Row="0" Grid.Column="1" Margin="0,10,0,0" FontWeight="Bold"/>
<TextBlock Text="Close this window to exit FVim." Grid.Row="3" Grid.Column="1" Margin="0,10,0,0" FontWeight="Bold"/>
<Image Source="/Assets/error.png" Grid.Row="0" Grid.RowSpan="4" Grid.Column="0" Height="64" Margin="0,0,0,50" />
<TextBox Text="{Binding MainMessage}" Grid.Row="1" Grid.Column="1" Margin="0,20,0,0"
AcceptsReturn="True" TextWrapping="Wrap" />
AcceptsReturn="True" FontFamily="{Binding Font}" />
<TextBlock Text="{Binding TipMessage}" Grid.Row="2" Grid.Column="1" Margin="0,20,0,0" />
<TextBlock Text="Stack trace:" Grid.Row="0" Grid.Column="2" Margin="0,10,0,0"/>
<TextBox Text="{Binding StackTrace}" Grid.Row="1" Grid.RowSpan="2" Grid.Column="2" Margin="10,10,10,10"
AcceptsReturn="True" TextWrapping="Wrap" />
AcceptsReturn="True" FontFamily="{Binding Font}" />
</Grid>
</Window>
1 change: 1 addition & 0 deletions Views/CrashReport.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ type CrashReport() as this =

do
AvaloniaXamlLoader.Load(this)

26 changes: 26 additions & 0 deletions def.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,35 @@ open common

open Avalonia.Media
open System
open System.Threading.Tasks

let inline private trace fmt = trace "def" fmt

[<Struct>]
type Request =
{
method: string
parameters: obj[]
}

[<Struct>]
type Response =
{
result: Result<obj, obj>
}

[<Struct>]
type Event =
| Request of reqId: int32 * req: Request * handler: (int32 -> Response -> unit Task)
| Response of rspId: int32 * rsp: Response
| Notification of nreq: Request
| Error of emsg: string
| Crash of ccode: int32
| ByteMessage of bmsg: byte
| UnhandledException of ex: exn
| Exit


[<Struct>]
type CursorShape =
| Block
Expand Down
28 changes: 16 additions & 12 deletions neovim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,22 @@ type Nvim() as nvim =

let pending = ConcurrentDictionary<int, TaskCompletionSource<Response>>()
let parse (data: obj) : Event =
match data :?> obj[] with
// request
| [| (Integer32 0); (Integer32 msg_id) ; (String method); :? (obj[]) as parameters |]
-> Request(msg_id, { method = method; parameters = parameters }, reply)
// response
| [| (Integer32 1); (Integer32 msg_id) ; err; result |]
-> Response(msg_id, { result = if err = null then Ok result else Result.Error err })
// notification
| [| (Integer32 2); (String method); :? (obj[]) as parameters |]
-> Notification { method = method; parameters = parameters }
// event forwarding
| [| :? Event as e |] -> e
match data with
| :? (obj[]) as data ->
match data with
// request
| [| (Integer32 0); (Integer32 msg_id) ; (String method); :? (obj[]) as parameters |]
-> Request(msg_id, { method = method; parameters = parameters }, reply)
// response
| [| (Integer32 1); (Integer32 msg_id) ; err; result |]
-> Response(msg_id, { result = if err = null then Ok result else Result.Error err })
// notification
| [| (Integer32 2); (String method); :? (obj[]) as parameters |]
-> Notification { method = method; parameters = parameters }
// event forwarding
| [| :? Event as e |] -> e
| _ -> raise <| EventParseException(data)
| :? byte as b -> ByteMessage b
| _ -> raise <| EventParseException(data)

let intercept (ev: Event) =
Expand Down
29 changes: 6 additions & 23 deletions states.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,11 @@ open Avalonia.Layout
open System.Threading.Tasks
open FSharp.Control.Tasks.V2

[<Struct>]
type Request =
{
method: string
parameters: obj[]
}

[<Struct>]
type Response =
{
result: Result<obj, obj>
}

[<Struct>]
type Event =
| Request of reqId: int32 * req: Request * handler: (int32 -> Response -> unit Task)
| Response of rspId: int32 * rsp: Response
| Notification of nreq: Request
| Error of emsg: string
| Crash of ccode: int32
| UnhandledException of ex: exn
| Exit

let private _stateChangeEvent = Event<string>()
let private _appLifetime = lazy(Avalonia.Application.Current.ApplicationLifetime :?> Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime)
let mutable private _crashcode = 0
let private _errormsgs = ResizeArray<string>()
let private _bytemsg = ResizeArray<byte>()

// request handlers are explicitly registered, 1:1, with no broadcast.
let requestHandlers = hashmap[]
Expand Down Expand Up @@ -274,7 +252,12 @@ let msg_dispatch =
| Error err ->
error "rpc" "neovim: %s" err
_errormsgs.Add err
| ByteMessage bmsg ->
_bytemsg.Add bmsg
| Exit ->
if _bytemsg.Count <> 0 then
let _bytemsg = System.Text.Encoding.UTF8.GetString(_bytemsg.ToArray())
failwithf "neovim says:\n%s" _bytemsg
trace "rpc" "shutting down application lifetime"
Shutdown 0
| Crash code ->
Expand Down

0 comments on commit 345b1b9

Please sign in to comment.