module Unison.Codebase.Editor.HandleInput.Dependencies
  ( handleDependencies,
  )
where

import Control.Arrow ((***))
import Data.Bifoldable (bifoldMap, binull)
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.Cli.NameResolutionUtils (resolveHQName)
import Unison.Codebase qualified as Codebase
import Unison.Codebase.Branch.Names qualified as Branch
import Unison.Codebase.Editor.Output
import Unison.Codebase.Editor.StructuredArgument qualified as SA
import Unison.HashQualified qualified as HQ
import Unison.HashQualifiedPrime qualified as HQ'
import Unison.LabeledDependency qualified as LD
import Unison.Name (Name)
import Unison.Name qualified as Name
import Unison.Prelude
import Unison.PrettyPrintEnv qualified as PPE
import Unison.PrettyPrintEnv.Names qualified as PPE
import Unison.Reference (Reference)
import Unison.Referent qualified as Referent
import Unison.Syntax.HashQualifiedPrime qualified as HQ'
import Unison.Util.Defns (Defns (..), DefnsF)

handleDependencies :: HQ.HashQualified Name -> Cli ()
handleDependencies :: HashQualified Name -> Cli ()
handleDependencies HashQualified Name
hq = do
  DefnsF Set Referent TypeReference
refs <- HashQualified Name -> Cli (DefnsF Set Referent TypeReference)
resolveHQName HashQualified Name
hq

  Bool -> Cli () -> Cli ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (DefnsF Set Referent TypeReference -> Bool
forall (t :: * -> * -> *) a b. Bifoldable t => t a b -> Bool
binull DefnsF Set Referent TypeReference
refs) do
    Output -> Cli ()
forall a. Output -> Cli a
Cli.returnEarly (HashQualified Name -> Output
LabeledReferenceNotFound HashQualified Name
hq)

  Branch0 IO
namespace <- Cli (Branch0 IO)
Cli.getCurrentProjectRoot0
  let ppe :: PrettyPrintEnv
ppe =
        let names :: Names
names = Branch0 IO -> Names
forall (m :: * -> *). Branch0 m -> Names
Branch.toNames Branch0 IO
namespace
         in Namer -> Suffixifier -> PrettyPrintEnv
PPE.makePPE (Int -> Names -> Namer
PPE.hqNamer Int
10 Names
names) (Names -> Suffixifier
PPE.suffixifyByHash Names
names)

  DefnsF Set TypeReference TypeReference
dependencies <- do
    Transaction (DefnsF Set TypeReference TypeReference)
-> Cli (DefnsF Set TypeReference TypeReference)
forall a. Transaction a -> Cli a
Cli.runTransaction (Transaction (DefnsF Set TypeReference TypeReference)
 -> Cli (DefnsF Set TypeReference TypeReference))
-> Transaction (DefnsF Set TypeReference TypeReference)
-> Cli (DefnsF Set TypeReference TypeReference)
forall a b. (a -> b) -> a -> b
$ DefnsF Set Referent TypeReference
-> Transaction (DefnsF Set TypeReference TypeReference)
Codebase.directDependencies DefnsF Set Referent TypeReference
refs

  let dependencyNames ::
        DefnsF
          []
          (HQ.HashQualified Name, HQ.HashQualified Name)
          (HQ.HashQualified Name, HQ.HashQualified Name)
      dependencyNames :: DefnsF
  []
  (HashQualified Name, HashQualified Name)
  (HashQualified Name, HashQualified Name)
dependencyNames =
        (Set TypeReference -> [(HashQualified Name, HashQualified Name)])
-> (Set TypeReference
    -> [(HashQualified Name, HashQualified Name)])
-> DefnsF Set TypeReference TypeReference
-> DefnsF
     []
     (HashQualified Name, HashQualified Name)
     (HashQualified Name, HashQualified Name)
forall a b c d. (a -> b) -> (c -> d) -> Defns a c -> Defns b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap
          ((TypeReference -> [(HashQualified Name, HashQualified Name)])
-> Set TypeReference -> [(HashQualified Name, HashQualified Name)]
f (TypeReference -> Referent
Referent.fromTermReference (TypeReference -> Referent)
-> (Referent -> [(HashQualified Name, HashQualified Name)])
-> TypeReference
-> [(HashQualified Name, HashQualified Name)]
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> PrettyPrintEnv
-> Referent -> [(HashQualified Name, HashQualified Name)]
PPE.termNames PrettyPrintEnv
ppe))
          ((TypeReference -> [(HashQualified Name, HashQualified Name)])
-> Set TypeReference -> [(HashQualified Name, HashQualified Name)]
f (PrettyPrintEnv
-> TypeReference -> [(HashQualified Name, HashQualified Name)]
PPE.typeNames PrettyPrintEnv
ppe))
          DefnsF Set TypeReference TypeReference
