Skip to content

Commit 8a980ca

Browse files
authored
Added command line completion for CLI
I've also added docs/COMPLETIONS.md which explains how to add shell completion for bash, zsh and fish shell manually! Note: This commit doesn't affect the working of quick-lint-js. Fixes issue: #188
1 parent b3021e8 commit 8a980ca

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ add_subdirectory(plugin/emacs)
3737
add_subdirectory(src)
3838
add_subdirectory(tools)
3939
add_subdirectory(website/demo)
40+
add_subdirectory(completions)
4041
include(CTest)
4142
if (BUILD_TESTING)
4243
add_subdirectory(test)

completions/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (C) 2020 Matthew Glazar
2+
# See end of file for extended copyright information.
3+
4+
cmake_minimum_required(VERSION 3.10)
5+
include(GNUInstallDirs)
6+
7+
install(
8+
FILES _quick-lint-js
9+
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/zsh/site-functions"
10+
)
11+
12+
install(
13+
FILES quick-lint-js.bash
14+
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions"
15+
)
16+
17+
install(
18+
FILES quick-lint-js.fish
19+
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/fish/vendor_completions.d"
20+
)
21+
22+
# quick-lint-js finds bugs in JavaScript programs.
23+
# Copyright (C) 2020 Matthew Glazar
24+
#
25+
# This file is part of quick-lint-js.
26+
#
27+
# quick-lint-js is free software: you can redistribute it and/or modify
28+
# it under the terms of the GNU General Public License as published by
29+
# the Free Software Foundation, either version 3 of the License, or
30+
# (at your option) any later version.
31+
#
32+
# quick-lint-js is distributed in the hope that it will be useful,
33+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
34+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35+
# GNU General Public License for more details.
36+
#
37+
# You should have received a copy of the GNU General Public License
38+
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.

completions/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Shell completions
2+
3+
To get automatic completions for quick-lint-js's options you can install the provided shell completions.
4+
5+
## Zsh
6+
7+
To install the completions for zsh, you can place the `completions/_quick-lint-js` file in any
8+
directory referenced by `$fpath`.
9+
10+
If you do not already have such a directory registered through your `~/.zshrc`, you can add one like this:
11+
12+
```sh
13+
mkdir -p ${ZDOTDIR:-~}/.zsh_functions
14+
echo 'fpath+=${ZDOTDIR:-~}/.zsh_functions' >> ${ZDOTDIR:-~}/.zshrc
15+
```
16+
17+
Then copy the completion file to this directory:
18+
19+
```sh
20+
cp completions/_quick-lint-js ${ZDOTDIR:-~}/.zsh_functions/_quick-lint-js
21+
```
22+
23+
## Bash
24+
25+
To install the completions for bash, you can `source` the `completions/quick-lint-js.bash` file
26+
in your `~/.bashrc` file.
27+
28+
If you do not plan to delete the source folder of quick-lint-js, you can run
29+
30+
```sh
31+
echo "source $(pwd)/completions/quick-lint-js.bash" >> ~/.bashrc
32+
```
33+
34+
Otherwise you can copy it to the `~/.bash_completion` folder and source it from there:
35+
36+
```sh
37+
mkdir -p ~/.bash_completion
38+
cp completions/quick-lint-js.bash ~/.bash_completion/quick-lint-js
39+
echo "source ~/.bash_completion/quick-lint-js" >> ~/.bashrc
40+
```
41+
42+
## Fish
43+
44+
To install the completions for fish, run
45+
46+
```sh
47+
mkdir -p $fish_complete_path[1]
48+
cp completions/quick-lint-js.fish $fish_complete_path[1]/quick-lint-js.fish
49+
```

