Skip to content

Commit

Permalink
merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
jindraivanek committed Sep 9, 2018
2 parents 6ac2faf + 5719708 commit 9edfec4
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 22 deletions.
9 changes: 8 additions & 1 deletion src/Fantomas.Tests/OperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,11 @@ let output = 2 >>.~ 3
""" config
|> should equal """let (>>.~) (g : int) (h : int) : int = g + h
let output = 2 >>.~ 3
"""
"""

[<Test>]
let ``should not add newline before = operator after |>``() =
formatSourceString false """1 |> max 0 = 1""" config
|> should equal """1
|> max 0 = 1
"""
70 changes: 70 additions & 0 deletions src/Fantomas.Tests/PreserveEOLTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,73 @@ let rainbow =
b3 = "3"
}
"""


[<Test>]
let ``keep single pipe with pEOL``() =
formatSourceString false """
try
()
with
| :? _ ->
()
let y = 4
""" config
|> should equal """
try
()
with
| :? _ ->
()
let y = 4
"""

[<Test>]
let ``remove single pipe without pEOL``() =
formatSourceString false """
try
()
with
| :? _ ->
()
let y = 4
""" { config with PreserveEndOfLine = false }
|> prepend newline
|> should equal """
try
()
with :? _ -> ()
let y = 4
"""

[<Test>]
let ``keep multiple pipes with pEOL``() =
formatSourceString false """
match ts with
| 0
| 1 -> 2
| _ -> 3
""" config
|> should equal """
match ts with
| 0
| 1 -> 2
| _ -> 3
"""

[<Test>]
let ``newline handling in directives``() =
formatSourceString false """
#if NOT_DEFINED
let x = 1
let y = 2
#endif
""" config
|> should equal """
#if NOT_DEFINED
let x = 1
let y = 2
#endif
"""
15 changes: 15 additions & 0 deletions src/Fantomas.Tests/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ let alu = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"B
"""

[<Test>]
let ``multiline string piped``() =
formatSourceString false """
let f a b =
a "
" |> b
""" config
|> prepend newline
|> should equal """
let f a b =
a "
"
|> b
"""

[<Test>]
let ``preserve uncommon literals``() =
formatSourceString false """
Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -685,15 +685,15 @@ and genLetOrUseList astContext = function
/// When 'hasNewLine' is set, the operator is forced to be in a new line
and genInfixApps astContext hasNewLine synExprs =
match synExprs with
| (s, e)::es when (NoBreakInfixOps.Contains s) ->
(sepSpace -- s +> sepSpace +> genExpr astContext e)
+> genInfixApps astContext (hasNewLine || checkNewLine e es) es
| (s, e)::es when(hasNewLine) ->
(sepNln -- s +> sepSpace +> genExpr astContext e)
+> genInfixApps astContext (hasNewLine || checkNewLine e es) es
| (s, e)::es when(NoSpaceInfixOps.Contains s) ->
(!- s +> autoNln (genExpr astContext e))
+> genInfixApps astContext (hasNewLine || checkNewLine e es) es
| (s, e)::es when (NoBreakInfixOps.Contains s) ->
(sepSpace -- s +> sepSpace +> genExpr astContext e)
+> genInfixApps astContext (hasNewLine || checkNewLine e es) es
| (s, e)::es ->
(sepSpace +> autoNln (!- s +> sepSpace +> genExpr astContext e))
+> genInfixApps astContext (hasNewLine || checkNewLine e es) es
Expand Down
4 changes: 4 additions & 0 deletions src/Fantomas/SourceTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module List =
/// Check whether an expression should be broken into multiple lines.
/// Notice that order of patterns matters due to non-disjoint property.
let rec multiline synExpr =
let isConstMultiline (Unresolved(_, _, s)) = String.normalizeNewLine s |> String.exists ((=)'\n')

match synExpr with
| ConstExpr _
| NullExpr
Expand Down Expand Up @@ -55,6 +57,8 @@ let rec multiline synExpr =
| Tuple es ->
List.exists multiline es

| App(e, (ConstExpr c :: _)) -> multiline e || isConstMultiline c

// An infix app is multiline if it contains at least two new line infix ops
| InfixApps(e, es) ->
multiline e
Expand Down
56 changes: 38 additions & 18 deletions src/Fantomas/TokenMatcher.fs
Original file line number Diff line number Diff line change
Expand Up @@ -471,26 +471,45 @@ let integrateComments isPreserveEOL CompilationDefines (originalText : string) (
addText (String.replicate c " ")
f()

let preserveLineBreaks ots (nts:(Token * string) list) =
let rec newSpacingLength =
match nts with
| (EOL, _)::(Space t)::(Tok(_, _), "[<")::_ -> 2
| (EOL, _)::(EOL, _)::(Tok(_, _), "[<")::_-> 0
| (EOL, _)::_ -> 1
| (Tok(_, _), ";")::_ ->
let preserveLineBreaks ots (nts:(Token * string) list) =
let addMissingSemicolon xs =
match xs with
| (Tok(_, _), ";")::_ ->
addText ";"
1
| (Space t)::_ -> String.length t
| _ -> 0

| _ -> ()

let indentWithNewSpacing xs ys =
let rec newSpacingLength zs =
match zs with
| (EOL, _)::(Space _)::(Tok(_, _), "[<")::_ -> 2
| (EOL, _)::(EOL, _)::(Tok(_, _), "[<")::_ -> 0
| (EOL, _)::(Space _)::_ -> 1
| (EOL, _)::rs -> newSpacingLength rs
| (Tok(_, _), ";")::_ -> 1
| (Space t)::_ -> String.length t
| _ -> 0

match xs with
| SpaceToken t::_ ->
let nsLen = newSpacingLength ys
let oi = if nsLen <= String.length t then t.Substring(nsLen) else t
addText oi
| _ -> ()

let addMissingPipe xs ys =
let isWhiteSpace (_, s) = String.IsNullOrWhiteSpace(s)
let tos = xs |> List.skipWhile ((|Wrapped|) >> isWhiteSpace)
let tns = ys |> List.skipWhile (isWhiteSpace)
match tos, tns with
| Marked(_, "|", _)::_, (Tok(_, _), s)::_ when s <> "|" ->
addText " |"
| _, _ -> ()

addMissingSemicolon nts
buffer.Append Environment.NewLine |> ignore

match ots with
| SpaceToken t::_ ->
let nsLen = newSpacingLength
let oi = if nsLen <= String.length t then t.Substring(nsLen) else t
addText oi
| _ -> ()
indentWithNewSpacing ots nts
addMissingPipe ots nts

let addNewLineToDirective newTokens moreOrigTokens =
let isWhiteSpace (_, s) = String.IsNullOrWhiteSpace(s)
Expand Down Expand Up @@ -604,8 +623,9 @@ let integrateComments isPreserveEOL CompilationDefines (originalText : string) (
else
addText Environment.NewLine
addText line
) lines
) lines

if isPreserveEOL then addText Environment.NewLine
loop moreOrigTokens newTokens

| (LineCommentChunk true (commentTokensText, moreOrigTokens)), [] ->
Expand Down

0 comments on commit 9edfec4

Please sign in to comment.