Avoid returning absolute names unless there are no other options

This commit is contained in:
Paul Chiusano 2021-08-03 16:51:04 -04:00
parent 0527874d97
commit 64c57bb189
2 changed files with 24 additions and 2 deletions

View File

@ -408,7 +408,7 @@ searchBranchExact len names queries =
searchTypes :: HQ.HashQualified Name -> [SR.SearchResult]
searchTypes query =
-- a bunch of references will match a HQ ref.
let refs = toList $ Names3.lookupHQType query names
let refs = toList $ Names3.lookupRelativeHQType query names
mayName r Nothing = HQ'.fromNamedReference "" r
mayName _ (Just n) = n
in refs <&> \r ->
@ -426,7 +426,7 @@ searchBranchExact len names queries =
searchTerms :: HQ.HashQualified Name -> [SR.SearchResult]
searchTerms query =
-- a bunch of references will match a HQ ref.
let refs = toList $ Names3.lookupHQTerm query names
let refs = toList $ Names3.lookupRelativeHQTerm query names
mayName r Nothing = HQ'.fromNamedReferent "" r
mayName _ (Just n) = n
in refs <&> \r ->

View File

@ -121,6 +121,17 @@ shadowing prio (Names current old) =
makeAbsolute0:: Names0 -> Names0
makeAbsolute0 = map0 Name.makeAbsolute
-- Find all types whose name has a suffix matching the provided `HashQualified`,
-- returning types with relative names if they exist, and otherwise
-- returning types with absolute names.
lookupRelativeHQType :: HashQualified Name -> Names -> Set Reference
lookupRelativeHQType hq ns@Names{..} = let
rs = lookupHQType hq ns
keep r = any (not . Name.isAbsolute) (R.lookupRan r (Names.types currentNames))
in case Set.filter keep rs of
rs' | Set.null rs' -> rs
| otherwise -> rs'
-- Find all types whose name has a suffix matching the provided `HashQualified`.
lookupHQType :: HashQualified Name -> Names -> Set Reference
lookupHQType hq Names{..} = case hq of
@ -143,6 +154,17 @@ hasTermNamed n ns = not (Set.null $ lookupHQTerm (HQ.NameOnly n) ns)
hasTypeNamed :: Name -> Names -> Bool
hasTypeNamed n ns = not (Set.null $ lookupHQType (HQ.NameOnly n) ns)
-- Find all terms whose name has a suffix matching the provided `HashQualified`,
-- returning terms with relative names if they exist, and otherwise
-- returning terms with absolute names.
lookupRelativeHQTerm :: HashQualified Name -> Names -> Set Referent
lookupRelativeHQTerm hq ns@Names{..} = let
rs = lookupHQTerm hq ns
keep r = any (not . Name.isAbsolute) (R.lookupRan r (Names.terms currentNames))
in case Set.filter keep rs of
rs' | Set.null rs' -> rs
| otherwise -> rs'
-- Find all terms whose name has a suffix matching the provided `HashQualified`.
lookupHQTerm :: HashQualified Name -> Names -> Set Referent
lookupHQTerm hq Names{..} = case hq of