mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-24 03:52:50 -06:00
Nicer spacing and indentation in JavaScript pretty-printer.
This commit is contained in:
@@ -18,22 +18,19 @@ doc = (:)
|
|||||||
render :: Doc -> String
|
render :: Doc -> String
|
||||||
render d = rend 0 (map ($ "") $ d []) "" where
|
render d = rend 0 (map ($ "") $ d []) "" where
|
||||||
rend i ss = case ss of
|
rend i ss = case ss of
|
||||||
"[" :ts -> showChar '[' . rend i ts
|
t:ts | not (spaceAfter t) -> showString t . rend i ts
|
||||||
"(" :ts -> showChar '(' . rend i ts
|
t:ts@(t':_) | not (spaceBefore t') -> showString t . rend i ts
|
||||||
"{" :ts -> showChar '{' . new (i+1) . rend (i+1) ts
|
t:ts -> space t . rend i ts
|
||||||
"}" : ";":ts -> new (i-1) . space "}" . showChar ';' . new (i-1) . rend (i-1) ts
|
[] -> id
|
||||||
"}" :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
|
|
||||||
new i = showChar '\n' . replicateS (2*i) (showChar ' ') . dropWhile isSpace
|
new i = showChar '\n' . replicateS (2*i) (showChar ' ') . dropWhile isSpace
|
||||||
space t = showString t . (\s -> if null s then "" else (' ':s))
|
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 :: Doc -> Doc
|
||||||
parenth ss = doc (showChar '(') . ss . doc (showChar ')')
|
parenth ss = doc (showChar '(') . ss . doc (showChar ')')
|
||||||
|
|
||||||
@@ -100,7 +97,7 @@ instance Print Element where
|
|||||||
|
|
||||||
prtList es = case es of
|
prtList es = case es of
|
||||||
[] -> (concatD [])
|
[] -> (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
|
instance Print Stmt where
|
||||||
prt i e = case e of
|
prt i e = case e of
|
||||||
|
|||||||
Reference in New Issue
Block a user