@@ -178,16 +178,18 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
178178 goesAfter
179179 && logicallyFollows
180180 && noEltBetween
181− && (continuation || supersedesParentElt)
181+ && (continuation || lastInEnclosing || supersedesParentElt)
182182 where
183183 -- 1) The comment starts after end of the AST element:
184184 goesAfter =
185185 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:
187187 logicallyFollows
188188 = theSameLine l ref -- a) it's on the same line
189189 || isPrevHaddock comment -- b) it's a Haddock string starting with -- ^
190190 || continuation -- c) it's a continuation of a comment block
191+ || lastInEnclosing -- d) it's the last element in the enclosing construct
192+
191193 -- 3) There is no other AST element between this element and the comment:
192194 noEltBetween =
193195 case mnSpn of
@@ -217,6 +219,15 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
217219 Nothing -> False
218220 Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
219221
222+ lastInEnclosing =
223+ case (,) <$> meSpn <*> mnSpn of
224+ Nothing -> False
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
230+
220231-- | Output a 'Comment'. This is a low-level printing function.
221232
222233spitComment :: Comment -> R ()