Skip to content

Commit

Permalink
Control special file/directory behavior in completions
Browse files Browse the repository at this point in the history
(cherry picked from commit ba0d981)
  • Loading branch information
roberth committed Jan 17, 2024
1 parent 659c018 commit 133c563
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/Options/Applicative/BashCompletion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ bashCompletionQuery pinfo pprefs richness ws i _ = case runCompletion compl ppre
render_item :: CompletionItem -> [String]
render_item CompletionItem { ciOptions = opts, ciValue = val } =
[ "%addspace" | cioAddSpace opts ]
++ [ "%files" | cioFiles opts ]
++ ["%value", val]

-- | Generated bash shell completion script
Expand All @@ -188,7 +189,7 @@ bashCompletionScript prog progn = unlines
, " CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg)"
, " done"
, ""
, " compopt -o nospace"
, " compopt -o nospace +o filenames"
, " COMPREPLY=()"
, " for ln in $(" ++ prog ++ " \"${CMDLINE[@]}\"); do"
, " if $value_mode; then"
Expand All @@ -202,6 +203,9 @@ bashCompletionScript prog progn = unlines
, " %addspace)"
, " compopt +o nospace"
, " ;;"
, " %files)"
, " compopt -o filenames"
, " ;;"
, " esac"
, " fi"
, " done"
Expand Down Expand Up @@ -309,6 +313,7 @@ zshCompletionScript prog progn = unlines
, " fi"
, " value_mode=false"
, " addspace=false"
, " files=false"
, " else"
, " case $word in"
, " %value)"
Expand All @@ -317,6 +322,9 @@ zshCompletionScript prog progn = unlines
, " %addspace)"
, " addspace=true"
, " ;;"
, " %files)"
, " files=true"
, " ;;"
, " esac"
, " fi"
, "done"
Expand Down
12 changes: 9 additions & 3 deletions src/Options/Applicative/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,21 @@ data CompletionItemOptions = CompletionItemOptions {
--
-- Set this value to 'False' if the completion is only a prefix of the final
-- valid values.
cioAddSpace :: Bool
cioAddSpace :: Bool,

-- | Whether to treat the completions as file names (if they exists) and
-- add a trailing slash to completions that are directories.
-- Defaults to 'True'
cioFiles :: Bool
}
instance Semigroup CompletionItemOptions where
a <> b =
CompletionItemOptions {
cioAddSpace = cioAddSpace a && cioAddSpace b
cioAddSpace = cioAddSpace a && cioAddSpace b,
cioFiles = cioFiles a && cioFiles b
}
instance Monoid CompletionItemOptions where
mempty = CompletionItemOptions True
mempty = CompletionItemOptions True True
mappend = (<>)

-- | A shell complete function.
Expand Down

0 comments on commit 133c563

Please sign in to comment.