Skip to content

Commit

Permalink
Don't add space before indexed expression inside DotIndexedSet and Do…
Browse files Browse the repository at this point in the history
…tIndexedGet. Fixes #853. Fixes #943 (#962)
  • Loading branch information
nojaf authored Jul 11, 2020
1 parent 311bff8 commit eac097c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
36 changes: 35 additions & 1 deletion src/Fantomas.Tests/SpaceBeforeUppercaseInvocationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,38 @@ let ``default config should not add space before parentheses in uppercase functi
let ``spaceBeforeUppercaseInvocation should add space before parentheses in uppercase function call`` () =
formatSourceString false "let value = MyFunction(a+b)" spaceBeforeConfig
|> should equal """let value = MyFunction (a + b)
"""
"""

[<Test>]
let ``space before uppercase function application cannot apply with dot-chaining, 943`` () =
formatSourceString false """foo.Bar().[5]
""" { config with SpaceBeforeUppercaseInvocation = true }
|> prepend newline
|> should equal """
foo.Bar().[5]
"""

[<Test>]
let ``space before uppercase DotIndexedSet`` () =
formatSourceString false """foo.Bar().[5] <- 5
""" { config with SpaceBeforeUppercaseInvocation = true }
|> prepend newline
|> should equal """
foo.Bar().[5] <- 5
"""

[<Test>]
let ``setting SpaceBeforeUppercaseInvocation is not applied in the middle of a invocation chain, 853`` () =
formatSourceString false """
module SomeModule =
let DoSomething (a:SomeType) =
let someValue = a.Some.Thing("aaa").[0]
someValue
""" { config with SpaceBeforeUppercaseInvocation = true }
|> prepend newline
|> should equal """
module SomeModule =
let DoSomething (a: SomeType) =
let someValue = a.Some.Thing("aaa").[0]
someValue
"""
11 changes: 7 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ type ASTContext =
/// Check whether the context is inside a SynMemberDefn.Member(memberDefn,range)
/// This is required to correctly detect the setting SpaceBeforeMember
IsMemberDefinition: bool
/// Check whether the context is inside a SynExpr.DotIndexedGet
IsInsideDotIndexed: bool
}
static member Default =
{ TopLevelModuleName = ""
IsFirstChild = false; InterfaceRange = None
IsCStylePattern = false; IsNakedRange = false
HasVerticalBar = false; IsUnionField = false
IsFirstTypeParam = false; IsInsideDotGet = false
IsMemberDefinition = false }
IsMemberDefinition = false
IsInsideDotIndexed = false }

let rec addSpaceBeforeParensInFunCall functionOrMethod arg (ctx:Context) =
match functionOrMethod, arg with
Expand Down Expand Up @@ -1345,7 +1348,7 @@ and genExpr astContext synExpr =
| App(e1, [e2]) ->
fun (ctx:Context) ->
let hasPar = hasParenthesis e2
let addSpaceBefore = addSpaceBeforeParensInFunCall e1 e2 ctx
let addSpaceBefore = not astContext.IsInsideDotIndexed && addSpaceBeforeParensInFunCall e1 e2 ctx
let genApp =
atCurrentColumn (
genExpr astContext e1
Expand Down Expand Up @@ -1767,8 +1770,8 @@ and genExpr astContext synExpr =
| LongIdentSet(s, e, _) ->
!- (sprintf "%s <- " s)
+> autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)
| DotIndexedGet(e, es) -> addParenIfAutoNln e (genExpr astContext) -- "." +> sepOpenLFixed +> genIndexers astContext es +> sepCloseLFixed
| DotIndexedSet(e1, es, e2) -> addParenIfAutoNln e1 (genExpr astContext) -- ".[" +> genIndexers astContext es -- "] <- " +> genExpr astContext e2
| DotIndexedGet(e, es) -> addParenIfAutoNln e (genExpr { astContext with IsInsideDotIndexed = true }) -- "." +> sepOpenLFixed +> genIndexers astContext es +> sepCloseLFixed
| DotIndexedSet(e1, es, e2) -> addParenIfAutoNln e1 (genExpr { astContext with IsInsideDotIndexed = true }) -- ".[" +> genIndexers astContext es -- "] <- " +> genExpr astContext e2
| NamedIndexedPropertySet(ident, e1, e2) ->
!- ident +> genExpr astContext e1 -- " <- " +> genExpr astContext e2
| DotNamedIndexedPropertySet(e, ident, e1, e2) ->
Expand Down

0 comments on commit eac097c

Please sign in to comment.