Skip to content

Commit

Permalink
Reload projects after delay of being marked out of date
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAngryByrd committed Mar 14, 2023
1 parent a80fdd5 commit 01c6958
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ open Ionide.LanguageServerProtocol.Types.LspResult
open Ionide.LanguageServerProtocol.Types
open Newtonsoft.Json.Linq
open Ionide.ProjInfo.ProjectSystem
open System.Reactive
open System.Reactive.Linq

open FSharp.Control.Reactive
open FsToolkit.ErrorHandling
open FsOpenTelemetry
open FsAutoComplete.Utils.Tracing
Expand Down Expand Up @@ -129,6 +132,19 @@ module AVal =
result }
:> aval<_>

/// <summary>
/// Creates observables from adaptive values
/// </summary>
module Observable =
/// <summary>
/// Creates an observable with the given object that will be executed whenever the object gets marked out-of-date. Note that it does not trigger when the object is currently out-of-date.
/// </summary>
/// <param name="aval">The aval to get out-of-date information from.</param>
/// <returns>An observable</returns>
let onWeakMarking (aval : #aval<_>) =
Observable.Create(fun (obs: IObserver<unit>) ->
aval.AddWeakMarkingCallback(fun () -> obs.OnNext() )
)

module ASet =
/// Creates an amap with the keys from the set and the values given by mapping and
Expand Down Expand Up @@ -180,7 +196,7 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar

let thisType = typeof<AdaptiveFSharpLspServer>

let disposables = new System.Reactive.Disposables.CompositeDisposable()
let disposables = new Disposables.CompositeDisposable()

let rootPath = cval<string option> None

Expand Down Expand Up @@ -811,7 +827,21 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
return options |> List.map fst
}

let forceLoadProjects () = loadedProjectOptions |> AVal.force
/// <summary>
/// Evaluates the adaptive value <see cref='F:loadedProjectOptions '/> and returns its current value.
/// This should not be used inside the adaptive evaluation of other AdaptiveObjects since it does not track dependencies.
/// </summary>
/// <returns>A list of FSharpProjectOptions</returns>
let forceLoadProjects () =
loadedProjectOptions |> AVal.force

do
// Reload Projects with some debouncing if `loadedProjectOptions` is out of date.
AVal.Observable.onWeakMarking loadedProjectOptions
|> Observable.throttleOn Concurrency.NewThreadScheduler.Default (TimeSpan.FromMilliseconds(200.))
|> Observable.observeOn Concurrency.NewThreadScheduler.Default
|> Observable.subscribe(forceLoadProjects >> ignore)
|> disposables.Add


let sourceFileToProjectOptions =
Expand Down Expand Up @@ -2085,8 +2115,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar

try
logger.info (Log.setMessage "Initialized request {p}" >> Log.addContextDestructured "p" p)
// Starts getting project options to cache
forceLoadProjects () |> ignore

return ()
with e ->
Expand Down Expand Up @@ -4097,8 +4125,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> HashSet.single
|> WorkspaceChosen.Projs)

forceLoadProjects () |> ignore

return! Helpers.notImplemented
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4193,7 +4219,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore // mapping unit option to unit

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4223,7 +4248,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4253,7 +4277,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4451,7 +4474,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4482,7 +4504,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4513,7 +4534,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4543,7 +4563,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4573,7 +4592,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4604,7 +4622,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down Expand Up @@ -4636,7 +4653,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
let fileUri = Path.FilePathToUri fullPath
diagnosticCollections.ClearFor fileUri

Expand Down Expand Up @@ -4669,7 +4685,6 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore
Expand Down

0 comments on commit 01c6958

Please sign in to comment.