module Unison.Codebase.Editor.HandleInput.MoveType (doMoveType, moveTypeSteps) where
import Data.Set qualified as Set
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.MonadUtils qualified as Cli
import Unison.Codebase qualified as Codebase
import Unison.Codebase.Branch (Branch0)
import Unison.Codebase.BranchUtil qualified as BranchUtil
import Unison.Codebase.Editor.Output qualified as Output
import Unison.Codebase.Path (Path')
import Unison.Codebase.Path qualified as Path
import Unison.Codebase.ProjectPath qualified as PP
import Unison.HashQualifiedPrime qualified as HQ'
import Unison.Prelude
moveTypeSteps ::
HQ'.HashQualified (Path.Split Path') -> Path.Split Path' -> Cli [(Path.Absolute, Branch0 m -> Branch0 m)]
moveTypeSteps :: forall (m :: * -> *).
HashQualified (Split Path')
-> Split Path' -> Cli [(Absolute, Branch0 m -> Branch0 m)]
moveTypeSteps HashQualified (Split Path')
src' Split Path'
dest' = do
HashQualified (Split (ProjectPathG Project ProjectBranch))
src <- (Split Path' -> Cli (Split (ProjectPathG Project ProjectBranch)))
-> HashQualified (Split Path')
-> Cli (HashQualified (Split (ProjectPathG Project ProjectBranch)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HashQualified a -> f (HashQualified b)
traverse Split Path' -> Cli (Split (ProjectPathG Project ProjectBranch))
Cli.resolveSplit' HashQualified (Split Path')
src'
Set TypeReference
srcTypes <- HashQualified (Split (ProjectPathG Project ProjectBranch))
-> Cli (Set TypeReference)
Cli.getTypesAt HashQualified (Split (ProjectPathG Project ProjectBranch))
src
case Set TypeReference -> [TypeReference]
forall a. Set a -> [a]
Set.toList Set TypeReference
srcTypes of
[] -> [(Absolute, Branch0 m -> Branch0 m)]
-> Cli [(Absolute, Branch0 m -> Branch0 m)]
forall a. a -> Cli a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
TypeReference
_ : TypeReference
_ : [TypeReference]
_ -> do
Int
hqLength <- Transaction Int -> Cli Int
forall a. Transaction a -> Cli a
Cli.runTransaction Transaction Int
Codebase.hashLength
Output -> Cli [(Absolute, Branch0 m -> Branch0 m)]
forall a. Output -> Cli a
Cli.returnEarly (Int
-> HashQualified (Split Path')
-> Set Referent
-> Set TypeReference
-> Output
Output.DeleteNameAmbiguous Int
hqLength HashQualified (Split Path')
src' Set Referent
forall a. Set a
Set.empty Set TypeReference
srcTypes)
[TypeReference
srcType] -> do
Split (ProjectPathG Project ProjectBranch)
dest <- Split Path' -> Cli (Split (ProjectPathG Project ProjectBranch))
Cli.resolveSplit' Split Path'
dest'
Set TypeReference
destTypes <- HashQualified (Split (ProjectPathG Project ProjectBranch))
-> Cli (Set TypeReference)
Cli.getTypesAt (HashQualified (Split (ProjectPathG Project ProjectBranch))
-> Cli (Set TypeReference))
-> HashQualified (Split (ProjectPathG Project ProjectBranch))
-> Cli (Set TypeReference)
forall a b. (a -> b) -> a -> b
$ Split (ProjectPathG Project ProjectBranch)
-> HashQualified (Split (ProjectPathG Project ProjectBranch))
forall n. n -> HashQualified n
HQ'.NameOnly Split (ProjectPathG Project ProjectBranch)
dest
Bool -> Cli () -> Cli ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Set TypeReference -> Bool
forall a. Set a -> Bool
Set.null Set TypeReference
destTypes)) do
Output -> Cli ()
forall a. Output -> Cli a
Cli.returnEarly (Split Path' -> Set TypeReference -> Output
Output.TypeAlreadyExists Split Path'
dest' Set TypeReference
destTypes)
pure
[
Split Absolute
-> TypeReference -> (Absolute, Branch0 m -> Branch0 m)
forall p (m :: * -> *).
Split p -> TypeReference -> (p, Branch0 m -> Branch0 m)
BranchUtil.makeDeleteTypeName ((ProjectPathG Project ProjectBranch -> Absolute)
-> Split (ProjectPathG Project ProjectBranch) -> Split Absolute
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Getting Absolute (ProjectPathG Project ProjectBranch) Absolute
-> ProjectPathG Project ProjectBranch -> Absolute
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Absolute (ProjectPathG Project ProjectBranch) Absolute
forall p b (f :: * -> *).
Functor f =>
(Absolute -> f Absolute)
-> ProjectPathG p b -> f (ProjectPathG p b)
PP.absPath_) (Split (ProjectPathG Project ProjectBranch) -> Split Absolute)
-> Split (ProjectPathG Project ProjectBranch) -> Split Absolute
forall a b. (a -> b) -> a -> b
$ HashQualified (Split (ProjectPathG Project ProjectBranch))
-> Split (ProjectPathG Project ProjectBranch)
forall n. HashQualified n -> n
HQ'.toName HashQualified (Split (ProjectPathG Project ProjectBranch))
src) TypeReference
srcType,
Split Absolute
-> TypeReference -> (Absolute, Branch0 m -> Branch0 m)
forall p (m :: * -> *).
Split p -> TypeReference -> (p, Branch0 m -> Branch0 m)
BranchUtil.makeAddTypeName ((ProjectPathG Project ProjectBranch -> Absolute)
-> Split (ProjectPathG Project ProjectBranch) -> Split Absolute
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Getting Absolute (ProjectPathG Project ProjectBranch) Absolute
-> ProjectPathG Project ProjectBranch -> Absolute
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Absolute (ProjectPathG Project ProjectBranch) Absolute
forall p b (f :: * -> *).
Functor f =>
(Absolute -> f Absolute)
-> ProjectPathG p b -> f (ProjectPathG p b)
PP.absPath_) Split (ProjectPathG Project ProjectBranch)
dest) TypeReference
srcType
]
doMoveType :: HQ'.HashQualified (Path.Split Path') -> Path.Split Path' -> Text -> Cli ()
doMoveType :: HashQualified (Split Path') -> Split Path' -> Text -> Cli ()
doMoveType HashQualified (Split Path')
src' Split Path'
dest' Text
description = do
[(Absolute, Branch0 IO -> Branch0 IO)]
steps <- HashQualified (Split Path')
-> Split Path' -> Cli [(Absolute, Branch0 IO -> Branch0 IO)]
forall (m :: * -> *).
HashQualified (Split Path')
-> Split Path' -> Cli [(Absolute, Branch0 m -> Branch0 m)]
moveTypeSteps HashQualified (Split Path')
src' Split Path'
dest'
Bool -> Cli () -> Cli ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([(Absolute, Branch0 IO -> Branch0 IO)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Absolute, Branch0 IO -> Branch0 IO)]
steps) do
Output -> Cli ()
forall a. Output -> Cli a
Cli.returnEarly (HashQualified (Split Path') -> Output
Output.TypeNotFound HashQualified (Split Path')
src')
ProjectBranch
pb <- Cli ProjectBranch
Cli.getCurrentProjectBranch
ProjectBranch
-> Text -> [(Absolute, Branch0 IO -> Branch0 IO)] -> Cli ()
Cli.stepManyAt ProjectBranch
pb Text
description [(Absolute, Branch0 IO -> Branch0 IO)]
steps
Output -> Cli ()
Cli.respond Output
Output.Success