diff --git a/src/Data/Syntax/Declaration.hs b/src/Data/Syntax/Declaration.hs index 8e5d59fa4..2fbd0a511 100644 --- a/src/Data/Syntax/Declaration.hs +++ b/src/Data/Syntax/Declaration.hs @@ -9,6 +9,7 @@ import qualified Data.Set as Set import Diffing.Algorithm import Prologue import Proto3.Suite.Class +import Reprinting.Tokenize data Function a = Function { functionContext :: ![a], functionName :: !a, functionParameters :: ![a], functionBody :: !a } deriving (Eq, Ord, Show, Foldable, Traversable, Functor, Generic1, Hashable1, ToJSONFields1, Named1, Message1) @@ -58,6 +59,14 @@ instance Evaluatable Method where pure (Rval addr) where paramNames = foldMap (maybeToList . declaredName . subterm) +instance Tokenize Method where + tokenize Method{..} = within TMethod $ do + yield TOpen + methodName + within TParams $ surround_ (sep_ methodParameters) + methodBody + yield TClose + instance Declarations1 Method where liftDeclaredName declaredName = declaredName . methodName diff --git a/src/Language/Ruby/Translate.hs b/src/Language/Ruby/Translate.hs index f639361d1..779ea62c3 100644 --- a/src/Language/Ruby/Translate.hs +++ b/src/Language/Ruby/Translate.hs @@ -13,18 +13,24 @@ translatingRuby = flattened <~ auto step where step :: Splice -> Seq Splice step s@(Unhandled el cs) = case (el, cs) of - (Open, (Declaration DeclMethod):_) -> emit "def" <> directive Space - (Close, (Declaration DeclMethod):_) -> emit "end" - (Open, (List:(Declaration DeclMethod):_)) -> emit "(" - (Close, (List:(Declaration DeclMethod):_)) -> emit ")" + (TOpen, TMethod:_) -> emit "def" <> layout Space + (TClose, TMethod:_) -> emit "end" - (Open, Imperative:xs) -> stimesMonoid (n xs) (directives [HardWrap, Indent]) - (Separator, Imperative:xs) -> stimesMonoid (n xs) (directives [HardWrap, Indent]) - (Close, Imperative:_) -> directive HardWrap + (TOpen, TParams:TMethod:_) -> emit "(" + (TSep, TParams:TMethod:_) -> emit "," <> layout Space + (TClose, TParams:TMethod:_) -> emit ")" + + (TOpen, Imperative:xs) -> indented (n xs) + (TSep, Imperative:xs) -> indented (n xs) + (TClose, Imperative:_) -> layout HardWrap + + -- (TOpen, TComment:_) -> layout HardWrap + -- (TSep, TComment:_) -> layout HardWrap _ -> pure s where + indented times = stimesMonoid times (layouts [HardWrap, Indent]) emit = splice el cs n = length . filter (== Imperative)