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

    -- |
    -- Module      :  Yi.Keymap.Vim.Ex.Commands.Sort
    -- License     :  GPL-2
    -- Maintainer  :  yi-devel@googlegroups.com
    -- Stability   :  experimental
    -- Portability :  portable

    module Yi.Keymap.Vim.Ex.Commands.Sort (parse) where

    import           Control.Monad                    (void)
    import qualified Data.Attoparsec.Text             as P (match, string)
    import           Data.Monoid                      ((<>))
    import qualified Data.Text                        as T (Text)
    import           Yi.Buffer
    import           Yi.Keymap                        (Action (BufferA))
    import           Yi.Keymap.Vim.Common             (EventString)
    import qualified Yi.Keymap.Vim.Ex.Commands.Common as Common (parse, parseRange, pureExCommand)
    import           Yi.Keymap.Vim.Ex.Types           (ExCommand (cmdAction, cmdComplete, cmdShow))

    parse :: EventString -> Maybe ExCommand
    parse :: EventString -> Maybe ExCommand
    parse = Parser ExCommand -> EventString -> Maybe ExCommand
    Common.parse (Parser ExCommand -> EventString -> Maybe ExCommand)
    -> Parser ExCommand -> EventString -> Maybe ExCommand
    forall a b. (a -> b) -> a -> b
    $ do
        (regionText, region) <- Parser (Maybe (BufferM Region))
    -> Parser (Text, Maybe (BufferM Region))
    forall a. Parser a -> Parser (Text, a)
    P.match Parser (Maybe (BufferM Region))
    Common.parseRange
        void $ P.string "sort"
        return $ sort region regionText

    sort :: Maybe (BufferM Region) -> T.Text -> ExCommand
    sort :: Maybe (BufferM Region) -> Text -> ExCommand
    sort Maybe (BufferM Region)
    r Text
    rt = ExCommand
    Common.pureExCommand {
        cmdShow = rt <> "sort"
      , cmdAction = BufferA $ sortA r
      , cmdComplete = return [rt <> "sort"]
      }

    sortA :: Maybe (BufferM Region) -> BufferM ()
    sortA :: Maybe (BufferM Region) -> BufferM ()
    sortA Maybe (BufferM Region)
    r = do
        region <- case Maybe (BufferM Region)
    r of
            Maybe (BufferM Region)
    Nothing -> TextUnit -> BufferM Region
    regionOfB TextUnit
    Document
            Just BufferM Region
    r' -> BufferM Region
    r'
        sortLinesWithRegion region{regionEnd = regionEnd region - 1}