From a9dfb0e4772e36c0ac1031aa11b6f3f913bcde24 Mon Sep 17 00:00:00 2001 From: bringert Date: Sun, 21 Jan 2007 17:00:15 +0000 Subject: [PATCH] Nicer spacing and indentation in JavaScript pretty-printer. --- src/GF/JavaScript/PrintJS.hs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/GF/JavaScript/PrintJS.hs b/src/GF/JavaScript/PrintJS.hs index 402e20c9d..f901a62df 100644 --- a/src/GF/JavaScript/PrintJS.hs +++ b/src/GF/JavaScript/PrintJS.hs @@ -18,22 +18,19 @@ doc = (:) render :: Doc -> String render d = rend 0 (map ($ "") $ d []) "" where rend i ss = case ss of - "[" :ts -> showChar '[' . rend i ts - "(" :ts -> showChar '(' . rend i ts - "{" :ts -> showChar '{' . new (i+1) . rend (i+1) ts - "}" : ";":ts -> new (i-1) . space "}" . showChar ';' . new (i-1) . rend (i-1) ts - "}" :ts -> new (i-1) . showChar '}' . new (i-1) . rend (i-1) ts - ";" :ts -> showChar ';' . new i . rend i ts - t : "," :ts -> showString t . space "," . rend i ts - t : "." :ts -> showString t . showString "." . rend i ts - t : ")" :ts -> showString t . showChar ')' . rend i ts - t : "]" :ts -> showString t . showChar ']' . rend i ts - t : "[" :ts -> showString t . showChar '[' . rend i ts - t :ts -> space t . rend i ts - _ -> id + t:ts | not (spaceAfter t) -> showString t . rend i ts + t:ts@(t':_) | not (spaceBefore t') -> showString t . rend i ts + t:ts -> space t . rend i ts + [] -> id new i = showChar '\n' . replicateS (2*i) (showChar ' ') . dropWhile isSpace space t = showString t . (\s -> if null s then "" else (' ':s)) +spaceAfter :: String -> Bool +spaceAfter = (`notElem` [".","(","["]) + +spaceBefore :: String -> Bool +spaceBefore = (`notElem` [",",".",":",";","(",")","[","]","{","}"]) + parenth :: Doc -> Doc parenth ss = doc (showChar '(') . ss . doc (showChar ')') @@ -100,7 +97,7 @@ instance Print Element where prtList es = case es of [] -> (concatD []) - x:xs -> (concatD [prt 0 x , prt 0 xs]) + x:xs -> (concatD [prt 0 x , doc (showString "\n"), prt 0 xs]) -- HACKED! instance Print Stmt where prt i e = case e of