ionide / FsAutoComplete

F# language server using Language Server Protocol
Other
389 stars 151 forks source link

Add unnecessary parentheses analyzer & code fix #1235

Closed brianrourkeboll closed 4 months ago

brianrourkeboll commented 4 months ago

Note: this will need a small update to use https://github.com/dotnet/fsharp/pull/16461 when FSC 8.0.300 is shipped.

baronfel commented 4 months ago

Awesome - thanks for doing the parallel work here!

baronfel commented 4 months ago

How recent of an 8.0.300 does the 'fixed' version need? There's a 'nightly' branch we use to stage these kinds of fixes that pulls from the nightly FCS feeds that you could do the work on once this is merged into main, and main is merged into nightly.

brianrourkeboll commented 4 months ago

How recent of an 8.0.300 does the 'fixed' version need?

Any version that has https://github.com/dotnet/fsharp/pull/16462 and https://github.com/dotnet/fsharp/pull/16461 in it.

The update would basically look like this:

-let! unnecessaryParentheses = UnnecessaryParentheses.getUnnecessaryParentheses getSourceLine tyRes.GetAST
+let unnecessaryParentheses =
+    (HashSet Range.comparer, tyRes.GetAST)
+    ||> ParsedInput.fold (fun ranges path node ->
+        match node with
+        | SyntaxNode.SynExpr(SynExpr.Paren(expr = inner; rightParenRange = Some _; range = range)) when
+            not (SynExpr.shouldBeParenthesizedInContext getSourceLine path inner)
+            ->
+            ignore (ranges.Add range)
+            ranges
+
+        | SyntaxNode.SynPat(SynPat.Paren(inner, range)) when not (SynPat.shouldBeParenthesizedInContext path inner) ->
+            ignore (ranges.Add range)
+            ranges
+
+        | _ -> ranges)
baronfel commented 4 months ago

@brianrourkeboll let me know if you want to move this out of draft - I'm happy to merge it.

brianrourkeboll commented 4 months ago

@brianrourkeboll let me know if you want to move this out of draft - I'm happy to merge it.

Yeah I just wanted to be sure the CI passed to catch any dumb oversights I might have made.

baronfel commented 4 months ago

Thanks for this contribution!