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 071c935 commit 69946fb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
exclude: (^templates/.*|.*\.json$)
exclude: ^templates/.*|.*\.json$

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand All @@ -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
30 changes: 18 additions & 12 deletions src/autoconf_language_server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
"""

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

from make_language_server import __version__

from . import FILETYPE
from . import __name__ as NAME

try:
import shtab
except ImportError:
import _shtab as shtab

NAME = NAME.replace("_", "-")
VERSION = rf"""{NAME} {__version__}
Copyright (C) {datetime.now().year}
Expand All @@ -21,41 +25,43 @@
"""


def get_parser():
def get_parser() -> ArgumentParser:
r"""Get a parser for unit test."""
parser = ArgumentParser(
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=FILETYPE.__args__, # type: ignore
help="generate schema json",
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",
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",
help="when to display color, default: %(default)s",
)
return parser


def main():
def main() -> None:
r"""Parse arguments and provide shell completions."""
parser = get_parser()
args = parser.parse_args()
args = get_parser().parse_args()

if args.generate_schema:
from lsp_tree_sitter.utils import pprint
Expand Down
16 changes: 16 additions & 0 deletions src/autoconf_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
76 changes: 40 additions & 36 deletions src/make_language_server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
"""

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

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

try:
import shtab
except ImportError:
import _shtab as shtab

NAME = NAME.replace("_", "-")
VERSION = rf"""{NAME} {__version__}
Copyright (C) {datetime.now().year}
Expand All @@ -19,70 +23,70 @@
"""


def get_parser():
def get_parser() -> ArgumentParser:
r"""Get a parser for unit test."""
parser = ArgumentParser(
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=FILETYPE.__args__, # type: ignore
help="generate schema json",
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",
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
return parser


def main():
def main() -> None:
r"""Parse arguments and provide shell completions."""
parser = get_parser()
args = parser.parse_args()
args = get_parser().parse_args()

if args.generate_schema:
if args.generate_schema or args.check:
from lsp_tree_sitter.diagnose import check
from lsp_tree_sitter.utils import pprint
from tree_sitter_languages import get_parser as _get_parser

from .finders import DIAGNOSTICS_FINDER_CLASSES
from .misc import get_schema

pprint(get_schema(args.generate_schema), indent=args.indent)
exit()
from lsp_tree_sitter.diagnose import check
from tree_sitter_languages import get_parser as _get_parser

from .finders import DIAGNOSTICS_FINDER_CLASSES

parser = _get_parser("make")
result = check(
args.check,
parser.parse,
DIAGNOSTICS_FINDER_CLASSES,
None,
args.color,
)
if args.check:
exit(result)
if args.generate_schema:
pprint(get_schema(args.generate_schema), indent=args.indent)
parser = _get_parser("make")
exit(
check(
args.check,
parser.parse,
DIAGNOSTICS_FINDER_CLASSES,
None,
args.color,
)
)

from .server import MakeLanguageServer

Expand Down
16 changes: 16 additions & 0 deletions src/make_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 69946fb

Please sign in to comment.