Skip to content

Commit a6e028f

Browse files
committed
Traverse from the outside in.
1 parent 8bd8182 commit a6e028f

File tree

2 files changed

+30
-48
lines changed

2 files changed

+30
-48
lines changed

src/FsAutoComplete/CodeFixes/NegateBooleanExpression.fs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ open FsAutoComplete.LspHelpers
1212

1313
let title = "Negate boolean expression"
1414

15-
let booleanOperators = set [ "||"; "&&"; "=" ] // TODO: probably some others
15+
let booleanOperators = set [ "||"; "&&"; "="; "<>" ]
1616

1717
[<return: Struct>]
1818
let (|BooleanOperator|_|) =
@@ -76,65 +76,47 @@ let fix (getParseResultsForFile: GetParseResultsForFile) : CodeFix =
7676
getParseResultsForFile fileName fcsPos
7777

7878
let optFixData =
79-
ParsedInput.tryNode fcsPos parseAndCheckResults.GetParseResults.ParseTree
80-
|> Option.bind (fun (node, path) ->
81-
79+
(fcsPos, parseAndCheckResults.GetParseResults.ParseTree)
80+
||> ParsedInput.tryPick (fun path node ->
8281
match node with
8382
| SyntaxNode.SynExpr e ->
84-
match e, path with
85-
// &&
86-
| BooleanOperator operator,
87-
SyntaxNode.SynExpr(SynExpr.App(isInfix = true)) :: SyntaxNode.SynExpr(SynExpr.App(isInfix = false) as e) :: rest ->
83+
match e with
84+
// a && b
85+
| SynExpr.App(isInfix = false; funcExpr = SynExpr.App(isInfix = true; funcExpr = BooleanOperator operator)) ->
8886
Some
8987
{ Expr = e
9088
Ident = operator
91-
Path = rest
92-
NeedsParensAfterNot = true }
93-
94-
// $0X().Y
95-
| SynExpr.Ident _,
96-
SyntaxNode.SynExpr(SynExpr.App _) :: SyntaxNode.SynExpr(SynExpr.DotGet(
97-
longDotId = LastIdentFromSynLongIdent ident) as e) :: rest ->
98-
Some
99-
{ Expr = e
100-
Ident = ident
101-
Path = rest
89+
Path = path
10290
NeedsParensAfterNot = true }
10391

104-
// X$0()
105-
| SynExpr.Ident ident, SyntaxNode.SynExpr(SynExpr.App _ as e) :: rest ->
106-
Some
107-
{ Expr = e
108-
Ident = ident
109-
Path = rest
110-
NeedsParensAfterNot = true }
92+
// X().Y()
93+
| SynExpr.App(funcExpr = SynExpr.DotGet(longDotId = LastIdentFromSynLongIdent ident))
11194

112-
// X()$0
113-
| (SynExpr.Const(constant = SynConst.Unit) | SynExpr.Paren _),
114-
SyntaxNode.SynExpr(SynExpr.App(funcExpr = SynExpr.Ident ident) as e) :: rest ->
95+
// X().Y
96+
| SynExpr.DotGet(longDotId = LastIdentFromSynLongIdent ident) ->
11597
Some
11698
{ Expr = e
11799
Ident = ident
118-
Path = rest
100+
Path = path
119101
NeedsParensAfterNot = true }
120102

121-
// X().Y$0
122-
| SynExpr.DotGet(longDotId = LastIdentFromSynLongIdent ident), path ->
103+
// X()
104+
| SynExpr.App(funcExpr = SynExpr.Ident ident) ->
123105
Some
124106
{ Expr = e
125107
Ident = ident
126108
Path = path
127109
NeedsParensAfterNot = true }
128110

129111
// a.Y
130-
| SynExpr.LongIdent(isOptional = false; longDotId = LastIdentFromSynLongIdent ident), path ->
112+
| SynExpr.LongIdent(isOptional = false; longDotId = LastIdentFromSynLongIdent ident) ->
131113
Some
132114
{ Expr = e
133115
Ident = ident
134116
Path = path
135117
NeedsParensAfterNot = false }
136118
// a
137-
| SynExpr.Ident ident, path ->
119+
| SynExpr.Ident ident ->
138120
Some
139121
{ Expr = e
140122
Ident = ident

test/FsAutoComplete.Tests.Lsp/CodeFixTests/NegateBooleanExpressionTests.fs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let tests state =
1616
(fun server ->
1717
[ let selectCodeFix = CodeFix.withTitle NegateBooleanExpression.title
1818

19-
ftestCaseAsync "negate single identifier"
19+
testCaseAsync "negate single identifier"
2020
<| CodeFix.check
2121
server
2222
"let a = false
@@ -26,7 +26,7 @@ let b = a$0"
2626
"let a = false
2727
let b = not a"
2828

29-
ftestCaseAsync "negate boolean expression"
29+
testCaseAsync "negate boolean expression"
3030
<| CodeFix.check
3131
server
3232
"let a = false
@@ -36,7 +36,7 @@ let b = a $0|| false"
3636
"let a = false
3737
let b = not (a || false)"
3838

39-
ftestCaseAsync "negate longdotident expression"
39+
testCaseAsync "negate longdotident expression"
4040
<| CodeFix.check
4141
server
4242
"
@@ -52,7 +52,7 @@ module A =
5252
5353
let b = not A.a"
5454

55-
ftestCaseAsync "negate record field"
55+
testCaseAsync "negate record field"
5656
<| CodeFix.check
5757
server
5858
"
@@ -68,7 +68,7 @@ type X = { Y: bool }
6868
let a = { Y = true }
6969
let b = not a.Y"
7070

71-
ftestCaseAsync "negate class property"
71+
testCaseAsync "negate class property"
7272
<| CodeFix.check
7373
server
7474
"
@@ -84,7 +84,7 @@ type X() =
8484
8585
let b = not (X().Y)"
8686

87-
ftestCaseAsync "negate class property, cursor at start"
87+
testCaseAsync "negate class property, cursor at start"
8888
<| CodeFix.check
8989
server
9090
"
@@ -100,7 +100,7 @@ type X() =
100100
101101
let b = not (X().Y)"
102102

103-
ftestCaseAsync "negate unit function call"
103+
testCaseAsync "negate unit function call"
104104
<| CodeFix.check
105105
server
106106
"
@@ -112,7 +112,7 @@ let b = a$0 ()"
112112
let a () = false
113113
let b = not (a ())"
114114

115-
ftestCaseAsync "negate unit function call, cursor at end"
115+
testCaseAsync "negate unit function call, cursor at end"
116116
<| CodeFix.check
117117
server
118118
"
@@ -124,7 +124,7 @@ let b = a ()$0"
124124
let a () = false
125125
let b = not (a ())"
126126

127-
ftestCaseAsync "negate unit function call, cursor at end"
127+
testCaseAsync "negate non-unit function call, cursor at end"
128128
<| CodeFix.check
129129
server
130130
"
@@ -136,19 +136,19 @@ let b = a 4$0"
136136
let a _ = false
137137
let b = not (a 4)"
138138

139-
ftestCaseAsync "negate unit function call, cursor at end"
139+
testCaseAsync "negate non-unit function call, cursor in middle"
140140
<| CodeFix.check
141141
server
142142
"
143143
let a _ = false
144-
let b = a 4$0"
144+
let b = a $0 4"
145145
Diagnostics.acceptAll
146146
selectCodeFix
147147
"
148148
let a _ = false
149-
let b = not (a 4)"
149+
let b = not (a 4)"
150150

151-
ftestCaseAsync "negate unit member invocation"
151+
testCaseAsync "negate unit member invocation"
152152
<| CodeFix.check
153153
server
154154
"
@@ -164,7 +164,7 @@ type X() =
164164
165165
let b = not (X().Y())"
166166

167-
ftestCaseAsync "negate unit member invocation, cursor at end"
167+
testCaseAsync "negate unit member invocation, cursor at end"
168168
<| CodeFix.check
169169
server
170170
"

0 commit comments

Comments
 (0)