Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve autocompletion for zsh to be more context-aware for #356 #438

Merged
merged 6 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
7 changes: 7 additions & 0 deletions frontend/src/main/scala/bloop/engine/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import bloop.reporter.ReporterConfig
import bloop.testing.TestInternals
import bloop.engine.tasks.Tasks
import bloop.Project
import caseapp.core.CommandMessages
import monix.eval.Task
import monix.execution.misc.NonFatal

Expand Down Expand Up @@ -213,9 +214,15 @@ object Interpreter {
}
}

private val projectBound = CommandsMessages.messages.filter {
case (name, CommandMessages(args, _)) => args.exists(_.name == "project")
}.map(_._1).mkString(" ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter-map combo can be replaced with collect:

private val projectBound = CommandsMessages.messages.filter {
  case (name, CommandMessages(args, _)) if args.exists(_.name == "projects") => name
}.mkString(" ")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Duhemm ! I knew there was to be something in the library to do this in one go


private def autocomplete(cmd: Commands.Autocomplete, state: State): Task[State] = Task {

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