Skip to content

Commit

Permalink
Merge branch master
Browse files Browse the repository at this point in the history
  • Loading branch information
dungpa committed Jul 27, 2013
1 parent 554fce4 commit 581a9fd
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/ActivePatternTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ let (|Integer|_|) (str : string) =
let (|ParseRegex|_|) regex str =
let m = Regex(regex).Match(str)
if m.Success then
Some(List.tail [for x in m.Groups -> x.Value])
Some(List.tail [ for x in m.Groups -> x.Value ])
else None
"""
50 changes: 25 additions & 25 deletions src/Fantomas.Tests/DataStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ array1.[1] <- 3
""" config
|> prepend newline
|> should equal """
let array1 = [|1; 2; 3|]
let array1 = [| 1; 2; 3 |]
array1.[0..2]
array2.[2.., 0..]
Expand All @@ -35,15 +35,15 @@ let arr = [|(1, 1, 1); (1, 2, 2); (1, 3, 3); (2, 1, 2); (2, 2, 4); (2, 3, 6); (3
|> prepend newline
|> should equal """
let arr =
[|(1, 1, 1);
(1, 2, 2);
(1, 3, 3);
(2, 1, 2);
(2, 2, 4);
(2, 3, 6);
(3, 1, 3);
(3, 2, 6);
(3, 3, 9)|]
[| (1, 1, 1);
(1, 2, 2);
(1, 3, 3);
(2, 1, 2);
(2, 2, 4);
(2, 3, 6);
(3, 1, 3);
(3, 2, 6);
(3, 3, 9) |]
"""

[<Test>]
Expand Down Expand Up @@ -73,9 +73,9 @@ let rec printList l =
let listLength list =
match list with
| [] -> 0
| [_] -> 1
| [_; _] -> 2
| [_; _; _] -> 3
| [ _ ] -> 1
| [ _; _ ] -> 2
| [ _; _; _ ] -> 3
| _ -> List.length list
"""

Expand All @@ -92,9 +92,9 @@ let vectorLength vec =
|> should equal """
let vectorLength vec =
match vec with
| [|var1|] -> var1
| [|var1; var2|] -> sqrt (var1 * var1 + var2 * var2)
| [|var1; var2; var3|] -> sqrt (var1 * var1 + var2 * var2 + var3 * var3)
| [| var1 |] -> var1
| [| var1; var2 |] -> sqrt (var1 * var1 + var2 * var2)
| [| var1; var2; var3 |] -> sqrt (var1 * var1 + var2 * var2 + var3 * var3)
| _ ->
failwith "vectorLength called with an unsupported array size of %d."
(vec.Length)
Expand All @@ -110,9 +110,9 @@ let ``should keep -> notation``() =
|> prepend newline
|> should equal """
let environVars target =
[for e in Environment.GetEnvironmentVariables target ->
let e1 = e :?> Collections.DictionaryEntry
e1.Key, e1.Value]
[ for e in Environment.GetEnvironmentVariables target ->
let e1 = e :?> Collections.DictionaryEntry
e1.Key, e1.Value ]
"""

[<Test>]
Expand All @@ -123,9 +123,9 @@ let list0to3 = [0 .. 3]""" config
|> prepend newline
|> should equal """
let listOfSquares =
[for i in 1..10 -> i * i]
[ for i in 1..10 -> i * i ]
let list0to3 = [0..3]
let list0to3 = [ 0..3 ]
"""

