Skip to content

Commit

Permalink
Merge pull request #60 from ovatsus/master
Browse files Browse the repository at this point in the history
Fix for #38, #39 and #59
  • Loading branch information
dungpa committed Jul 4, 2013
2 parents f4e196a + 69a3a37 commit bf88825
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/Fantomas.Cmd/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open Fantomas.FormatConfig
/// Preferences:
/// --indent=[1-10] Set number of spaces to use for indentation
/// --pageWidth=[60-inf] Set the column where we break to new lines
/// [+|-]semicolonEOL Enable/disable semicolons at the end of line (default = true)
/// [+|-]semicolonEOL Enable/disable semicolons at the end of line (default = false)
/// [+|-]spaceBeforeArgument Enable/disable spaces before the first argument (default = false)
/// [+|-]spaceBeforeColon Enable/disable spaces before colons (default = true)
/// [+|-]spaceAfterComma Enable/disable spaces after commas (default = true)
Expand All @@ -37,7 +37,7 @@ let [<Literal>] stdOutText = " Write the formatted source code to standard outpu
let [<Literal>] indentText = "Set number of spaces for indentation (default = 4). The value should be between 1 and 10."
let [<Literal>] widthText = "Set the column where we break to new lines (default = 80). The value should be at least 60."

let [<Literal>] semicolonEOLText = "Disable semicolons at the end of line (default = true)."
let [<Literal>] semicolonEOLText = "Enable semicolons at the end of line (default = false)."
let [<Literal>] argumentText = "Enable spaces before the first argument (default = false)."
let [<Literal>] colonText = "Disable spaces before colons (default = true)."
let [<Literal>] commaText = "Disable spaces after commas (default = true)."
Expand Down Expand Up @@ -91,7 +91,7 @@ let main args =
let indent = ref 4
let pageWidth = ref 80

let semicolonEOL = ref true
let semicolonEOL = ref false
let spaceBeforeArgument = ref false
let spaceBeforeColon = ref true
let spaceAfterComma = ref true
Expand Down Expand Up @@ -194,7 +194,7 @@ let main args =
ArgInfo("--indent", ArgType.Int handleIndent, indentText);
ArgInfo("--pageWidth", ArgType.Int handlePageWidth, widthText);

