Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAngryByrd committed Apr 15, 2024
1 parent f83ff26 commit 3a5d660
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 96 deletions.
81 changes: 29 additions & 52 deletions src/FsAutoComplete.Core/AdaptiveExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,25 @@ module AdaptiveExtensions =

member cts.TryDispose() =
// try
if not <| isNull cts then
cts.Dispose()
// with _ -> ()
if not <| isNull cts then
cts.Dispose()
// with _ -> ()


type TaskCompletionSource<'a> with

/// https://github.com/dotnet/runtime/issues/47998
member tcs.TrySetFromTask(real: Task<'a>) =

real.ContinueWith(fun (task : Task<_>) ->
real.ContinueWith(fun (task: Task<_>) ->
match task.Status with
| TaskStatus.RanToCompletion -> tcs.TrySetResult task.Result |> ignore<bool>
| TaskStatus.Canceled -> tcs.TrySetCanceled(TaskCanceledException(task).CancellationToken) |> ignore<bool>
| TaskStatus.Canceled ->
tcs.TrySetCanceled(TaskCanceledException(task).CancellationToken)
|> ignore<bool>
| TaskStatus.Faulted ->
logger.error(
Log.setMessage "Error in TrySetFromTask with {count}"
>> Log.addExn task.Exception.InnerException
>> Log.addContext "count" task.Exception.InnerExceptions.Count
)
logger.warn(
Log.setMessage "task.Exception.StackTrace {trace}"
>> Log.addContext "trace" task.Exception.StackTrace
)
logger.warn(
Log.setMessage "task.Exception.StackTrace {trace}"
>> Log.addContext "trace" task.Exception.InnerException.StackTrace
)
// if task.Exception.StackTrace = null then
tcs.TrySetException(task.Exception.InnerExceptions) |> ignore<bool>
// else
// tcs.TrySetException(task.Exception) |> ignore<bool>

| _ -> ())
|> ignore<Task>

Expand Down Expand Up @@ -416,9 +403,9 @@ and AdaptiveCancellableTask<'a>(cancel: unit -> unit, real: Task<'a>) =
if real.IsCompleted then
real
else
cachedTcs <- new TaskCompletionSource<'a>()
cachedTcs.TrySetFromTask real
cachedTcs.Task
cachedTcs <- new TaskCompletionSource<'a>()
cachedTcs.TrySetFromTask real
cachedTcs.Task

cached <-
match cached with
Expand All @@ -434,12 +421,12 @@ and AdaptiveCancellableTask<'a>(cancel: unit -> unit, real: Task<'a>) =
cached

/// <summary>Will run the cancel function passed into the constructor and set the output Task to cancelled state.</summary>
member x.Cancel(cancellationToken : CancellationToken) =
member x.Cancel(cancellationToken: CancellationToken) =
lock x (fun () ->
cancel ()

if not <| isNull cachedTcs then
cachedTcs.TrySetCanceled(cancellationToken) |> ignore<bool>
)
cachedTcs.TrySetCanceled(cancellationToken) |> ignore<bool>)

/// <summary>The output of the passed in task to the constructor.</summary>
/// <returns></returns>
Expand Down Expand Up @@ -684,13 +671,11 @@ module AsyncAVal =
member x.Compute t =
if x.OutOfDate || Option.isNone cache then
let ref =
RefCountingTaskCreator(
fun ct -> task {
RefCountingTaskCreator(fun ct ->
task {
let v = input.GetValue t

use _s =
ct.Register(fun () ->
v.Cancel(ct))
use _s = ct.Register(fun () -> v.Cancel(ct))

let! i = v.Task

Expand All @@ -700,8 +685,7 @@ module AsyncAVal =
let! b = mapping i ct
dataCache <- ValueSome(struct (i, b))
return b
}
)
})

cache <- Some ref
ref.New()
Expand Down Expand Up @@ -736,8 +720,8 @@ module AsyncAVal =
member x.Compute t =
if x.OutOfDate || Option.isNone cache then
let ref =
RefCountingTaskCreator(
fun ct -> task {
RefCountingTaskCreator(fun ct ->
task {
let ta = ca.GetValue t
let tb = cb.GetValue t

Expand All @@ -755,8 +739,7 @@ module AsyncAVal =
let! vc = mapping ia ib ct
dataCache <- ValueSome(struct (ia, ib, vc))
return vc
}
)
})

cache <- Some ref
ref.New()
Expand Down Expand Up @@ -790,12 +773,10 @@ module AsyncAVal =
if x.OutOfDate then
if Interlocked.Exchange(&inputChanged, 0) = 1 || Option.isNone cache then
let outerTask =
RefCountingTaskCreator(
fun ct -> task {
RefCountingTaskCreator(fun ct ->
task {
let v = value.GetValue t
use _s =
ct.Register(fun () ->
v.Cancel(ct))
use _s = ct.Register(fun () -> v.Cancel(ct))

let! i = v.Task

Expand All @@ -806,22 +787,19 @@ module AsyncAVal =
outerDataCache <- Some(i, inner)
return inner

}
)
})

