44module Share.Postgres.NameLookups.Queries
55 ( termNamesForRefsWithinNamespaceOf ,
66 typeNamesForRefsWithinNamespaceOf ,
7+ NameSearchScope (.. ),
78 ShouldSuffixify (.. ),
89 termRefsForExactNamesOf ,
910 typeRefsForExactNamesOf ,
@@ -52,13 +53,19 @@ import Unison.Util.Monoid qualified as Monoid
5253
5354data ShouldSuffixify = Suffixify | NoSuffixify
5455
56+ data NameSearchScope
57+ = ProjectDefinitions
58+ | Dependencies
59+ | TransitiveDependencies
60+ deriving (Show , Eq , Ord )
61+
5562-- | Get the list of term names and suffixifications for a given Referent within a given root namespace perspective.
5663-- Considers one level of dependencies, but not transitive dependencies.
5764--
5865-- If NoSuffixify is provided, the suffixified name will be the same as the fqn.
5966termNamesForRefsWithinNamespaceOf ::
60- (PG. QueryM m ) => NamesPerspective m -> Maybe ReversedName -> ShouldSuffixify -> Traversal s t PGReferent [NameWithSuffix ] -> s -> m t
61- termNamesForRefsWithinNamespaceOf np maySuffix shouldSuffixify trav s = do
67+ (PG. QueryM m ) => NamesPerspective m -> Maybe ReversedName -> ShouldSuffixify -> NameSearchScope -> Traversal s t PGReferent [NameWithSuffix ] -> s -> m t
68+ termNamesForRefsWithinNamespaceOf np maySuffix shouldSuffixify nameScope trav s = do
6269 s & asListOf trav \ refs -> do
6370 let refsTable :: [(Int32 , Maybe Text , Maybe ComponentHashId , Maybe Int64 , Maybe Int64 )]
6471 refsTable =
@@ -74,22 +81,28 @@ termNamesForRefsWithinNamespaceOf np maySuffix shouldSuffixify trav s = do
7481 -- Need COALESCE because array_agg will return NULL rather than the empty array
7582 -- if there are no results.
7683 SELECT COALESCE(array_agg((names.reversed_name, names.suffixified_name) ORDER BY length(names.reversed_name) ASC), '{}')
77- FROM term_names_for_ref_within_namespace (
84+ FROM term_names_for_ref (
7885 #{bhId},
7986 #{namespacePrefix},
8087 #{reversedNamePrefix},
8188 #{shouldSuffixifyArg},
8289 refs.referent_builtin,
8390 refs.referent_component_hash_id,
8491 refs.referent_component_index,
85- refs.referent_constructor_index
92+ refs.referent_constructor_index,
93+ #{includeDependencies},
94+ #{includeTransitiveDependencies}
8695 ) AS names(reversed_name, suffixified_name)
8796 ) AS ref_names
8897 FROM refs
8998 ORDER BY refs.ord ASC
9099 |]
91100 <&> over (traversed . traversed . field @ " reversedName" ) (qualifyNameToPerspective np)
92101 where
102+ (includeDependencies, includeTransitiveDependencies) = case nameScope of
103+ ProjectDefinitions -> (False , False )
104+ Dependencies -> (True , False )
105+ TransitiveDependencies -> (True , True )
93106 -- Look in the current mount.
94107 bhId = perspectiveCurrentMountBranchHashId np
95108 shouldSuffixifyArg = case shouldSuffixify of
@@ -104,8 +117,8 @@ termNamesForRefsWithinNamespaceOf np maySuffix shouldSuffixify trav s = do
104117-- Considers one level of dependencies, but not transitive dependencies.
105118--
106119-- If NoSuffixify is provided, the suffixified name will be the same as the fqn.
107- typeNamesForRefsWithinNamespaceOf :: (PG. QueryM m ) => NamesPerspective m -> Maybe ReversedName -> ShouldSuffixify -> Traversal s t PGReference [NameWithSuffix ] -> s -> m t
108- typeNamesForRefsWithinNamespaceOf np maySuffix shouldSuffixify trav s = do
120+ typeNamesForRefsWithinNamespaceOf :: (PG. QueryM m ) => NamesPerspective m -> Maybe ReversedName -> ShouldSuffixify -> NameSearchScope -> Traversal s t PGReference [NameWithSuffix ] -> s -> m t
121+ typeNamesForRefsWithinNamespaceOf np maySuffix shouldSuffixify nameScope trav s = do
109122 s & asListOf trav \ refs -> do
110123 let refsTable :: [(Int32 , Maybe Text , Maybe ComponentHashId , Maybe Int64 )]
111124 refsTable =
@@ -121,21 +134,27 @@ typeNamesForRefsWithinNamespaceOf np maySuffix shouldSuffixify trav s = do
121134 -- Need COALESCE because array_agg will return NULL rather than the empty array
122135 -- if there are no results.
123136 SELECT COALESCE(array_agg((names.reversed_name, names.suffixified_name) ORDER BY length(names.reversed_name) ASC), '{}')
124- FROM type_names_for_ref_within_namespace (
137+ FROM type_names_for_ref (
125138 #{bhId},
126139 #{namespacePrefix},
127140 #{reversedNamePrefix},
128141 #{shouldSuffixifyArg},
129142 refs.reference_builtin,
130143 refs.reference_component_hash_id,
131- refs.reference_component_index
144+ refs.reference_component_index,
145+ #{includeDependencies},
146+ #{includeTransitiveDependencies}
132147 ) AS names(reversed_name, suffixified_name)
133148 ) AS ref_names
134149 FROM refs
135150 ORDER BY refs.ord ASC
136151 |]
137152 <&> over (traversed . traversed . field @ " reversedName" ) (qualifyNameToPerspective np)
138153 where
154+ (includeDependencies, includeTransitiveDependencies) = case nameScope of
155+ ProjectDefinitions -> (False , False )
156+ Dependencies -> (True , False )
157+ TransitiveDependencies -> (True , True )
139158 bhId = perspectiveCurrentMountBranchHashId np
140159 shouldSuffixifyArg = case shouldSuffixify of
141160 Suffixify -> True
0 commit comments