Skip to content

Commit

Permalink
Merge pull request #438 from rberenguel/fix/better_autocompletion_zsh
Browse files Browse the repository at this point in the history
Improve autocompletion for zsh to be more context-aware for #356
  • Loading branch information
jvican authored Apr 16, 2018
2 parents 2fccd7c + f46accc commit 55e47d5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
13 changes: 12 additions & 1 deletion etc/bash/bloop
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ _projects() {
return 0
}

_projects_or_flags() {
projects=$(bloop autocomplete --mode=projects --format=bash 2> /dev/null)
if [ "$projects" == " " ] || [ "$projects" == "" ]; then
command=$1
flags=$(bloop autocomplete --mode=flags --format=bash --command=$command)
COMPREPLY=( $(compgen -W "$flags" -- $cur) )
return 0
fi
_projects
}

_commands() {
commands=$(bloop autocomplete --mode=commands --format=bash 2> /dev/null || _notStarted)
COMPREPLY=( $(compgen -W "$commands" -- $cur) )
Expand Down Expand Up @@ -78,7 +89,7 @@ _bloop() {
;;

2)
_projects
_projects_or_flags "$prev"
;;

*)
Expand Down
47 changes: 36 additions & 11 deletions etc/zsh/_bloop
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

_projects() {
compadd "$@" $(bloop autocomplete --format zsh --mode projects 2> /dev/null)
return 0
}

_commands() {
compadd "$@" $(bloop autocomplete --format zsh --mode commands 2> /dev/null || _notStarted)
return 0
}

_notStarted() {
Expand All @@ -25,30 +27,53 @@ _boolean() {
}

_testsfqcn() {
project="$words[2]"
local project="$words[2]"
compadd "$@" $(bloop autocomplete --format zsh --mode testsfqcn --project $project)
}

_mainsfqcn() {
project="$words[2]"
local project="$words[2]"
compadd "$@" $(bloop autocomplete --format zsh --mode mainsfqcn --project $project)
}

_flags() {
cmd="$words[1]"
saveIFS=$IFS
local cmd="$words[1]"
_flags_for_cmd $cmd
}

_flags_for_cmd() {
local cmd=$1
local saveIFS=$IFS
IFS=$'\n'
results=($(bloop autocomplete --format zsh --mode flags --command $cmd))
# Add a dummy result without completion, otherwise no completion is shown.
results=($results '*:dummy:')
local flags=($(bloop autocomplete --format zsh --mode flags --command $cmd))
flags=($flags ':approximate-4:::' )
IFS=$saveIFS
_arguments $flags
return 0
}

_arguments $results
_project_or_flags() {
local project_cmd=($(bloop autocomplete --format zsh --mode project-commands))
local cmd=${words[2]}
# For project related commands, try to complete project. If not in a project
# folder, switch to flags. Otherwise, complete flags
if [[ $CURRENT == 3 ]] && [[ -n "${project_cmd[(r)$cmd]}" ]]; then
local projs=($(bloop autocomplete --format zsh --mode projects 2> /dev/null))
if [[ "$projs" != " " && "$projs" != "" ]]; then
_projects
else
cmd=${words[2]}
_flags_for_cmd $cmd
fi
else
cmd=${words[2]}
_flags_for_cmd $cmd
fi
}

_arguments \
":command:_commands" \
":project:_projects" \
"*::flags:_flags"
":command:_commands" \
":project_or_flags:_project_or_flags" \
"*::flags:_flags"

return 0
7 changes: 7 additions & 0 deletions frontend/src/main/scala/bloop/cli/Commands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package bloop.cli
import java.net.InetAddress
import java.nio.file.Path

import bloop.cli.CliParsers.CommandsMessages
import bloop.engine.ExecutionContext
import bloop.io.AbsolutePath
import caseapp.{ArgsName, CommandName, ExtraName, HelpMessage, Hidden, Recurse}
import caseapp.core.CommandMessages

object Commands {

Expand Down Expand Up @@ -42,6 +44,11 @@ object Commands {
project: Option[String]
) extends RawCommand

/** List of commands accepting a project as argument, used for autopomcletion */
val projectBound = CommandsMessages.messages.collect {
case (name, CommandMessages(args, _)) if args.exists(_.name == "project") => name
}.mkString(" ")

case class About(
@Recurse cliOptions: CliOptions = CliOptions.default
) extends RawCommand
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/main/scala/bloop/cli/completion/Mode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ object Mode {
/** Query autocompletion of project names */
case object Projects extends Mode("projects")

/** Query autocompletion of project-bound commands */
case object ProjectBoundCommands extends Mode("project-commands")

/** Query autocompletion of command flags */
case object Flags extends Mode("flags")

Expand All @@ -29,7 +32,7 @@ object Mode {
case object MainsFQCN extends Mode("mainsfqcn")

val modes: List[Mode] =
List(Commands, Projects, Flags, Reporters, Protocols, TestsFQCN, MainsFQCN)
List(Commands, Projects, ProjectBoundCommands, Flags, Reporters, Protocols, TestsFQCN, MainsFQCN)

implicit val completionModeRead: ArgParser[Mode] = {
ArgParser.instance[Mode]("mode") { input =>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/main/scala/bloop/engine/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ object Interpreter {
private def autocomplete(cmd: Commands.Autocomplete, state: State): Task[State] = Task {

cmd.mode match {
case Mode.ProjectBoundCommands =>
state.logger.info(Commands.projectBound)
case Mode.Commands =>
for {
(name, args) <- CommandsMessages.messages
Expand Down

0 comments on commit 55e47d5

Please sign in to comment.