Skip to content

Commit

Permalink
Merge pull request #4677 from unisonweb/cp/upgrade-autocompl
Browse files Browse the repository at this point in the history
  • Loading branch information
aryairani authored Feb 7, 2024
2 parents cf202fe + a0f5ce9 commit 14c9d97
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
19 changes: 19 additions & 0 deletions unison-cli/src/Unison/CommandLine/FZFResolvers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Unison.CommandLine.FZFResolvers
termDefinitionOptions,
typeDefinitionOptions,
namespaceOptions,
projectDependencyResolver,
projectNameOptions,
projectBranchOptions,
projectBranchOptionsWithinCurrentProject,
Expand All @@ -24,6 +25,7 @@ where

import Control.Lens
import Data.List.Extra qualified as List
import Data.Map qualified as Map
import Data.Set qualified as Set
import Data.Text qualified as Text
import U.Codebase.Sqlite.Project as SqliteProject
Expand All @@ -35,6 +37,7 @@ import Unison.Codebase.Branch qualified as Branch
import Unison.Codebase.Path (Path, Path' (..))
import Unison.Codebase.Path qualified as Path
import Unison.Name qualified as Name
import Unison.NameSegment qualified as NameSegment
import Unison.Names qualified as Names
import Unison.Parser.Ann (Ann)
import Unison.Position qualified as Position
Expand Down Expand Up @@ -94,6 +97,19 @@ namespaceOptions _codebase _projCtx searchBranch0 = do
& map (Path.toText' . intoPath')
& pure

-- | Lists all dependencies of the current project.
--
-- E.g. if the current project has `lib.base` and `lib.distributed`, it will list:
-- ["base", "distributed"]
projectDependencyOptions :: OptionFetcher
projectDependencyOptions _codebase _projCtx searchBranch0 = do
searchBranch0
& Branch.getAt0 (Path.singleton Name.libSegment)
& Branch.nonEmptyChildren
& Map.keys
& fmap NameSegment.toText
& pure

-- | Select a namespace from the given branch.
-- Returned Path's will match the provided 'Position' type.
fuzzySelectFromList :: [Text] -> FZFResolver
Expand Down Expand Up @@ -123,6 +139,9 @@ namespaceResolver = FZFResolver {getOptions = namespaceOptions}
namespaceOrDefinitionResolver :: FZFResolver
namespaceOrDefinitionResolver = multiResolver [definitionOptions, namespaceOptions]

projectDependencyResolver :: FZFResolver
projectDependencyResolver = FZFResolver {getOptions = projectDependencyOptions}

-- | A project name, branch name, or both.
projectAndOrBranchArg :: FZFResolver
projectAndOrBranchArg = multiResolver [projectBranchOptions, projectNameOptions]
Expand Down
13 changes: 12 additions & 1 deletion unison-cli/src/Unison/CommandLine/InputPatterns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3006,7 +3006,7 @@ upgrade =
{ patternName = "upgrade",
aliases = [],
visibility = I.Visible,
args = [],
args = [("dependency to upgrade", Required, dependencyArg), ("dependency to upgrade to", Required, dependencyArg)],
help =
P.wrap $
"`upgrade old new` upgrades library dependency `lib.old` to `lib.new`, and, if successful, deletes `lib.old`.",
Expand Down Expand Up @@ -3267,6 +3267,17 @@ namespaceOrDefinitionArg =
Just Resolvers.namespaceOrDefinitionResolver
}

-- | A dependency name. E.g. if your project has `lib.base`, `base` would be a dependency
-- name.
dependencyArg :: ArgumentType
dependencyArg =
ArgumentType
{ typeName = "project dependency",
suggestions = \q cb _http p -> Codebase.runTransaction cb do
prefixCompleteNamespace q (p Path.:> Name.libSegment),
fzfResolver = Just Resolvers.projectDependencyResolver
}

-- | Names of child branches of the branch, only gives options for one 'layer' deeper at a time.
childNamespaceNames :: Branch.Branch0 m -> [Text]
childNamespaceNames b = NameSegment.toText <$> Map.keys (Branch.nonEmptyChildren b)
Expand Down
11 changes: 11 additions & 0 deletions unison-src/transcripts/upgrade-happy-path.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ lib.new.foo = 18
thingy = lib.old.foo + 10
```


```ucm
proj/main> add
```

Test tab completion and fzf options of upgrade command.
```ucm
proj/main> debug.tab-complete upgrade ol
proj/main> debug.fuzzy-options upgrade _
proj/main> debug.fuzzy-options upgrade old _
```

```ucm
proj/main> upgrade old new
proj/main> ls lib
proj/main> view thingy
Expand Down
23 changes: 23 additions & 0 deletions unison-src/transcripts/upgrade-happy-path.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ proj/main> add
lib.old.foo : Nat
thingy : Nat
```
Test tab completion and fzf options of upgrade command.
```ucm
proj/main> debug.tab-complete upgrade ol
old
proj/main> debug.fuzzy-options upgrade _
Select a dependency to upgrade:
* builtin
* new
* old
proj/main> debug.fuzzy-options upgrade old _
Select a dependency to upgrade to:
* builtin
* new
* old
```
```ucm
proj/main> upgrade old new
I upgraded old to new, and removed old.
Expand Down

0 comments on commit 14c9d97

Please sign in to comment.