From 9a67492dcc44f4a3f234292df310c30ffb63b80e Mon Sep 17 00:00:00 2001 From: kokobd Date: Wed, 21 Sep 2022 23:20:24 +0800 Subject: [PATCH] wip: add comments --- .../src/Ide/Plugin/HaddockComments.hs | 1 - .../src/Ide/Plugin/HaddockComments/Data.hs | 23 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs index 724e82efbfa..66ea479416e 100644 --- a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs +++ b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs @@ -50,7 +50,6 @@ codeActionProvider ideState _pId (CodeActionParams _ _ (TextDocumentIdentifier u genList :: [Maybe [LHsDecl GhcPs] -> Maybe Anns -> Range -> Maybe (T.Text, TextEdit)] genList = [ runGenCommentsSimple genForSig, - -- runGenCommentsSimple genForRecord runGenComments genForDataDecl ] diff --git a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments/Data.hs b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments/Data.hs index ddc3c210303..5797f5614ca 100644 --- a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments/Data.hs +++ b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments/Data.hs @@ -35,24 +35,33 @@ updateDataAnns decl@(L declLoc (TyClD _ DataDecl {tcdDataDefn = HsDataDefn { dd_ getAnnsT >>= (\anns -> unless (missingSomeHaddock anns cons) (lift Nothing)) -- visit each constructor and field - addHaddockCommentsToList True declLoc cons + addHaddockCommentsToList True declLoc (G AnnVbar) cons for_ cons $ \case - L conLoc ConDeclH98 { con_args = RecCon (L _ fields) } -> addHaddockCommentsToList False conLoc fields + L conLoc ConDeclH98 { con_args = RecCon (L _ fields) } -> addHaddockCommentsToList False conLoc (G AnnComma) fields _ -> pure () modifyAnnsT $ Map.adjust (\ann -> ann {annPriorComments = []}) (mkAnnKey decl) -updateDataAnns _ = pure () +updateDataAnns _ = lift Nothing --- TODO Add explaination to this complex function. -addHaddockCommentsToList :: (Data a, Monad m) => Bool -> SrcSpan -> [Located a] -> TransformT m () -addHaddockCommentsToList usePrevNodeAsAnchor outerLoc nodes = +-- | Add haddock comments to a list of nodes +addHaddockCommentsToList + :: (Data a, Monad m) + => Bool -- ^ If true, for each node, use previous node in the list as the anchor. Otherwise, use the outer node + -> SrcSpan -- ^ The outer node + -> KeywordId -- ^ The seperator between adjacent nodes + -> [Located a] -- ^ The list of nodes. Haddock comments will be added to each of them + -> TransformT m () +addHaddockCommentsToList usePrevNodeAsAnchor outerLoc seperator nodes = for_ (zip nodes (Nothing: fmap Just nodes)) $ \(node, prevNode) -> do + -- We don't add new ones to nodes with existing haddock comments. addHaddockCommentToCurrentNode <- fmap (not . fromMaybe True . flip hasHaddock node) getAnnsT when addHaddockCommentToCurrentNode $ do + -- Check if the start of 'node' is on the end line of 'prevNode'. + -- When this occurs, we do not preserve the indentation. Instead, we move the seperator to the next line let sameLineAsPrev = maybe False (\prevNode' -> notSeperatedByLineEnding prevNode' node) prevNode when sameLineAsPrev $ modifyAnnsT $ \anns -> let updateSepAnn :: Annotation -> Annotation updateSepAnn ann = ann {annsDP = - Map.toList . Map.adjust (const (DP (1,0))) (G AnnVbar) . Map.fromList $ annsDP ann} + Map.toList . Map.adjust (const (DP (1,0))) seperator . Map.fromList $ annsDP ann} in flip (maybe anns) prevNode $ \prevNode' -> Map.adjust updateSepAnn (mkAnnKey prevNode') anns let anchorCol = maybe 0 srcSpanStartCol . realSpan . maybe outerLoc getLoc $ if usePrevNodeAsAnchor then prevNode else Nothing