Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetDependentFiles to slim service. #14

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions fcs/service_slim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeBasics
open FSharp.Compiler.TypedTreeOps
open FSharp.Compiler.BuildGraph
open FSharp.Compiler.GraphChecking

//-------------------------------------------------------------------------
// InteractiveChecker
Expand Down Expand Up @@ -403,3 +404,50 @@ type InteractiveChecker internal (compilerStateCache) =

return (parseFileResults, checkFileResults, projectResults)
}

/// Find the dependent files of the current file based on the untyped syntax tree
member _.GetDependentFiles(currentFileName: string, fileNames: string[], sourceReader: string -> int * Lazy<string>) = async {
// parse files
let! ct = Async.CancellationToken
let! compilerState = compilerStateCache.Get()
// parse files
let parsingOptions = FSharpParsingOptions.FromTcConfig(compilerState.tcConfig, fileNames, false)
let parseFile fileName =
let sourceHash, source = sourceReader fileName
ParseFile (fileName, sourceHash, source, parsingOptions, compilerState, ct)

// TODO: not sure if parse cache issues still applies
let parseResults = fileNames |> Array.Parallel.map parseFile
let sourceFiles: FileInProject array =
parseResults
|> Array.mapi (fun idx (parseResults: FSharpParseFileResults) ->
let input = parseResults.ParseTree
{
Idx = idx
FileName = input.FileName
ParsedInput = input
})

let currentFileIdx = Array.IndexOf(fileNames, currentFileName)

let filePairs = FilePairMap(sourceFiles)
let graph, _trie = DependencyResolution.mkGraph filePairs sourceFiles

let dependentFiles =
graph.Keys
|> Seq.choose (fun fileIndex ->
let isDependency =
// Only files that came after the current file are relevant
fileIndex > currentFileIdx
&&
// The current file is listed as a dependency
Array.contains currentFileIdx graph.[fileIndex]
if not isDependency then
None
else
Some(Array.item fileIndex fileNames)
)
|> Seq.toArray

return dependentFiles
}