Skip to content

Commit

Permalink
Sort project branches by recency in switch fzf
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPenner committed Jan 23, 2025
1 parent 5d2aa19 commit c260782
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
14 changes: 13 additions & 1 deletion codebase2/codebase-sqlite/U/Codebase/Sqlite/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module U.Codebase.Sqlite.Queries
loadProjectByName,
expectProject,
loadAllProjects,
loadAllProjectsByRecentlyAccessed,
loadAllProjectsBeginningWith,
insertProject,
renameProject,
Expand Down Expand Up @@ -3602,6 +3603,17 @@ loadAllProjects =
ORDER BY name ASC
|]

-- | Load all projects.
loadAllProjectsByRecentlyAccessed :: Transaction [Project]
loadAllProjectsByRecentlyAccessed =
queryListRow
[sql|
SELECT project.id, project.name
FROM project
JOIN project_branch ON project.id = project_branch.project_id
ORDER BY project_branch.last_accessed DESC NULLS LAST, project.name ASC
|]

-- | Load all projects whose name matches a prefix.
loadAllProjectsBeginningWith :: Maybe Text -> Transaction [Project]
loadAllProjectsBeginningWith mayPrefix = do
Expand Down Expand Up @@ -3740,7 +3752,7 @@ loadAllProjectBranchNamePairs =
FROM
project
JOIN project_branch ON project.id = project_branch.project_id
ORDER BY project.name ASC, project_branch.name ASC
ORDER BY project_branch.last_accessed DESC NULLS LAST, project.name ASC, project_branch.name ASC
|]
<&> fmap \(projectName, branchName, projectId, branchId) ->
( ProjectAndBranch projectName branchName,
Expand Down
17 changes: 6 additions & 11 deletions unison-cli/src/Unison/CommandLine/FZFResolvers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,20 @@ projectNameResolver = FZFResolver {getOptions = projectNameOptions}
-- E.g. '@unison/base'
projectNameOptions :: OptionFetcher
projectNameOptions codebase _projCtx _searchBranch0 = do
fmap (into @Text . SqliteProject.name) <$> Codebase.runTransaction codebase Q.loadAllProjects
fmap (into @Text . SqliteProject.name) <$> Codebase.runTransaction codebase Q.loadAllProjectsByRecentlyAccessed

-- | All possible local project/branch names.
-- E.g. '@unison/base/main'
projectBranchOptions :: OptionFetcher
projectBranchOptions codebase projCtx _searchBranch0 = do
projs <- Codebase.runTransaction codebase Q.loadAllProjectBranchNamePairs
projs
& foldMap
( \(names, projIds) ->
if projIds.project == projCtx.project.projectId
then -- If the branch is in the current project, put a shortened version of the branch name first,
-- then the long-form name at the end of the list (in case the user still types the full name)
[(0 :: Int, "/" <> into @Text names.branch), (2, into @Text names)]
else [(1, into @Text names)]
& filter
( \(_names, projIds) ->
-- If it's the same as the current branch, just omit it.
projIds.branch /= projCtx.branch.branchId
)
-- Put branches in this project first.
& List.sort
& fmap snd
& fmap (into @Text . fst)
& pure

-- | All possible local branch names within the current project.
Expand Down
3 changes: 0 additions & 3 deletions unison-src/transcripts/idempotent/fuzzy-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ myproject/main> branch mybranch
scratch/main> debug.fuzzy-options switch _
Select a project or branch to switch to:
* /empty
* /main
* myproject/main
* myproject/mybranch
* scratch/empty
* scratch/main
* myproject
* scratch
```

0 comments on commit c260782

Please sign in to comment.