Skip to content

Commit f56537e

Browse files
xperiandriclaude
andcommitted
Search a project's priority documents first in Navigate To
IFSharpNavigateToSearchService.SearchProjectAsync is handed the documents the user has open, and F# named the parameter and ignored it: every document of the project went through the same throttled pass in project order. The search that runs while the solution loads already puts them first, as does Roslyn's own C# and Visual Basic search. Search the priority documents in one throttled pass and the rest of the project in a second, so the files in view are parsed and cached before the others. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 6236f19 commit f56537e

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎vsintegration/src/FSharp.Editor/Navigation/NavigateToSearchService.fs‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,31 @@ type internal FSharpNavigateToSearchService
244244

245245
interface IFSharpNavigateToSearchService with
246246
member _.SearchProjectAsync
247-
(project, _priorityDocuments, searchPattern, kinds, cancellationToken)
247+
(project, priorityDocuments, searchPattern, kinds, cancellationToken)
248248
: Task<ImmutableArray<FSharpNavigateToSearchResult>> =
249249
cancellableTask {
250250
let tryMatch = createMatcherFor searchPattern
251+
let priorityIds = ImmutableHashSet.CreateRange(priorityDocuments |> Seq.map _.Id)
251252

252-
let! results =
253+
let priority, rest =
253254
project.Documents
255+
|> Seq.toArray
256+
|> Array.partition (fun document -> priorityIds.Contains document.Id)
257+
258+
let search documents =
259+
documents
254260
|> Seq.map (processDocument tryMatch kinds)
255261
// Throttle to avoid launching a parse per document in the project all at once.
256262
|> CancellableTask.whenAllThrottled (max 1 Environment.ProcessorCount)
257263

258-
return results |> Array.concat |> Array.toImmutableArray
264+
// The documents the user has open go first, so they are parsed and cached before the rest of the project.
265+
let! priorityResults = search priority
266+
let! restResults = search rest
267+
268+
return
269+
Array.append priorityResults restResults
270+
|> Array.concat
271+
|> Array.toImmutableArray
259272
}
260273
|> CancellableTask.start cancellationToken
261274

‎vsintegration/tests/FSharp.Editor.Tests/NavigateToSearchServiceTests.fs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace FSharp.Editor.Tests
44

55
open Xunit
6+
open System.Collections.Immutable
67
open System.Threading
8+
open Microsoft.CodeAnalysis
79
open Microsoft.CodeAnalysis.Text
810
open Microsoft.VisualStudio.FSharp.Editor
911
open FSharp.Compiler.Text
@@ -72,3 +74,35 @@ module HeyHo =
7274
[<Fact>]
7375
let ``nested containers`` () =
7476
assertResultsContain "hh.a.b.g.d" "Delta"
77+
78+
[<Fact>]
79+
let ``a project's priority documents are searched first`` () =
80+
let projectId = ProjectId.CreateNewId()
81+
82+
let solution =
83+
[
84+
RoslynTestHelpers.CreateDocumentInfo projectId "C:\\First.fs" "module First\n\nlet shared = 1\n"
85+
RoslynTestHelpers.CreateDocumentInfo projectId "C:\\Second.fs" "module Second\n\nlet shared = 2\n"
86+
]
87+
|> RoslynTestHelpers.CreateProjectInfo projectId "C:\\test.fsproj"
88+
|> List.singleton
89+
|> RoslynTestHelpers.CreateSolution
90+
91+
{ RoslynTestHelpers.DefaultProjectOptions with
92+
SourceFiles = [| "C:\\First.fs"; "C:\\Second.fs" |]
93+
}
94+
|> RoslynTestHelpers.SetProjectOptions projectId solution
95+
96+
let project = solution.GetProject projectId
97+
98+
let second =
99+
project.Documents |> Seq.find (fun document -> document.Name = "C:\\Second.fs")
100+
101+
let service: IFSharpNavigateToSearchService = provider.GetExportedValue()
102+
103+
let results =
104+
service
105+
.SearchProjectAsync(project, ImmutableArray.Create second, "shared", service.KindsProvided, CancellationToken.None)
106+
.Result
107+
108+
Assert.Equal<string list>([ "C:\\Second.fs"; "C:\\First.fs" ], results |> Seq.map _.NavigableItem.Document.Name |> Seq.toList)

0 commit comments

Comments
 (0)