    {-# LANGUAGE OverloadedStrings #-}
    {-# OPTIONS_HADDOCK show-extensions #-}

    -- |
    -- Module      :  Yi.Mode.Compilation
    -- License     :  GPL-2
    -- Maintainer  :  yi-devel@googlegroups.com
    -- Stability   :  experimental
    -- Portability :  portable
    --
    -- A 'Mode' for working with buffers showing the results of compilations.
    module Yi.Mode.Compilation where

    import           Lens.Micro.Platform           ((%~), (&), (.~))
    import           Data.Text            ()
    import           Yi.Buffer
    import           Yi.Core              (withSyntax)
    import           Yi.Editor            (shiftOtherWindow, withCurrentBuffer)
    import           Yi.File              (openingNewFile)
    import           Yi.Keymap            (Action (YiA), topKeymapA)
    import           Yi.Keymap.Keys       (Key (KEnter), spec, (<||), (?>>!))
    import           Yi.Lexer.Alex        (Posn (..), Tok (..))
    import qualified Yi.Lexer.Compilation as Compilation (Token (Report), lexer)
    import           Yi.Mode.Common       (TokenBasedMode, styleMode)
    import qualified Yi.Syntax.OnlineTree as OnlineTree (tokAtOrBefore)

    mode :: TokenBasedMode Compilation.Token
    mode :: TokenBasedMode Token
    mode = StyleLexer AlexState HlState Token AlexInput
    -> TokenBasedMode Token
    forall (l :: * -> *) s t i.
    Show (l s) =>
    StyleLexer l s t i -> TokenBasedMode t
    styleMode StyleLexer AlexState HlState Token AlexInput
    Compilation.lexer
      TokenBasedMode Token
    -> (TokenBasedMode Token -> TokenBasedMode Token)
    -> TokenBasedMode Token
    forall a b. a -> (a -> b) -> b
    & ((FilePath -> YiString -> Bool)
     -> Identity (FilePath -> YiString -> Bool))
    -> TokenBasedMode Token -> Identity (TokenBasedMode Token)
    forall syntax (f :: * -> *).
    Functor f =>
    ((FilePath -> YiString -> Bool)
     -> f (FilePath -> YiString -> Bool))
    -> Mode syntax -> f (Mode syntax)
    modeAppliesA (((FilePath -> YiString -> Bool)
      -> Identity (FilePath -> YiString -> Bool))
     -> TokenBasedMode Token -> Identity (TokenBasedMode Token))
    -> (FilePath -> YiString -> Bool)
    -> TokenBasedMode Token
    -> TokenBasedMode Token
    forall s t a b. ASetter s t a b -> b -> s -> t
    .~ FilePath -> YiString -> Bool
    forall a b. a -> b -> Bool
    modeNeverApplies
      TokenBasedMode Token
    -> (TokenBasedMode Token -> TokenBasedMode Token)
    -> TokenBasedMode Token
    forall a b. a -> (a -> b) -> b
    & (Text -> Identity Text)
    -> TokenBasedMode Token -> Identity (TokenBasedMode Token)
    forall syntax (f :: * -> *).
    Functor f =>
    (Text -> f Text) -> Mode syntax -> f (Mode syntax)
    modeNameA ((Text -> Identity Text)
     -> TokenBasedMode Token -> Identity (TokenBasedMode Token))
    -> Text -> TokenBasedMode Token -> TokenBasedMode Token
    forall s t a b. ASetter s t a b -> b -> s -> t
    .~ Text
    "compilation"
      TokenBasedMode Token
    -> (TokenBasedMode Token -> TokenBasedMode Token)
    -> TokenBasedMode Token
    forall a b. a -> (a -> b) -> b
    & ((KeymapSet -> KeymapSet) -> Identity (KeymapSet -> KeymapSet))
    -> TokenBasedMode Token -> Identity (TokenBasedMode Token)
    forall syntax (f :: * -> *).
    Functor f =>
    ((KeymapSet -> KeymapSet) -> f (KeymapSet -> KeymapSet))
    -> Mode syntax -> f (Mode syntax)
    modeKeymapA (((KeymapSet -> KeymapSet) -> Identity (KeymapSet -> KeymapSet))
     -> TokenBasedMode Token -> Identity (TokenBasedMode Token))
    -> (KeymapSet -> KeymapSet)
    -> TokenBasedMode Token
    -> TokenBasedMode Token
    forall s t a b. ASetter s t a b -> b -> s -> t
    .~ (Keymap -> Identity Keymap) -> KeymapSet -> Identity KeymapSet
    Lens' KeymapSet Keymap
    topKeymapA ((Keymap -> Identity Keymap) -> KeymapSet -> Identity KeymapSet)
    -> (Keymap -> Keymap) -> KeymapSet -> KeymapSet
    forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
    %~ ((Key -> Event
    spec Key
    KEnter Event -> YiM HlState -> Keymap
    forall (m :: * -> *) a x.
    (MonadInteract m Action Event, YiAction a x, Show x) =>
    Event -> a -> m HlState
    ?>>! (forall syntax. Mode syntax -> syntax -> Action) -> YiM HlState
    forall x a.
    (Show x, YiAction a x) =>
    (forall syntax. Mode syntax -> syntax -> a) -> YiM HlState
    withSyntax Mode syntax -> syntax -> Action
    forall syntax. Mode syntax -> syntax -> Action
    modeFollow) Keymap -> Keymap -> Keymap
    forall (f :: * -> *) w e a.
    MonadInteract f w e =>
    f a -> f a -> f a
    <||)
      TokenBasedMode Token
    -> (TokenBasedMode Token -> TokenBasedMode Token)
    -> TokenBasedMode Token
    forall a b. a -> (a -> b) -> b
    & ((Tree (Tok Token) -> Action)
     -> Identity (Tree (Tok Token) -> Action))
    -> TokenBasedMode Token -> Identity (TokenBasedMode Token)
    forall syntax (f :: * -> *).
    Functor f =>
    ((syntax -> Action) -> f (syntax -> Action))
    -> Mode syntax -> f (Mode syntax)
    modeFollowA (((Tree (Tok Token) -> Action)
      -> Identity (Tree (Tok Token) -> Action))
     -> TokenBasedMode Token -> Identity (TokenBasedMode Token))
    -> (Tree (Tok Token) -> Action)
    -> TokenBasedMode Token
    -> TokenBasedMode Token
    forall s t a b. ASetter s t a b -> b -> s -> t
    .~ YiM HlState -> Action
    forall a. Show a => YiM a -> Action
    YiA (YiM HlState -> Action)
    -> (Tree (Tok Token) -> YiM HlState) -> Tree (Tok Token) -> Action
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Tree (Tok Token) -> YiM HlState
    forall {t :: * -> *}. Foldable t => t (Tok Token) -> YiM HlState
    follow
      where
        follow :: t (Tok Token) -> YiM HlState
    follow t (Tok Token)
    errs = BufferM Point -> YiM Point
    forall (m :: * -> *) a. MonadEditor m => BufferM a -> m a
    withCurrentBuffer BufferM Point
    pointB YiM Point -> (Point -> YiM HlState) -> YiM HlState
    forall a b. YiM a -> (a -> YiM b) -> YiM b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= \Point
    point ->
          case Point -> t (Tok Token) -> Maybe (Tok Token)
    forall (t :: * -> *) t1.
    Foldable t =>
    Point -> t (Tok t1) -> Maybe (Tok t1)
    OnlineTree.tokAtOrBefore Point
    point t (Tok Token)
    errs of
            Just t :: Tok Token
    t@Tok {tokT :: forall t. Tok t -> t
    tokT = Compilation.Report FilePath
    filename Int
    line Int
    col FilePath
    _} -> do
              BufferM HlState -> YiM HlState
    forall (m :: * -> *) a. MonadEditor m => BufferM a -> m a
    withCurrentBuffer (BufferM HlState -> YiM HlState)
    -> (Posn -> BufferM HlState) -> Posn -> YiM HlState
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Point -> BufferM HlState
    moveTo (Point -> BufferM HlState)
    -> (Posn -> Point) -> Posn -> BufferM HlState
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Posn -> Point
    posnOfs (Posn -> YiM HlState) -> Posn -> YiM HlState
    forall a b. (a -> b) -> a -> b
    $ Tok Token -> Posn
    forall t. Tok t -> Posn
    tokPosn Tok Token
    t
              YiM HlState
    forall (m :: * -> *). MonadEditor m => m HlState
    shiftOtherWindow
              FilePath -> BufferM HlState -> YiM HlState
    forall a. FilePath -> BufferM a -> YiM HlState
    openingNewFile FilePath
    filename (BufferM HlState -> YiM HlState) -> BufferM HlState -> YiM HlState
    forall a b. (a -> b) -> a -> b
    $ Int -> BufferM Int
    gotoLn Int
    line BufferM Int -> BufferM HlState -> BufferM HlState
    forall a b. BufferM a -> BufferM b -> BufferM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Int -> BufferM HlState
    rightN Int
    col
            Maybe (Tok Token)
    _ -> HlState -> YiM HlState
    forall a. a -> YiM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()