Skip to content

Commit

Permalink
Fix "Convert to named patterns" code fix for multiple clauses (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
dawedawe committed Mar 12, 2023
1 parent bd47dd1 commit 5324e2f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/FsAutoComplete/CodeFixes/ConvertPositionalDUToNamed.fs
Expand Up @@ -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
Expand Down Expand Up @@ -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 }

Expand Down
4 changes: 4 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs
Expand Up @@ -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
Expand All @@ -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" <|
Expand All @@ -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
Expand All @@ -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" <|
Expand Down

0 comments on commit 5324e2f

Please sign in to comment.