Skip to content

Commit

Permalink
Merge pull request #4485 from unisonweb/cp/exact-term-search
Browse files Browse the repository at this point in the history
Unison Local - Search for docs by exact name, not suffix
  • Loading branch information
aryairani authored Dec 12, 2023
2 parents 2f72b34 + e9e7532 commit 063bade
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 297 deletions.
2 changes: 1 addition & 1 deletion parser-typechecker/src/Unison/Codebase/MainTerm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ getMainTerm loadTypeOfTerm parseNames mainName mainType =
case HQ.fromString mainName of
Nothing -> pure (NotAFunctionName mainName)
Just hq -> do
let refs = NamesWithHistory.lookupHQTerm hq (NamesWithHistory.NamesWithHistory parseNames mempty)
let refs = NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes hq (NamesWithHistory.NamesWithHistory parseNames mempty)
let a = Parser.Ann.External
case toList refs of
[] -> pure (NotFound mainName)
Expand Down
12 changes: 6 additions & 6 deletions parser-typechecker/src/Unison/Syntax/TermParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ typeLink' :: (Monad m, Var v) => P v m (L.Token Reference)
typeLink' = do
id <- hqPrefixId
ns <- asks names
case NamesWithHistory.lookupHQType (L.payload id) ns of
case NamesWithHistory.lookupHQType NamesWithHistory.IncludeSuffixes (L.payload id) ns of
s
| Set.size s == 1 -> pure $ const (Set.findMin s) <$> id
| otherwise -> customFailure $ UnknownType id s
Expand All @@ -120,7 +120,7 @@ termLink' :: (Monad m, Var v) => P v m (L.Token Referent)
termLink' = do
id <- hqPrefixId
ns <- asks names
case NamesWithHistory.lookupHQTerm (L.payload id) ns of
case NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes (L.payload id) ns of
s
| Set.size s == 1 -> pure $ const (Set.findMin s) <$> id
| otherwise -> customFailure $ UnknownTerm id s
Expand All @@ -129,7 +129,7 @@ link' :: (Monad m, Var v) => P v m (Either (L.Token Reference) (L.Token Referent
link' = do
id <- hqPrefixId
ns <- asks names
case (NamesWithHistory.lookupHQTerm (L.payload id) ns, NamesWithHistory.lookupHQType (L.payload id) ns) of
case (NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes (L.payload id) ns, NamesWithHistory.lookupHQType NamesWithHistory.IncludeSuffixes (L.payload id) ns) of
(s, s2) | Set.size s == 1 && Set.null s2 -> pure . Right $ const (Set.findMin s) <$> id
(s, s2) | Set.size s2 == 1 && Set.null s -> pure . Left $ const (Set.findMin s2) <$> id
(s, s2) -> customFailure $ UnknownId id s s2
Expand Down Expand Up @@ -279,7 +279,7 @@ parsePattern = label "pattern" root
names <- asks names
-- probably should avoid looking up in `names` if `L.payload tok`
-- starts with a lowercase
case NamesWithHistory.lookupHQPattern (L.payload tok) ct names of
case NamesWithHistory.lookupHQPattern NamesWithHistory.IncludeSuffixes (L.payload tok) ct names of
s
| Set.null s -> die tok s
| Set.size s > 1 -> die tok s
Expand Down Expand Up @@ -420,7 +420,7 @@ resolveHashQualified tok = do
names <- asks names
case L.payload tok of
HQ.NameOnly n -> pure $ Term.var (ann tok) (Name.toVar n)
_ -> case NamesWithHistory.lookupHQTerm (L.payload tok) names of
_ -> case NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes (L.payload tok) names of
s
| Set.null s -> failCommitted $ UnknownTerm tok s
| Set.size s > 1 -> failCommitted $ UnknownTerm tok s
Expand Down Expand Up @@ -1151,7 +1151,7 @@ substImports ns imports =
-- not in Names, but in a later term binding
[ (suffix, Type.var () full)
| (suffix, full) <- imports,
NamesWithHistory.hasTypeNamed (Name.unsafeFromVar full) ns
NamesWithHistory.hasTypeNamed NamesWithHistory.IncludeSuffixes (Name.unsafeFromVar full) ns
]

