Skip to content

Commit daaef83

Browse files
committed
PR feedback
1 parent 147f7cc commit daaef83

File tree

6 files changed

+140
-71
lines changed

6 files changed

+140
-71
lines changed

parser-typechecker/src/Unison/Codebase/Path.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Unison.Codebase.Path
2424
prefix,
2525
unprefix,
2626
prefixName,
27+
prefixName2,
2728
unprefixName,
2829
HQSplit,
2930
Split,
@@ -192,6 +193,11 @@ prefix (Absolute (Path prefix)) = \case
192193
AbsolutePath' abs -> unabsolute abs
193194
RelativePath' rel -> Path $ prefix <> toSeq (unrelative rel)
194195

196+
prefix2 :: Path -> Path' -> Path
197+
prefix2 (Path prefix) = \case
198+
AbsolutePath' abs -> unabsolute abs
199+
RelativePath' rel -> Path $ prefix <> toSeq (unrelative rel)
200+
195201
-- | Finds the longest shared path prefix of two paths.
196202
-- Returns (shared prefix, path to first location from shared prefix, path to second location from shared prefix)
197203
--
@@ -273,6 +279,9 @@ unprefixName prefix = toName . unprefix prefix . fromName'
273279
prefixName :: Absolute -> Name -> Name
274280
prefixName p n = fromMaybe n . toName . prefix p . fromName' $ n
275281

282+
prefixName2 :: Path -> Name -> Name
283+
prefixName2 p n = fromMaybe n . toName . prefix2 p . fromName' $ n
284+
276285
singleton :: NameSegment -> Path
277286
singleton n = fromList [n]
278287

