From d00937c6eb0716be64b4ac3b0ead223ae590d5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sun, 24 Oct 2021 23:15:22 +0200 Subject: [PATCH] When reporting problematic blocks, focus on the relevant lines. --- org_rw/org_rw.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 2e7c2f4..d715151 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -12,6 +12,8 @@ from typing import Generator, List, Tuple, Union from . import dom +DEBUG_DIFF_CONTEXT = 10 + BASE_ENVIRONMENT = { "org-footnote-section": "Footnotes", "org-options-keywords": ( @@ -1880,7 +1882,24 @@ def loads(s, environment=BASE_ENVIRONMENT, extra_cautious=True): ) ) - sys.stderr.writelines(diff) + context_start = None + context_last_line = None + for i, line in enumerate(diff): + if not line.startswith(" "): + if context_start is None: + context_start = i + context_last_line = i + elif context_start: + if i > (context_last_line + DEBUG_DIFF_CONTEXT): + start = max(0, context_start - DEBUG_DIFF_CONTEXT) + end = min(len(diff), context_last_line + DEBUG_DIFF_CONTEXT) + print( + "## Lines {} to {}".format(start + 1, end + 1), + file=sys.stderr, + ) + sys.stderr.writelines(diff[start:end]) + context_start = None + context_last_line = None # print("---\n" + after_dump + "\n---") raise Exception("Difference found between existing version and dumped")