Skip to content

Commit

Permalink
Fix printing of indented block comments (#3225)
Browse files Browse the repository at this point in the history
- Closes #3143 

The formatting of block comments has changed slightly.
A comment block has the syntax `{- multi-line-text -}`.
The rules are:
1. The first line, i.e. the line that starts with `{-` is trimmed of
trailing space.
2. The last line, i.e. the line that ends with `-}` is trimmed of prefix
space.
3. Lines in the middle are trimmed of prefix and trailing space.

This makes it impossible to have such a comment:
```
{-
  indented
-}
```
The reasoning is that if we don't want to have an indentation sensitive
parser we have two choices.
1. Print all comments flushed to the left
2. Do not have indentation in the middle lines.

I thought 2. was better

---------

Co-authored-by: Łukasz Czajka <62751+lukaszcz@users.noreply.github.com>
  • Loading branch information
janmasrovira and lukaszcz authored Dec 6, 2024
1 parent 5029a56 commit 936045f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
46 changes: 41 additions & 5 deletions src/Juvix/Data/Comment.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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
import Prettyprinter
import Prettyprinter hiding (concatWith)

newtype Comments = Comments
{ _commentsByFile :: HashMap (Path Abs File) FileComments
Expand Down Expand Up @@ -99,14 +101,48 @@ 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 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
2 changes: 1 addition & 1 deletion src/Juvix/Data/Effect/ExactPrint/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
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 936045f

Please sign in to comment.