From 567ea957c5a55f09cc0957124e43ecedd191f56e Mon Sep 17 00:00:00 2001 From: kerams Date: Fri, 29 Apr 2022 16:10:27 +0200 Subject: [PATCH] Improve record completions (#12873) --- src/fsharp/service/ServiceParsedInputOps.fs | 7 +++++-- .../tests/UnitTests/CompletionProviderTests.fs | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/fsharp/service/ServiceParsedInputOps.fs b/src/fsharp/service/ServiceParsedInputOps.fs index 0c4c4aaa1..62bbe1eee 100644 --- a/src/fsharp/service/ServiceParsedInputOps.fs +++ b/src/fsharp/service/ServiceParsedInputOps.fs @@ -1105,12 +1105,15 @@ module ParsedInput = Some CompletionContext.PatternType | _ -> defaultTraverse ty - member _.VisitRecordDefn(_path, fields, _range) = - fields |> List.tryPick (fun (SynField (idOpt = idOpt; range = fieldRange)) -> + member _.VisitRecordDefn(_path, fields, range) = + fields + |> List.tryPick (fun (SynField (idOpt = idOpt; range = fieldRange)) -> match idOpt with | Some id when rangeContainsPos id.idRange pos -> Some(CompletionContext.RecordField(RecordContext.Declaration true)) | _ when rangeContainsPos fieldRange pos -> Some(CompletionContext.RecordField(RecordContext.Declaration false)) | _ -> None) + // No completions in a record outside of all fields + |> Option.orElseWith (fun () -> if rangeContainsPos range pos then Some CompletionContext.Invalid else None) member _.VisitUnionDefn(_path, cases, _range) = cases |> List.tryPick (fun (SynUnionCase (ident = id; caseType = caseType)) -> diff --git a/vsintegration/tests/UnitTests/CompletionProviderTests.fs b/vsintegration/tests/UnitTests/CompletionProviderTests.fs index 823fa2584..d0238c305 100644 --- a/vsintegration/tests/UnitTests/CompletionProviderTests.fs +++ b/vsintegration/tests/UnitTests/CompletionProviderTests.fs @@ -757,6 +757,20 @@ type A<'lType> = { Field: l } """ VerifyCompletionList(fileContents, "Field: l", ["LanguagePrimitives"; "List"], ["let"; "log"]) +[] +let ``No completion on record stub with no fields at declaration site``() = + let fileContents = """ +type A = { } +""" + VerifyNoCompletionList(fileContents, "{ ") + +[] +let ``No completion on record outside of all fields at declaration site``() = + let fileContents = """ +type A = { Field: string; } +""" + VerifyNoCompletionList(fileContents, "; ") + [] let ``No completion on union case identifier at declaration site``() = let fileContents = """ -- GitLab