@@ -38,6 +38,20 @@ p_classDecl ctx name HsQTvs {..} fixity fdeps csigs cdefs cats catdefs cdocs = d
3838 signatureSpans = getLoc name : variableSpans
3939 dependencySpans = getLoc <$> fdeps
4040 combinedSpans = getLoc ctx : (signatureSpans ++ dependencySpans)
41+ -- GHC's AST does not necessarily store each kind of element in source
42+ -- location order. This happens because different declarations are stored
43+ -- in different lists. Consequently, to get all the declarations in proper
44+ -- order, they need to be manually sorted.
45+ sigs = (getLoc &&& fmap (SigD NoExt)) <$> csigs
46+ vals = (getLoc &&& fmap (ValD NoExt)) <$> toList cdefs
47+ tyFams = (getLoc &&& fmap (TyClD NoExt . FamDecl NoExt)) <$> cats
48+ docs = (getLoc &&& fmap (DocD NoExt)) <$> cdocs
49+ tyFamDefs =
50+ ( getLoc &&& fmap (InstD NoExt . TyFamInstD NoExt . defltEqnToInstDecl)
51+ )
52+ <$> catdefs
53+ allDecls =
54+ snd <$> sortOn fst (sigs <> vals <> tyFams <> tyFamDefs <> docs)
4155 txt "class"
4256 switchLayout combinedSpans $ do
4357 breakpoint
@@ -50,23 +64,10 @@ p_classDecl ctx name HsQTvs {..} fixity fdeps csigs cdefs cats catdefs cdocs = d
5064 (p_rdrName name)
5165 (located' p_hsTyVarBndr <$> hsq_explicit)
5266 inci (p_classFundeps fdeps)
53− -- GHC's AST does not necessarily store each kind of element in source
54− -- location order. This happens because different declarations are stored
55− -- in different lists. Consequently, to get all the declarations in proper
56− -- order, they need to be manually sorted.
57− let sigs = (getLoc &&& fmap (SigD NoExt)) <$> csigs
58− vals = (getLoc &&& fmap (ValD NoExt)) <$> toList cdefs
59− tyFams = (getLoc &&& fmap (TyClD NoExt . FamDecl NoExt)) <$> cats
60− docs = (getLoc &&& fmap (DocD NoExt)) <$> cdocs
61− tyFamDefs =
62− ( getLoc &&& fmap (InstD NoExt . TyFamInstD NoExt . defltEqnToInstDecl)
63− )
64− <$> catdefs
65− allDecls =
66− snd <$> sortOn fst (sigs <> vals <> tyFams <> tyFamDefs <> docs)
67+ unless (null allDecls) $ do
68+ breakpoint
69+ txt "where"
6770 unless (null allDecls) $ do
68− space
69− txt "where"
7071 breakpoint -- Ensure whitespace is added after where clause.
7172 inci (p_hsDeclsRespectGrouping Associated allDecls)
7273p_classDecl _ _ XLHsQTyVars {} _ _ _ _ _ _ _ = notImplemented "XLHsQTyVars"