module Unison.Codebase.Editor.HandleInput.Rename (handleRename) where
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.MonadUtils qualified as Cli
import Unison.Codebase.Editor.HandleInput.MoveBranch (moveBranchFunc)
import Unison.Codebase.Editor.HandleInput.MoveTerm (moveTermSteps)
import Unison.Codebase.Editor.HandleInput.MoveType (moveTypeSteps)
import Unison.Codebase.Editor.Output qualified as Output
import Unison.Codebase.Path qualified as Path
import Unison.HashQualifiedPrime qualified as HQ'
import Unison.NameSegment (NameSegment)
import Unison.Prelude
handleRename :: Path.Path' -> NameSegment -> Text -> Cli ()
handleRename :: Path' -> NameSegment -> Text -> Cli ()
handleRename Path'
src' NameSegment
newNameSeg Text
description = do
case Path' -> Maybe (Split Path')
forall path. Pathy path => path -> Maybe (Split path)
Path.split Path'
src' of
Maybe (Split Path')
Nothing -> Output -> Cli ()
Cli.respond (Path' -> Output
Output.MoveNothingFound Path'
src')
Just (Path'
parentPath, NameSegment
_oldNameSeg) -> do
let dest' :: Path'
dest' = Split Path' -> Path'
forall path. Pathy path => Split path -> path
Path.unsplit (Path'
parentPath, NameSegment
newNameSeg)
moveBranchFunc <- Bool
-> Path' -> Path' -> Cli (Maybe (Absolute, Branch IO -> Branch IO))
moveBranchFunc Bool
False Path'
src' Path'
dest'
moveTermTypeSteps <- case (,) <$> Path.split src' <*> Path.split dest' of
Maybe (Split Path', Split Path')
Nothing -> [(Absolute, Branch0 IO -> Branch0 IO)]
-> Cli [(Absolute, Branch0 IO -> Branch0 IO)]
forall a. a -> Cli a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
Just (Split Path' -> HashQualified (Split Path')
forall n. n -> HashQualified n
HQ'.NameOnly -> HashQualified (Split Path')
src, Split Path'
dest) -> do
termSteps <- HashQualified (Split Path')
-> Split Path' -> Cli [(Absolute, Branch0 IO -> Branch0 IO)]
forall (m :: * -> *).
HashQualified (Split Path')
-> Split Path' -> Cli [(Absolute, Branch0 m -> Branch0 m)]
moveTermSteps HashQualified (Split Path')
src Split Path'
dest
typeSteps <- moveTypeSteps src dest
pure (termSteps ++ typeSteps)
case (moveBranchFunc, moveTermTypeSteps) of
(Maybe (Absolute, Branch IO -> Branch IO)
Nothing, []) -> Output -> Cli ()
Cli.respond (Path' -> Output
Output.MoveNothingFound Path'
src')
(Maybe (Absolute, Branch IO -> Branch IO)
mupdates, [(Absolute, Branch0 IO -> Branch0 IO)]
steps) -> do
pp <- Cli ProjectPath
Cli.getCurrentProjectPath
Cli.updateAndStepAt description (pp ^. #branch) (maybeToList mupdates) steps
Cli.respond (Output.RenameResult src' dest')