Skip to content

Commit

Permalink
Fix reference count positions (#1319)
Browse files Browse the repository at this point in the history
Forces reference lenses to use up to date typecheck results to prevent floating around.
  • Loading branch information
TheAngryByrd authored Sep 4, 2024
1 parent df1f95a commit 3f6d1bd
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 108 deletions.
43 changes: 28 additions & 15 deletions src/FsAutoComplete/LspServers/AdaptiveServerState.fs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ type AdaptiveWorkspaceChosen =





[<CustomEquality; NoComparison>]
type LoadedProject =
{ ProjectOptions: Types.ProjectOptions
Expand Down Expand Up @@ -1280,20 +1282,22 @@ type AdaptiveState

/// <summary>Parses a source code for a file and caches the results. Returns an AST that can be traversed for various features.</summary>
/// <param name="checker">The FSharpCompilerServiceChecker.</param>
/// <param name="sourceFilePath">The source to be parsed.</param>
/// <param name="file">The source to be parsed.</param>
/// <param name="compilerOptions"></param>
/// <returns></returns>
let parseFile (checker: FSharpCompilerServiceChecker) (sourceFilePath) (compilerOptions: CompilerProjectOption) =

let parseFile (checker: FSharpCompilerServiceChecker) (file: VolatileFile) (compilerOptions: CompilerProjectOption) =
task {
let! result =
match compilerOptions with
| CompilerProjectOption.TransparentCompiler snap ->
taskResult { return! checker.ParseFile(sourceFilePath, snap) }
taskResult { return! checker.ParseFile(file.FileName, snap) }
| CompilerProjectOption.BackgroundCompiler opts ->
taskResult {
let! file = forceFindOpenFileOrRead sourceFilePath
return! checker.ParseFile(sourceFilePath, file.Source, opts)


return! checker.ParseFile(file.FileName, file.Source, opts)
}

let! ct = Async.CancellationToken
Expand Down Expand Up @@ -1324,8 +1328,12 @@ type AdaptiveState
|> HashSet.toArray
|> Array.collect (fun (snap) -> snap.SourceFilesTagged |> List.toArray |> Array.map (fun s -> snap, s))
|> Array.map (fun (snap, filePath) ->
taskResult {
let! vFile = forceFindOpenFileOrRead filePath
return! parseFile checker vFile snap

})

parseFile checker filePath snap)
|> Task.WhenAll
}

Expand Down Expand Up @@ -1435,14 +1443,19 @@ type AdaptiveState

let allFSharpFilesAndProjectOptions =
asyncAVal {
let wins =
openFilesToChangesAndProjectOptions
|> AMap.map (fun _k v -> v |> AsyncAVal.mapSync (fun (_, projects) _ -> projects))
let wins = openFilesToChangesAndProjectOptions

let! sourceFileToProjectOptions = sourceFileToProjectOptions

let loses =
sourceFileToProjectOptions |> AMap.map (fun _ v -> AsyncAVal.constant (Ok v))
sourceFileToProjectOptions
|> AMap.map (fun file proj ->
asyncAVal {
let! lastTouched = AdaptiveFile.GetLastWriteTimeUtc(UMX.untag file)
let! vFile = createVolatileFileFromDisk lastTouched file

return vFile, Ok proj
})

return AMap.union loses wins
}
Expand All @@ -1462,7 +1475,7 @@ type AdaptiveState

return
allFSharpFilesAndProjectOptions
|> AMapAsync.mapAsyncAVal (fun filePath (options: Result<LoadedProject list, string>) _ctok ->
|> AMapAsync.mapAsyncAVal (fun filePath (file, options) _ctok ->
asyncAVal {
let! (checker: FSharpCompilerServiceChecker) = checker
and! selectProject = projectSelector
Expand All @@ -1473,7 +1486,7 @@ type AdaptiveState
match loadedProject with
| Ok x ->
let! snap = x.FSharpProjectCompilerOptions
let! r = parseFile checker filePath snap
let! r = parseFile checker file snap
return r
| Error e -> return Error e
})
Expand Down Expand Up @@ -1505,7 +1518,7 @@ type AdaptiveState

return
set
|> Array.choose (fun (k, v) ->
|> Array.choose (fun (k, (_, v)) ->
v
|> Result.bind (findProject k)
|> Result.toOption
Expand All @@ -1524,7 +1537,7 @@ type AdaptiveState
|> Array.map (AsyncAVal.forceAsync)
|> Async.parallel75

let set = set |> Array.choose (Result.toOption)
let set = set |> Array.choose (snd >> Result.toOption)

return set |> Array.collect (List.toArray)
}
Expand All @@ -1539,7 +1552,7 @@ type AdaptiveState
let! allFilesToFSharpProjectOptions = allFilesToFSharpProjectOptions

match! allFilesToFSharpProjectOptions |> AMapAsync.tryFindA filePath with
| Some projs -> return projs
| Some(_, projs) -> return projs
| None -> return Error $"Couldn't find project for {filePath}. Have you tried restoring your project/solution?"
}

Expand Down
Loading

0 comments on commit 3f6d1bd

Please sign in to comment.