ArgInfo("--noSemicolonEOL", ArgType.Clear semicolonEOL, semicolonEOLText);
ArgInfo("--semicolonEOL", ArgType.Set semicolonEOL, semicolonEOLText);
ArgInfo("--spaceBeforeArgument", ArgType.Set spaceBeforeArgument, argumentText);
ArgInfo("--noSpaceBeforeColon", ArgType.Clear spaceBeforeColon, colonText);
ArgInfo("--noSpaceAfterComma", ArgType.Clear spaceAfterComma, commaText);
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas.Tests/AttributeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ type vector3D<[<Measure>] 'u> = { x : float<'u>; y : float<'u>; z : float<'u>}""
let genericSumUnits (x : float<'u>) (y : float<'u>) = x + y
type vector3D<[<Measure>] 'u> =
{ x : float<'u>;
y : float<'u>;
{ x : float<'u>
y : float<'u>
z : float<'u> }"""

[<Test>]
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ type IlxGenOptions =
/// Whenever possible, use callvirt instead of call
alwaysCallVirt: bool}
""" config
""" { config with SemicolonAtEndOfLine = true }
|> prepend newline
|> should equal """
[<NoEquality>]
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/DataStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let ``array values``() =
formatSourceString false """
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)|]
""" config
""" { config with SemicolonAtEndOfLine = true }
|> prepend newline
|> should equal """
let arr =
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 @@ -112,7 +112,7 @@ let r =
"a"
"b"
"" ]
|> List.map id""" { config with SemicolonAtEndOfLine = false }
|> List.map id""" config
|> should equal """
let r =
["abc"
Expand Down
10 changes: 10 additions & 0 deletions src/Fantomas.Tests/OperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,13 @@ let ``should keep space between ( and * in *= operator definition``() =
formatSourceString false """let inline ( *=) l v = update (( *) v) l
""" config
|> should equal """let inline ( *= ) l v = update ((*) v) l"""
[<Test>]
let ``should not add space around ? operator``() =
formatSourceString false """let x = y?z.d?c.[2]?d.xpto()""" config
|> should equal """let x = y?z.d?c.[2]?d.xpto()"""
[<Test>]
let ``should not mess up ?<- operator``() =
formatSourceString false """x?v <- 2""" config
|> should equal """x?v <- 2"""
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ let IsMatchByName record1 (name: string) =
|> prepend newline
|> should equal """
type MyRecord =
{ Name : string;
{ Name : string
ID : int }
let IsMatchByName record1 (name : string) =
Expand Down
10 changes: 5 additions & 5 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Element =
static member WithText : text: string -> self: Element-> Element
/// Replaces the children with a single text node.
static member ( -- ) : self: Element * text: string -> Element""" config
static member ( -- ) : self: Element * text: string -> Element""" { config with SemicolonAtEndOfLine = true }
|> prepend newline
|> should equal """
/// Represents simple XML elements.
Expand Down Expand Up @@ -93,12 +93,12 @@ let myRecord3 = { myRecord2 with Y = 100; Z = 2 }""" config
|> prepend newline
|> should equal """
type Car =
{ Make : string;
Model : string;
{ Make : string
Model : string
mutable Odometer : int }
let myRecord3 =
{ myRecord2 with Y = 100;
{ myRecord2 with Y = 100
Z = 2 }"""

// the current behavior results in a compile error since the if is not aligned properly
Expand All @@ -124,7 +124,7 @@ let ``should not break inside of if statements in records``() =
Samples = [];
}
""" config
""" { config with SemicolonAtEndOfLine = true }
|> should equal """let XpkgDefaults() =
{ ToolPath = "./tools/xpkg/xpkg.exe";
WorkingDir = "./";
Expand Down
16 changes: 8 additions & 8 deletions src/Fantomas.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ let ``should keep the (string * string) list type signature in records``() =
""" config
|> should equal """type MSBuildParams =
{ Targets : string list;
Properties : (string * string) list;
MaxCpuCount : int option option;
ToolsVersion : string option;
Verbosity : MSBuildVerbosity option;
{ Targets : string list
Properties : (string * string) list
MaxCpuCount : int option option
ToolsVersion : string option
Verbosity : MSBuildVerbosity option
FileLoggers : MSBuildFileLoggerConfig list option }"""

[<Test>]
Expand Down Expand Up @@ -55,8 +55,8 @@ let ``should not add parens in signature``() =
""" config
|> should equal """type Route =
{ Verb : string;
Path : string;
{ Verb : string
Path : string
Handler : Map<string, string> -> HttpListenerContext -> string }
override x.ToString() = sprintf "%s %s" x.Verb x.Path"""

Expand All @@ -77,7 +77,7 @@ let ``should keep the (string option * Node) list type signature``() =
{ Name : string;
NextNodes : (string option * Node) list }
""" config
""" { config with SemicolonAtEndOfLine = true }
|> should equal """type Node =
{ Name : string;
NextNodes : (string option * Node) list }"""
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ type Point2D =
val X : float
val Y : float
new(x : float, y : float) =
{ X = x;
{ X = x
Y = y }
end"""

Expand Down
3 changes: 3 additions & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ and genExpr = function
let hasNewLine = multiline e || not (List.atMostOne es)
atCurrentColumn (genExpr e +> genInfixApps hasNewLine es)

| TernaryApp(e1,e2,e3) ->
atCurrentColumn (genExpr e1 +> !- "?" +> genExpr e2 +> sepSpace +> !- "<-" +> sepSpace +> genExpr e3)

// This filters a few long examples of App
| DotGetAppSpecial(s, es) ->
!- s
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas/FormatConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type FormatConfig =
IndentOnTryWith : bool }
static member Default =
{ IndentSpaceNum = 4; PageWidth = 80;
SemicolonAtEndOfLine = true; SpaceBeforeArgument = false; SpaceBeforeColon = true;
SemicolonAtEndOfLine = false; SpaceBeforeArgument = false; SpaceBeforeColon = true;
SpaceAfterComma = true; SpaceAfterSemicolon = true; IndentOnTryWith = false }

/// Wrapping IndentedTextWriter with a current column position
Expand Down Expand Up @@ -306,7 +306,7 @@ let internal unindentOnWith(ctx : Context) =
if ctx.Config.IndentOnTryWith then unindent ctx else ctx

/// Don't put space before and after these operators
let internal NoSpaceInfixOps = set [".."]
let internal NoSpaceInfixOps = set [".."; "?"]

/// Always break into newlines on these operators
let internal NewLineInfixOps = set ["|>"; "||>"; "|||>"; ">>"]
Expand Down
5 changes: 5 additions & 0 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,11 @@ let private (|InfixApp|_|) = function
Some(s, e1, e2)
| _ -> None

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

/// We should return the whole triple for convenient check
let (|InfixApps|_|) e =
let rec loop = function
Expand Down

0 comments on commit bf88825

Please sign in to comment.