Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConvertPositionalDUToNamed when other SynLongIdent.Pats are traversed #1090

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/FsAutoComplete/CodeFixes/ConvertPositionalDUToNamed.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ type ParseAndCheckResults with
function
| SynPat.LongIdent(
longDotId = ident
argPats = SynArgPats.Pats [ SynPat.Paren(pat = SynPat.Tuple(elementPats = duFieldPatterns); range = parenRange) ]) ->
argPats = SynArgPats.Pats [ SynPat.Paren(pat = SynPat.Tuple(elementPats = duFieldPatterns); range = parenRange) ]) when
rangeContainsPos parenRange pos
->
Some(ident, duFieldPatterns, parenRange)
| SynPat.LongIdent(
longDotId = ident; argPats = SynArgPats.Pats [ SynPat.Paren(pat = singleDUFieldPattern; range = parenRange) ]) ->
longDotId = ident; argPats = SynArgPats.Pats [ SynPat.Paren(pat = singleDUFieldPattern; range = parenRange) ]) when
rangeContainsPos parenRange pos
->
Some(ident, [ singleDUFieldPattern ], parenRange)
| SynPat.Paren(pat = UnionNameAndPatterns(ident, duFieldPatterns, parenRange)) ->
Some(ident, duFieldPatterns, parenRange)
Expand Down
60 changes: 56 additions & 4 deletions test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ let private convertPositionalDUToNamedTests state =
type A = A of a: int * b: bool

match A(1, true) with
| A(_, 23) -> ()
| A(_, false) -> ()
| A(a$0, b) -> ()
"""
Diagnostics.acceptAll
Expand All @@ -582,7 +582,7 @@ let private convertPositionalDUToNamedTests state =
type A = A of a: int * b: bool

match A(1, true) with
| A(_, 23) -> ()
| A(_, false) -> ()
| A(a = a; b = b;) -> ()
"""
testCaseAsync "in parenthesized match" <|
Expand All @@ -591,7 +591,7 @@ let private convertPositionalDUToNamedTests state =
type A = A of a: int * b: bool

match A(1, true) with
| (A(_, 23)) -> ()
| (A(_, false)) -> ()
| (A(a$0, b)) -> ()
"""
Diagnostics.acceptAll
Expand All @@ -600,7 +600,7 @@ let private convertPositionalDUToNamedTests state =
type A = A of a: int * b: bool

match A(1, true) with
| (A(_, 23)) -> ()
| (A(_, false)) -> ()
| (A(a = a; b = b;)) -> ()
"""
testCaseAsync "when there is one new field on the DU" <|
Expand Down Expand Up @@ -641,6 +641,58 @@ let private convertPositionalDUToNamedTests state =
type A = A of a: int * b: bool * c: bool * d: bool
let (A(a = a;b = b; c = c; d = d;)) = A(1, true, false, false)
"""

testCaseAsync "when there are multiple SynLongIdent Pats" <|
CodeFix.check server
"""
type MyDiscUnion = Case1 of field1: int * field2: int

type MyC() =

let x = Case1 (1, 2)

member _.Func2 () =
match x with
| Case1(3$0, 4) -> ()
| _ -> ()
"""
Diagnostics.acceptAll
selectCodeFix
"""
type MyDiscUnion = Case1 of field1: int * field2: int

type MyC() =

let x = Case1 (1, 2)

member _.Func2 () =
match x with
| Case1(field1 = 3; field2 = 4;) -> ()
| _ -> ()
"""

testCaseAsync "when surrounding function takes union parameter" <|
CodeFix.check server
"""
type MyDiscUnion = X of field1: int * field2: int

let f () =
let x = X (1, 2)
match x with
| X(32$0, 23) -> ()
| _ -> ()
"""
Diagnostics.acceptAll
selectCodeFix
"""
type MyDiscUnion = X of field1: int * field2: int

let f () =
let x = X (1, 2)
match x with
| X(field1 = 32; field2 = 23;) -> ()
| _ -> ()
"""
])

let private convertTripleSlashCommentToXmlTaggedDocTests state =
Expand Down