Skip to content

Commit a3429a0

Browse files
committed
FL0022
1 parent a2378c6 commit a3429a0

2 files changed

Lines changed: 74 additions & 77 deletions

File tree

src/FSharpLint.Client/FSharpLintToolLocator.fs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,15 @@ let private fsharpLintVersionOnPath () : (FSharpLintExecutableFile * FSharpLintV
132132
| Some path -> path.Split([| if isWindows then ';' else ':' |], StringSplitOptions.RemoveEmptyEntries)
133133
| None -> Array.empty
134134
|> Seq.choose (fun folder ->
135-
if isWindows then
136-
let fsharpLintExe = Path.Combine(folder, $"{FSharpLintToolName}.exe")
137-
if File.Exists fsharpLintExe then Some fsharpLintExe
138-
else None
139-
else
140-
let fsharpLint = Path.Combine(folder, FSharpLintToolName)
141-
if File.Exists fsharpLint then Some fsharpLint
142-
else None)
135+
let fsharpLint =
136+
if isWindows then Path.Combine(folder, $"{FSharpLintToolName}.exe")
137+
else Path.Combine(folder, FSharpLintToolName)
138+
if File.Exists fsharpLint then Some fsharpLint
139+
else None)
143140
|> Seq.tryHead
144141
|> Option.bind File.From
145142

