Skip to content

Commit

Permalink
Server.fs: load solution-in-sync when initializing
Browse files Browse the repository at this point in the history
This will help "server initializing" notification work better for
clients that depend on `initialize` request not to complete until the
server/solution is properly loaded initialized.

should fix #40 (lsp-mode loading spinner no longer working)
  • Loading branch information
razzmatazz committed Aug 15, 2022
1 parent 0ec23a0 commit c29fe97
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
13 changes: 11 additions & 2 deletions src/CSharpLanguageServer/RoslynHelpers.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module CSharpLanguageServer.RoslynHelpers

open System
open System.Linq
open System.Collections.Generic
open System.IO
open System.Reflection
open System.Threading
open Ionide.LanguageServerProtocol.Types
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.CodeActions
Expand Down Expand Up @@ -574,6 +572,17 @@ let findAndLoadSolutionOnDir logMessage dir = async {
return solution
}

let loadSolutionOnSolutionPathOrCwd logMessage solutionPathMaybe =
match solutionPathMaybe with
| Some solutionPath ->
logMessage (sprintf "loading specified solution file: %s.." solutionPath)
tryLoadSolutionOnPath logMessage solutionPath

| None ->
let cwd = Directory.GetCurrentDirectory()
logMessage (sprintf "attempting to find and load solution based on cwd: \"%s\".." cwd)
findAndLoadSolutionOnDir logMessage cwd

let getRoslynCodeActions (doc: Document) (textSpan: TextSpan): Async<CodeAction list> = async {
let! ct = Async.CancellationToken

Expand Down
12 changes: 6 additions & 6 deletions src/CSharpLanguageServer/Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ let setupServerHandlers options (lspClient: LspClient) =
}
}

initializeResult |> success |> async.Return
async {
// load solution (on stateActor)
do! stateActor.PostAndAsyncReply(SolutionReloadInSync)

return initializeResult |> success
}

let handleInitialized (scope: ServerRequestScope) (_p: InitializedParams): Async<LspResult<unit>> =
logMessage "\"initialized\" notification received from client"
Expand All @@ -238,11 +243,6 @@ let setupServerHandlers options (lspClient: LspClient) =
| Ok _ -> ()
| Error error -> infoMessage (sprintf " ...didChangeWatchedFiles registration has failed with %s" (error |> string))

//
// start solution loading (on stateActor)
//
scope.Emit(SolutionReload)

return LspResult.Ok()
}

Expand Down
22 changes: 8 additions & 14 deletions src/CSharpLanguageServer/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type ServerStateEvent =
| ProcessRequestQueue
| SolutionReloadRequest
| SolutionReload
| SolutionReloadInSync of AsyncReplyChannel<unit>
| PeriodicTimerTick

let getDocumentForUriOfType state docType (u: string) =
Expand Down Expand Up @@ -224,20 +225,13 @@ let processServerEvent logMessage state postMsg msg: Async<ServerState> = async
return { state with SolutionReloadPending = DateTime.Now.AddSeconds(5) |> Some }

| SolutionReload ->
let! solution =
match state.Options.SolutionPath with
| Some solutionPath -> async {
logMessage (sprintf "loading specified solution file: %s.." solutionPath)
return! tryLoadSolutionOnPath logMessage solutionPath
}

| None -> async {
let cwd = Directory.GetCurrentDirectory()
logMessage (sprintf "attempting to find and load solution based on cwd: \"%s\".." cwd)
return! findAndLoadSolutionOnDir logMessage cwd
}

return { state with Solution = solution }
let! newSolution = loadSolutionOnSolutionPathOrCwd logMessage state.Options.SolutionPath
return { state with Solution = newSolution }

| SolutionReloadInSync replyChannel ->
let! newSolution = loadSolutionOnSolutionPathOrCwd logMessage state.Options.SolutionPath
replyChannel.Reply(())
return { state with Solution = newSolution }

| PeriodicTimerTick ->
let solutionReloadTime = state.SolutionReloadPending
Expand Down

0 comments on commit c29fe97

Please sign in to comment.