cache <- Some outerTask

let outerTask = cache.Value

let ref =
RefCountingTaskCreator(
fun ct -> task {
RefCountingTaskCreator(fun ct ->
task {

let inner = outerTask.New()

use _s =
ct.Register(fun () ->
inner.Cancel(ct))
use _s = ct.Register(fun () -> inner.Cancel(ct))

let! inner = inner.Task

Expand All @@ -836,8 +814,7 @@ module AsyncAVal =

return! innerTask.Task

}
)
})

innerCache <- Some ref

Expand Down
35 changes: 14 additions & 21 deletions src/FsAutoComplete.Core/CompilerServiceInterface.fs
Original file line number Diff line number Diff line change
Expand Up @@ -318,26 +318,19 @@ type FSharpCompilerServiceChecker(hasAnalyzers, typecheckCacheSize, parallelRefe
| (true, v) -> Some v
| _ -> None

member _.TryGetRecentCheckResultsForFile
(
file: string<LocalPath>,
snapshot: FSharpProjectSnapshot
) =
let opName = sprintf "TryGetRecentCheckResultsForFile - %A" file
member _.TryGetRecentCheckResultsForFile(file: string<LocalPath>, snapshot: FSharpProjectSnapshot) =
let opName = sprintf "TryGetRecentCheckResultsForFile - %A" file

checkerLogger.info (Log.setMessage "{opName} - {hash}" >> Log.addContextDestructured "opName" opName)

checker.TryGetRecentCheckResultsForFile(UMX.untag file, snapshot, opName)
|> Option.map (fun (pr, cr) ->
checkerLogger.info (
Log.setMessage "{opName} - {hash}"
Log.setMessage "{opName} - got results - {version}"
>> Log.addContextDestructured "opName" opName
)

checker.TryGetRecentCheckResultsForFile(UMX.untag file, snapshot, opName)
|> Option.map (fun (pr, cr) ->
checkerLogger.info (
Log.setMessage "{opName} - got results - {version}"
>> Log.addContextDestructured "opName" opName
)

ParseAndCheckResults(pr, cr, entityCache))
ParseAndCheckResults(pr, cr, entityCache))


member _.GetUsesOfSymbol
Expand Down Expand Up @@ -370,34 +363,34 @@ type FSharpCompilerServiceChecker(hasAnalyzers, typecheckCacheSize, parallelRefe
return res |> Array.concat
}

member x.FindReferencesForSymbolInFile(file : string<LocalPath>, project: FSharpProjectSnapshot, symbol) =
member x.FindReferencesForSymbolInFile(file: string<LocalPath>, project: FSharpProjectSnapshot, symbol) =
async {
checkerLogger.info (
Log.setMessage "FindReferencesForSymbolInFile - {file} - {projectFile}"
>> Log.addContextDestructured "file" file
>> Log.addContextDestructured "projectFile" project.ProjectFileName
)

let file = UMX.untag file
// let file =
// file.Substring(0, 1).ToUpper() + file.Substring(1)
try
// let! _ = checker.ParseAndCheckFileInProject(file, project)
let! results = checker.FindBackgroundReferencesInFile(file, project, symbol, userOpName = "find references")

checkerLogger.info (
Log.setMessage "FindReferencesForSymbolInFile - {file} - {projectFile} - {results}"
>> Log.addContextDestructured "file" file
>> Log.addContextDestructured "projectFile" project.ProjectFileName
>> Log.addContextDestructured "projectFile" project.ProjectFileName
>> Log.addContextDestructured "results" results
)

return results
with e ->
checkerLogger.error (
Log.setMessage "FindReferencesForSymbolInFile - {file} - {projectFile}"
>> Log.addContextDestructured "projectFile" project.ProjectFileName
>> Log.addContextDestructured "projectFile" project.ProjectFileName
>> Log.addContextDestructured "file" file
>> Log.addExn e
)

return [||]
}

Expand Down
1 change: 1 addition & 0 deletions src/FsAutoComplete.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module Seq =

module ProcessHelper =
open IcedTasks

let WaitForExitAsync (p: Process) =
asyncEx {
let tcs = TaskCompletionSource<obj>()
Expand Down
29 changes: 12 additions & 17 deletions src/FsAutoComplete/LspServers/AdaptiveServerState.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ type FindFirstProject() =
|> Seq.sortBy (fun p -> p.ProjectFileName)
|> Seq.tryFind (fun p -> p.SourceFilesTagged |> Array.exists (fun f -> f = sourceFile))
|> Result.ofOption (fun () ->
let allProjects = String.join ", " (projects |> Seq.map (fun p -> p.ProjectFileName))
let allProjects =
String.join ", " (projects |> Seq.map (fun p -> p.ProjectFileName))

$"Couldn't find a corresponding project for {sourceFile}. \n Projects include {allProjects}. \nHave the projects loaded yet or have you tried restoring your project/solution?")


Expand Down Expand Up @@ -1024,8 +1026,7 @@ type AdaptiveState(lspClient: FSharpLspClient, sourceTextFactory: ISourceTextFac
let! projects =
// need to bind to a single value to keep the threadpool from being exhausted as LoadingProjects can be a long running operation
// and when other adaptive values await on this, the scheduler won't block those other tasks
loadProjects loader binlogConfig projects
|> AMap.toAVal
loadProjects loader binlogConfig projects |> AMap.toAVal

and! checker = checker
checker.ClearCaches()
Expand Down Expand Up @@ -1079,23 +1080,15 @@ type AdaptiveState(lspClient: FSharpLspClient, sourceTextFactory: ISourceTextFac
// |> Observable.subscribe (fun _ -> forceLoadProjects () |> ignore<list<LoadedProject>>)
// |> disposables.Add

let AMapReKeyMany f map =
map
|> AMap.toASet
|> ASet.collect f
|> AMap.ofASet
let AMapReKeyMany f map = map |> AMap.toASet |> ASet.collect f |> AMap.ofASet

let sourceFileToProjectOptions =
asyncAVal {
let! loadedProjects = loadedProjects

let sourceFileToProjectOptions =
loadedProjects
|> AMapReKeyMany(fun (_,v) ->
v.SourceFilesTagged
|> ASet.ofArray
|> ASet.map(fun source -> source, v)
)
|> AMapReKeyMany(fun (_, v) -> v.SourceFilesTagged |> ASet.ofArray |> ASet.map (fun source -> source, v))
|> AMap.map' HashSet.toList

return sourceFileToProjectOptions
Expand Down Expand Up @@ -1477,13 +1470,12 @@ type AdaptiveState(lspClient: FSharpLspClient, sourceTextFactory: ISourceTextFac
=
asyncEx {
let tags =
[
SemanticConventions.fsac_sourceCodePath, box (UMX.untag file.Source.FileName)
[ SemanticConventions.fsac_sourceCodePath, box (UMX.untag file.Source.FileName)
SemanticConventions.projectFilePath, box (options.ProjectFileName)
"source.text", box (file.Source.String)
"source.version", box (file.Version)

]
]

use _ = fsacActivitySource.StartActivityForType(thisType, tags = tags)

Expand Down Expand Up @@ -2185,7 +2177,9 @@ type AdaptiveState(lspClient: FSharpLspClient, sourceTextFactory: ISourceTextFac

let dependentProjectsAndSourceFiles =
dependentProjects
|> List.collect (fun (snap) -> snap.SourceFiles |> List.map (fun sourceFile -> snap, Utils.normalizePath sourceFile.FileName))
|> List.collect (fun (snap) ->
snap.SourceFiles
|> List.map (fun sourceFile -> snap, Utils.normalizePath sourceFile.FileName))
|> List.toArray

let mutable checksCompleted = 0
Expand All @@ -2203,6 +2197,7 @@ type AdaptiveState(lspClient: FSharpLspClient, sourceTextFactory: ISourceTextFac
Array.concat [| dependentFiles; dependentProjectsAndSourceFiles |]
|> Array.filter (fun (_, file) ->
let file = UMX.untag file

file.Contains "AssemblyInfo.fs" |> not
&& file.Contains "AssemblyAttributes.fs" |> not)

Expand Down
12 changes: 6 additions & 6 deletions src/FsAutoComplete/LspServers/ProjectWorkspace.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ module Snapshots =
unresolvedReferences
originalLoadReferences

let private createFSharpFileSnapshotOnDisk (sourceTextFactory: aval<ISourceTextFactory>) (sourceFilePath: string<LocalPath>) =
let private createFSharpFileSnapshotOnDisk
(sourceTextFactory: aval<ISourceTextFactory>)
(sourceFilePath: string<LocalPath>)
=
aval {
let file = UMX.untag sourceFilePath
let! writeTime = AdaptiveFile.GetLastWriteTimeUtc file
Expand Down Expand Up @@ -236,8 +239,7 @@ module Snapshots =
| None -> return! createFSharpFileSnapshotOnDisk sourceTextFactory sourcePath
})

let references =
p.OtherOptions |> List.filter (fun x -> x.StartsWith("-r:"))
let references = p.OtherOptions |> List.filter (fun x -> x.StartsWith("-r:"))

let otherOptions = p.OtherOptions |> ASet.ofList |> ASet.map (AVal.constant)

Expand Down Expand Up @@ -292,7 +294,5 @@ module Snapshots =
let optionsToSnapshot =
optionsToSnapshot cachedSnapshots inMemorySourceFiles sourceTextFactory mapReferences

ps
|> HashMap.map (fun _ v -> (v, optionsToSnapshot v))
)
ps |> HashMap.map (fun _ v -> (v, optionsToSnapshot v)))
|> AMap.ofAVal

0 comments on commit 3a5d660

Please sign in to comment.