146-
fsharpLintExecutableOnPathOpt
147-
|> Option.bind (fun fsharpLintExecutablePath ->
143+
let extractFsharpLintVersion fsharpLintExecutablePath =
148144
let processStart = ProcessStartInfo(
149145
FileName = File.Unwrap fsharpLintExecutablePath,
150146
Arguments = "--version",
@@ -167,7 +163,10 @@ let private fsharpLintVersionOnPath () : (FSharpLintExecutableFile * FSharpLintV
167163
else
168164
None)
169165
| Error(ProcessStartError.ExecutableFileNotFound _)
170-
| Error(ProcessStartError.UnexpectedException _) -> None)
166+
| Error(ProcessStartError.UnexpectedException _) -> None
167+
168+
fsharpLintExecutableOnPathOpt
169+
|> Option.bind extractFsharpLintVersion
171170

172171
let findFSharpLintTool (workingDir: Folder) : Result<FSharpLintToolFound, FSharpLintToolError> =
173172
// First try and find a local tool for the folder.

src/FSharpLint.Client/LSPFSharpLintService.fs

Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -58,69 +58,70 @@ let private createAgent (ct: CancellationToken) =
5858
Some { state with FolderToVersion = Map.add folder version state.FolderToVersion }
5959
| None -> None
6060

61-
MailboxProcessor.Start(
62-
(fun inbox ->
63-
let rec messageLoop (state: ServiceState) =
64-
async {
65-
let! msg = inbox.Receive()
66-
67-
let nextState =
68-
match msg with
69-
| GetDaemon(folder, replyChannel) ->
70-
// get the version for that folder
71-
// look in the cache first
72-
let versionFromCache = Map.tryFind folder state.FolderToVersion
73-
match versionFromCache with
74-
| Some version ->
61+
let processor (inbox: MailboxProcessor<Msg>) =
62+
let rec messageLoop (state: ServiceState) =
63+
async {
64+
let! msg = inbox.Receive()
65+
66+
let nextState =
67+
match msg with
68+
| GetDaemon(folder, replyChannel) ->
69+
// get the version for that folder
70+
// look in the cache first
71+
let versionFromCache = Map.tryFind folder state.FolderToVersion
72+
match versionFromCache with
73+
| Some version ->
74+
tryGetVersionFromCache state version folder replyChannel
75+
|> Option.defaultWith(fun () ->
76+
// This is a strange situation, we know what version is linked to that folder but there is no daemon
77+
// The moment a version is added, is also the moment a daemon is re-used or created
78+
replyChannel.Reply(
79+
Error(GetDaemonError.CompatibleVersionIsKnownButNoDaemonIsRunning version)
80+
)
81+
82+
state)
83+
| None ->
84+
// Try and find a version of fsharplint daemon for our current folder
85+
let fsharpLintToolResult: Result<FSharpLintToolFound, FSharpLintToolError> =
86+
findFSharpLintTool folder
87+
88+
match fsharpLintToolResult with
89+
| Ok(FSharpLintToolFound(version, startInfo)) ->
90+
let createDaemon() =
91+
let createDaemonResult = createFor startInfo
92+
93+
match createDaemonResult with
94+
| Ok daemon ->
95+
replyChannel.Reply(Ok daemon.RpcClient)
96+
97+
{ Daemons = Map.add version daemon state.Daemons
98+
FolderToVersion = Map.add folder version state.FolderToVersion }
99+
| Error pse ->
100+
replyChannel.Reply(Error(GetDaemonError.FSharpLintProcessStart pse))
101+
state
102+
103+
75104
tryGetVersionFromCache state version folder replyChannel
76-
|> Option.defaultWith(fun () ->
77-
// This is a strange situation, we know what version is linked to that folder but there is no daemon
78-
// The moment a version is added, is also the moment a daemon is re-used or created
79-
replyChannel.Reply(
80-
Error(GetDaemonError.CompatibleVersionIsKnownButNoDaemonIsRunning version)
81-
)
82-
83-
state)
84-
| None ->
85-
// Try and find a version of fsharplint daemon for our current folder
86-
let fsharpLintToolResult: Result<FSharpLintToolFound, FSharpLintToolError> =
87-
findFSharpLintTool folder
88-
89-
match fsharpLintToolResult with
90-
| Ok(FSharpLintToolFound(version, startInfo)) ->
91-
tryGetVersionFromCache state version folder replyChannel
92-
|> Option.defaultWith(fun () ->
93-
let createDaemonResult = createFor startInfo
94-
95-
match createDaemonResult with
96-
| Ok daemon ->
97-
replyChannel.Reply(Ok daemon.RpcClient)
98-
99-
{ Daemons = Map.add version daemon state.Daemons
100-
FolderToVersion = Map.add folder version state.FolderToVersion }
101-
| Error pse ->
102-
replyChannel.Reply(Error(GetDaemonError.FSharpLintProcessStart pse))
103-
state
104-
)
105-
| Error FSharpLintToolError.NoCompatibleVersionFound ->
106-
replyChannel.Reply(Error GetDaemonError.InCompatibleVersionFound)
107-
state
108-
| Error(FSharpLintToolError.DotNetListError dotNetToolListError) ->
109-
replyChannel.Reply(Error(GetDaemonError.DotNetToolListError dotNetToolListError))
110-
state
111-
| Reset replyChannel ->
112-
Map.toList state.Daemons
113-
|> List.iter (fun (_, daemon) -> (daemon :> IDisposable).Dispose())
114-
115-
replyChannel.Reply()
116-
ServiceState.Empty
117-
118-
return! messageLoop nextState
119-
}
120-
121-
messageLoop ServiceState.Empty),
122-
cancellationToken = ct
123-
)
105+
|> Option.defaultWith(fun () -> createDaemon())
106+
| Error FSharpLintToolError.NoCompatibleVersionFound ->
107+
replyChannel.Reply(Error GetDaemonError.InCompatibleVersionFound)
108+
state
109+
| Error(FSharpLintToolError.DotNetListError dotNetToolListError) ->
110+
replyChannel.Reply(Error(GetDaemonError.DotNetToolListError dotNetToolListError))
111+
state
112+
| Reset replyChannel ->
113+
Map.toList state.Daemons
114+
|> List.iter (fun (_, daemon) -> (daemon :> IDisposable).Dispose())
115+
116+
replyChannel.Reply()
117+
ServiceState.Empty
118+
119+
return! messageLoop nextState
120+
}
121+
122+
messageLoop ServiceState.Empty
123+
124+
MailboxProcessor.Start(processor, cancellationToken = ct)
124125

125126
type FSharpLintServiceError =
126127
| DaemonNotFound of GetDaemonError
@@ -253,10 +254,7 @@ type LSPFSharpLintService() =
253254
|> Result.bind (getDaemon agent)
254255
|> Result.map (fun client ->
255256
client
256-
.InvokeWithCancellationAsync<string>(
257-
Methods.Version,
258-
cancellationToken = Option.defaultValue cts.Token cancellationToken
259-
)
257+
.InvokeWithCancellationAsync<string>(Methods.Version, cancellationToken = Option.defaultValue cts.Token cancellationToken)
260258
.ContinueWith(fun (task: Task<string>) ->
261259
{ Code = int FSharpLintResponseCode.OkCurrentDaemonVersion
262260
Result = Content task.Result

0 commit comments

Comments
 (0)