[<Test>]
Expand All @@ -137,13 +137,13 @@ let a3 = [| for n in 1 .. 100 do if isPrime n then yield n |]""" config
|> prepend newline
|> should equal """
let a1 =
[|for i in 1..10 -> i * i|]
[| for i in 1..10 -> i * i |]
let a2 = [|0..99|]
let a2 = [| 0..99 |]
let a3 =
[|for n in 1..100 do
if isPrime n then yield n|]
[| for n in 1..100 do
if isPrime n then yield n |]
"""

[<Test>]
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/FormattingSelectionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ let r =
|> List.map id""" config
|> should equal """
let r =
["abc"; "a"; "b"; ""] |> List.map id"""
[ "abc"; "a"; "b"; "" ] |> List.map id"""

[<Test>]
let ``should preserve line breaks before and after selection``() =
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ type NumberStrings() =
|> prepend newline
|> should equal """
type NumberStrings() =
let mutable ordinals = [|"one"|]
let mutable cardinals = [|"first"|]
let mutable ordinals = [| "one" |]
let mutable cardinals = [| "first" |]
member this.Item
with get index = ordinals.[index]
Expand Down
14 changes: 11 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ and genExpr = function
| TypedExpr(Upcast, e, t) -> genExpr e -- " :> " +> genType false t
| TypedExpr(Typed, e, t) -> genExpr e +> sepColon +> genType false t
| Tuple es -> atCurrentColumn (colAutoNlnSkip0i sepComma es (fun i -> if i = 0 then genExpr else noIndentBreakNln))
| ArrayOrList(isArray, [], _) ->
ifElse isArray (sepOpenAFixed +> sepCloseAFixed) (sepOpenLFixed +> sepCloseLFixed)
| ArrayOrList(isArray, xs, isSimple) ->
let sep = ifElse isSimple sepSemi sepSemiNln
ifElse isArray (sepOpenA +> atCurrentColumn (colAutoNlnSkip0 sep xs genExpr) +> sepCloseA)
Expand Down Expand Up @@ -487,7 +489,7 @@ and genExpr = function
// At this stage, all symbolic operators have been handled.
| OptVar(s, isOpt) -> ifElse isOpt (!- "?") sepNone -- s
| LongIdentSet(s, e) -> !- (sprintf "%s <- " s) +> genExpr e
| DotIndexedGet(e, es) -> genExpr e -- "." +> sepOpenL +> genIndexedVars es +> sepCloseL
| DotIndexedGet(e, es) -> genExpr e -- "." +> sepOpenLFixed +> genIndexedVars es +> sepCloseLFixed
| DotIndexedSet(e1, es, e2) -> genExpr e1 -- ".[" +> genIndexedVars es -- "] <- " +> genExpr e2
| DotGet(e, s) -> genExpr e -- sprintf ".%s" s
| DotSet(e1, s, e2) -> genExpr e1 -- sprintf ".%s <- " s +> genExpr e2
Expand Down Expand Up @@ -917,8 +919,14 @@ and genPat = function
| PatParen(PatConst(c)) -> genConst c
| PatParen(p) -> sepOpenT +> genPat p +> sepCloseT
| PatSeq(PatTuple, ps) -> atCurrentColumn (colAutoNlnSkip0 sepComma ps genPat)
| PatSeq(PatList, ps) -> sepOpenL +> atCurrentColumn (colAutoNlnSkip0 sepSemi ps genPat) +> sepCloseL
| PatSeq(PatArray, ps) -> sepOpenA +> atCurrentColumn (colAutoNlnSkip0 sepSemi ps genPat) +> sepCloseA
| PatSeq(PatList, ps) ->
ifElse ps.IsEmpty (sepOpenLFixed +> sepCloseLFixed)
(sepOpenL +> atCurrentColumn (colAutoNlnSkip0 sepSemi ps genPat) +> sepCloseL)

| PatSeq(PatArray, ps) ->
ifElse ps.IsEmpty (sepOpenAFixed +> sepCloseAFixed)
(sepOpenA +> atCurrentColumn (colAutoNlnSkip0 sepSemi ps genPat) +> sepCloseA)

| PatRecord(xs) ->
sepOpenS +> atCurrentColumn (colAutoNlnSkip0 sepSemi xs genPatRecordFieldName) +> sepCloseS
| PatConst(c) -> genConst c
Expand Down
72 changes: 57 additions & 15 deletions src/Fantomas/FormatConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ type FormatConfig =
IndentOnTryWith : bool;
/// Reordering and deduplicating open statements
ReorderOpenDeclaration : bool;
SpaceAroundDelimiter : bool;
/// Prettyprinting based on ASTs only
StrictMode : bool }

static member Default =
{ IndentSpaceNum = 4; PageWidth = 80;
SemicolonAtEndOfLine = false; SpaceBeforeArgument = true; SpaceBeforeColon = true;
SpaceAfterComma = true; SpaceAfterSemicolon = true;
IndentOnTryWith = false; ReorderOpenDeclaration = false; StrictMode = false }
IndentOnTryWith = false; ReorderOpenDeclaration = false;
SpaceAroundDelimiter = true; StrictMode = false }