block' :: (Monad m, Var v) => IsTop -> String -> P v m (L.Token ()) -> P v m (L.Token ()) -> TermP v m
Expand Down
2 changes: 1 addition & 1 deletion parser-typechecker/src/Unison/Syntax/TypeParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typeAtom =
HQ.NameOnly n -> pure $ Type.var (ann tok) (Name.toVar n)
hq -> do
names <- asks names
let matches = Names.lookupHQType hq names
let matches = Names.lookupHQType Names.IncludeSuffixes hq names
if Set.size matches /= 1
then P.customFailure (UnknownType tok matches)
else pure $ Type.ref (ann tok) (Set.findMin matches)
Expand Down
30 changes: 15 additions & 15 deletions unison-cli/src/Unison/Codebase/Editor/HandleInput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ loop e = do
doRemoveReplacement from patchPath isTerm = do
let patchPath' = fromMaybe Cli.defaultPatchPath patchPath
patch <- Cli.getPatchAt patchPath'
QueryResult misses allHits <- hqNameQuery [from]
QueryResult misses allHits <- hqNameQuery NamesWithHistory.IncludeSuffixes [from]
let tpRefs = Set.fromList $ typeReferences allHits
tmRefs = Set.fromList $ termReferences allHits
(hits, opHits) =
Expand Down Expand Up @@ -560,7 +560,7 @@ loop e = do
let nameSearch = NameSearch.makeNameSearch hqLength (NamesWithHistory.fromCurrentNames basicPrettyPrintNames)
Cli.Env {codebase, runtime} <- ask
mdText <- liftIO $ do
docRefs <- Backend.docsForDefinitionName codebase nameSearch docName
docRefs <- Backend.docsForDefinitionName codebase nameSearch NamesWithHistory.IncludeSuffixes docName
for docRefs $ \docRef -> do
Identity (_, _, doc, _evalErrs) <- Backend.renderDocRefs pped (Pretty.Width 80) codebase runtime (Identity docRef)
pure . Md.toText $ Md.toMarkdown doc
Expand Down Expand Up @@ -715,8 +715,8 @@ loop e = do
pure (currentNames, pped)

let unsuffixifiedPPE = PPED.unsuffixifiedPPE pped
terms = NamesWithHistory.lookupHQTerm query names
types = NamesWithHistory.lookupHQType query names
terms = NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes query names
types = NamesWithHistory.lookupHQType NamesWithHistory.IncludeSuffixes query names
terms' :: [(Referent, [HQ'.HashQualified Name])]
terms' = map (\r -> (r, PPE.allTermNames unsuffixifiedPPE r)) (Set.toList terms)
types' :: [(Reference, [HQ'.HashQualified Name])]
Expand Down Expand Up @@ -937,8 +937,8 @@ loop e = do

let patchPath' = fromMaybe Cli.defaultPatchPath patchPath
patch <- Cli.getPatchAt patchPath'
QueryResult fromMisses' fromHits <- hqNameQuery [from]
QueryResult toMisses' toHits <- hqNameQuery [to]
QueryResult fromMisses' fromHits <- hqNameQuery NamesWithHistory.IncludeSuffixes [from]
QueryResult toMisses' toHits <- hqNameQuery NamesWithHistory.IncludeSuffixes [to]
let termsFromRefs = termReferences fromHits
termsToRefs = termReferences toHits
typesFromRefs = typeReferences fromHits
Expand Down Expand Up @@ -1664,7 +1664,7 @@ lookupRewrite onErr prepare rule = do
ot <- case ot of
Just _ -> pure ot
Nothing -> do
case NamesWithHistory.lookupHQTerm rule currentNames of
case NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes rule currentNames of
s
| Set.size s == 1,
Referent.Ref (Reference.DerivedId r) <- Set.findMin s ->
Expand Down Expand Up @@ -1961,7 +1961,7 @@ handleIOTest main = do

