Skip to content

Commit b33f201

Browse files
xperiandriclaude
andcommitted
Put what System holds first among the qualifications too
The three qualifications offered were the first three alphabetically, so for `WriteLine` `Microsoft.VisualBasic.FileSystem.WriteLine` took a slot and `System.Diagnostics.Trace.WriteLine` was cut. They are now ordered the way the opens already were, before the list is cut. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7fa6d34 commit b33f201

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎vsintegration/src/FSharp.Editor/CodeFixes/AddOpenCodeFixProvider.fs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ type internal AddOpenCodeFixProvider [<ImportingConstructor>] (assemblyContentPr
2727

2828
// Which assembly the entity crawler reached first is no order to offer suggestions in. Sort them
2929
// the way Roslyn's add-import fix does: what `System` holds first, the rest alphabetically after.
30-
let suggestionOrder (declaration: string) =
31-
let opened = declaration.Substring(declaration.LastIndexOf ' ' + 1)
30+
let systemFirst (name: ReadOnlySpan<char>) (sortText: string) =
31+
let isInSystem =
32+
name.Equals("System".AsSpan(), StringComparison.Ordinal)
33+
|| name.StartsWith("System.".AsSpan(), StringComparison.Ordinal)
3234

33-
let isSystem =
34-
opened.Equals("System", StringComparison.Ordinal)
35-
|| opened.StartsWith("System.", StringComparison.Ordinal)
35+
(if isInSystem then 0 else 1), sortText
3636

37-
(if isSystem then 0 else 1), declaration
37+
let openOrder (declaration: string) =
38+
systemFirst (declaration.AsSpan(declaration.LastIndexOf ' ' + 1)) declaration
39+
40+
let qualificationOrder (fullName: string) =
41+
systemFirst (fullName.AsSpan()) fullName
3842

3943
let fixUnderscoresInMenuText (text: string) = text.Replace("_", "__")
4044

@@ -109,7 +113,7 @@ type internal AddOpenCodeFixProvider [<ImportingConstructor>] (assemblyContentPr
109113
|> Seq.distinctBy (fun (name, _) -> name)
110114
|> Seq.sortBy fst
111115
|> Seq.toArray)
112-
|> Seq.sortBy (fst >> suggestionOrder)
116+
|> Seq.sortBy (fst >> openOrder)
113117
|> Seq.map (fun (declaration, names) ->
114118
let multipleNames = names |> Array.length > 1
115119
names |> Seq.map (fun (name, ctx) -> declaration, name, ctx, multipleNames))
@@ -121,7 +125,7 @@ type internal AddOpenCodeFixProvider [<ImportingConstructor>] (assemblyContentPr
121125
|> Seq.filter (fun (entity, _, _) -> not (entity.LastIdent.StartsWith "op_")) // Don't include qualified operator names. The resultant codefix won't compile because it won't be an infix operator anymore.
122126
|> Seq.map (fun (entity, _, _) -> entity.FullRelativeName, entity.Qualifier)
123127
|> Seq.distinct
124-
|> Seq.sort
128+
|> Seq.sortBy (fst >> qualificationOrder)
125129
|> Seq.truncate maxQualifySuggestions
126130
|> Seq.map (qualifySymbolFix firstIdentSpan)
127131

‎vsintegration/tests/FSharp.Editor.Tests/CodeFixes/AddOpenOnTopOnTests.fs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,28 @@ let write () = WriteLine "hi"
680680

681681
Assert.Equal<TestCodeFix list>(expected, actual)
682682

683+
[<Fact>] // The qualifications are capped as well, so what `System` holds goes first there too
684+
let ``Offers the System qualifications first`` () =
685+
let code =
686+
"""module Module1
687+
688+
let write () = WriteLine "hi"
689+
"""
690+
691+
let expected =
692+
[
693+
"System.Console.WriteLine"
694+
"System.Diagnostics.Debug.WriteLine"
695+
"System.Diagnostics.Trace.WriteLine"
696+
]
697+
698+
let actual =
699+
allFixes code Auto
700+
|> List.map _.Message
701+
|> List.filter (fun message -> not (message.StartsWith("open ", StringComparison.Ordinal)))
702+
703+
Assert.Equal<string list>(expected, actual)
704+
683705
[<Fact>] // NEGATIVE: `open type` needs the type arguments of a generic type, and the name does not carry them
684706
let ``Doesn't offer to open a generic type`` () =
685707
let code =

0 commit comments

Comments
 (0)