Skip to content

Commit

Permalink
Add GetDependentFiles to slim service. (dotnet#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Oct 25, 2023
1 parent c0e7fb2 commit ad39ec6
Showing 1 changed file with 48 additions and 0 deletions.
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
}

0 comments on commit ad39ec6

Please sign in to comment.