|
26 | 26 | # OR OTHER DEALINGS IN THE SOFTWARE. |
27 | 27 | # |
28 | 28 |
|
| 29 | +# stdlib |
| 30 | +from typing import Mapping, Optional |
| 31 | + |
29 | 32 | # 3rd party |
30 | 33 | from domdf_python_tools.paths import PathPlus |
31 | 34 | from domdf_python_tools.typing import PathLike |
32 | | -from formate.config import formats_filetypes, wants_filename |
| 35 | +from formate.config import formats_filetypes, wants_filename, wants_global_config |
33 | 36 |
|
34 | 37 | # this package |
35 | 38 | from ._formate_js import Configuration, ConfigurationBuilder, FormatTextOptions, format_text |
|
39 | 42 |
|
40 | 43 | @formats_filetypes(".ts", ".js") |
41 | 44 | @wants_filename |
| 45 | +@wants_global_config |
42 | 46 | def javascript_hook( |
43 | 47 | source: str, |
44 | 48 | formate_filename: PathLike, |
| 49 | + formate_global_config: Optional[Mapping] = None, |
45 | 50 | **kwargs, |
46 | 51 | ) -> str: |
47 | 52 | r""" |
48 | 53 | Reformat JavaScript and TypeScript with dprint. |
49 | 54 |
|
50 | 55 | :param source: The source to reformat. |
51 | 56 | :param formate_filename: The name of the file being formatted. |
| 57 | + :param formate_global_config: The global configuration dictionary. Optional. |
52 | 58 | :param \*\*kwargs: |
53 | 59 |
|
54 | 60 | :returns: The reformatted source. |
55 | 61 | """ |
56 | 62 |
|
| 63 | + if "line_width" not in kwargs and formate_global_config: |
| 64 | + if "line_length" in (formate_global_config or {}): |
| 65 | + kwargs["line_width"] = formate_global_config["line_length"] |
| 66 | + |
| 67 | + if "use_tabs" not in kwargs and formate_global_config: |
| 68 | + if "indent" in (formate_global_config or {}): |
| 69 | + indent_setting = formate_global_config["indent"] |
| 70 | + if indent_setting == '\t': |
| 71 | + kwargs["use_tabs"] = True |
| 72 | + else: |
| 73 | + kwargs["use_tabs"] = False |
| 74 | + if set(indent_setting) == {' '}: |
| 75 | + # All spaces |
| 76 | + kwargs.setdefault("indent_width", len(indent_setting)) |
| 77 | + |
57 | 78 | config = Configuration(**kwargs) |
58 | 79 |
|
59 | 80 | filename_p = PathPlus(formate_filename) |
|
0 commit comments