module Proteome.Tags.Nav where

import Path (Abs, File, Path)
import Prelude hiding (tag)
import Ribosome (Buffer, Rpc)
import Ribosome.Api (bufferForFile, edit, nvimBufIsLoaded, nvimCommand, nvimGetCurrentWin, nvimWinSetBuf, setCursor)
import qualified Ribosome.Data.FileBuffer as FileBuffer

filterUnloaded ::
  Member Rpc r =>
  Buffer ->
  Sem r (Maybe Buffer)
filterUnloaded :: forall (r :: EffectRow).
Member Rpc r =>
Buffer -> Sem r (Maybe Buffer)
filterUnloaded Buffer
buffer =
  Buffer -> Sem r Bool
forall (m :: * -> *). MonadRpc m => Buffer -> m Bool
nvimBufIsLoaded Buffer
buffer Sem r Bool -> (Bool -> Maybe Buffer) -> Sem r (Maybe Buffer)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
    Bool
True -> Buffer -> Maybe Buffer
forall a. a -> Maybe a
Just Buffer
buffer
    Bool
False -> Maybe Buffer
forall a. Maybe a
Nothing

loadOrEdit ::
  Member Rpc r =>
  Path Abs File ->
  Int ->
  Sem r ()
loadOrEdit :: forall (r :: EffectRow).
Member Rpc r =>
Path Abs File -> Int -> Sem r ()
loadOrEdit Path Abs File
file Int
line = do
  Maybe Buffer
existingBuffer <- Maybe (Maybe Buffer) -> Maybe Buffer
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe Buffer) -> Maybe Buffer)
-> Sem r (Maybe (Maybe Buffer)) -> Sem r (Maybe Buffer)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((FileBuffer -> Sem r (Maybe Buffer))
-> Maybe FileBuffer -> Sem r (Maybe (Maybe Buffer))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (Buffer -> Sem r (Maybe Buffer)
forall (r :: EffectRow).
Member Rpc r =>
Buffer -> Sem r (Maybe Buffer)
filterUnloaded (Buffer -> Sem r (Maybe Buffer))
-> (FileBuffer -> Buffer) -> FileBuffer -> Sem r (Maybe Buffer)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FileBuffer -> Buffer
FileBuffer.buffer) (Maybe FileBuffer -> Sem r (Maybe (Maybe Buffer)))
-> Sem r (Maybe FileBuffer) -> Sem r (Maybe (Maybe Buffer))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Path Abs File -> Sem r (Maybe FileBuffer)
forall (m :: * -> *).
MonadRpc m =>
Path Abs File -> m (Maybe FileBuffer)
bufferForFile Path Abs File
file)
  Window
window <- Sem r Window
forall (m :: * -> *). MonadRpc m => m Window
nvimGetCurrentWin
  Sem r () -> (Buffer -> Sem r ()) -> Maybe Buffer -> Sem r ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Path Abs File -> Sem r ()
forall (r :: EffectRow) b t. Member Rpc r => Path b t -> Sem r ()
edit Path Abs File
file) (Window -> Buffer -> Sem r ()
forall (m :: * -> *). MonadRpc m => Window -> Buffer -> m ()
nvimWinSetBuf Window
window) Maybe Buffer
existingBuffer
  Window -> Int -> Int -> Sem r ()
forall (r :: EffectRow).
Member Rpc r =>
Window -> Int -> Int -> Sem r ()
setCursor Window
window Int
line Int
0
  Text -> Sem r ()
forall (m :: * -> *). MonadRpc m => Text -> m ()
nvimCommand Text
"normal! zv"
  Text -> Sem r ()
forall (m :: * -> *). MonadRpc m => Text -> m ()
nvimCommand Text
"normal! zz"