Skip to content

Commit 9e7592c

Browse files
committed
Rearrange types to support codegen serverclient
1 parent ee96fb5 commit 9e7592c

File tree

8 files changed

+466
-457
lines changed

8 files changed

+466
-457
lines changed

src/Client.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Ionide.LanguageServerProtocol
22

33
open Ionide.LanguageServerProtocol.Types
4+
open Ionide.LanguageServerProtocol.JsonRpc
45

56
module private ClientUtil =
67
/// Return the JSON-RPC "not implemented" error
@@ -186,4 +187,5 @@ type LspClient() =
186187
member this.WindowWorkDoneProgressCreate(p: WorkDoneProgressCreateParams) = this.WindowWorkDoneProgressCreate(p)
187188
member this.Progress(p: ProgressParams) = this.Progress(p)
188189
member this.CancelRequest(p: CancelParams) : Async<unit> = this.CancelRequest(p)
189-
member this.LogTrace(p: LogTraceParams) : Async<unit> = this.LogTrace(p)
190+
member this.LogTrace(p: LogTraceParams) : Async<unit> = this.LogTrace(p)
191+
member this.Dispose() : unit = failwith "Not Implemented"

src/ClientServer.cg.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
namespace Ionide.LanguageServerProtocol
22

33
open Ionide.LanguageServerProtocol.Types
4+
open Ionide.LanguageServerProtocol.JsonRpc
45

5-
type ILSPServer =
6+
type ILspServer =
7+
inherit System.IDisposable
68
// Notifications
79
/// The `workspace/didChangeWorkspaceFolders` notification is sent from the client to the server when the workspace
810
/// folder configuration changes.
@@ -344,6 +346,7 @@ type ILSPServer =
344346
abstract member WorkspaceExecuteCommand: ExecuteCommandParams -> AsyncLspResult<LSPAny option>
345347

346348
type ILspClient =
349+
inherit System.IDisposable
347350
// Notifications
348351
/// The show message notification is sent from a server to a client to ask
349352
/// the client to display a particular message in the user interface.

src/Ionide.LanguageServerProtocol.fsproj

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
repo.
2121
-->
2222
<Compile Include="FsLibLog.fs" />
23-
<Compile Include="JsonRpc.fs" />
2423
<Compile Include="Types.fs" />
2524
<Compile Include="Types.cg.fs" />
25+
<Compile Include="JsonRpc.fs" />
2626
<Compile Include="TypeDefaults.fs" />
2727
<Compile Include="ClientServer.cg.fs" />
2828
<Compile Include="Client.fs" />
@@ -73,17 +73,17 @@
7373
<Exec
7474
Command="@(_GenerateCommand, ' ')" />
7575

76-
<ItemGroup>
77-
<_GenerateCommand2 Include="dotnet;@(_GeneratorApp)" />
78-
<_GenerateCommand2 Include="clientserver" />
79-
<_GenerateCommand2 Include="--metamodelpath" />
80-
<_GenerateCommand2 Include="%(_MetaModelInputs.FullPath)" />
81-
<_GenerateCommand2 Include="--outputfilepath" />
82-
<_GenerateCommand2 Include="%(_MetaModelClientServerOutputs.FullPath)" />
83-
</ItemGroup>
84-
85-
<Exec
86-
Command="@(_GenerateCommand2, ' ')" />
76+
<ItemGroup>
77+
<_GenerateCommand2 Include="dotnet;@(_GeneratorApp)" />
78+
<_GenerateCommand2 Include="clientserver" />
79+
<_GenerateCommand2 Include="--metamodelpath" />
80+
<_GenerateCommand2 Include="%(_MetaModelInputs.FullPath)" />
81+
<_GenerateCommand2 Include="--outputfilepath" />
82+
<_GenerateCommand2 Include="%(_MetaModelClientServerOutputs.FullPath)" />
83+
</ItemGroup>
84+
85+
<Exec
86+
Command="@(_GenerateCommand2, ' ')" />
8787

8888
</Target>
8989
</Project>

src/JsonRpc.fs

Lines changed: 112 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ module Ionide.LanguageServerProtocol.JsonRpc
33
open Newtonsoft.Json
44
open Newtonsoft.Json.Linq
55

6-
type MessageTypeTest =
7-
{ [<JsonProperty("jsonrpc")>]
8-
Version: string
9-
Id: int option
10-
Method: string option }
6+
type MessageTypeTest = {
7+
[<JsonProperty("jsonrpc")>]
8+
Version: string
9+
Id: int option
10+
Method: string option
11+
}
1112

1213
[<RequireQualifiedAccess>]
1314
type MessageType =
@@ -23,115 +24,132 @@ let getMessageType messageTest =
2324
| { Version = "2.0"; Id = None; Method = Some _ } -> MessageType.Notification
2425
| _ -> MessageType.Error
2526

