From defb7a9d269936fd8f545189afd2bc97503f6df2 Mon Sep 17 00:00:00 2001 From: Jan Mas Rovira Date: Wed, 4 Dec 2024 13:29:16 +0100 Subject: [PATCH 1/2] fix imports --- src/Juvix/Compiler/Concrete/Data/Name.hs | 6 +++++- src/Juvix/Data/CodeAnn.hs | 6 +++++- src/Juvix/Data/Comment.hs | 24 ++++++++++++++++++++---- src/Juvix/Data/Effect/ExactPrint/Base.hs | 2 +- src/Juvix/Data/NameKind.hs | 2 +- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Juvix/Compiler/Concrete/Data/Name.hs b/src/Juvix/Compiler/Concrete/Data/Name.hs index 98346fc14b..8b9f4c6d88 100644 --- a/src/Juvix/Compiler/Concrete/Data/Name.hs +++ b/src/Juvix/Compiler/Concrete/Data/Name.hs @@ -1,8 +1,12 @@ module Juvix.Compiler.Concrete.Data.Name where import Data.List.NonEmpty.Extra qualified as NonEmpty +import Juvix.Data.Fixity +import Juvix.Data.Loc +import Juvix.Data.TopModulePathKey +import Juvix.Data.WithLoc import Juvix.Extra.Serialize -import Juvix.Prelude +import Juvix.Prelude.Base import Juvix.Prelude.Pretty as Pretty type Symbol = WithLoc Text diff --git a/src/Juvix/Data/CodeAnn.hs b/src/Juvix/Data/CodeAnn.hs index f61b6680c3..0e700f4a3c 100644 --- a/src/Juvix/Data/CodeAnn.hs +++ b/src/Juvix/Data/CodeAnn.hs @@ -6,10 +6,14 @@ module Juvix.Data.CodeAnn where import Juvix.Compiler.Concrete.Data.Name +import Juvix.Data.Error.GenericError +import Juvix.Data.IsImplicit import Juvix.Data.Keyword +import Juvix.Data.NameId import Juvix.Data.NameKind +import Juvix.Data.WithLoc import Juvix.Extra.Strings qualified as Str -import Juvix.Prelude +import Juvix.Prelude.Base import Juvix.Prelude.Pretty hiding (braces, brackets, group, list, parens) import Prettyprinter.Render.Terminal (Color (..), bold, colorDull) diff --git a/src/Juvix/Data/Comment.hs b/src/Juvix/Data/Comment.hs index 57a7f2e349..aee583fc52 100644 --- a/src/Juvix/Data/Comment.hs +++ b/src/Juvix/Data/Comment.hs @@ -1,6 +1,8 @@ module Juvix.Data.Comment where import Data.HashMap.Strict qualified as HashMap +import Data.Text qualified as Text +import Juvix.Data.CodeAnn (CodeAnn (..), PrettyCodeAnn, ppCodeAnn) import Juvix.Data.Loc import Juvix.Extra.Strings qualified as Str import Juvix.Prelude.Base @@ -99,14 +101,28 @@ instance HasLoc SpaceSection where instance HasLoc Comment where getLoc = (^. commentInterval) +instance PrettyCodeAnn Comment where + ppCodeAnn = annotate AnnComment . pretty + instance Pretty Comment where pretty :: Comment -> Doc ann - pretty c = delim (pretty (c ^. commentText)) + pretty c = delim (c ^. commentText) where - delim :: Doc ann -> Doc ann + delim :: Text -> Doc ann delim = case c ^. commentType of - CommentOneLine -> (Str.commentLineStart <>) - CommentBlock -> enclose Str.commentBlockStart Str.commentBlockEnd + CommentOneLine -> (Str.commentLineStart <>) . pretty + CommentBlock -> + enclose Str.commentBlockStart Str.commentBlockEnd + . pretty + . trimPrefixSpace + + trimPrefixSpace :: Text -> Text + trimPrefixSpace = + fromJust + . Text.stripSuffix "\n" + . Text.unlines + . map Text.strip + . Text.lines allComments :: Comments -> [Comment] allComments c = diff --git a/src/Juvix/Data/Effect/ExactPrint/Base.hs b/src/Juvix/Data/Effect/ExactPrint/Base.hs index 1fd17ce248..e918077f85 100644 --- a/src/Juvix/Data/Effect/ExactPrint/Base.hs +++ b/src/Juvix/Data/Effect/ExactPrint/Base.hs @@ -154,7 +154,7 @@ printSpaceSpan = mapM_ printSpaceSection . (^. spaceSpan) printComment :: (Members '[State Builder] r) => Comment -> Sem r () printComment c = do - append' (annotate AnnComment (P.pretty c)) + append' (ppCodeAnn c) hardline' popQueue :: (Members '[State Builder] r) => Sem r () diff --git a/src/Juvix/Data/NameKind.hs b/src/Juvix/Data/NameKind.hs index f2bd976169..88f05a4b61 100644 --- a/src/Juvix/Data/NameKind.hs +++ b/src/Juvix/Data/NameKind.hs @@ -1,7 +1,7 @@ module Juvix.Data.NameKind where import Juvix.Extra.Serialize -import Juvix.Prelude +import Juvix.Prelude.Base import Juvix.Prelude.Pretty import Prettyprinter.Render.Terminal From 4546bc5fcffabd7183699371fec09faa55f1bef6 Mon Sep 17 00:00:00 2001 From: Jan Mas Rovira Date: Wed, 4 Dec 2024 13:43:54 +0100 Subject: [PATCH 2/2] fix comment printing --- src/Juvix/Data/Comment.hs | 34 +++++++++++++++++++++++++++------- tests/positive/Format.juvix | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/Juvix/Data/Comment.hs b/src/Juvix/Data/Comment.hs index aee583fc52..061b0673ae 100644 --- a/src/Juvix/Data/Comment.hs +++ b/src/Juvix/Data/Comment.hs @@ -6,7 +6,7 @@ import Juvix.Data.CodeAnn (CodeAnn (..), PrettyCodeAnn, ppCodeAnn) import Juvix.Data.Loc import Juvix.Extra.Strings qualified as Str import Juvix.Prelude.Base -import Prettyprinter +import Prettyprinter hiding (concatWith) newtype Comments = Comments { _commentsByFile :: HashMap (Path Abs File) FileComments @@ -117,12 +117,32 @@ instance Pretty Comment where . trimPrefixSpace trimPrefixSpace :: Text -> Text - trimPrefixSpace = - fromJust - . Text.stripSuffix "\n" - . Text.unlines - . map Text.strip - . Text.lines + trimPrefixSpace txt = case Text.unsnoc txt of + Nothing -> "" + Just (_, l) -> + appendNl + . striplines + $ txt + where + striplines :: Text -> Text + striplines = + concatWith (\a b -> a <> "\n" <> b) + . run + . execOutputList + . go True + . Text.lines + go :: (Members '[Output Text] r) => Bool -> [Text] -> Sem r () + go isFirst = \case + [] -> return () + a : as + | isFirst && isLast -> output a + | isFirst -> output (Text.stripEnd a) >> go False as + | isLast -> output (Text.stripStart a) + | otherwise -> output (Text.strip a) >> go False as + where + isLast = null as + lastnl = '\n' == l + appendNl = if lastnl then (<> "\n") else id allComments :: Comments -> [Comment] allComments c = diff --git a/tests/positive/Format.juvix b/tests/positive/Format.juvix index 85d245f4a1..3ade7476bc 100644 --- a/tests/positive/Format.juvix +++ b/tests/positive/Format.juvix @@ -534,6 +534,22 @@ module MultiIf; | else := 2; end; +{-hi + +-} +module CommentAfterFun; + myfun (a : Nat) : Nat := + {- + My comment. + -} + {-nospaces-} + {- somespaces -} + {- first line + + last line -} + 1; +end; + module PublicImports; module Inner;