Skip to content

Commit

Permalink
Put multi line named argument on next line. Fixes #1158. (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Sep 25, 2020
1 parent 693de17 commit e81d192
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 46 deletions.
9 changes: 8 additions & 1 deletion build.fsx
Expand Up @@ -428,7 +428,14 @@ Target.create "Format" (fun _ ->

Target.create "FormatChanged" (fun _ ->
Fake.Tools.Git.FileStatus.getChangedFilesInWorkingCopy "." "HEAD"
|> Seq.choose (fun (_, file) -> if file.StartsWith("src") then Some file else None)
|> Seq.choose (fun (_, file) ->
let ext = Path.GetExtension(file)

if file.StartsWith("src")
&& (ext = ".fs" || ext = ".fsi") then
Some file
else
None)
|> formatCode
|> Async.RunSynchronously
|> printfn "Formatted files: %A")
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Tests/Fantomas.Tests.fsproj
Expand Up @@ -74,6 +74,7 @@
<Compile Include="InterpolatedStringTests.fs" />
<Compile Include="DotGetTests.fs" />
<Compile Include="HashDirectiveTests.fs" />
<Compile Include="SynExprNewTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Extras\Fantomas.Extras.fsproj" />
Expand Down
40 changes: 40 additions & 0 deletions src/Fantomas.Tests/SynExprNewTests.fs
@@ -0,0 +1,40 @@
module Fantomas.Tests.SynExprNewTests

open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelper

[<Test>]
let ``combination of named and non named arguments, 1158`` () =
formatSourceString false """
let private sendTooLargeError () =
new HttpResponseMessage(HttpStatusCode.RequestEntityTooLarge,
Content =
new StringContent("File was too way too large",
System.Text.Encoding.UTF16,
"application/text"))
""" config
|> prepend newline
|> should equal """
let private sendTooLargeError () =
new HttpResponseMessage(HttpStatusCode.RequestEntityTooLarge,
Content =
new StringContent("File was too way too large",
System.Text.Encoding.UTF16,
"application/text"))
"""

[<Test>]
let ``single multi line named argument instance`` () =
formatSourceString false """
let myInstance =
new EvilBadRequest(Content = new StringContent("File was too way too large",
System.Text.Encoding.UTF16,
"application/text"))
""" config
|> prepend newline
|> should equal """
let myInstance =
new EvilBadRequest(Content =
new StringContent("File was too way too large", System.Text.Encoding.UTF16, "application/text"))
"""
65 changes: 36 additions & 29 deletions src/Fantomas/CodePrinter.fs
Expand Up @@ -1146,14 +1146,42 @@ and genAnonRecordFieldName astContext (AnonRecordFieldName (s, e)) =
!-s +> sepEq +> expr

and genTuple astContext es =
let f =
addParenForTupleWhen (genExpr astContext)
let genExpr e =
let expr e =
match e with
| InfixApp (equal, operatorExpr, e1, e2) when (equal = "=") ->
genNamedArgumentExpr astContext operatorExpr e1 e2
| _ -> genExpr astContext e

addParenForTupleWhen expr e

let shortExpression = col sepComma es f
let longExpression = col (sepComma +> sepNln) es f
let shortExpression = col sepComma es genExpr
let longExpression = col (sepComma +> sepNln) es genExpr

atCurrentColumn (expressionFitsOnRestOfLine shortExpression longExpression)

and genNamedArgumentExpr (astContext: ASTContext) operatorExpr e1 e2 =
let short =
genExpr astContext e1
+> sepSpace
+> genInfixOperator "=" operatorExpr
+> sepSpace
+> genExpr astContext e2

match e2 with
| MultilineString _ -> short
| _ ->
let long =
genExpr astContext e1
+> sepSpace
+> genInfixOperator "=" operatorExpr
+> indent
+> sepNln
+> genExpr astContext e2
+> unindent

expressionFitsOnRestOfLine short long

and genExpr astContext synExpr =
let appNlnFun e =
match e with
Expand Down Expand Up @@ -1346,31 +1374,6 @@ and genExpr astContext synExpr =
genExpr astContext e
+> sepColon
+> genType astContext false t
| TupleWithInfixEqualsApps es ->
let expr (e1, opE, e2) =
let shortExpr =
genExpr astContext e1
+> sepSpace
+> genInfixOperator "=" opE
+> sepSpace
+> genExpr astContext e2

let longExpr =
genExpr astContext e1
+> sepSpace
+> genInfixOperator "=" opE
+> indent
+> sepNln
+> genExpr astContext e2
+> unindent

expressionFitsOnRestOfLine shortExpr longExpr

let shortExpression = col sepComma es expr
let longExpression = col (sepComma +> sepNln) es expr

atCurrentColumn (expressionFitsOnRestOfLine shortExpression longExpression)

| Tuple es -> genTuple astContext es
| StructTuple es ->
!- "struct "
Expand Down Expand Up @@ -1710,6 +1713,10 @@ and genExpr astContext synExpr =
e
+> indentIfNeeded sepNone)
+> sepCloseTFor rpr
| InfixApp (equal, operatorExpr, e1, e2) when (equal = "=") ->
sepOpenTFor (Some lpr)
+> genNamedArgumentExpr astContext operatorExpr e1 e2
+> sepCloseTFor rpr
| _ ->
// Parentheses nullify effects of no space inside DotGet
sepOpenTFor (Some lpr)
Expand Down
16 changes: 0 additions & 16 deletions src/Fantomas/SourceParser.fs
Expand Up @@ -835,22 +835,6 @@ let (|SameInfixApps|_|) e =
| _ -> None
| _ -> None

let (|TupleWithInfixEqualsApps|_|) e =
let isEqualInfix =
function
| InfixApp ("=", _, _, _) -> true
| _ -> false

match e with
| Tuple es when (List.forall isEqualInfix es) ->
es
|> List.map (fun e ->
match e with
| InfixApp ("=", opE, e1, e2) -> e1, opE, e2
| _ -> failwith "should not be possible")
|> Some
| _ -> None

let (|TernaryApp|_|) =
function
| SynExpr.App (_, _, SynExpr.App (_, _, SynExpr.App (_, true, Var "?<-", e1, _), e2, _), e3, _) -> Some(e1, e2, e3)
Expand Down

0 comments on commit e81d192

Please sign in to comment.