static member create(indentSpaceNum, pageWith, semicolonAtEndOfLine,
spaceBeforeArgument, spaceBeforeColon, spaceAfterComma,
Expand All @@ -53,7 +55,7 @@ type FormatConfig =

static member create(indentSpaceNum, pageWith, semicolonAtEndOfLine,
spaceBeforeArgument, spaceBeforeColon, spaceAfterComma,
spaceAfterSemicolon, indentOnTryWith, reorderOpenDeclaration, strictMode) =
spaceAfterSemicolon, indentOnTryWith, reorderOpenDeclaration, spaceAroundDelimiter) =
{ FormatConfig.Default with
IndentSpaceNum = indentSpaceNum;
PageWidth = pageWith;
Expand All @@ -64,6 +66,23 @@ type FormatConfig =
SpaceAfterSemicolon = spaceAfterSemicolon;
IndentOnTryWith = indentOnTryWith;
ReorderOpenDeclaration = reorderOpenDeclaration;
SpaceAroundDelimiter = spaceAroundDelimiter }

static member create(indentSpaceNum, pageWith, semicolonAtEndOfLine,
spaceBeforeArgument, spaceBeforeColon, spaceAfterComma,
spaceAfterSemicolon, indentOnTryWith, reorderOpenDeclaration,
spaceAroundDelimiter, strictMode) =
{ FormatConfig.Default with
IndentSpaceNum = indentSpaceNum;
PageWidth = pageWith;
SemicolonAtEndOfLine = semicolonAtEndOfLine;
SpaceBeforeArgument = spaceBeforeArgument;
SpaceBeforeColon = spaceBeforeColon;
SpaceAfterComma = spaceAfterComma;
SpaceAfterSemicolon = spaceAfterSemicolon;
IndentOnTryWith = indentOnTryWith;
ReorderOpenDeclaration = reorderOpenDeclaration;
SpaceAroundDelimiter = spaceAroundDelimiter;
StrictMode = strictMode }

/// Wrapping IndentedTextWriter with current column position
Expand Down Expand Up @@ -279,19 +298,42 @@ let internal sepNone = id
let internal sepBar = !- "| "

/// opening token of list
let internal sepOpenL = !- "["
let internal sepOpenL (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str "[ " ctx else str "[" ctx

/// closing token of list
let internal sepCloseL (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str " ]" ctx else str "]" ctx

/// opening token of list
let internal sepOpenLFixed = !- "["
/// closing token of list
let internal sepCloseL = !- "]"
let internal sepCloseLFixed = !- "]"

/// opening token of array
let internal sepOpenA = !- "[|"
let internal sepOpenA (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str "[| " ctx else str "[|" ctx

/// closing token of array
let internal sepCloseA = !- "|]"
let internal sepCloseA (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str " |]" ctx else str "|]" ctx

/// opening token of list
let internal sepOpenAFixed = !- "[|"
/// closing token of list
let internal sepCloseAFixed = !- "|]"

/// opening token of sequence
let internal sepOpenS = !- "{ "
let internal sepOpenS (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str "{ " ctx else str "{" ctx

/// closing token of sequence
let internal sepCloseS = !- " }"
let internal sepCloseS (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str " }" ctx else str "}" ctx

/// opening token of tuple
let internal sepOpenT = !- "("

/// closing token of tuple
let internal sepCloseT = !- ")"

Expand Down Expand Up @@ -322,30 +364,30 @@ let internal noNln f (ctx : Context) : Context =
ctx.BreakLines <- true
res

let internal sepColon(ctx : Context) =
let internal sepColon (ctx : Context) =
if ctx.Config.SpaceBeforeColon then str " : " ctx else str ": " ctx

let internal sepColonFixed = !- ":"

let internal sepComma(ctx : Context) =
let internal sepComma (ctx : Context) =
if ctx.Config.SpaceAfterComma then str ", " ctx else str "," ctx

let internal sepSemi(ctx : Context) =
let internal sepSemi (ctx : Context) =
if ctx.Config.SpaceAfterSemicolon then str "; " ctx else str ";" ctx

let internal sepSemiNln(ctx : Context) =
let internal sepSemiNln (ctx : Context) =
// sepNln part is essential to indentation
if ctx.Config.SemicolonAtEndOfLine then (!- ";" +> sepNln) ctx else sepNln ctx

let internal sepBeforeArg(ctx : Context) =
let internal sepBeforeArg (ctx : Context) =
if ctx.Config.SpaceBeforeArgument then str " " ctx else str "" ctx

/// Conditional indentation on with keyword
let internal indentOnWith(ctx : Context) =
let internal indentOnWith (ctx : Context) =
if ctx.Config.IndentOnTryWith then indent ctx else ctx

/// Conditional unindentation on with keyword
let internal unindentOnWith(ctx : Context) =
let internal unindentOnWith (ctx : Context) =
if ctx.Config.IndentOnTryWith then unindent ctx else ctx

let internal sortAndDeduplicate by l (ctx : Context) =
Expand Down

0 comments on commit 581a9fd

Please sign in to comment.