-- Then, if we get here (because nothing in the scratch file matched), look at the terms in the codebase.
Cli.runTransaction do
forMaybe (Set.toList (NamesWithHistory.lookupHQTerm main parseNames)) \ref0 ->
forMaybe (Set.toList (NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes main parseNames)) \ref0 ->
runMaybeT do
ref <- MaybeT (pure (Referent.toTermReferenceId ref0))
typ <- MaybeT (loadTypeOfTerm codebase (Referent.fromTermReferenceId ref))
Expand Down Expand Up @@ -2025,7 +2025,7 @@ handleShowDefinition outputLoc showDefinitionScope inputQuery = do
pure (currentNames, ppe)
Backend.DefinitionResults terms types misses <- do
let nameSearch = NameSearch.makeNameSearch hqLength names
Cli.runTransaction (Backend.definitionsBySuffixes codebase nameSearch includeCycles query)
Cli.runTransaction (Backend.definitionsByName codebase nameSearch includeCycles NamesWithHistory.IncludeSuffixes query)
outputPath <- getOutputPath
when (not (null types && null terms)) do
-- We need an 'isTest' check in the output layer, so it can prepend "test>" to tests in a scratch file. Since we
Expand Down Expand Up @@ -2764,7 +2764,7 @@ displayI prettyPrintNames outputLoc hq = do
case addWatch (HQ.toString hq) latestTypecheckedFile of
Nothing -> do
let parseNames = (`NamesWithHistory.NamesWithHistory` mempty) prettyPrintNames
results = NamesWithHistory.lookupHQTerm hq parseNames
results = NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes hq parseNames
pped <- prettyPrintEnvDecl parseNames
ref <-
Set.asSingleton results & onNothing do
Expand Down Expand Up @@ -2805,7 +2805,7 @@ docsI srcLoc prettyPrintNames src =
fileByName = do
ns <- maybe mempty UF.typecheckedToNames <$> Cli.getLatestTypecheckedFile
fnames <- pure $ NamesWithHistory.NamesWithHistory ns mempty
case NamesWithHistory.lookupHQTerm dotDoc fnames of
case NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes dotDoc fnames of
s | Set.size s == 1 -> do
-- the displayI command expects full term names, so we resolve
-- the hash back to its full name in the file
Expand All @@ -2831,7 +2831,7 @@ docsI srcLoc prettyPrintNames src =
codebaseByName :: Cli ()
codebaseByName = do
parseNames <- basicParseNames
case NamesWithHistory.lookupHQTerm dotDoc (NamesWithHistory.NamesWithHistory parseNames mempty) of
case NamesWithHistory.lookupHQTerm NamesWithHistory.IncludeSuffixes dotDoc (NamesWithHistory.NamesWithHistory parseNames mempty) of
s
| Set.size s == 1 -> displayI prettyPrintNames ConsoleLocation dotDoc
| Set.size s == 0 -> Cli.respond $ ListOfLinks PPE.empty []
Expand Down Expand Up @@ -3058,16 +3058,16 @@ loadTypeOfTerm _ Referent.Con {} =
error $
reportBug "924628772" "Attempt to load a type declaration which is a builtin!"

hqNameQuery :: [HQ.HashQualified Name] -> Cli QueryResult
hqNameQuery query = do
hqNameQuery :: NamesWithHistory.SearchType -> [HQ.HashQualified Name] -> Cli QueryResult
hqNameQuery searchType query = do
Cli.Env {codebase} <- ask
root' <- Cli.getRootBranch
currentPath <- Cli.getCurrentPath
Cli.runTransaction do
hqLength <- Codebase.hashLength
let parseNames = Backend.parseNamesForBranch root' (Backend.AllNames (Path.unabsolute currentPath))
let nameSearch = NameSearch.makeNameSearch hqLength (NamesWithHistory.fromCurrentNames parseNames)
Backend.hqNameQuery codebase nameSearch query
Backend.hqNameQuery codebase nameSearch searchType query

-- | Select a definition from the given branch.
-- Returned names will match the provided 'Position' type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Unison.Name (Name)
import Unison.Names (Names)
import Unison.NamesWithHistory
( NamesWithHistory (..),
SearchType (..),
lookupHQTerm,
)
import Unison.Parser.Ann (Ann)
Expand All @@ -41,7 +42,7 @@ addHistory :: Names -> NamesWithHistory
addHistory names = NamesWithHistory names mempty

lookupTerm :: HQ.HashQualified Name -> Names -> [Referent]
lookupTerm hq parseNames = toList (lookupHQTerm hq hnames)
lookupTerm hq parseNames = toList (lookupHQTerm IncludeSuffixes hq hnames)
where
hnames = addHistory parseNames

Expand Down
3 changes: 2 additions & 1 deletion unison-cli/src/Unison/LSP/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Unison.LabeledDependency
import Unison.LabeledDependency qualified as LD
import Unison.Lexer.Pos (Pos (..))
import Unison.Name (Name)
import Unison.NamesWithHistory (SearchType (..))
import Unison.Parser.Ann (Ann)
import Unison.Parser.Ann qualified as Ann
import Unison.Pattern qualified as Pattern
Expand Down Expand Up @@ -386,7 +387,7 @@ markdownDocsForFQN fileUri fqn =
nameSearch <- lift $ getNameSearch
Env {codebase, runtime} <- ask
liftIO $ do
docRefs <- Backend.docsForDefinitionName codebase nameSearch name
docRefs <- Backend.docsForDefinitionName codebase nameSearch ExactName name
for docRefs $ \docRef -> do
Identity (_, _, doc, _evalErrs) <- Backend.renderDocRefs pped (Pretty.Width 80) codebase runtime (Identity docRef)
pure . Md.toText $ Md.toMarkdown doc
Loading

0 comments on commit 063bade

Please sign in to comment.