unison-cli/src/Unison/Codebase/Editor/HandleInput.hs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,30 +1429,28 @@ handleFindI ::
14291429
Cli ()
14301430
handleFindI isVerbose fscope ws input = do
14311431
Cli.Env {codebase} <- ask
1432-
currentBranch0 <- Cli.getCurrentBranch0
1433-
(pped, names) <- case fscope of
1434-
FindLocal -> do
1435-
let names = Branch.toNames (Branch.withoutLib currentBranch0)
1432+
(pped, names, searchRoot, branch0) <- case fscope of
1433+
FindLocal p -> do
1434+
searchRoot <- Cli.resolvePath p
1435+
branch0 <- Cli.getBranch0At searchRoot
1436+
let names = Branch.toNames (Branch.withoutLib branch0)
14361437
-- Don't exclude anything from the pretty printer, since the type signatures we print for
14371438
-- results may contain things in lib.
14381439
pped <- Cli.currentPrettyPrintEnvDecl
1439-
pure (pped, names)
1440-
FindLocalSubnamespace p -> do
1441-
path <- Cli.resolvePath p
1442-
branch0 <- Cli.getBranch0At path
1443-
let names = Branch.toNames branch0
1444-
pped <- Cli.currentPrettyPrintEnvDecl
1445-
pure (pped, names)
1446-
FindLocalAndDeps -> do
1447-
let names = Branch.toNames (Branch.withoutTransitiveLibs currentBranch0)
1440+
pure (pped, names, Just p, branch0)
1441+
FindLocalAndDeps p -> do
1442+
searchRoot <- Cli.resolvePath p
1443+
branch0 <- Cli.getBranch0At searchRoot
1444+
let names = Branch.toNames (Branch.withoutTransitiveLibs branch0)
14481445
-- Don't exclude anything from the pretty printer, since the type signatures we print for
14491446
-- results may contain things in lib.
14501447
pped <- Cli.currentPrettyPrintEnvDecl
1451-
pure (pped, names)
1448+
pure (pped, names, Just p, branch0)
14521449
FindGlobal -> do
14531450
globalNames <- Names.makeAbsolute . Branch.toNames <$> Cli.getRootBranch0
14541451
pped <- Cli.prettyPrintEnvDeclFromNames globalNames
1455-
pure (pped, globalNames)
1452+
currentBranch0 <- Cli.getCurrentBranch0
1453+
pure (pped, globalNames, Nothing, currentBranch0)
14561454
let suffixifiedPPE = PPED.suffixifiedPPE pped
14571455
let getResults :: Names -> Cli [SearchResult]
14581456
getResults names =
@@ -1461,7 +1459,7 @@ handleFindI isVerbose fscope ws input = do
14611459
-- type query
14621460
":" : ws -> do
14631461
typ <- parseSearchType (show input) (unwords ws)
1464-
let keepNamed = Set.intersection (Branch.deepReferents currentBranch0)
1462+
let keepNamed = Set.intersection (Branch.deepReferents branch0)
14651463
(noExactTypeMatches, matches) <- do
14661464
Cli.runTransaction do
14671465
matches <- keepNamed <$> Codebase.termsOfType codebase typ
@@ -1487,16 +1485,16 @@ handleFindI isVerbose fscope ws input = do
14871485
(mapMaybe (HQ.parseTextWith anythingBeforeHash . Text.pack) qs)
14881486
pure $ uniqueBy SR.toReferent srs
14891487
let respondResults results = do
1490-
Cli.setNumberedArgs $ fmap searchResultToHQString results
1488+
Cli.setNumberedArgs $ fmap (searchResultToHQString searchRoot) results
14911489
results' <- Cli.runTransaction (Backend.loadSearchResults codebase results)
14921490
Cli.respond $ ListOfDefinitions fscope suffixifiedPPE isVerbose results'
14931491
results <- getResults names
14941492
case (results, fscope) of
1495-
([], FindLocal) -> do
1493+
([], FindLocal {}) -> do
14961494
Cli.respond FindNoLocalMatches
14971495
-- We've already searched everything else, so now we search JUST the
14981496
-- names in lib.
1499-
let mayOnlyLibBranch = currentBranch0 & Branch.children %%~ \cs -> Map.singleton NameSegment.libSegment <$> Map.lookup NameSegment.libSegment cs
1497+
let mayOnlyLibBranch = branch0 & Branch.children %%~ \cs -> Map.singleton NameSegment.libSegment <$> Map.lookup NameSegment.libSegment cs
15001498
case mayOnlyLibBranch of
15011499
Nothing -> respondResults []
15021500
Just onlyLibBranch -> do
@@ -1810,11 +1808,14 @@ confirmedCommand i = do
18101808
pure $ Just i == (loopState ^. #lastInput)
18111809

18121810
-- | restores the full hash to these search results, for _numberedArgs purposes
1813-
searchResultToHQString :: SearchResult -> String
1814-
searchResultToHQString = \case
1815-
SR.Tm' n r _ -> Text.unpack $ HQ.toText $ HQ.requalify n r
1816-
SR.Tp' n r _ -> Text.unpack $ HQ.toText $ HQ.requalify n (Referent.Ref r)
1811+
searchResultToHQString :: Maybe Path -> SearchResult -> String
1812+
searchResultToHQString oprefix = \case
1813+
SR.Tm' n r _ -> Text.unpack $ HQ.toText $ HQ.requalify (addPrefix <$> n) r
1814+
SR.Tp' n r _ -> Text.unpack $ HQ.toText $ HQ.requalify (addPrefix <$> n) (Referent.Ref r)
18171815
_ -> error "impossible match failure"
1816+
where
1817+
addPrefix :: Name -> Name
1818+
addPrefix = maybe id Path.prefixName2 oprefix
18181819

18191820
-- return `name` and `name.<everything>...`
18201821
_searchBranchPrefix :: Branch m -> Name -> [SearchResult]

unison-cli/src/Unison/Codebase/Editor/Input.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,8 @@ data OutputLocation
313313
deriving (Eq, Show)
314314

315315
data FindScope
316-
= FindLocal
317-
| FindLocalAndDeps
318-
| FindLocalSubnamespace Path
316+
= FindLocal Path
317+
| FindLocalAndDeps Path
319318
| FindGlobal
320319
deriving stock (Eq, Show)
321320

unison-cli/src/Unison/CommandLine/InputPatterns.hs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Unison.Cli.ProjectUtils qualified as ProjectUtils
3030
import Unison.Codebase (Codebase)
3131
import Unison.Codebase qualified as Codebase
3232
import Unison.Codebase.Branch.Merge qualified as Branch
33-
import Unison.Codebase.Editor.Input (DeleteOutput (..), DeleteTarget (..), FindScope (FindLocalSubnamespace), Input)
33+
import Unison.Codebase.Editor.Input (DeleteOutput (..), DeleteTarget (..), Input)
3434
import Unison.Codebase.Editor.Input qualified as Input
3535
import Unison.Codebase.Editor.Output.PushPull (PushPull (Pull, Push))
3636
import Unison.Codebase.Editor.Output.PushPull qualified as PushPull
@@ -531,7 +531,7 @@ sfind :: InputPattern
531531
sfind =
532532
InputPattern "rewrite.find" ["sfind"] I.Visible [("rewrite-rule definition", Required, definitionQueryArg)] msg parse
533533
where
534-
parse [q] = Input.StructuredFindI Input.FindLocal <$> parseHashQualifiedName q
534+
parse [q] = Input.StructuredFindI (Input.FindLocal Path.empty) <$> parseHashQualifiedName q
535535
parse _ = Left "expected exactly one argument"
536536
msg =
537537
P.lines
@@ -589,27 +589,31 @@ sfindReplace =
589589
]
590590

591591
find :: InputPattern
592-
find = find' "find" Input.FindLocal
592+
find = find' "find" (Input.FindLocal Path.empty)
593593

594594
findAll :: InputPattern
595-
findAll = find' "find.all" Input.FindLocalAndDeps
595+
findAll = find' "find.all" (Input.FindLocalAndDeps Path.empty)
596596

597597
findGlobal :: InputPattern
598598
findGlobal = find' "find.global" Input.FindGlobal
599599

