add updates libs
This commit is contained in:
@@ -4,16 +4,17 @@ import argparse
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
from tclint.cli.utils import resolve_sources, register_codec_warning
|
||||
from tclint.cli.resolver import Resolver
|
||||
from tclint.cli.utils import register_codec_warning
|
||||
from tclint.commands.plugins import PluginManager
|
||||
from tclint.config import (
|
||||
get_config,
|
||||
setup_tclfmt_config_cli_args,
|
||||
Config,
|
||||
ConfigError,
|
||||
RunConfig,
|
||||
SpacesInBraces,
|
||||
setup_tclfmt_config_cli_args,
|
||||
)
|
||||
from tclint.parser import Parser, TclSyntaxError
|
||||
from tclint.format import Formatter, FormatterOpts
|
||||
from tclint.parser import Parser, TclSyntaxError
|
||||
|
||||
try:
|
||||
from tclint._version import __version__ # type: ignore
|
||||
@@ -27,22 +28,41 @@ EXIT_SYNTAX_ERROR = 2
|
||||
EXIT_INPUT_ERROR = 4
|
||||
|
||||
|
||||
def format(script: str, config: Config, debug=False) -> str:
|
||||
plugins = [config.commands] if config.commands is not None else []
|
||||
parser = Parser(debug=debug, command_plugins=plugins)
|
||||
def format(
|
||||
script: str,
|
||||
config: Config,
|
||||
plugins: PluginManager,
|
||||
debug=False,
|
||||
debug_whitespace=False,
|
||||
partial=False,
|
||||
) -> str:
|
||||
parser = Parser(debug=debug, commands=plugins.get_commands(config.commands))
|
||||
|
||||
formatter = Formatter(
|
||||
FormatterOpts(
|
||||
indent=config.get_indent(),
|
||||
spaces_in_braces=config.style_spaces_in_braces,
|
||||
indent_mixed_tab_size=config.get_indent_mixed_tab_size(),
|
||||
spaces_in_braces=(
|
||||
config.style_spaces_in_braces == SpacesInBraces.ALWAYS
|
||||
or config.style_spaces_in_braces == SpacesInBraces.BALANCED_YES
|
||||
),
|
||||
balanced_spaces_in_braces=(
|
||||
config.style_spaces_in_braces == SpacesInBraces.BALANCED_NO
|
||||
or config.style_spaces_in_braces == SpacesInBraces.BALANCED_YES
|
||||
),
|
||||
max_blank_lines=config.style_max_blank_lines,
|
||||
indent_namespace_eval=config.style_indent_namespace_eval,
|
||||
emacs=config.style_emacs,
|
||||
debug_whitespace=debug_whitespace,
|
||||
)
|
||||
)
|
||||
return formatter.format_top(script, parser)
|
||||
if partial:
|
||||
return formatter.format_partial(script, parser)
|
||||
else:
|
||||
return formatter.format_top(script, parser)
|
||||
|
||||
|
||||
def check(path: pathlib.Path, script: str, formatted: str):
|
||||
def check(path: str, script: str, formatted: str):
|
||||
parser = Parser()
|
||||
original_tree = parser.parse(script)
|
||||
formatted_tree = parser.parse(formatted)
|
||||
@@ -86,6 +106,12 @@ def main():
|
||||
" of output (e.g. -dd)"
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--debug-whitespace",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="display whitespace in debug mode.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
"--config",
|
||||
@@ -94,41 +120,46 @@ def main():
|
||||
default=None,
|
||||
metavar="<path>",
|
||||
)
|
||||
setup_tclfmt_config_cli_args(parser)
|
||||
parser.add_argument(
|
||||
"--partial",
|
||||
help="treat input as a fragment of a script",
|
||||
action="store_true",
|
||||
)
|
||||
|
||||
cwd = pathlib.Path.cwd()
|
||||
setup_tclfmt_config_cli_args(parser, cwd)
|
||||
args = parser.parse_args()
|
||||
|
||||
global_config = None
|
||||
if args.config is not None:
|
||||
try:
|
||||
global_config = Config.from_path(args.config, cwd)
|
||||
global_config.apply_cli_args(args)
|
||||
except FileNotFoundError:
|
||||
print(f"Config file path doesn't exist: {args.config}")
|
||||
return EXIT_INPUT_ERROR
|
||||
except ConfigError as e:
|
||||
print(f"Invalid config file: {e}")
|
||||
return EXIT_INPUT_ERROR
|
||||
|
||||
resolver = Resolver(args, global_config)
|
||||
try:
|
||||
config = get_config(args.config, pathlib.Path.cwd())
|
||||
sources = resolver.resolve_sources(args.source, cwd)
|
||||
except FileNotFoundError as e:
|
||||
print(f"Invalid path provided: {e}")
|
||||
return EXIT_INPUT_ERROR
|
||||
except ConfigError as e:
|
||||
print(f"Invalid config file: {e}")
|
||||
return EXIT_INPUT_ERROR
|
||||
|
||||
if config is None:
|
||||
config = RunConfig()
|
||||
|
||||
config.apply_cli_args(args)
|
||||
|
||||
try:
|
||||
# TODO: we should eventually allow tclfmt to find a config by walking up
|
||||
# directories, at which point exclude_root should be the parent dir of
|
||||
# the config file, unless -c is used (eslint rules)
|
||||
exclude_root = pathlib.Path.cwd()
|
||||
sources = resolve_sources(
|
||||
args.source,
|
||||
exclude_patterns=config.exclude,
|
||||
exclude_root=exclude_root,
|
||||
extensions=config.extensions,
|
||||
)
|
||||
except FileNotFoundError as e:
|
||||
print(f"Invalid path provided: {e}")
|
||||
return EXIT_INPUT_ERROR
|
||||
plugin_manager = PluginManager(trust_uninstalled=args.trust_plugins)
|
||||
|
||||
retcode = EXIT_OK
|
||||
|
||||
register_codec_warning("replace_with_warning")
|
||||
|
||||
reformat_count = 0
|
||||
for path in sources:
|
||||
for path, config in sources:
|
||||
if path is None:
|
||||
script = sys.stdin.read()
|
||||
out_prefix = "(stdin)"
|
||||
@@ -139,7 +170,12 @@ def main():
|
||||
|
||||
try:
|
||||
formatted = format(
|
||||
script, config.get_for_path(path), debug=(args.debug > 1)
|
||||
script,
|
||||
config,
|
||||
plugin_manager,
|
||||
debug=(args.debug > 1),
|
||||
debug_whitespace=args.debug_whitespace,
|
||||
partial=args.partial,
|
||||
)
|
||||
if args.in_place and path:
|
||||
with open(path, "w") as f:
|
||||
@@ -155,9 +191,15 @@ def main():
|
||||
print(formatted, end="")
|
||||
|
||||
if args.debug > 0:
|
||||
check(path, script, formatted)
|
||||
if args.debug_whitespace:
|
||||
print(
|
||||
"Warning: --debug-whitespace enabled, disabling original vs."
|
||||
" formatted syntax tree check"
|
||||
)
|
||||
else:
|
||||
check(out_prefix, script, formatted)
|
||||
except TclSyntaxError as e:
|
||||
line, col = e.pos
|
||||
line, col = e.start
|
||||
print(f"{out_prefix}:{line}:{col}: syntax error: {e}", file=sys.stderr)
|
||||
retcode |= EXIT_SYNTAX_ERROR
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user