<commit_before>module Language.Java.Pretty where

import Text.PrettyPrint
import Data.Char (toLower)
import Data.List (intersperse)

import Language.Java.Syntax


prettyPrint :: Pretty a => a -> String
prettyPrint = show . pretty

parenPrec :: Int -> Int -> Doc -> Doc
parenPrec inheritedPrec currentPrec t
        | inheritedPrec <= 0          = t
	| inheritedPrec < currentPrec = parens t
	| otherwise                   = t

class Pretty a where
  pretty :: a -> Doc
  pretty = prettyPrec 0
  
  prettyPrec :: Int -> a -> Doc
  prettyPrec _ = pretty

-----------------------------------------------------------------------
-- Packages

instance Pretty CompilationUnit where
  prettyPrec p (CompilationUnit mpd ids tds) =
    vcat $ ((maybePP p mpd): map (prettyPrec p) ids) ++ map (prettyPrec p) tds

instance Pretty PackageDecl where
  prettyPrec p (PackageDecl name) = text "package" <+> prettyPrec p name <> semi

instance Pretty ImportDecl where
  prettyPrec p (ImportDecl st name wc) =
    text "import" <+> opt st (text "static")
                  <+> prettyPrec p name <> opt wc (text ".*")
                  <> semi

-----------------------------------------------------------------------
-- Declarations

instance Pretty TypeDecl where
  prettyPrec p (ClassTypeDecl     cd) = prettyPrec p cd
  prettyPrec p (InterfaceTypeDecl id) = prettyPrec p id

instance Pretty ClassDecl where
  prettyPrec p (EnumDecl mods ident impls body) =
    hsep [hsep (map (prettyPrec p) mods)
          , text "enum"
          , prettyPrec p ident 
          , ppImplements p impls
         ] $$ prettyPrec p body

  prettyPrec p (ClassDecl mods ident tParams mSuper impls body) =
    hsep [hsep (map (prettyPrec p) mods)
          , text "class"
          , prettyPrec p ident
          , ppTypeParams p tParams
          , ppExtends p (maybe [] return mSuper)
          , ppImplements p impls
         ] $$ prettyPrec p body

instance Pretty ClassBody where
  prettyPrec p (ClassBody ds) =
    braceBlock (map (prettyPrec p) ds)
    
instance Pretty EnumBody where
  prettyPrec p (EnumBody cs ds) =
    braceBlock $ 
        punctuate comma (map (prettyPrec p) cs) ++ 
        opt (not $ null ds) semi : map (prettyPrec p) ds

instance Pretty EnumConstant where
  prettyPrec p (EnumConstant ident args mBody) =
    prettyPrec p ident 
        -- needs special treatment since even the parens are optional
        <> opt (not $ null args) (ppArgs p args) 
      $$ maybePP p mBody

instance Pretty InterfaceDecl where
  prettyPrec p (InterfaceDecl mods ident tParams impls body) =
    hsep [hsep (map (prettyPrec p) mods)
          , text "interface"
          , prettyPrec p ident
          , ppTypeParams p tParams
          , ppImplements p impls
         ] $$ prettyPrec p body

instance Pretty InterfaceBody where
  prettyPrec p (InterfaceBody mds) =
    braceBlock (map (prettyPrec p) mds)

instance Pretty Decl where
  prettyPrec p (MemberDecl md) = prettyPrec p md
  prettyPrec p (InitDecl b bl) =
    opt b (text "static") <+> prettyPrec p bl

instance Pretty MemberDecl where
  prettyPrec p (FieldDecl mods t vds) =
    hsep (map (prettyPrec p) mods ++ prettyPrec p t:map (prettyPrec p) vds) <> semi

  prettyPrec p (MethodDecl mods tParams mt ident fParams throws body) =
    hsep [hsep (map (prettyPrec p) mods)
          , ppTypeParams p tParams
          , ppResultType p mt
          , prettyPrec p ident
          , ppArgs p fParams
          , ppThrows p throws
         ] $$ prettyPrec p body

  prettyPrec p (ConstructorDecl mods tParams ident fParams throws body) =<commit_msg>fix fieldDecl printer for multiple variables with same type part<commit_after>module Language.Java.Pretty where

import Text.PrettyPrint
import Data.Char (toLower)
import Data.List (intersperse)

