Skip to content

Commit

Permalink
🚸 Improve shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Mar 5, 2024
1 parent 55b5b23 commit 973d796
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
- id: check-toml
- id: check-json
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.4
rev: v1.5.5
hooks:
- id: remove-crlf
- repo: https://github.com/codespell-project/codespell
Expand Down
51 changes: 26 additions & 25 deletions src/mutt_language_server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
`python -m <https://docs.python.org/3/library/__main__.html>`_.
"""

import logging
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from contextlib import suppress
from datetime import datetime

from . import FILETYPE, __version__
from . import __name__ as NAME
from . import __version__

logger = logging.getLogger(__name__)
try:
import shtab
except ImportError:
import _shtab as shtab

NAME = NAME.replace("_", "-")
VERSION = rf"""{NAME} {__version__}
Copyright (C) {datetime.now().year}
Expand All @@ -27,46 +29,43 @@ def get_parser():
epilog=EPILOG,
formatter_class=RawDescriptionHelpFormatter,
)
with suppress(ImportError):
import shtab

shtab.add_argument_to(parser)
shtab.add_argument_to(parser)
parser.add_argument("--version", version=VERSION, action="version")
parser.add_argument(
"--generate-schema",
choices=["neomuttrc"], # type: ignore
choices=FILETYPE.__args__, # type: ignore
help="generate schema in an output format",
)
parser.add_argument(
"--output-format",
choices=["json", "yaml", "toml"],
default="json",
help="output format: %(default)s",
)
parser.add_argument(
"--indent",
type=int,
default=2,
help="generated json's indent",
)
parser.add_argument(
"--check",
nargs="*",
default={},
help="check file's errors and warnings",
help="generated json, yaml's indent, ignored by toml: %(default)s",
)
parser.add_argument(
"--color",
choices=["auto", "always", "never"],
default="auto",
help="when to display color, default: %(default)s",
)
parser.add_argument(
"--check",
nargs="*",
default={},
help="check file's errors and warnings",
).complete = shtab.FILE # type: ignore
parser.add_argument(
"--convert",
nargs="*",
default={},
help="convert files to output format",
)
parser.add_argument(
"--output-format",
choices=["json", "yaml", "toml"],
default="json",
help="output format: %(default)s",
)
).complete = shtab.FILE # type: ignore
return parser


Expand All @@ -85,12 +84,14 @@ def main() -> None:
if args.generate_schema:
from .misc import get_schema

kwargs = (
{"indent": args.indent} if args.output_format != "toml" else {}
)
pprint(
get_schema(),
filetype=args.output_format,
indent=args.indent,
**kwargs,
)
return None
for file in args.convert:
pprint(
MuttTrie.from_file(file, parser.parse).to_json(),
Expand Down
16 changes: 16 additions & 0 deletions src/mutt_language_server/_shtab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
r"""Fake shtab
==============
"""

from argparse import ArgumentParser
from typing import Any

FILE = None
DIRECTORY = DIR = None


def add_argument_to(parser: ArgumentParser, *args: Any, **kwargs: Any):
from argparse import Action

Action.complete = None # type: ignore
return parser

0 comments on commit 973d796

Please sign in to comment.