module Unison.Codebase.Editor.HandleInput.Ls
  ( handleLs,
  )
where

import Control.Monad.Reader (ask)
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.MonadUtils qualified as Cli
import Unison.Cli.NamesUtils qualified as Cli
import Unison.Codebase qualified as Codebase
import Unison.Codebase.Editor.Output
import Unison.Codebase.Editor.StructuredArgument qualified as SA
import Unison.Codebase.Path (Path')
import Unison.Codebase.ProjectPath (ProjectPathG (..))
import Unison.Prelude
import Unison.PrettyPrintEnv.Names qualified as PPE
import Unison.PrettyPrintEnvDecl qualified as PPED
import Unison.PrettyPrintEnvDecl.Names qualified as PPED
import Unison.Server.Backend qualified as Backend

handleLs :: Path' -> Cli ()
handleLs :: Path' -> Cli ()
handleLs Path'
pathArg = do
  Cli.Env {Codebase IO Symbol Ann
codebase :: Codebase IO Symbol Ann
$sel:codebase:Env :: Env -> Codebase IO Symbol Ann
codebase} <- Cli Env
forall r (m :: * -> *). MonadReader r m => m r
ask
  ProjectPath
pp <- Path' -> Cli ProjectPath
Cli.resolvePath' Path'
pathArg
  Branch Transaction
projectRootBranch <- Transaction (Branch Transaction) -> Cli (Branch Transaction)
forall a. Transaction a -> Cli a
Cli.runTransaction (Transaction (Branch Transaction) -> Cli (Branch Transaction))
-> Transaction (Branch Transaction) -> Cli (Branch Transaction)
forall a b. (a -> b) -> a -> b
$ ProjectBranch -> Transaction (Branch Transaction)
Codebase.expectShallowProjectBranchRoot ProjectPath
pp.branch
  [ShallowListEntry Symbol Ann]
entries <- IO [ShallowListEntry Symbol Ann]
-> Cli [ShallowListEntry Symbol Ann]
forall a. IO a -> Cli a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Codebase IO Symbol Ann
-> Branch Transaction
-> Absolute
-> IO [ShallowListEntry Symbol Ann]
forall (m :: * -> *).
MonadIO m =>
Codebase m Symbol Ann
-> Branch Transaction
-> Absolute
-> m [ShallowListEntry Symbol Ann]
Backend.lsAtPath Codebase IO Symbol Ann
codebase Branch Transaction
projectRootBranch (ProjectPath
pp.absPath))
  NumberedArgs -> Cli ()
Cli.setNumberedArgs (NumberedArgs -> Cli ()) -> NumberedArgs -> Cli ()
forall a b. (a -> b) -> a -> b
$ (ShallowListEntry Symbol Ann -> StructuredArgument)
-> [ShallowListEntry Symbol Ann] -> NumberedArgs
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Path' -> ShallowListEntry Symbol Ann -> StructuredArgument
SA.ShallowListEntry Path'
pathArg) [ShallowListEntry Symbol Ann]
entries
  Names
names <- Cli Names
Cli.currentNames
  let pped :: PrettyPrintEnvDecl
pped = Namer -> Suffixifier -> PrettyPrintEnvDecl
PPED.makePPED (Int -> Names -> Namer
PPE.hqNamer Int
10 Names
names) (Names -> Suffixifier
PPE.suffixifyByHash Names
names)
  let suffixifiedPPE :: PrettyPrintEnv
suffixifiedPPE = PrettyPrintEnvDecl -> PrettyPrintEnv
PPED.suffixifiedPPE PrettyPrintEnvDecl
pped
  -- This used to be a delayed action which only forced the loading of the root
  -- branch when it was necessary for printing the results, but that got wiped out
  -- when we ported to the new Cli monad.
  -- It would be nice to restore it, but it's pretty rare that it actually results
  -- in an improvement, so perhaps it's not worth the effort.
  let buildPPE :: IO PrettyPrintEnv
buildPPE = PrettyPrintEnv -> IO PrettyPrintEnv
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PrettyPrintEnv
suffixifiedPPE
  Output -> Cli ()
Cli.respond (Output -> Cli ()) -> Output -> Cli ()
forall a b. (a -> b) -> a -> b
$ IO PrettyPrintEnv -> [ShallowListEntry Symbol Ann] -> Output
ListShallow IO PrettyPrintEnv
buildPPE [ShallowListEntry Symbol Ann]
entries