Skip to content

Commit

Permalink
fix comment printing
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Dec 4, 2024
1 parent dff0142 commit ce6d87a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/Juvix/Data/Comment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
16 changes: 16 additions & 0 deletions tests/positive/Format.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ce6d87a

Please sign in to comment.