Skip to content

Commit d2de949

Browse files
authored
Fix record completions in patterns (#15945)
1 parent 1f37bd6 commit d2de949

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/Compiler/Checking/CheckExpressions.fs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,8 +1812,6 @@ let BuildFieldMap (cenv: cenv) env isPartial ty (flds: ((Ident list * Ident) * '
18121812
let g = cenv.g
18131813
let ad = env.eAccessRights
18141814

1815-
if isNil flds then invalidArg "flds" "BuildFieldMap"
1816-
18171815
let allFields = flds |> List.map (fun ((_, ident), _) -> ident)
18181816
if allFields.Length > 1 then
18191817
// In the case of nested record fields on the same level in record copy-and-update.

src/Compiler/Service/ServiceParsedInputOps.fs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ module ParsedInput =
13131313
| _ ->
13141314
pats
13151315
|> List.tryPick (fun pat -> TryGetCompletionContextInPattern false pat None pos)
1316-
| SynPat.Record (fieldPats = pats) ->
1316+
| SynPat.Record (fieldPats = pats; range = m) when rangeContainsPos m pos ->
13171317
pats
13181318
|> List.tryPick (fun ((_, fieldId), _, pat) ->
13191319
if rangeContainsPos fieldId.idRange pos then
@@ -1328,15 +1328,16 @@ module ParsedInput =
13281328
// That is, pos is after the last field and still within braces
13291329
if
13301330
pats
1331-
|> List.forall (fun (_, m, _) ->
1332-
match m with
1333-
| Some m -> rangeBeforePos m pos
1334-
| None -> false)
1331+
|> List.forall (fun (_, mEquals, pat) ->
1332+
match mEquals, pat with
1333+
| Some mEquals, SynPat.Wild mPat -> rangeBeforePos mEquals pos && mPat.StartColumn <> mPat.EndColumn
1334+
| Some mEquals, _ -> rangeBeforePos mEquals pos
1335+
| _ -> false)
13351336
then
13361337
let referencedFields = pats |> List.map (fun ((_, x), _, _) -> x.idText, x.idRange)
13371338
Some(CompletionContext.Pattern(PatternContext.RecordFieldIdentifier referencedFields))
13381339
else
1339-
None)
1340+
Some(CompletionContext.Pattern PatternContext.Other))
13401341
| SynPat.Ands (pats = pats)
13411342
| SynPat.ArrayOrList (elementPats = pats) ->
13421343
pats

vsintegration/tests/FSharp.Editor.Tests/CompletionProviderTests.fs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,14 @@ type R1 = { A: int; B: int }
19351935
type R2 = { C: int; D: int }
19361936
19371937
match [] with
1938-
| [ { A = 2; l = 2 } ]
1938+
| [ { A = 2; l = 2 } ] -> ()
1939+
1940+
match { A = 1; B = 2 } with
1941+
| { A = 1; } -> ()
1942+
| { A = 2; s } -> ()
1943+
| { B = } -> ()
1944+
| { X = ; A = 3 } -> ()
1945+
| { } -> ()
19391946
"""
19401947

19411948
VerifyCompletionList(
@@ -1945,6 +1952,16 @@ match [] with
19451952
[ "A"; "C"; "D"; "DU"; "X"; "log"; "let"; "Lazy" ]
19461953
)
19471954

1955+
VerifyCompletionList(fileContents, "| { A = 1; ", [ "B"; "R1"; "R2" ], [ "C"; "D" ])
1956+
VerifyCompletionList(fileContents, "| { A = 2; s", [ "B"; "R1"; "R2" ], [ "C"; "D" ])
1957+
VerifyCompletionList(fileContents, "| { B =", [ "R1"; "R2"; "Some"; "None"; "System"; "DU" ], [ "A"; "B"; "C"; "D" ])
1958+
VerifyCompletionList(fileContents, "| { B = ", [ "R1"; "R2"; "Some"; "None"; "System"; "DU" ], [ "A"; "B"; "C"; "D" ])
1959+
VerifyCompletionList(fileContents, "| { X =", [ "R1"; "R2"; "Some"; "None"; "System"; "DU" ], [ "A"; "B"; "C"; "D" ])
1960+
VerifyCompletionList(fileContents, "| { X = ", [ "R1"; "R2"; "Some"; "None"; "System"; "DU" ], [ "A"; "B"; "C"; "D" ])
1961+
1962+
// Ideally C and D should not be present here, but right now we're not able to filter fields in an empty record pattern stub
1963+
VerifyCompletionList(fileContents, "| { ", [ "A"; "B"; "C"; "D"; "R1"; "R2" ], [])
1964+
19481965
[<Fact>]
19491966
let ``Completion list for record field identifier in a pattern contains fields of all records in scope when the record type is not known yet``
19501967
()

0 commit comments

Comments
 (0)