Skip to content

Commit f1038d0

Browse files
committed
Set "line_width" and "use_tabs" from formate's global configuration.
1 parent c216295 commit f1038d0

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

python/formate_js/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
# OR OTHER DEALINGS IN THE SOFTWARE.
2727
#
2828

29+
# stdlib
30+
from typing import Mapping, Optional
31+
2932
# 3rd party
3033
from domdf_python_tools.paths import PathPlus
3134
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
3336

3437
# this package
3538
from ._formate_js import Configuration, ConfigurationBuilder, FormatTextOptions, format_text
@@ -39,21 +42,39 @@
3942

4043
@formats_filetypes(".ts", ".js")
4144
@wants_filename
45+
@wants_global_config
4246
def javascript_hook(
4347
source: str,
4448
formate_filename: PathLike,
49+
formate_global_config: Optional[Mapping] = None,
4550
**kwargs,
4651
) -> str:
4752
r"""
4853
Reformat JavaScript and TypeScript with dprint.
4954
5055
:param source: The source to reformat.
5156
:param formate_filename: The name of the file being formatted.
57+
:param formate_global_config: The global configuration dictionary. Optional.
5258
:param \*\*kwargs:
5359
5460
:returns: The reformatted source.
5561
"""
5662

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+
5778
config = Configuration(**kwargs)
5879

5980
filename_p = PathPlus(formate_filename)

0 commit comments

Comments
 (0)