@@ -3,11 +3,12 @@ module Ionide.LanguageServerProtocol.JsonRpc
33open Newtonsoft.Json
44open 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>]
1314type 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
4555module 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
0 commit comments