completions/_quick-lint-js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#compdef _quick-lint-js quick-lint-js
2+
3+
# Copyright (C) 2020 Matthew Glazar
4+
# See end of file for extended copyright information.
5+
6+
# This script is used to provide command line completion for
7+
# quick-lint-js when using zsh shell
8+
9+
typeset -A opt_args
10+
11+
function _quick-lint-js() {
12+
local curcontext="$curcontext" state line
13+
local -i ret=1
14+
local -a args
15+
16+
args+=(
17+
'(- *)'--help'[Print help message]'
18+
'(- *)'--version'[Print version information]'
19+
'(--lsp-server)*:file:_files'
20+
'(--exit-fail-on --output-format *)'--lsp-server'[Run quick-lint-js in LSP server mode]'
21+
'(--lsp-server)--exit-fail-on=[Fail with a non-zero exit code if any of these errors are found (default\: "all")]'
22+
'(--lsp-server)--output-format=[Format to print feedback where FORMAT is one of\: gnu-like (default if omitted), vim-qflist-json]:format:->format'
23+
'--vim-file-bufnr=[Select a vim buffer for outputting feedback]')
24+
25+
_arguments -C -S "${args[@]}" && ret=0
26+
27+
case "$state" in
28+
format)
29+
formats=( 'vim-qflist-json' 'gnu-like' )
30+
_describe 'format' formats && ret=0
31+
;;
32+
esac
33+
return ret
34+
}
35+
36+
# quick-lint-js finds bugs in JavaScript programs.
37+
# Copyright (C) 2020 Matthew Glazar
38+
#
39+
# This file is part of quick-lint-js.
40+
#
41+
# quick-lint-js is free software: you can redistribute it and/or modify
42+
# it under the terms of the GNU General Public License as published by
43+
# the Free Software Foundation, either version 3 of the License, or
44+
# (at your option) any later version.
45+
#
46+
# quick-lint-js is distributed in the hope that it will be useful,
47+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
48+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49+
# GNU General Public License for more details.
50+
#
51+
# You should have received a copy of the GNU General Public License
52+
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.

completions/quick-lint-js.bash

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (C) 2020 Matthew Glazar
2+
# See end of file for extended copyright information.
3+
4+
_quick-lint-js () {
5+
local cur opts
6+
_init_completion -n = || return
7+
8+
opts='--help --version --lsp-server --exit-fail-on= --output-format= --vim-file-bufnr='
9+
10+
case $cur in
11+
--output-format=*)
12+
COMPREPLY=($(compgen -W 'gnu-like vim-qflint-json' -- "${cur#*=}"))
13+
return
14+
;;
15+
--exit-fail-on=*|--vim-file-bufnr=*)
16+
return
17+
;;
18+
esac
19+
20+
if [[ $cur == -* ]]; then
21+
COMPREPLY=($(compgen -W '${opts}' -- $cur))
22+
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
23+
return
24+
else
25+
_filedir
26+
fi
27+
}
28+
29+
complete -F _quick-lint-js quick-lint-js
30+
31+
# quick-lint-js finds bugs in JavaScript programs.
32+
# Copyright (C) 2020 Matthew Glazar
33+
#
34+
# This file is part of quick-lint-js.
35+
#
36+
# quick-lint-js is free software: you can redistribute it and/or modify
37+
# it under the terms of the GNU General Public License as published by
38+
# the Free Software Foundation, either version 3 of the License, or
39+
# (at your option) any later version.
40+
#
41+
# quick-lint-js is distributed in the hope that it will be useful,
42+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
43+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44+
# GNU General Public License for more details.
45+
#
46+
# You should have received a copy of the GNU General Public License
47+
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.

completions/quick-lint-js.fish

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (C) 2020 Matthew Glazar
2+
# See end of file for extended copyright information.
3+
4+
complete -c quick-lint-js -l help -d 'Print help message' -f
5+
complete -c quick-lint-js -l version -d 'Print version information' -f
6+
complete -c quick-lint-js -l lsp-server -d 'Run quick-lint-js in LSP server mode' -r
7+
complete -c quick-lint-js -l exit-fail-on -d 'Fail with a non-zero exit code if any of these errors are found (default: "all")' -r
8+
complete -c quick-lint-js -l output-format -d 'Format to print feedback where FORMAT is one of: gnu-like (default if omitted), vim-qflist-json' -xa 'gnu-like vim-qflist-json'
9+
complete -c quick-lint-js -l vim-file-bufnr -d 'Select a vim buffer for outputting feedback' -r
10+
11+
# quick-lint-js finds bugs in JavaScript programs.
12+
# Copyright (C) 2020 Matthew Glazar
13+
#
14+
# This file is part of quick-lint-js.
15+
#
16+
# quick-lint-js is free software: you can redistribute it and/or modify
17+
# it under the terms of the GNU General Public License as published by
18+
# the Free Software Foundation, either version 3 of the License, or
19+
# (at your option) any later version.
20+
#
21+
# quick-lint-js is distributed in the hope that it will be useful,
22+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
# GNU General Public License for more details.
25+
#
26+
# You should have received a copy of the GNU General Public License
27+
# along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.
28+
29+
# vim: ft=sh:

0 commit comments

Comments
 (0)