@@ -178,16 +178,18 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
178 goesAfter
178 goesAfter
179 && logicallyFollows
179 && logicallyFollows
180 && noEltBetween
180 && noEltBetween
181− && (continuation || supersedesParentElt)
181+ && (continuation || lastInEnclosing || supersedesParentElt)
183 -- 1) The comment starts after end of the AST element:
183 -- 1) The comment starts after end of the AST element:
184 goesAfter =
184 goesAfter =
185 realSrcSpanStart l >= realSrcSpanEnd ref
185 realSrcSpanStart l >= realSrcSpanEnd ref
186− -- 2) The comment logically belongs to the element, three cases:
186+ -- 2) The comment logically belongs to the element, four cases:
187 logicallyFollows
187 logicallyFollows
188 = theSameLine l ref -- a) it's on the same line
188 = theSameLine l ref -- a) it's on the same line
189 || isPrevHaddock comment -- b) it's a Haddock string starting with -- ^
189 || isPrevHaddock comment -- b) it's a Haddock string starting with -- ^
190 || continuation -- c) it's a continuation of a comment block
190 || continuation -- c) it's a continuation of a comment block
191+ || lastInEnclosing -- d) it's the last element in the enclosing construct
191 -- 3) There is no other AST element between this element and the comment:
193 -- 3) There is no other AST element between this element and the comment:
192 noEltBetween =
194 noEltBetween =
193 case mnSpn of
195 case mnSpn of
@@ -217,6 +219,15 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
217 Nothing -> False
219 Nothing -> False
218 Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
220 Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
223+ case (,) <$> meSpn <*> mnSpn of
225+ Just (espn, nspn) ->
226+ -- Make sure that the comment is inside the parent
227+ realSrcSpanEnd l <= realSrcSpanEnd espn
228+ -- Check if the next element is outside of the parent
229+ && realSrcSpanEnd espn < realSrcSpanStart nspn
220-- | Output a 'Comment'. This is a low-level printing function.
231-- | Output a 'Comment'. This is a low-level printing function.
222spitComment :: Comment -> R ()
233spitComment :: Comment -> R ()