Skip to content

Commit

Permalink
Print trivia in LongIdentWithDots application. (#2063)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Feb 6, 2022
1 parent c34ada5 commit b5d7e33
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/Fantomas.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,9 +1318,10 @@ let ``app tuple inside dotget expression`` () =
"""
(st :> IProvidedCustomAttributeProvider)
.GetHasTypeProviderEditorHideMethodsAttribute(
info.ProvidedType.TypeProvider.PUntaintNoFailure(
id
)
info
.ProvidedType
.TypeProvider
.PUntaintNoFailure(id)
)
"""

Expand Down
46 changes: 46 additions & 0 deletions src/Fantomas.Tests/LongIdentWithDotsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,49 @@ let shrinkInput input =
//stdout.WriteLine("Can't shrink {0} further.", sprintf "%A" input)
Seq.empty
"""

[<Test>]
let ``comment in LongIdent application, 2062`` () =
formatSourceString
false
"""
Rollbar
.RollbarLocator
.RollbarInstance
// .AsBlockingLogger(System.TimeSpan.FromSeconds 5)
.Error(package, custom)
"""
config
|> prepend newline
|> should
equal
"""
Rollbar
.RollbarLocator
.RollbarInstance
// .AsBlockingLogger(System.TimeSpan.FromSeconds 5)
.Error(package, custom)
"""

[<Test>]
let ``comment inside LongIdentWithDots preserved, 2027`` () =
formatSourceString
false
"""
let path =
match normalizedPath with
| path ->
path // translate path to Python relative syntax
.Replace("../../../", "....")
"""
config
|> prepend newline
|> should
equal
"""
let path =
match normalizedPath with
| path ->
path // translate path to Python relative syntax
.Replace("../../../", "....")
"""
30 changes: 25 additions & 5 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,29 @@ and genExpr astContext synExpr ctx =

expressionFitsOnRestOfLine short long

// path.Replace("../../../", "....")
| AppSingleParenArg (LongIdentPiecesExpr lids as functionOrMethod, px) ->
let addSpace =
onlyIfCtx (addSpaceBeforeParensInFunCall functionOrMethod px) sepSpace

let shortLids =
col sepDot lids (fun (s, r) ->
genTriviaFor Ident_ r !-s
+> sepNlnWhenWriteBeforeNewlineNotEmpty sepNone)

let short = shortLids +> addSpace +> genExpr astContext px

let long =
let args =
addSpace
+> expressionFitsOnRestOfLine
(genExpr astContext px)
(genMultilineFunctionApplicationArguments astContext px)

ifElseCtx (futureNlnCheck shortLids) (genFunctionNameWithMultilineLids args lids) (shortLids +> args)

expressionFitsOnRestOfLine short long

| AppSingleParenArg (e, px) ->
let sepSpace (ctx: Context) =
match e with
Expand Down Expand Up @@ -2805,9 +2828,7 @@ and genLidsWithDots (lids: (string * range) list) =
+> col !- "." lids (fun (s, _) -> !-s)

and genLidsWithDotsAndNewlines (lids: (string * range) list) =
optSingle (fun (_, r) -> enterNodeFor Ident_ r) (List.tryHead lids)
+> !- "."
+> col (sepNln +> !- ".") lids (fun (s, _) -> !-s)
col sepNln lids (fun (s, r) -> genTriviaFor Ident_ r !- $".{s}")

and genSpaceBeforeLids
(currentIndex: int)
Expand All @@ -2833,8 +2854,7 @@ and genSpaceBeforeLids
and genFunctionNameWithMultilineLids f lids =
match lids with
| (s, r) :: t ->
enterNodeFor Ident_ r
+> !-s
genTriviaFor Ident_ r !-s
+> indent
+> sepNln
+> genLidsWithDotsAndNewlines t
Expand Down

0 comments on commit b5d7e33

Please sign in to comment.