26-
type Request =
27-
{ [<JsonProperty("jsonrpc")>]
28-
Version: string
29-
Id: int
30-
Method: string
31-
Params: JToken option }
32-
33-
static member Create(id: int, method': string, rpcParams: JToken option) =
34-
{ Version = "2.0"; Id = id; Method = method'; Params = rpcParams }
35-
36-
type Notification =
37-
{ [<JsonProperty("jsonrpc")>]
38-
Version: string
39-
Method: string
40-
Params: JToken option }
41-
42-
static member Create(method': string, rpcParams: JToken option) =
43-
{ Version = "2.0"; Method = method'; Params = rpcParams }
27+
type Request = {
28+
[<JsonProperty("jsonrpc")>]
29+
Version: string
30+
Id: int
31+
Method: string
32+
Params: JToken option
33+
} with
34+
35+
static member Create(id: int, method': string, rpcParams: JToken option) = {
36+
Version = "2.0"
37+
Id = id
38+
Method = method'
39+
Params = rpcParams
40+
}
41+
42+
type Notification = {
43+
[<JsonProperty("jsonrpc")>]
44+
Version: string
45+
Method: string
46+
Params: JToken option
47+
} with
48+
49+
static member Create(method': string, rpcParams: JToken option) = {
50+
Version = "2.0"
51+
Method = method'
52+
Params = rpcParams
53+
}
4454

4555
module ErrorCodes =
46-
open System
47-
let parseError = -32700
48-
let invalidRequest = -32600
49-
let methodNotFound = -32601
50-
let invalidParams = -32602
51-
let internalError = -32603
52-
53-
5456
///<summary>This is the start range of JSON-RPC reserved error codes.
5557
///It doesn't denote a real error code. No LSP error codes should
5658
///be defined between the start and end range. For backwards
5759
///compatibility the <see cref="ServerNotInitialized"/> and the <see cref="UnknownErrorCode" />
5860
///are left in the range.</summary>
5961
let jsonrpcReservedErrorRangeStart = -32099
60-
61-
[<Obsolete("Use jsonRpcReservedErrorRangeStart instead")>]
62-
let serverErrorStart = jsonrpcReservedErrorRangeStart
63-
64-
/// Error code indicating that a server received a notification or
65-
/// request before the server has received the `initialize` request.
66-
let serverNotInitialized = -32002
67-
6862
///This is the end range of JSON-RPC reserved error codes. It doesn't denote a real error code.
6963
let jsonrpcReservedErrorRangeEnd = -32000
7064

71-
[<Obsolete("Use jsonRpcReservedErrorRangeEnd instead")>]
72-
let serverErrorEnd = jsonrpcReservedErrorRangeEnd
73-
74-
7565
/// This is the start range of LSP reserved error codes. It doesn't denote a real error code.
7666
let lspReservedErrorRangeStart = -32899
77-
78-
/// A request failed but it was syntactically correct, e.g the
79-
/// method name was known and the parameters were valid. The error
80-
/// message should contain human readable information about why
81-
/// the request failed.
82-
/// @since 3.17.0
83-
let RequestFailed = -32803
84-
85-
86-
/// The server cancelled the request. This error code should
87-
/// only be used for requests that explicitly support being
88-
/// server cancellable.
89-
///
90-
/// @since 3.17.0
91-
let serverCancelled = -32802
92-
93-
/// The server detected that the content of a document got
94-
/// modified outside normal conditions. A server should
95-
/// NOT send this error code if it detects a content change
96-
/// in it unprocessed messages. The result even computed
97-
/// on an older state might still be useful for the client.
98-
///
99-
/// If a client decides that a result is not of any use anymore
100-
/// the client should cancel the request.
101-
let contentModified = -32801
102-
103-
/// The client has canceled a request and a server has detected the cancel.
104-
let requestCancelled = -32800
10567
/// This is the end range of LSP reserved error codes. It doesn't denote a real error code.
10668
let lspReservedErrorRangeEnd = -32899
10769

108-
type Error =
109-
{ Code: int
110-
Message: string
111-
Data: JToken option }
70+
open Ionide.LanguageServerProtocol.Types
71+
72+
type Error = {
73+
Code: int
74+
Message: string
75+
Data: JToken option
76+
} with
11277

11378
static member Create(code: int, message: string) = { Code = code; Message = message; Data = None }
114-
static member ParseError = Error.Create(ErrorCodes.parseError, "Parse error")
115-
static member InvalidRequest = Error.Create(ErrorCodes.invalidRequest, "Invalid Request")
116-
static member MethodNotFound = Error.Create(ErrorCodes.methodNotFound, "Method not found")
117-
static member InvalidParams = Error.Create(ErrorCodes.invalidParams, "Invalid params")
118-
static member InternalError = Error.Create(ErrorCodes.internalError, "Internal error")
119-
static member InternalErrorMessage message = Error.Create(ErrorCodes.internalError, message)
120-
static member RequestCancelled = Error.Create(ErrorCodes.requestCancelled, "Request cancelled")
121-
static member RequestCancelledMessage message = Error.Create(ErrorCodes.requestCancelled, message)
122-
123-
type Response =
124-
{ [<JsonProperty("jsonrpc")>]
125-
Version: string
126-
Id: int option
127-
Error: Error option
128-
[<JsonProperty(NullValueHandling = NullValueHandling.Include)>]
129-
Result: JToken option }
79+
80+
static member ParseError(?message) =
81+
let message = defaultArg message "Parse error"
82+
Error.Create(int ErrorCodes.ParseError, message)
83+
84+
static member InvalidRequest(?message) =
85+
let message = defaultArg message "Invalid Request"
86+
Error.Create(int ErrorCodes.InvalidRequest, message)
87+
88+
static member MethodNotFound(?message) =
89+
let message = defaultArg message "Method not found"
90+
Error.Create(int ErrorCodes.MethodNotFound, message)
91+
92+
static member InvalidParams(?message) =
93+
let message = defaultArg message "Invalid params"
94+
Error.Create(int ErrorCodes.InvalidParams, message)
95+
96+
static member InternalError(?message: string) =
97+
let message = defaultArg message "Internal error"
98+
Error.Create(int ErrorCodes.InternalError, message)
99+
100+
static member RequestCancelled(?message) =
101+
let message = defaultArg message "Request cancelled"
102+
Error.Create(int LSPErrorCodes.RequestCancelled, message)
103+
104+
type Response = {
105+
[<JsonProperty("jsonrpc")>]
106+
Version: string
107+
Id: int option
108+
Error: Error option
109+
[<JsonProperty(NullValueHandling = NullValueHandling.Include)>]
110+
Result: JToken option
111+
} with
130112

131113
/// Json.NET conditional property serialization, controlled by naming convention
132114
member x.ShouldSerializeResult() = x.Error.IsNone
133115

134-
static member Success(id: int, result: JToken option) =
135-
{ Version = "2.0"; Id = Some id; Result = result; Error = None }
116+
static member Success(id: int, result: JToken option) = {
117+
Version = "2.0"
118+
Id = Some id
119+
Result = result
120+
Error = None
121+
}
122+
123+
static member Failure(id: int, error: Error) = { Version = "2.0"; Id = Some id; Result = None; Error = Some error }
124+
125+
126+
/// Result type composed of a success value or an error of type JsonRpc.Error
127+
type LspResult<'t> = Result<'t, Error>
128+
/// Async Result type composed of a success value or an error of type JsonRpc.Error
129+
type AsyncLspResult<'t> = Async<LspResult<'t>>
130+
131+
132+
module LspResult =
133+
134+
let success x : LspResult<_> = Result.Ok x
135+
136+
let invalidParams message : LspResult<_> = Result.Error(Error.InvalidParams message)
137+
138+
let internalError<'a> (message: string) : LspResult<'a> =
139+
Result.Error(Error.Create(int ErrorCodes.InvalidParams, message))
140+
141+
let notImplemented<'a> : LspResult<'a> = Result.Error(Error.MethodNotFound())
142+
143+
let requestCancelled<'a> : LspResult<'a> = Result.Error(Error.RequestCancelled())
144+
145+
module AsyncLspResult =
146+
147+
let success x : AsyncLspResult<_> = async.Return(Result.Ok x)
148+
149+
let invalidParams message : AsyncLspResult<_> = async.Return(LspResult.invalidParams message)
150+
151+
let internalError message : AsyncLspResult<_> = async.Return(LspResult.internalError message)
152+
153+
let notImplemented<'a> : AsyncLspResult<'a> = async.Return LspResult.notImplemented
136154

137-
static member Failure(id: int, error: Error) = { Version = "2.0"; Id = Some id; Result = None; Error = Some error }
155+
let requestCancelled<'a> : AsyncLspResult<'a> = async.Return LspResult.requestCancelled

src/LanguageServerProtocol.fs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,11 @@ module Server =
361361
"workspace/executeCommand", requestHandling (fun s p -> s.WorkspaceExecuteCommand(p))
362362
"window/workDoneProgress/cancel",
363363
requestHandling (fun s p ->
364-
s.WorkDoneProgressCancel(p)
364+
s.WindowWorkDoneProgressCancel(p)
365365
|> notificationSuccess
366366
)
367367
"workspace/diagnostic", requestHandling (fun s p -> s.WorkspaceDiagnostic(p))
368-
"shutdown",
369-
requestHandling (fun s () ->
370-
s.Shutdown()
371-
|> notificationSuccess
372-
)
368+
"shutdown", requestHandling (fun s () -> s.Shutdown())
373369
"exit",
374370
requestHandling (fun s () ->
375371
s.Exit()
@@ -629,13 +625,15 @@ module Client =
629625
| Some handling ->
630626
try
631627
match notification.Params with
632-
| None -> return Result.Error(JsonRpc.Error.InvalidParams)
628+
| None -> return Result.Error(JsonRpc.Error.InvalidParams())
633629
| Some prms ->
634630
let! result = handling.Run prms
635631
return Result.Ok()
636632
with ex ->
637-
return Result.Error(JsonRpc.Error.Create(JsonRpc.ErrorCodes.internalError, ex.ToString()))
638-
| None -> return Result.Error(JsonRpc.Error.MethodNotFound)
633+
return
634+
JsonRpc.Error.InternalError(ex.ToString())
635+
|> Result.Error
636+
| None -> return Result.Error(JsonRpc.Error.MethodNotFound())
639637
}
640638

641639
let messageHandler str =

0 commit comments

Comments
 (0)