import Language.Java.Syntax


prettyPrint :: Pretty a => a -> String
prettyPrint = show . pretty

parenPrec :: Int -> Int -> Doc -> Doc
parenPrec inheritedPrec currentPrec t
        | inheritedPrec <= 0          = t
	| inheritedPrec < currentPrec = parens t
	| otherwise                   = t

class Pretty a where
  pretty :: a -> Doc
  pretty = prettyPrec 0
  
  prettyPrec :: Int -> a -> Doc
  prettyPrec _ = pretty

-----------------------------------------------------------------------
-- Packages

instance Pretty CompilationUnit where
  prettyPrec p (CompilationUnit mpd ids tds) =
    vcat $ ((maybePP p mpd): map (prettyPrec p) ids) ++ map (prettyPrec p) tds

instance Pretty PackageDecl where
  prettyPrec p (PackageDecl name) = text "package" <+> prettyPrec p name <> semi

instance Pretty ImportDecl where
  prettyPrec p (ImportDecl st name wc) =
    text "import" <+> opt st (text "static")
                  <+> prettyPrec p name <> opt wc (text ".*")
                  <> semi

-----------------------------------------------------------------------
-- Declarations

instance Pretty TypeDecl where
  prettyPrec p (ClassTypeDecl     cd) = prettyPrec p cd
  prettyPrec p (InterfaceTypeDecl id) = prettyPrec p id

instance Pretty ClassDecl where
  prettyPrec p (EnumDecl mods ident impls body) =
    hsep [hsep (map (prettyPrec p) mods)
          , text "enum"
          , prettyPrec p ident 
          , ppImplements p impls
         ] $$ prettyPrec p body

  prettyPrec p (ClassDecl mods ident tParams mSuper impls body) =
    hsep [hsep (map (prettyPrec p) mods)
          , text "class"
          , prettyPrec p ident
          , ppTypeParams p tParams
          , ppExtends p (maybe [] return mSuper)
          , ppImplements p impls
         ] $$ prettyPrec p body

instance Pretty ClassBody where
  prettyPrec p (ClassBody ds) =
    braceBlock (map (prettyPrec p) ds)
    
instance Pretty EnumBody where
  prettyPrec p (EnumBody cs ds) =
    braceBlock $ 
        punctuate comma (map (prettyPrec p) cs) ++ 
        opt (not $ null ds) semi : map (prettyPrec p) ds

instance Pretty EnumConstant where
  prettyPrec p (EnumConstant ident args mBody) =
    prettyPrec p ident 
        -- needs special treatment since even the parens are optional
        <> opt (not $ null args) (ppArgs p args) 
      $$ maybePP p mBody

instance Pretty InterfaceDecl where
  prettyPrec p (InterfaceDecl mods ident tParams impls body) =
    hsep [hsep (map (prettyPrec p) mods)
          , text "interface"
          , prettyPrec p ident
          , ppTypeParams p tParams
          , ppImplements p impls
         ] $$ prettyPrec p body

instance Pretty InterfaceBody where
  prettyPrec p (InterfaceBody mds) =
    braceBlock (map (prettyPrec p) mds)

instance Pretty Decl where
  prettyPrec p (MemberDecl md) = prettyPrec p md
  prettyPrec p (InitDecl b bl) =
    opt b (text "static") <+> prettyPrec p bl

instance Pretty MemberDecl where
  prettyPrec p (FieldDecl mods t vds) =
    hsep (map (prettyPrec p) mods ++ prettyPrec p t:punctuate (text ",") (map (prettyPrec p) vds)) <> semi

  prettyPrec p (MethodDecl mods tParams mt ident fParams throws body) =
    hsep [hsep (map (prettyPrec p) mods)
          , ppTypeParams p tParams
          , ppResultType p mt
          , prettyPrec p ident
          , ppArgs p fParams
          , ppThrows p throws
         ] $$ prettyPrec p body

  prettyPrec p (ConstructorDecl mods tParams ident fParams throws body) =
{'fraction_non_alphanumeric': 0.11075862068965517, 'fraction_numerical': 0.0005517241379310345, 'mean_word_length': 2.2765476728422955, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7355029', 'n_tokens_mistral': 2181, 'n_tokens_neox': 1998, 'n_words': 809}