forked from unisonweb/unison
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request unisonweb#5466 from unisonweb/24-10-16-edit-depend…
…ents feat: add `edit.dependents`
- Loading branch information
Showing
19 changed files
with
816 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-- | Utilities related to resolving names to things. | ||
module Unison.Cli.NameResolutionUtils | ||
( resolveHQName, | ||
resolveHQToLabeledDependencies, | ||
) | ||
where | ||
|
||
import Control.Monad.Reader (ask) | ||
import Data.Bifoldable (bifoldMap) | ||
import Data.Set qualified as Set | ||
import Unison.Cli.Monad (Cli) | ||
import Unison.Cli.Monad qualified as Cli | ||
import Unison.Cli.NamesUtils qualified as Cli | ||
import Unison.HashQualified qualified as HQ | ||
import Unison.LabeledDependency (LabeledDependency) | ||
import Unison.LabeledDependency qualified as LD | ||
import Unison.Name (Name) | ||
import Unison.Name qualified as Name | ||
import Unison.Names qualified as Names | ||
import Unison.Prelude | ||
import Unison.Reference (TypeReference) | ||
import Unison.Referent (Referent) | ||
import Unison.Server.NameSearch.Sqlite qualified as Sqlite | ||
import Unison.ShortHash (ShortHash) | ||
import Unison.Util.Defns (Defns (..), DefnsF) | ||
|
||
resolveHQName :: HQ.HashQualified Name -> Cli (DefnsF Set Referent TypeReference) | ||
resolveHQName = \case | ||
HQ.NameOnly name -> do | ||
names <- Cli.currentNames | ||
pure | ||
Defns | ||
{ terms = Name.searchByRankedSuffix name names.terms, | ||
types = Name.searchByRankedSuffix name names.types | ||
} | ||
-- rationale: the hash should be unique enough that the name never helps | ||
-- mitchell says: that seems wrong | ||
HQ.HashQualified _n hash -> resolveHashOnly hash | ||
HQ.HashOnly hash -> resolveHashOnly hash | ||
where | ||
resolveHashOnly :: ShortHash -> Cli (DefnsF Set Referent TypeReference) | ||
resolveHashOnly hash = do | ||
env <- ask | ||
Cli.runTransaction do | ||
terms <- Sqlite.termReferentsByShortHash env.codebase hash | ||
types <- Sqlite.typeReferencesByShortHash hash | ||
pure Defns {terms, types} | ||
|
||
resolveHQToLabeledDependencies :: HQ.HashQualified Name -> Cli (Set LabeledDependency) | ||
resolveHQToLabeledDependencies = | ||
fmap (bifoldMap (Set.map LD.referent) (Set.map LD.typeRef)) . resolveHQName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
unison-cli/src/Unison/Codebase/Editor/HandleInput/Dependents.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Unison.Codebase.Editor.HandleInput.Dependents | ||
( handleDependents, | ||
) | ||
where | ||
|
||
import Data.Set qualified as Set | ||
import U.Codebase.Sqlite.Queries qualified as Queries | ||
import Unison.Cli.Monad (Cli) | ||
import Unison.Cli.Monad qualified as Cli | ||
import Unison.Cli.NameResolutionUtils (resolveHQToLabeledDependencies) | ||
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.ConstructorReference (GConstructorReference (..)) | ||
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.NameSegment qualified as NameSegment | ||
import Unison.Prelude | ||
import Unison.PrettyPrintEnv qualified as PPE | ||
import Unison.PrettyPrintEnv.Names qualified as PPE | ||
import Unison.PrettyPrintEnvDecl qualified as PPE hiding (biasTo, empty) | ||
import Unison.PrettyPrintEnvDecl.Names qualified as PPED | ||
import Unison.Reference (Reference) | ||
import Unison.Referent qualified as Referent | ||
import Unison.Syntax.HashQualified qualified as HQ (toText) | ||
import Unison.Util.List (nubOrdOn) | ||
|
||
handleDependents :: HQ.HashQualified Name -> Cli () | ||
handleDependents hq = do | ||
-- todo: add flag to handle transitive efficiently | ||
lds <- resolveHQToLabeledDependencies hq | ||
-- Use an unsuffixified PPE here, so we display full names (relative to the current path), | ||
-- rather than the shortest possible unambiguous name. | ||
names <- Cli.currentNames | ||
let pped = PPED.makePPED (PPE.hqNamer 10 names) (PPE.suffixifyByHash names) | ||
let fqppe = PPE.unsuffixifiedPPE pped | ||
let ppe = PPE.suffixifiedPPE pped | ||
when (null lds) do | ||
Cli.returnEarly (LabeledReferenceNotFound hq) | ||
|
||
results <- for (toList lds) \ld -> do | ||
-- The full set of dependent references, any number of which may not have names in the current namespace. | ||
dependents <- | ||
let tp = Codebase.dependents Queries.ExcludeOwnComponent | ||
tm = \case | ||
Referent.Ref r -> Codebase.dependents Queries.ExcludeOwnComponent r | ||
Referent.Con (ConstructorReference r _cid) _ct -> | ||
Codebase.dependents Queries.ExcludeOwnComponent r | ||
in Cli.runTransaction (LD.fold tp tm ld) | ||
let -- True is term names, False is type names | ||
results :: [(Bool, HQ.HashQualified Name, Reference)] | ||
results = do | ||
r <- Set.toList dependents | ||
Just (isTerm, hq) <- [(True,) <$> PPE.terms fqppe (Referent.Ref r), (False,) <$> PPE.types fqppe r] | ||
fullName <- [HQ'.toName hq] | ||
guard (not (Name.beginsWithSegment fullName NameSegment.libSegment)) | ||
Just shortName <- pure $ PPE.terms ppe (Referent.Ref r) <|> PPE.types ppe r | ||
pure (isTerm, HQ'.toHQ shortName, r) | ||
pure results | ||
let sort = fmap fst . nubOrdOn snd . Name.sortByText (HQ.toText . fst) | ||
let types = sort [(n, r) | (False, n, r) <- join results] | ||
let terms = sort [(n, r) | (True, n, r) <- join results] | ||
Cli.setNumberedArgs . map SA.HashQualified $ types <> terms | ||
Cli.respond (ListDependents ppe lds types terms) |
Oops, something went wrong.