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