forked from ionide/FsAutoComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFsAutoComplete.Suave.fs
218 lines (196 loc) · 10.5 KB
/
FsAutoComplete.Suave.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
module FsAutoComplete.Suave
open System.IO
open Suave
open Suave.Http
open Suave.Operators
open Suave.Web
open Suave.WebPart
open Suave.WebSocket
open Suave.Sockets.Control
open Suave.Filters
open Newtonsoft.Json
open Microsoft.FSharp.Compiler
open FsAutoComplete
open FsAutoComplete.JsonSerializer
[<AutoOpen>]
module Contract =
type ParseRequest = { FileName : string; IsAsync : bool; Lines : string[]; Version : int }
type ProjectRequest = { FileName : string;}
type DeclarationsRequest = {FileName : string; Version : int}
type HelptextRequest = {Symbol : string}
type CompletionRequest = {FileName : string; SourceLine : string; Line : int; Column : int; Filter : string; IncludeKeywords : bool;}
type PositionRequest = {FileName : string; Line : int; Column : int; Filter : string}
type LintRequest = {FileName : string}
[<AutoOpen>]
module internal Utils =
let private fromJson<'a> json =
JsonConvert.DeserializeObject(json, typeof<'a>) :?> 'a
let getResourceFromReq<'a> (req : HttpRequest) =
let getString rawForm =
System.Text.Encoding.UTF8.GetString(rawForm)
req.rawForm |> getString |> fromJson<'a>
let zombieCheckWithHostPID pid quit =
try
let hostProcess = System.Diagnostics.Process.GetProcessById(pid)
ProcessWatcher.watch hostProcess (fun _ -> quit ())
with
| e ->
printfn "Host process ID %i not found: %s" pid e.Message
// If the process dies before we get here then request shutdown
// immediately
quit ()
open Argu
type CLIArguments =
| [<MainCommand; Unique>] Port of tcp_port:int
| [<CustomCommandLine("--hostPID")>] HostPID of pid:int
with
interface IArgParserTemplate with
member s.Usage =
match s with
| Port _ -> "the listening port."
| HostPID _ -> "the Host process ID."
[<EntryPoint>]
let main argv =
let mutable client : WebSocket option = None
System.Threading.ThreadPool.SetMinThreads(8, 8) |> ignore
let commands = Commands(writeJson)
let originalFs = AbstractIL.Internal.Library.Shim.FileSystem
let fs = new FileSystem(originalFs, commands.Files.TryFind)
AbstractIL.Internal.Library.Shim.FileSystem <- fs
// commands.FileChecked
// |> Event.add (fun response ->
// client |> Option.iter (fun socket ->
// async {
// let! res = response
// let cnt = res |> List.toArray |> Json.toJson
// return! socket.send Text cnt true
// } |> Async.Ignore |> Async.Start ))
let handler f : WebPart = fun (r : HttpContext) -> async {
let data = r.request |> getResourceFromReq
let! res = Async.Catch (f data)
match res with
| Choice1Of2 res ->
let res' = res |> List.toArray |> Json.toJson
return! Response.response HttpCode.HTTP_200 res' r
| Choice2Of2 e ->
printfn "Unhandled error - %s \n %s" e.Message e.StackTrace
return! Response.response HttpCode.HTTP_500 (Json.toJson e) r
}
let positionHandler (f : PositionRequest -> ParseAndCheckResults -> string -> string [] -> Async<string list>) : WebPart = fun (r : HttpContext) ->
async {
let data = r.request |> getResourceFromReq<PositionRequest>
let file = Path.GetFullPath data.FileName
let! res =
match commands.TryGetFileCheckerOptionsWithLinesAndLineStr(file, { Line = data.Line; Col = data.Column }) with
| Failure s -> async.Return ([CommandResponse.error writeJson s])
| Success (options, lines, lineStr) ->
// TODO: Should sometimes pass options.Source in here to force a reparse
// for completions e.g. `(some typed expr).$`
try
let tyResOpt = commands.TryGetRecentTypeCheckResultsForFile(file, options)
match tyResOpt with
| None -> async.Return [CommandResponse.info writeJson "Cached typecheck results not yet available"]
| Some tyRes ->
async {
let! r = Async.Catch (f data tyRes lineStr lines)
match r with
| Choice1Of2 r -> return r
| Choice2Of2 e -> return [CommandResponse.error writeJson e.Message]
}
with e -> async.Return [CommandResponse.error writeJson e.Message]
let res' = res |> List.toArray |> Json.toJson
return! Response.response HttpCode.HTTP_200 res' r
}
let echo (webSocket : WebSocket) =
fun cx ->
client <- Some webSocket
socket {
let loop = ref true
while !loop do
let! msg = webSocket.read()
match msg with
| (Ping, _, _) -> do! webSocket.send Pong [||] true
| (Close, _, _) ->
do! webSocket.send Close [||] true
client <- None
loop := false
| _ -> ()
}
let app =
choose [
// path "/notify" >=> handShake echo
path "/parse" >=> handler (fun (data : ParseRequest) -> commands.Parse data.FileName data.Lines data.Version)
path "/parseProjects" >=> handler (fun (data : ProjectRequest) -> commands.ParseProjectsForFile data.FileName)
//TODO: Add filewatcher
path "/parseProjectsInBackground" >=> handler (fun (data : ProjectRequest) -> commands.ParseAndCheckProjectsInBackgroundForFile data.FileName)
path "/project" >=> handler (fun (data : ProjectRequest) -> commands.Project data.FileName false ignore)
path "/declarations" >=> handler (fun (data : DeclarationsRequest) -> commands.Declarations data.FileName (Some data.Version) )
path "/declarationsProjects" >=> fun httpCtx ->
async {
let! errors = commands.DeclarationsInProjects ()
let res = errors |> List.toArray |> Json.toJson
return! Response.response HttpCode.HTTP_200 res httpCtx
}
path "/helptext" >=> handler (fun (data : HelptextRequest) -> commands.Helptext data.Symbol |> async.Return)
path "/completion" >=> handler (fun (data : CompletionRequest) -> async {
let file = Path.GetFullPath data.FileName
match commands.TryGetFileCheckerOptionsWithLines file with
| Failure s -> return [CommandResponse.error writeJson s]
| Success (options, lines) ->
let line = data.Line
let col = data.Column
let lineStr = data.SourceLine
let ok = line <= lines.Length && line >= 1 && col <= lineStr.Length + 1 && col >= 1
if not ok then
return [CommandResponse.error writeJson "Position is out of range"]
else
let tyResOpt = commands.TryGetRecentTypeCheckResultsForFile(file, options)
match tyResOpt with
| None -> return [ CommandResponse.info writeJson "Cached typecheck results not yet available"]
| Some tyRes -> return! commands.Completion tyRes { Line = data.Line; Col = data.Column } lineStr (Some data.Filter) data.IncludeKeywords
})
path "/tooltip" >=> positionHandler (fun data tyRes lineStr _ -> commands.ToolTip tyRes { Line = data.Line; Col = data.Column } lineStr)
path "/signature" >=> positionHandler (fun data tyRes lineStr _ -> commands.Typesig tyRes { Line = data.Line; Col = data.Column } lineStr)
path "/symboluseproject" >=> positionHandler (fun data tyRes lineStr _ -> commands.SymbolUseProject tyRes { Line = data.Line; Col = data.Column } lineStr)
path "/symboluse" >=> positionHandler (fun data tyRes lineStr _ -> commands.SymbolUse tyRes { Line = data.Line; Col = data.Column } lineStr)
path "/finddeclaration" >=> positionHandler (fun data tyRes lineStr _ -> commands.FindDeclarations tyRes { Line = data.Line; Col = data.Column } lineStr)
path "/methods" >=> positionHandler (fun data tyRes _ lines -> commands.Methods tyRes { Line = data.Line; Col = data.Column } lines)
path "/help" >=> positionHandler (fun data tyRes line _ -> commands.Help tyRes { Line = data.Line; Col = data.Column } line)
path "/compilerlocation" >=> fun httpCtx ->
async {
let res = commands.CompilerLocation() |> List.toArray |> Json.toJson
return! Response.response HttpCode.HTTP_200 res httpCtx
}
path "/lint" >=> handler (fun (data: LintRequest) -> commands.Lint data.FileName)
path "/namespaces" >=> positionHandler (fun data tyRes lineStr _ -> commands.GetNamespaceSuggestions tyRes { Line = data.Line; Col = data.Column } lineStr)
path "/unionCaseGenerator" >=> positionHandler (fun data tyRes lineStr lines -> commands.GetUnionPatternMatchCases tyRes { Line = data.Line; Col = data.Column } lines lineStr)
]
let main (args: ParseResults<CLIArguments>) =
let port = args.GetResult (<@ Port @>, defaultValue = 8088)
let defaultBinding = defaultConfig.bindings.[0]
let withPort = { defaultBinding.socketBinding with port = uint16 port }
let serverConfig =
{ defaultConfig with bindings = [{ defaultBinding with socketBinding = withPort }]}
try
match args.TryGetResult (<@ HostPID @>) with
| Some pid ->
serverConfig.logger.Log Logging.LogLevel.Info (fun () -> Logging.LogLine.mk "FsAutoComplete.Suave" Logging.LogLevel.Info Logging.TraceHeader.empty None (sprintf "tracking host PID %i" pid))
zombieCheckWithHostPID pid (fun () -> exit 0)
| None -> ()
startWebServer serverConfig app
0
with
| e ->
printfn "Server crashing error - %s \n %s" e.Message e.StackTrace
1
let parser = ArgumentParser.Create<CLIArguments>(programName = "FsAutoComplete.Suave.exe")
try
let results = parser.Parse argv
main results
with
| :? ArguParseException as ex ->
printfn "%s" (parser.PrintUsage())
2
| e ->
printfn "Server crashing error - %s \n %s" e.Message e.StackTrace
3