Skip to content

Commit 1522858

Browse files
committed
🚸 Improve shell completions
1 parent 21913ef commit 1522858

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
exclude: (^templates/.*|.*\.json$)
2+
exclude: ^templates/.*|.*\.json$
33

44
repos:
55
- repo: https://github.com/pre-commit/pre-commit-hooks
@@ -23,7 +23,7 @@ repos:
2323
- id: check-toml
2424
- id: check-json
2525
- repo: https://github.com/Lucas-C/pre-commit-hooks
26-
rev: v1.5.4
26+
rev: v1.5.5
2727
hooks:
2828
- id: remove-crlf
2929
- repo: https://github.com/codespell-project/codespell
@@ -46,6 +46,7 @@ repos:
4646
rev: 3.0.0
4747
hooks:
4848
- id: check-mailmap
49+
# https://github.com/koalaman/shellcheck/issues/2909
4950
- id: shellcheck
5051
exclude_types:
5152
- zsh

src/termux_language_server/__main__.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
"""
44

55
from argparse import ArgumentParser, RawDescriptionHelpFormatter
6-
from contextlib import suppress
76
from datetime import datetime
87

98
from . import FILETYPE, __version__
109
from . import __name__ as NAME
1110

11+
try:
12+
import shtab
13+
except ImportError:
14+
import _shtab as shtab
15+
1216
NAME = NAME.replace("_", "-")
1317
VERSION = rf"""{NAME} {__version__}
1418
Copyright (C) {datetime.now().year}
@@ -25,52 +29,49 @@ def get_parser():
2529
epilog=EPILOG,
2630
formatter_class=RawDescriptionHelpFormatter,
2731
)
28-
with suppress(ImportError):
29-
import shtab
30-
31-
shtab.add_argument_to(parser)
32+
shtab.add_argument_to(parser)
3233
parser.add_argument("--version", version=VERSION, action="version")
3334
parser.add_argument(
3435
"--generate-schema",
3536
choices=FILETYPE.__args__, # type: ignore
3637
help="generate schema in an output format",
3738
)
39+
parser.add_argument(
40+
"--output-format",
41+
choices=["json", "yaml", "toml"],
42+
default="json",
43+
help="output format: %(default)s",
44+
)
3845
parser.add_argument(
3946
"--indent",
4047
type=int,
4148
default=2,
42-
help="generated json's indent",
49+
help="generated json, yaml's indent, ignored by toml: %(default)s",
50+
)
51+
parser.add_argument(
52+
"--color",
53+
choices=["auto", "always", "never"],
54+
default="auto",
55+
help="when to display color, default: %(default)s",
4356
)
4457
parser.add_argument(
4558
"--check",
4659
nargs="*",
4760
default={},
4861
help="check file's errors and warnings",
49-
)
62+
).complete = shtab.FILE # type: ignore
5063
parser.add_argument(
5164
"--format",
5265
nargs="*",
5366
default={},
5467
help="format files",
55-
)
56-
parser.add_argument(
57-
"--color",
58-
choices=["auto", "always", "never"],
59-
default="auto",
60-
help="when to display color, default: %(default)s",
61-
)
68+
).complete = shtab.FILE # type: ignore
6269
parser.add_argument(
6370
"--convert",
6471
nargs="*",
6572
default={},
6673
help="convert files to output format",
67-
)
68-
parser.add_argument(
69-
"--output-format",
70-
choices=["json", "yaml", "toml"],
71-
default="json",
72-
help="output format: %(default)s",
73-
)
74+
).complete = shtab.FILE # type: ignore
7475
return parser
7576

7677

@@ -92,10 +93,13 @@ def main():
9293
if args.generate_schema:
9394
from .misc import get_schema
9495

96+
kwargs = (
97+
{"indent": args.indent} if args.output_format != "toml" else {}
98+
)
9599
pprint(
96100
get_schema(args.generate_schema),
97101
filetype=args.output_format,
98-
indent=args.indent,
102+
**kwargs,
99103
)
100104
for file in args.convert:
101105
pprint(

src/termux_language_server/_shtab.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
r"""Fake shtab
2+
==============
3+
"""
4+
5+
from argparse import ArgumentParser
6+
from typing import Any
7+
8+
FILE = None
9+
DIRECTORY = DIR = None
10+
11+
12+
def add_argument_to(parser: ArgumentParser, *args: Any, **kwargs: Any):
13+
from argparse import Action
14+
15+
Action.complete = None # type: ignore
16+
return parser

0 commit comments

Comments
 (0)