Skip to content

Commit

Permalink
Don't remove "in" from "let ... in ..." binding. (#342)
Browse files Browse the repository at this point in the history
* test

* Use "let .. in .." if pattern and expr are at the same line.

* Added fix for comment after in

* Revert removed lines from fsproj

* Merge fix

* Fixed failing tests
  • Loading branch information
jindraivanek authored and nojaf committed Oct 30, 2018
1 parent 693baa3 commit 60d0ac5
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Fantomas.Cmd/Fantomas.Cmd.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
<ProjectReference Include="..\Fantomas\Fantomas.fsproj" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
</Project>
2 changes: 1 addition & 1 deletion src/Fantomas.CoreGlobalTool/Fantomas.CoreGlobalTool.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
<ProjectReference Include="..\Fantomas\Fantomas.fsproj" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
</Project>
2 changes: 0 additions & 2 deletions src/Fantomas.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ namespace Internal.Utilities.Diagnostic
type ExtensibleDumper = A | B
#endif
#endif
"""

[<Test>]
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.9.1</Version>
Expand Down Expand Up @@ -40,10 +39,11 @@
<Compile Include="FormattingPropertyTests.fs" />
<Compile Include="SpecialConstructsTests.fs" />
<Compile Include="FormatAstTests.fs" />
<Compile Include="LetBindingTests.fs" />
<Compile Include="NoTrailingSpacesTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas\Fantomas.fsproj" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
</Project>
35 changes: 35 additions & 0 deletions src/Fantomas.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Fantomas.Tests.LetBindingTests

open NUnit.Framework
open FsUnit

open Fantomas.CodeFormatter
open Fantomas.Tests.TestHelper

[<Test>]
let ``let in should be preserved``() =
formatSourceString false "let x = 1 in ()" config
|> should equal """let x = 1 in ()
"""

[<Test>]
let ``let in should be preserved with PreserveEOL option``() =
formatSourceString false "let x = 1 in ()" { config with PreserveEndOfLine = true }
|> should equal """let x = 1 in ()
"""

[<Test>]
let ``multiple let in lines, should remove in`` () =
let codeSnippet = """
let f () =
let x = 1 in // the "in" keyword is available in F#
let y = 2 in
x + y
"""

formatSourceString false codeSnippet config
|> should equal """let f() =
let x = 1 // the "in" keyword is available in F#
let y = 2
x + y
"""
9 changes: 3 additions & 6 deletions src/Fantomas.Tests/TypeProviderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@ let ``should handle lines with more than 512 characters``() =
|> should equal """
(new CsvFile<string * decimal * decimal>(new Func<obj, string [], string * decimal * decimal>(fun (parent : obj) (row : string []) -> CommonRuntime.GetNonOptionalValue("Name", CommonRuntime.ConvertString(TextConversions.AsOption(row.[0])), TextConversions.AsOption(row.[0])), CommonRuntime.GetNonOptionalValue("Distance", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[1])), TextConversions.AsOption(row.[1])), CommonRuntime.GetNonOptionalValue("Time", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[2])), TextConversions.AsOption(row.[2]))),
new Func<string * decimal * decimal, string []>(fun (row : string * decimal * decimal) ->
[| CommonRuntime.ConvertStringBack(CommonRuntime.GetOptionalValue((let x, _, _ = row
x)))
[| CommonRuntime.ConvertStringBack(CommonRuntime.GetOptionalValue((let x, _, _ = row in x)))
CommonRuntime.ConvertDecimalBack("",
CommonRuntime.GetOptionalValue((let _, x, _ = row
x)))
CommonRuntime.GetOptionalValue((let _, x, _ = row in x)))
CommonRuntime.ConvertDecimalBack("",
CommonRuntime.GetOptionalValue((let _, _, x = row
x))) |]), (ProviderFileSystem.readTextAtRunTimeWithDesignTimeOptions @"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data" "" "SmallTest.csv"), "", '"', true, false))
CommonRuntime.GetOptionalValue((let _, _, x = row in x))) |]), (ProviderFileSystem.readTextAtRunTimeWithDesignTimeOptions @"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data" "" "SmallTest.csv"), "", '"', true, false))
.Cache()
"""

Expand Down
6 changes: 5 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,11 @@ and genExpr astContext = function

| TypeApp(e, ts) -> genExpr astContext e -- "<" +> col sepComma ts (genType astContext false) -- ">"
| LetOrUses(bs, e) ->
atCurrentColumn (genLetOrUseList astContext bs +> sepNln +> genExpr astContext e)
let isInSameLine =
match bs with
| [_, LetBinding(ats, px, ao, isInline, isMutable, p, _)] -> p.Range.EndLine = e.Range.StartLine
| _ -> false
atCurrentColumn (genLetOrUseList astContext bs +> ifElse isInSameLine (!- " in ") sepNln +> genExpr astContext e)

// Could customize a bit if e is single line
| TryWith(e, cs) ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/Fantomas.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
<None Include="CodeFormatter.fsx" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
</Project>
36 changes: 33 additions & 3 deletions src/Fantomas/TokenMatcher.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
open System
open System.Collections.Generic
open System.Diagnostics
open Fantomas
open Microsoft.FSharp.Compiler.Range
open Microsoft.FSharp.Compiler.PrettyNaming
open Microsoft.FSharp.Compiler.SourceCodeServices
open System.Text.RegularExpressions
open Fantomas

#if INTERACTIVE
type Debug = Console
Expand Down Expand Up @@ -594,10 +594,17 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (
addText Environment.NewLine
for x in tokensText do addText x
addNewLineToDirective newTokens moreOrigTokens

let isEOL (token:Token) =
match token with
| EOL _ -> true
| _ -> false

let moreNewTokens =
if String.startsWithOrdinal "#endif" text then
match newTokens with
| allEOL when (allEOL |> List.forall (fst >> isEOL)) ->
[]
| WhiteSpaces(ws, moreNewTokens) ->
let origIndent =
moreOrigTokens
Expand Down Expand Up @@ -673,8 +680,31 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (
loop moreOrigTokens moreNewTokens

// Emit end-of-line from new tokens
| _, (NewLine newTokText :: moreNewTokens) ->
| (Marked(inToken,_,_)::oldTokens), (NewLine newTokText :: moreNewTokens) ->
Debug.WriteLine("emitting newline in new tokens '{0}'", newTokText)
let nextOldTokens =
match (inToken) with
| Tok(fsInToken,_) when (fsInToken.TokenName = "IN") ->
// find tokens before newline in old source
let tokensBeforeNewline =
oldTokens
|> List.takeWhile (fun t ->
match t with
| Marked(EOL,_,_) -> false
| _ -> true
)
|> List.map (fun t ->
match t with
| Marked(_,tokenString,_) -> tokenString
)

List.iter addText tokensBeforeNewline

oldTokens
|> List.skip (List.length tokensBeforeNewline)

| _ -> origTokens

let nextNewTokens =
if not isPreserveEOL then
addText newTokText
Expand All @@ -686,7 +716,7 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (
rs
| _ -> moreNewTokens

loop origTokens nextNewTokens
loop nextOldTokens nextNewTokens

| _, ((Token newTok, newTokText) :: moreNewTokens)
when newTok.CharClass = FSharpTokenCharKind.WhiteSpace && newTok.ColorClass <> FSharpTokenColorKind.InactiveCode ->
Expand Down

0 comments on commit 60d0ac5

Please sign in to comment.