-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): names command can search for multiple names
- Loading branch information
Showing
15 changed files
with
273 additions
and
122 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
70 changes: 70 additions & 0 deletions
70
unison-cli/src/Unison/Codebase/Editor/HandleInput/Names.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,70 @@ | ||
module Unison.Codebase.Editor.HandleInput.Names (handleNames) where | ||
|
||
import Control.Monad (when) | ||
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.Codebase qualified as Codebase | ||
import Unison.Codebase.Branch qualified as Branch | ||
import Unison.Codebase.Branch.Names qualified as Branch | ||
import Unison.Codebase.Editor.HandleInput.Global qualified as Global | ||
import Unison.Codebase.Editor.Input (ErrorMessageOrName, RawQuery) | ||
import Unison.Codebase.Editor.Output (Output (..)) | ||
import Unison.HashQualifiedPrime qualified as HQ' | ||
import Unison.Name (Name) | ||
import Unison.NamesWithHistory qualified as Names | ||
import Unison.PrettyPrintEnv qualified as PPE | ||
import Unison.PrettyPrintEnv.Names qualified as PPE | ||
import Unison.PrettyPrintEnvDecl qualified as PPED | ||
import Unison.PrettyPrintEnvDecl.Names qualified as PPED | ||
import Unison.Reference (Reference) | ||
import Unison.Referent (Referent) | ||
import Unison.Util.Pretty qualified as P | ||
|
||
-- | Handles a single @NamesI@ input query returning terms that match a given name. | ||
-- | ||
-- Parameters: | ||
-- | ||
-- * @global :: Bool@ | ||
-- ** If @True@, search all projects and branches. | ||
-- ** If @False@, search only the current branch. | ||
-- | ||
-- * @query :: (RawQuery, ErrorMessageOrName)@ | ||
-- ** The first member is the raw @nameQuery@ being handled. | ||
-- ** The second member is the parsed @nameQuery@ that is either an error message | ||
-- to be printed or a name that can be looked up in the codebase. | ||
handleNames :: | ||
Bool -> | ||
(RawQuery, ErrorMessageOrName) -> | ||
Cli () | ||
handleNames _ (nameQuery, Left errMsg) = do | ||
Cli.respond $ | ||
PrintMessage $ | ||
P.indentAfterNewline " " (P.sepNonEmpty "\n" [P.red $ P.bold $ P.string (nameQuery <> ":"), errMsg]) | ||
handleNames global (nameQuery, Right query) = do | ||
hqLength <- Cli.runTransaction Codebase.hashLength | ||
let searchNames names = do | ||
let pped = PPED.makePPED (PPE.hqNamer 10 names) (PPE.suffixifyByHash names) | ||
unsuffixifiedPPE = PPED.unsuffixifiedPPE pped | ||
terms = Names.lookupHQTerm Names.IncludeSuffixes query names | ||
types = Names.lookupHQType Names.IncludeSuffixes query names | ||
terms' :: [(Referent, [HQ'.HashQualified Name])] | ||
terms' = map (\r -> (r, PPE.allTermNames unsuffixifiedPPE r)) (Set.toList terms) | ||
types' :: [(Reference, [HQ'.HashQualified Name])] | ||
types' = map (\r -> (r, PPE.allTypeNames unsuffixifiedPPE r)) (Set.toList types) | ||
pure (terms', types') | ||
let prettyNameQuery = | ||
PrintMessage $ | ||
P.green (P.bold $ P.string nameQuery) <> ":\n" | ||
if global | ||
then do | ||
Global.forAllProjectBranches \(projBranchNames, _ids) branch -> do | ||
let names = Branch.toNames . Branch.head $ branch | ||
(terms, types) <- searchNames names | ||
when (not (null terms) || not (null types)) do | ||
Cli.respond $ BatchedOutput [prettyNameQuery, IndentedOutput 2 $ GlobalListNames projBranchNames hqLength types terms] | ||
else do | ||
names <- Cli.currentNames | ||
(terms, types) <- searchNames names | ||
Cli.respond $ BatchedOutput [prettyNameQuery, IndentedOutput 2 $ ListNames hqLength types terms] |
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
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
Oops, something went wrong.