600-
findIn :: InputPattern
601-
findIn =
600+
findIn, findInAll :: InputPattern
601+
findIn = findIn' "find-in" Input.FindLocal
602+
findInAll = findIn' "find-in.all" Input.FindLocalAndDeps
603+
604+
findIn' :: String -> (Path.Path -> Input.FindScope) -> InputPattern
605+
findIn' cmd mkfscope =
602606
InputPattern
603-
"find-in"
607+
cmd
604608
[]
605609
I.Visible
606-
[("namespace", Required, namespaceArg)]
610+
[("namespace", Required, namespaceArg), ("query", ZeroPlus, exactDefinitionArg)]
607611
findHelp
608612
\case
609613
p : args -> first P.text do
610614
p <- Path.parsePath p
611-
pure (Input.FindI False (FindLocalSubnamespace p) args)
612-
_ -> Left (I.help findIn)
615+
pure (Input.FindI False (mkfscope p) args)
616+
_ -> Left findHelp
613617

614618
findHelp :: P.Pretty CT.ColorText
615619
findHelp =
@@ -623,7 +627,9 @@ findHelp =
623627
"lists all definitions with a name similar to 'foo' or 'bar' in the "
624628
<> "current namespace (excluding those under 'lib')."
625629
),
626-
("`find-in namespace`", "lists all definitions in the specified subnamespace."),
630+
( "`find-in namespace`",
631+
"lists all definitions in the specified subnamespace."
632+
),
627633
( "`find-in namespace foo bar`",
628634
"lists all definitions with a name similar to 'foo' or 'bar' in the "
629635
<> "specified subnamespace."
@@ -632,6 +638,13 @@ findHelp =
632638
"lists all definitions with a name similar to 'foo' in the current "
633639
<> "namespace (including one level of 'lib')."
634640
),
641+
( "`find-in.all namespace`",
642+
"lists all definitions in the specified subnamespace (including one level of its 'lib')."
643+
),
644+
( "`find-in.all namespace foo bar`",
645+
"lists all definitions with a name similar to 'foo' or 'bar' in the "
646+
<> "specified subnamespace (including one level of its 'lib')."
647+
),
635648
( "find.global foo",
636649
"lists all definitions with a name similar to 'foo' in any namespace"
637650
)
@@ -679,7 +692,7 @@ findVerbose =
679692
( "`find.verbose` searches for definitions like `find`, but includes hashes "
680693
<> "and aliases in the results."
681694
)
682-
(pure . Input.FindI True Input.FindLocal)
695+
(pure . Input.FindI True (Input.FindLocal Path.empty))
683696

684697
findVerboseAll :: InputPattern
685698
findVerboseAll =
@@ -691,7 +704,7 @@ findVerboseAll =
691704
( "`find.all.verbose` searches for definitions like `find.all`, but includes hashes "
692705
<> "and aliases in the results."
693706
)
694-
(pure . Input.FindI True Input.FindLocalAndDeps)
707+
(pure . Input.FindI True (Input.FindLocalAndDeps Path.empty))
695708

696709
findPatch :: InputPattern
697710
findPatch =
@@ -993,7 +1006,7 @@ up =
9931006
InputPattern
9941007
"deprecated.up"
9951008
[]
996-
I.Visible
1009+
I.Hidden
9971010
[]
9981011
(P.wrapColumn2 [(makeExample up [], "move current path up one level (deprecated)")])
9991012
( \case
@@ -3010,6 +3023,7 @@ validInputs =
30103023
find,
30113024
findIn,
30123025
findAll,
3026+
findInAll,
30133027
findGlobal,
30143028
findPatch,
30153029
findShallow,
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,50 @@
1-
```unison
1+
```ucm:hide
2+
.> builtins.merge
3+
.> move builtin lib.builtin
4+
```
5+
6+
```unison:hide
27
foo = 1
38
lib.foo = 2
49
lib.bar = 3
10+
cat.foo = 4
11+
cat.lib.foo = 5
12+
cat.lib.bar = 6
13+
somewhere.bar = 7
514
```
615

7-
```ucm
16+
```ucm:hide
817
.> add
918
```
1019

1120
```ucm
1221
.> find foo
22+
.> view 1
23+
.> find.all foo
24+
.> view 1
25+
```
26+
27+
```ucm
28+
.> find-in cat foo
29+
.> view 1
30+
.> find-in.all cat foo
31+
.> view 1
1332
```
1433

1534
```ucm
16-
.somewhere> find.global foo
35+
.somewhere> find bar
36+
.somewhere> find.global bar
1737
```
1838

1939
```ucm
2040
.> find bar
41+
.> find-in somewhere bar
2142
```
2243

2344
```ucm:error
2445
.> find baz
2546
```
2647

2748
```ucm:error
28-
.> find.global nothere
49+
.> find.global notHere
2950
```

0 commit comments

Comments
 (0)