@@ -690,6 +690,46 @@ let private dependsOnDefines (defines: string Set) (parseTree: ParsedInput) =
690690 | ConditionalDirectiveTrivia.Else _
691691 | ConditionalDirectiveTrivia.EndIf _ -> false )
692692
693+ /// The files two instances of one project compile identically, counting from the first. F# reads a
694+ /// name's meaning from the files ahead of it, so a file that follows one parsing differently can
695+ /// resolve differently however plain it looks itself - the shared run ends at the first such file.
696+ let private identicallyCompiledPrefix
697+ differingDefines
698+ ( documentsByPath : Dictionary < string , Document >)
699+ ( sourceFiles : string array )
700+ ( searchedSourceFiles : string array )
701+ userOpName
702+ =
703+ cancellableTask {
704+ let mutable index = 0
705+ let mutable diverged = false
706+
707+ while not diverged && index < sourceFiles.Length do
708+ let path = sourceFiles[ index]
709+
710+ let! compilesTheSame =
711+ if
712+ index >= searchedSourceFiles.Length
713+ || not ( String.Equals( path, searchedSourceFiles[ index], StringComparison.OrdinalIgnoreCase))
714+ then
715+ CancellableTask.singleton false
716+ elif Set.isEmpty differingDefines then
717+ CancellableTask.singleton true
718+ else
719+ match documentsByPath.TryGetValue path with
720+ | true , document ->
721+ document.GetFSharpParseResultsAsync userOpName
722+ |> CancellableTask.map ( fun parseResults -> not ( dependsOnDefines differingDefines parseResults.ParseTree))
723+ | _ -> CancellableTask.singleton false
724+
725+ if compilesTheSame then
726+ index <- index + 1
727+ else
728+ diverged <- true
729+
730+ return HashSet( Seq.truncate index sourceFiles, StringComparer.OrdinalIgnoreCase)
731+ }
732+
693733/// How many documents of one project a search keeps in flight. The throttle it shares with the other
694734/// projects decides how many of those actually run.
695735[<Literal>]
@@ -698,9 +738,9 @@ let private WorkersPerProject = 4
698738type Project with
699739
700740 /// Find F# references in the given project. When ` searchedInstance ` is another target-framework
701- /// instance of the same project file that has already been searched, only the documents whose
702- /// sources can differ from it are searched: files compiled only here and files whose conditional
703- /// compilation tests a define the two instances disagree on .
741+ /// instance of the same project file that has already been searched, the leading files both
742+ /// compile identically are left to it; the search starts at the first file that can differ and
743+ /// covers everything after it .
704744 member this.FindFSharpReferencesAsync
705745 (
706746 symbol : FSharpSymbol ,
@@ -740,46 +780,38 @@ type Project with
740780 }
741781 | _ -> CancellableTask.singleton Set.empty
742782
743- let searchedPaths =
744- searchedInstance
745- |> ValueOption.map ( fun instance -> HashSet( instance.Documents |> Seq.map _. FilePath, StringComparer.OrdinalIgnoreCase))
746-
747783 // Only the defines one instance has and the other lacks can make a shared file parse
748784 // differently; the ones they agree on cannot, however many directives test them.
749- let definesOf ( project : Project ) =
785+ let optionsOf ( project : Project ) =
750786 getFSharpOptionsForProject project
751- |> CancellableTask.map ( fun ( _ , _ , parsingOptions : FSharpParsingOptions , _ ) -> Set parsingOptions.ConditionalDefines)
787+ |> CancellableTask.map ( fun ( _ , _ , parsingOptions : FSharpParsingOptions , options : FSharpProjectOptions ) ->
788+ Set parsingOptions.ConditionalDefines, options.SourceFiles)
752789
753- let! differingDefines =
790+ let! alreadyCovered =
754791 match searchedInstance with
755- | ValueNone -> CancellableTask.singleton Set.empty
792+ | ValueNone -> CancellableTask.singleton ( HashSet < string > StringComparer.OrdinalIgnoreCase )
756793 | ValueSome instance ->
757794 cancellableTask {
758- let! defines = definesOf this
759- let! searchedDefines = definesOf instance
760- return ( defines - searchedDefines) + ( searchedDefines - defines)
761- }
795+ let! defines , sourceFiles = optionsOf this
796+ let! searchedDefines , searchedSourceFiles = optionsOf instance
762797
763- let needsSearch ( document : Document ) =
764- match searchedPaths with
765- | ValueSome paths when paths.Contains document.FilePath ->
766- if differingDefines.IsEmpty then
767- CancellableTask.singleton false
768- else
769- document.GetFSharpParseResultsAsync userOpName
770- |> CancellableTask.map ( fun parseResults -> dependsOnDefines differingDefines parseResults.ParseTree )
771- | _ -> CancellableTask.singleton true
798+ let differingDefines = ( defines - searchedDefines ) + ( searchedDefines - defines )
799+
800+ let documentsByPath = Dictionary ( StringComparer.OrdinalIgnoreCase )
801+
802+ for document in this.Documents do
803+ documentsByPath [ document.FilePath ] <- document
804+
805+ return ! identicallyCompiledPrefix differingDefines documentsByPath sourceFiles searchedSourceFiles userOpName
806+ }
772807
773808 let search ( document : Document ) =
774809 cancellableTask {
775810 let! ct = CancellableTask.getCancellationToken ()
776811 do ! throttle.WaitAsync ct
777812
778813 try
779- let! needed = needsSearch document
780-
781- if needed then
782- do ! document.FindFSharpReferencesAsync( symbol, projectSnapshot, onFound document, userOpName)
814+ do ! document.FindFSharpReferencesAsync( symbol, projectSnapshot, onFound document, userOpName)
783815 finally
784816 throttle.Release() |> ignore
785817 }
@@ -788,7 +820,8 @@ type Project with
788820 this.Documents
789821 |> Seq.filter ( fun document ->
790822 isFSharpSourceFile document.FilePath
791- && not ( canSkipDocuments.Contains document.FilePath))
823+ && not ( canSkipDocuments.Contains document.FilePath)
824+ && not ( alreadyCovered.Contains document.FilePath))
792825 // Workers take the next document when they free up. Starting one task per document
793826 // instead would leave every document of the solution parked on the throttle at once.
794827 |> CancellableTask.forEachThrottled WorkersPerProject search
0 commit comments