Skip to content

Commit

Permalink
Improved triviaBetweenAttributeAndParentBinding (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jun 12, 2020
1 parent 57496fa commit 53b4b39
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions src/Fantomas/Trivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ let private findParsedHashOnLineAndEndswith (triviaNodes: TriviaNodeAssigner lis

// Only return the attributeList when the trivia is under it and above the AST node of which the attribute is a child node.
// f.ex.
// [Foo()]
// [<Foo()>]
// #if BAR
// let meh = ()
// The trivia '#if BAR' should be linked to the [Foo()] attribute
// The trivia '#if BAR' should be linked to the [<Foo()>] attribute
//
// The reason for this is that the range of the attribute is not part of the range of the parent binding.
// This can lead to weird results when used in CodePrinter.
Expand All @@ -198,37 +198,25 @@ let private triviaBetweenAttributeAndParentBinding (triviaNodes: TriviaNodeAssig
triviaNodes
|> List.filter (fun t ->
match t.Type with
| MainNode("SynAttribute")
| MainNode("SynExpr.Paren")
| MainNode("SynExpr.Tuple")
| MainNode("SynExpr.Const") ->
false
| MainNode(_) -> true
| MainNode("SynAttributeList")
| MainNode("SynModuleDecl.Let")
| MainNode("SynModuleDecl.Types")
| MainNode("SynModuleSigDecl.NestedModule")
| MainNode("ValSpfn")
| MainNode("SynMemberDefn.Member")
| MainNode("SynMemberDefn.LetBindings") -> true
| _ -> false
)
|> List.indexed

let parentBinding =
filteredNodes
|> List.tryFind (fun (_,t) ->
match t.Type with
| MainNode("SynModuleDecl.Let") when (t.Range.StartLine > line) -> true
| MainNode("SynAttributeList") when (t.Range.StartLine > line) -> true
| MainNode("SynModuleDecl.Types") when (t.Range.StartLine > line) -> true
| _ -> false
)

let attributeList =
filteredNodes
|> List.tryFind (fun (_,t) ->
match t.Type with
| MainNode("SynAttributeList") when (t.Range.StartLine < line) -> true
| _ -> false
)

match attributeList, parentBinding with
| Some (ai,a), Some (mdli,_) when (ai + 1 = mdli && a.Range.StartLine = a.Range.EndLine) -> Some a
| _ -> None
|> List.pairwise
filteredNodes |> List.tryFind (function
| a, p when (a.Type = MainNode("SynAttributeList") && a.Range.StartLine < line && a.Range.StartLine = a.Range.EndLine) ->
match p.Type with
| MainNode("SynModuleDecl.Let") when (p.Range.StartLine > line) -> true
| MainNode("SynAttributeList") when (p.Range.StartLine > line) -> true
| MainNode("SynModuleDecl.Types") when (p.Range.StartLine > line) -> true
| _ -> false
| _ -> false)
|> Option.map fst

let private findASTNodeOfTypeThatContains (nodes: TriviaNodeAssigner list) typeName range =
nodes
Expand Down

0 comments on commit 53b4b39

Please sign in to comment.