Skip to content

Commit 1ae5448

Browse files
committed
Apply more granular changes by using # fmt: skip
1 parent e0d3069 commit 1ae5448

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/darker/__main__.py

+52-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from pathlib import Path
1111
from typing import Collection, Generator, List, Tuple
1212

13+
from black.comments import FMT_SKIP
14+
1315
from darker.black_diff import (
1416
BlackConfig,
1517
filter_python_files,
@@ -191,15 +193,63 @@ def _blacken_single_file( # pylint: disable=too-many-arguments,too-many-locals
191193
"""
192194
absolute_path_in_rev2 = root / relative_path_in_rev2
193195

196+
# find the fmt_skip comment that won't conflict with the code
197+
fmt_skip = None
198+
for comment in FMT_SKIP:
199+
if comment not in rev2_isorted.string:
200+
fmt_skip = comment
201+
if fmt_skip is None:
202+
logger.warning(
203+
"Detected the mixed styles of `# fmt: skip` comments in "
204+
f"{absolute_path_in_rev2}, "
205+
"please use one style to get the best result"
206+
)
207+
# decorate lines with fmt_skip
208+
lines = []
209+
modified_line_nums = edited_linenums_differ.revision_vs_lines(
210+
relative_path_in_repo, rev2_isorted, 0
211+
)
212+
for i, line in enumerate(rev2_isorted.lines):
213+
line_num = i + 1
214+
if (
215+
fmt_skip is not None
216+
and line_num not in modified_line_nums
217+
and not any(f in line for f in FMT_SKIP)
218+
):
219+
line = f"{line} {fmt_skip}"
220+
lines.append(line)
221+
rev2_isorted_decorated = TextDocument.from_lines(
222+
lines,
223+
encoding=rev2_isorted.encoding,
224+
newline=rev2_isorted.newline,
225+
mtime=rev2_isorted.mtime,
226+
)
227+
194228
# 4. run black
195-
formatted = run_black(rev2_isorted, black_config)
229+
formatted = run_black(rev2_isorted_decorated, black_config)
196230
logger.debug(
197231
"Read %s lines from edited file %s",
198-
len(rev2_isorted.lines),
232+
len(rev2_isorted_decorated.lines),
199233
absolute_path_in_rev2,
200234
)
201235
logger.debug("Black reformat resulted in %s lines", len(formatted.lines))
202236

237+
# redecorate lines without fmt_skip
238+
lines = []
239+
for line in formatted.lines:
240+
if fmt_skip is not None:
241+
if line.endswith(f" {fmt_skip}"):
242+
line = line[: -len(f" {fmt_skip}")]
243+
elif line == fmt_skip:
244+
line = ""
245+
lines.append(line)
246+
formatted = TextDocument.from_lines(
247+
lines,
248+
encoding=formatted.encoding,
249+
newline=formatted.newline,
250+
mtime=formatted.mtime,
251+
)
252+
203253
# 5. get the diff between the edited and reformatted file
204254
# 6. convert the diff into chunks
205255
black_chunks = diff_chunks(rev2_isorted, formatted)

0 commit comments

Comments
 (0)