dependencies
        where
          f ::
            (Reference -> [(HQ'.HashQualified Name, HQ'.HashQualified Name)]) ->
            Set Reference ->
            [(HQ.HashQualified Name, HQ.HashQualified Name)]
          f :: (TypeReference -> [(HashQualified Name, HashQualified Name)])
-> Set TypeReference -> [(HashQualified Name, HashQualified Name)]
f TypeReference -> [(HashQualified Name, HashQualified Name)]
g =
            Set TypeReference -> [TypeReference]
forall a. Set a -> [a]
Set.toList
              -- Pick the best name for a reference (with `listToMaybe`), else use the ref (if nameless)
              (Set TypeReference -> [TypeReference])
-> ([TypeReference] -> [(HashQualified Name, HashQualified Name)])
-> Set TypeReference
-> [(HashQualified Name, HashQualified Name)]
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (TypeReference
 -> Either TypeReference (HashQualified Name, HashQualified Name))
-> [TypeReference]
-> [Either TypeReference (HashQualified Name, HashQualified Name)]
forall a b. (a -> b) -> [a] -> [b]
map (\TypeReference
x -> Either TypeReference (HashQualified Name, HashQualified Name)
-> ((HashQualified Name, HashQualified Name)
    -> Either TypeReference (HashQualified Name, HashQualified Name))
-> Maybe (HashQualified Name, HashQualified Name)
-> Either TypeReference (HashQualified Name, HashQualified Name)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (TypeReference
-> Either TypeReference (HashQualified Name, HashQualified Name)
forall a b. a -> Either a b
Left TypeReference
x) (HashQualified Name, HashQualified Name)
-> Either TypeReference (HashQualified Name, HashQualified Name)
forall a b. b -> Either a b
Right ([(HashQualified Name, HashQualified Name)]
-> Maybe (HashQualified Name, HashQualified Name)
forall a. [a] -> Maybe a
listToMaybe (TypeReference -> [(HashQualified Name, HashQualified Name)]
g TypeReference
x)))
              ([TypeReference]
 -> [Either TypeReference (HashQualified Name, HashQualified Name)])
-> ([Either TypeReference (HashQualified Name, HashQualified Name)]
    -> [(HashQualified Name, HashQualified Name)])
-> [TypeReference]
-> [(HashQualified Name, HashQualified Name)]
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> [Either TypeReference (HashQualified Name, HashQualified Name)]
-> ([TypeReference], [(HashQualified Name, HashQualified Name)])
forall a b. [Either a b] -> ([a], [b])
partitionEithers
              -- Sort the named references alphabetically, then stick the hash-only ones at the end
              ([Either TypeReference (HashQualified Name, HashQualified Name)]
 -> ([TypeReference], [(HashQualified Name, HashQualified Name)]))
-> (([TypeReference], [(HashQualified Name, HashQualified Name)])
    -> [(HashQualified Name, HashQualified Name)])
-> [Either TypeReference (HashQualified Name, HashQualified Name)]
-> [(HashQualified Name, HashQualified Name)]
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ([TypeReference], [(HashQualified Name, HashQualified Name)])
-> [(HashQualified Name, HashQualified Name)]
h

          h ::
            ([Reference], [(HQ'.HashQualified Name, HQ'.HashQualified Name)]) ->
            [(HQ.HashQualified Name, HQ.HashQualified Name)]
          h :: ([TypeReference], [(HashQualified Name, HashQualified Name)])
-> [(HashQualified Name, HashQualified Name)]
h ([TypeReference]
nameless, [(HashQualified Name, HashQualified Name)]
named) =
            [[(HashQualified Name, HashQualified Name)]]
