{-# LANGUAGE TemplateHaskell, DeriveGeneric, StandaloneDeriving #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Interact.Types ( InteractName(..) , _LeftEditor, _RightEditor, _PrimitiveName , Validity, pattern Valid, pattern Invalid , InteractState(..) , HasLeft(..), HasRight(..), HasComplement(..), HasFocus(..), HasFocused(..), HasActive(..), HasLoadBrowser(..) , InteractInitial(..) , _InitialLeft, _InitialRight, _InitialEmpty , InteractConfig(..) , HasInitial(..), HasLens(..), HasCompileError(..) , InteractEvent , InteractApp , InteractDirection(..) , charseq , WithName(..) , module Control.Edit , module Control.Lens.Edit , module Control.DFST.Lens , module Data.Semigroup , module Numeric.Natural ) where import Data.Text (Text) import qualified Data.Text as Text import Data.Text.Lens import Data.Sequence (Seq(..)) import qualified Data.Sequence as Seq import qualified Data.Foldable as Foldable import Data.Semigroup (Semigroup(..), Last(..)) import Numeric.Natural import Brick import Brick.Focus import Brick.Widgets.Edit import Brick.Widgets.FileBrowser import Control.Lens import Control.Lens.TH import Control.Edit import Control.Lens.Edit import Control.DFST.Lens import Data.Text.Zipper.Generic import Control.DeepSeq import GHC.Generics (Generic) deriving instance Generic (StringEdit n c) instance (NFData n, NFData c) => NFData (StringEdit n c) deriving instance Generic (StringEdits n c) instance (NFData n, NFData c) => NFData (StringEdits n c) deriving instance Generic (DFSTAction s c c') instance (NFData s, NFData c, NFData c') => NFData (DFSTAction s c c') instance (NFData s, NFData c, NFData c') => NFData (DFSTComplement s c c') where rnf = foldr deepseq () data InteractName = LeftEditor | RightEditor | LoadBrowser | PrimitiveName !Text deriving (Eq, Ord, Show, Read, Generic) makePrisms ''InteractName type Validity = Bool pattern Valid = True pattern Invalid = False data InteractState c = InteractState { istLeft, istRight :: (Last Validity, Last (Seq Char, Int), StringEdits Natural Char) , istComplement :: c , istFocus :: FocusRing InteractName , istActive :: Bool , istLoadBrowser :: Maybe (FileBrowser InteractName) } makeLensesWith abbreviatedFields ''InteractState class HasFocused s a | s -> a where focused :: Traversal' s a instance HasFocused (InteractState c) (Last Validity, Last (Seq Char, Int), StringEdits Natural Char) where focused f st@InteractState{..} = case focusGetCurrent istFocus of Just LeftEditor -> left f st Just RightEditor -> right f st _other -> pure st data InteractInitial = InitialLeft Text | InitialRight Text | InitialEmpty deriving (Eq, Ord, Show, Read) makePrisms ''InteractInitial data InteractConfig c = InteractConfig { icfgInitial :: InteractInitial , icfgLens :: EditLens c (StringEdits Natural Char) (StringEdits Natural Char) , icfgCompileError :: Maybe String } instance HasEditLens (InteractConfig c) (StringEdits Natural Char) (StringEdits Natural Char) where type Complement (InteractConfig c) = c ground = ground . icfgLens propR = propR . icfgLens propL = propL . icfgLens makeLensesWith abbreviatedFields ''InteractConfig charseq :: Iso' Text (Seq Char) charseq = iso Text.unpack Text.pack . iso Seq.fromList Foldable.toList type InteractEvent = () type InteractApp c = App (InteractState c) InteractEvent InteractName data InteractDirection = PropagateLeft | PropagateRight deriving (Eq, Ord, Enum, Bounded, Show, Read) makePrisms ''InteractDirection infixr 1 `WithName` data WithName x n = WithName x n deriving (Eq, Ord, Show, Read) instance Named (x `WithName` n) n where getName (_ `WithName` n) = n