PageRenderTime 74ms CodeModel.GetById 27ms app.highlight 24ms RepoModel.GetById 19ms app.codeStats 0ms

/src/Editing/Show.hs

http://github.com/Eelis/geordi
Haskell | 147 lines | 109 code | 36 blank | 2 comment | 0 complexity | 7b8c97dce74a001c701fec0e116182d9 MD5 | raw file
  1{-# LANGUAGE UnicodeSyntax, TypeSynonymInstances, FlexibleInstances, PatternGuards #-}
  2
  3module Editing.Show (showTextEdit, Show(..)) where
  4
  5import Cxx.Show ()
  6import qualified Data.List as List
  7import Util (isVowel, show_long_opts, capitalize, commas_and, Ordinal)
  8import Cxx.Basics (DeclaratorId, Findable)
  9import Data.Foldable (toList)
 10import Editing.Basics
 11import Editing.Commands
 12import qualified Prelude
 13import Prelude hiding (Show(..))
 14
 15class Show a where show :: a  String
 16  -- To let us define our own instances for things like Either and String.
 17
 18instance Show Ordinal where show = Prelude.show
 19instance Show DeclaratorId where show = Prelude.show
 20
 21instance Show Wrapping where
 22  show (Wrapping "<" ">") = "angle brackets"
 23  show (Wrapping "{" "}") = "curly brackets"
 24  show (Wrapping "[" "]") = "square brackets"
 25  show (Wrapping "(" ")") = "parentheses"
 26  show (Wrapping "'" "'") = "single quotes"
 27  show (Wrapping "\"" "\"") = "double quotes"
 28  show (Wrapping x y) = x ++ " and " ++ y
 29
 30instance Show a  Show (EverythingOr a) where
 31  show Everything = "everything"
 32  show (NotEverything x) = show x
 33
 34instance (Show a, Show b)  Show (Either a b) where
 35  show (Left x) = show x; show (Right x) = show x
 36
 37instance Show Side where show Before = "before"; show After = "after"
 38
 39instance Show a  Show (Ranked a) where
 40  show (Sole s) = show s
 41  show (Ranked r s) = show r ++ " " ++ show s
 42
 43instance Show Findable where show = Prelude.show
 44
 45instance Show Position where
 46  show (Position Before (In (Absolute Everything) Nothing)) = "at start"
 47  show (Position After (In (Absolute Everything) Nothing)) = "at end"
 48  show (Position a x) = show a ++ " " ++ show x
 49
 50instance Show a  Show (AndList a) where
 51  show (AndList l) = concat $ List.intersperse " and " $ map show $ toList l
 52
 53instance Show Substrs where show (Substrs l) = show l
 54
 55instance Show PositionsClause where show (PositionsClause ba s) = show ba ++ " " ++ show s
 56
 57instance Show AppendPositionsClause where
 58  show (NonAppendPositionsClause pc) = show pc
 59  show (AppendIn d) = show d
 60
 61instance Show PrependPositionsClause where
 62  show (NonPrependPositionsClause pc) = show pc
 63  show (PrependIn d) = show d
 64
 65instance Show Bound where
 66  show (Bound mba x) = maybe "" ((++ " ") . show) mba ++ show x
 67
 68instance Show Betw where show (Betw x y) = "between " ++ show x ++ " and " ++ show y
 69
 70instance Show a  Show (Relative a) where
 71  show (Absolute x) = show x
 72  show (Between x y) = show x ++ " " ++ show y
 73  show (Relative x y z) = show x ++ " " ++ show y ++ " " ++ show z
 74  show (FromTill b c) = "from " ++ show b ++ " till " ++ show c
 75
 76instance Show a  Show (In a) where show (In x incl) = show x ++ maybe "" show incl
 77
 78instance Show ImplicitBodyOf where show (ImplicitBodyOf x) = show x
 79instance Show ImplicitDeclarationOf where show (ImplicitDeclarationOf x) = show x
 80
 81instance Show InClause where show (InClause x) = "in " ++ show x
 82
 83instance Show RelativeBound where
 84  show Front = "front"; show Back = "back"
 85  show (RelativeBound mba x) = maybe "" ((++ " ") . show) mba ++ show x
 86
 87instance Show OccurrencesClause where show (OccurrencesClause l) = show (AndList l)
 88
 89instance Show a  Show (Rankeds a) where
 90  show (Sole' x) = show x
 91  show (All x) = "all " ++ show x
 92  show (AllBut x y) = "all but " ++ show x ++ " " ++ show y
 93  show (Rankeds l x) = show l ++ " " ++ show x
 94
 95instance Show Replacer where
 96  show (Replacer x y) = show x ++ " with " ++ show y
 97  show (ReplaceOptions o o') = show_long_opts o ++ " with " ++ show_long_opts o'
 98instance Show Changer where
 99  show (Changer x y) = show x ++ " to " ++ show y
100  show (ChangeOptions o o') = show_long_opts o ++ " to " ++ show_long_opts o'
101instance Show Eraser where
102  show (EraseText l) = show l
103  show (EraseOptions o) = show_long_opts o
104  show (EraseAround w l) = show w ++ " " ++ show l
105
106instance Show UsePattern where show (UsePattern p) = p
107instance Show UseClause where show (UseString s) = show s; show (UseOptions o) = show_long_opts o
108instance Show Mover where show (Mover x y) = show x ++ " to " ++ show y
109
110instance Show String where show  = showEditOperand
111
112instance Show a  Show (Around a) where show (Around a) = "around " ++ show a
113
114data Tense = Present | Past
115
116past :: String  String
117past "wrap" = "wrapped"
118past "swap" = "swapped"
119past s | isVowel (List.last s) = s ++ "d"
120past s = s ++ "ed"
121
122tense :: Tense  String  String
123tense Present s = s
124tense Past s = past s
125
126instance Show Insertee where
127  show (SimpleInsert s) = show s
128  show (WrapInsert w) = show w
129
130show_command :: Tense  Command  String
131show_command t (Insert s p) = tense t "insert" ++ " " ++ show s ++ " " ++ show p
132show_command t (Erase l) = tense t "erase" ++ " " ++ show l
133show_command t (Replace l) = tense t "replace" ++ " " ++ show l
134show_command t (Change l) = tense t "change" ++ " " ++ show l
135show_command t (Use l) = tense t "use" ++ " " ++ show l
136show_command t (Prepend s mp) = tense t "prepend" ++ " " ++ show s ++ maybe "" ((" " ++) . show) mp
137show_command t (Append s mp) = tense t "append" ++ " " ++ show s ++ maybe "" ((" " ++) . show) mp
138show_command t (Move l) = tense t "move" ++ " " ++ show l
139show_command t (Swap l Nothing) = tense t "swap" ++ " " ++ show l
140show_command t (Swap l (Just y)) = tense t "swap" ++ " " ++ show l ++ " with " ++ show y
141show_command _ _ = "<command>"
142
143instance Prelude.Show Command where
144  show = show_command Past
145  showList l r = capitalize (commas_and $ map Prelude.show l) ++ "." ++ r
146
147instance Prelude.Show Position where show = Editing.Show.show