Add tab-completion and fzf for upgrade command

This commit is contained in:
Chris Penner 2024-02-07 13:37:32 -08:00
parent cf202fef9a
commit a0f5ce9e52
4 changed files with 65 additions and 1 deletions

View File

@ -4,6 +4,7 @@ module Unison.CommandLine.FZFResolvers
termDefinitionOptions,
typeDefinitionOptions,
namespaceOptions,
projectDependencyResolver,
projectNameOptions,
projectBranchOptions,
projectBranchOptionsWithinCurrentProject,
@ -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
@ -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
@ -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
@ -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]

View File

@ -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`.",
@ -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)

View File

@ -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

View File

@ -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.