From 5324e2f6147b9e1f8fc8158cea113f88b0d79b38 Mon Sep 17 00:00:00 2001 From: dawe Date: Sun, 12 Mar 2023 18:39:34 +0100 Subject: [PATCH] Fix "Convert to named patterns" code fix for multiple clauses (#1073) --- .../CodeFixes/ConvertPositionalDUToNamed.fs | 9 +++++++-- test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/FsAutoComplete/CodeFixes/ConvertPositionalDUToNamed.fs b/src/FsAutoComplete/CodeFixes/ConvertPositionalDUToNamed.fs index de7c949f0..9261194f8 100644 --- a/src/FsAutoComplete/CodeFixes/ConvertPositionalDUToNamed.fs +++ b/src/FsAutoComplete/CodeFixes/ConvertPositionalDUToNamed.fs @@ -24,6 +24,7 @@ open FsAutoComplete open FsAutoComplete.LspHelpers open FSharp.Compiler.CodeAnalysis open FSharp.Compiler.Symbols +open FSharp.Compiler.Text.Range open FsAutoComplete.FCSPatches open FSharp.Compiler.Syntax open FSharp.Compiler.Syntax.SyntaxTraversal @@ -65,14 +66,18 @@ type ParseAndCheckResults with | None -> clauses |> List.tryPick (function - | SynMatchClause(pat = UnionNameAndPatterns(ident, duFieldPatterns, parenRange)) -> + | SynMatchClause(pat = UnionNameAndPatterns(ident, duFieldPatterns, parenRange)) when + rangeContainsPos parenRange pos + -> Some(ident, duFieldPatterns, parenRange) | _ -> None) | _ -> defaultTraverse expr member x.VisitMatchClause(path, defaultTraverse, matchClause) = match matchClause with - | SynMatchClause(pat = UnionNameAndPatterns(ident, duFieldPatterns, parenRange)) -> + | SynMatchClause(pat = UnionNameAndPatterns(ident, duFieldPatterns, parenRange)) when + rangeContainsPos parenRange pos + -> Some(ident, duFieldPatterns, parenRange) | _ -> defaultTraverse matchClause } diff --git a/test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs b/test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs index b0687b700..dc63bdb79 100644 --- a/test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs +++ b/test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs @@ -573,6 +573,7 @@ let private convertPositionalDUToNamedTests state = type A = A of a: int * b: bool match A(1, true) with + | A(_, 23) -> () | A(a$0, b) -> () """ Diagnostics.acceptAll @@ -581,6 +582,7 @@ let private convertPositionalDUToNamedTests state = type A = A of a: int * b: bool match A(1, true) with + | A(_, 23) -> () | A(a = a; b = b;) -> () """ testCaseAsync "in parenthesized match" <| @@ -589,6 +591,7 @@ let private convertPositionalDUToNamedTests state = type A = A of a: int * b: bool match A(1, true) with + | (A(_, 23)) -> () | (A(a$0, b)) -> () """ Diagnostics.acceptAll @@ -597,6 +600,7 @@ let private convertPositionalDUToNamedTests state = type A = A of a: int * b: bool match A(1, true) with + | (A(_, 23)) -> () | (A(a = a; b = b;)) -> () """ testCaseAsync "when there is one new field on the DU" <|