Skip to content

Commit 5d83c44

Browse files
committed
Ensure use_tabs/indent and column_limit/line_length options are passed through to yapf when using separate config file.
1 parent 9c5496f commit 5d83c44

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

formate/__init__.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,32 +188,41 @@ def yapf_hook(source: str, formate_global_config: Optional[Mapping] = None, **kw
188188
# 3rd party
189189
from yapf.yapflib.yapf_api import FormatCode # type: ignore[import-untyped]
190190

191-
if "yapf_style" in kwargs:
192-
# yapf_style may be a filename or the name of a style
193-
# If `yapf_style` is a filename (or the name of a style, as opposed to a path), look in CWD and parent directories
194-
yapf_style = _find_from_parents(PathPlus(kwargs["yapf_style"]))
191+
with TemporaryPathPlus() as tmpdir:
192+
config_file = tmpdir / ".style.yapf"
193+
config = ConfigParser()
195194

196-
return FormatCode(source, style_config=str(yapf_style))[0]
195+
if "yapf_style" in kwargs:
196+
# yapf_style may be a filename or the name of a style
197+
# If `yapf_style` is a filename (or the name of a style, as opposed to a path), look in CWD and parent directories
198+
yapf_style = _find_from_parents(PathPlus(kwargs["yapf_style"]))
197199

198-
else:
199-
if "use_tabs" not in kwargs and formate_global_config:
200-
if "indent" in (formate_global_config or {}):
201-
kwargs["use_tabs"] = formate_global_config["indent"] == TAB
200+
with yapf_style.open() as fp:
201+
config.read_file(fp)
202202

203-
if "column_limit" not in kwargs and formate_global_config:
204-
if "line_length" in (formate_global_config or {}):
205-
kwargs["column_limit"] = formate_global_config["line_length"]
203+
if "use_tabs" not in config["style"] and formate_global_config:
204+
if "indent" in (formate_global_config or {}):
205+
config["style"]["use_tabs"] = str(formate_global_config["indent"] == TAB)
206+
207+
if "column_limit" not in config["style"] and formate_global_config:
208+
if "line_length" in (formate_global_config or {}):
209+
config["style"]["column_limit"] = str(formate_global_config["line_length"])
210+
211+
else:
212+
if "use_tabs" not in kwargs and formate_global_config:
213+
if "indent" in (formate_global_config or {}):
214+
kwargs["use_tabs"] = formate_global_config["indent"] == TAB
206215

207-
with TemporaryPathPlus() as tmpdir:
208-
config_file = tmpdir / ".style.yapf"
216+
if "column_limit" not in kwargs and formate_global_config:
217+
if "line_length" in (formate_global_config or {}):
218+
kwargs["column_limit"] = formate_global_config["line_length"]
209219

210-
config = ConfigParser()
211220
config.read_dict({"style": kwargs})
212221

213-
with config_file.open('w') as fp:
214-
config.write(fp)
222+
with config_file.open('w') as fp:
223+
config.write(fp)
215224

216-
return FormatCode(source, style_config=str(config_file))[0]
225+
return FormatCode(source, style_config=str(config_file))[0]
217226

218227

219228
class Reformatter:

0 commit comments

Comments
 (0)