-> [(HashQualified Name, HashQualified Name)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
              [ [(HashQualified Name, HashQualified Name)]
named
                  [(HashQualified Name, HashQualified Name)]
-> ([(HashQualified Name, HashQualified Name)]
    -> [(HashQualified Name, HashQualified Name)])
-> [(HashQualified Name, HashQualified Name)]
forall a b. a -> (a -> b) -> b
& ((HashQualified Name, HashQualified Name) -> Text)
-> [(HashQualified Name, HashQualified Name)]
-> [(HashQualified Name, HashQualified Name)]
forall a. (a -> Text) -> [a] -> [a]
Name.sortByText ((HashQualified Name, HashQualified Name) -> HashQualified Name
forall a b. (a, b) -> a
fst ((HashQualified Name, HashQualified Name) -> HashQualified Name)
-> (HashQualified Name -> Text)
-> (HashQualified Name, HashQualified Name)
-> Text
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> HashQualified Name -> Text
HQ'.toText)
                  [(HashQualified Name, HashQualified Name)]
-> ([(HashQualified Name, HashQualified Name)]
    -> [(HashQualified Name, HashQualified Name)])
-> [(HashQualified Name, HashQualified Name)]
forall a b. a -> (a -> b) -> b
& ((HashQualified Name, HashQualified Name)
 -> (HashQualified Name, HashQualified Name))
-> [(HashQualified Name, HashQualified Name)]
-> [(HashQualified Name, HashQualified Name)]
forall a b. (a -> b) -> [a] -> [b]
map (HashQualified Name -> HashQualified Name
forall n. HashQualified n -> HashQualified n
HQ'.toHQ (HashQualified Name -> HashQualified Name)
-> (HashQualified Name -> HashQualified Name)
-> (HashQualified Name, HashQualified Name)
-> (HashQualified Name, HashQualified Name)
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** HashQualified Name -> HashQualified Name
forall n. HashQualified n -> HashQualified n
HQ'.toHQ),
                [TypeReference]
nameless
                  [TypeReference]
-> ([TypeReference] -> [(HashQualified Name, HashQualified Name)])
-> [(HashQualified Name, HashQualified Name)]
forall a b. a -> (a -> b) -> b
& (TypeReference -> (HashQualified Name, HashQualified Name))
-> [TypeReference] -> [(HashQualified Name, HashQualified Name)]
forall a b. (a -> b) -> [a] -> [b]
map (\TypeReference
x -> let y :: HashQualified Name
y = TypeReference -> HashQualified Name
HQ.fromReference TypeReference
x in (HashQualified Name
y, HashQualified Name
y))
              ]

  -- Set numbered args
  (DefnsF
  []
  (HashQualified Name, HashQualified Name)
  (HashQualified Name, HashQualified Name)
dependencyNames.types [(HashQualified Name, HashQualified Name)]
-> [(HashQualified Name, HashQualified Name)]
-> [(HashQualified Name, HashQualified Name)]
forall a. [a] -> [a] -> [a]
++ DefnsF
  []
  (HashQualified Name, HashQualified Name)
  (HashQualified Name, HashQualified Name)
dependencyNames.terms)
    [(HashQualified Name, HashQualified Name)]
-> ([(HashQualified Name, HashQualified Name)]
    -> [StructuredArgument])
-> [StructuredArgument]
forall a b. a -> (a -> b) -> b
& ((HashQualified Name, HashQualified Name) -> StructuredArgument)
-> [(HashQualified Name, HashQualified Name)]
-> [StructuredArgument]
forall a b. (a -> b) -> [a] -> [b]
map (HashQualified Name -> StructuredArgument
SA.HashQualified (HashQualified Name -> StructuredArgument)
-> ((HashQualified Name, HashQualified Name) -> HashQualified Name)
-> (HashQualified Name, HashQualified Name)
-> StructuredArgument
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HashQualified Name, HashQualified Name) -> HashQualified Name
forall a b. (a, b) -> a
fst)
    [StructuredArgument] -> ([StructuredArgument] -> Cli ()) -> Cli ()
forall a b. a -> (a -> b) -> b
& [StructuredArgument] -> Cli ()
Cli.setNumberedArgs

  let lds :: Set LabeledDependency
lds = (Set Referent -> Set LabeledDependency)
-> (Set TypeReference -> Set LabeledDependency)
-> DefnsF Set Referent TypeReference
-> Set LabeledDependency
forall m a b. Monoid m => (a -> m) -> (b -> m) -> Defns a b -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap ((Referent -> LabeledDependency)
-> Set Referent -> Set LabeledDependency
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map Referent -> LabeledDependency
LD.referent) ((TypeReference -> LabeledDependency)
-> Set TypeReference -> Set LabeledDependency
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map TypeReference -> LabeledDependency
LD.typeRef) DefnsF Set Referent TypeReference
refs
  Output -> Cli ()
Cli.respond (PrettyPrintEnv
-> Set LabeledDependency
-> DefnsF
     []
     (HashQualified Name, HashQualified Name)
     (HashQualified Name, HashQualified Name)
-> Output
ListDependencies PrettyPrintEnv
ppe Set LabeledDependency
lds DefnsF
  []
  (HashQualified Name, HashQualified Name)
  (HashQualified Name, HashQualified Name)
dependencyNames)