Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9232a26

Browse filesBrowse files
committed
Correctly handle variable indentation for the body of the code block.
1 parent 91e7afd commit 9232a26
Copy full SHA for 9232a26

File tree

1 file changed

+3
-5
lines changed
Filter options

1 file changed

+3
-5
lines changed

‎snippet_fmt/__init__.py

Copy file name to clipboardExpand all lines: snippet_fmt/__init__.py
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858

5959
__all__ = ["CodeBlockError", "RSTReformatter", "reformat_file"]
6060

61-
INDENT_RE = re.compile("^[ \t]+(?=[^ ])", re.MULTILINE)
6261
TRAILING_NL_RE = re.compile(r'\n+\Z', re.MULTILINE)
6362

6463

@@ -127,10 +126,10 @@ def run(self) -> bool:
127126
rf'(?P<before>'
128127
rf'^(?P<indent>[ \t]*)\.\.[ \t]*('
129128
rf'({directives})::\s*(?P<lang>[A-Za-z0-9-_]+)?)\n'
130-
rf'((?P=indent)[ \t]+:.*\n)*'
129+
rf'((?P=indent)[ \t]+:.*\n)*' # Limitation: should be `(?P=body_indent)` rather than `[ \t]+`
131130
rf'\n*'
132131
rf')'
133-
rf'(?P<code>(^((?P=indent)[ \t]+.*)?\n)+)',
132+
rf'(?P<code>^((?P=indent)(?P<body_indent>[ \t]+).*)?\n(^((?P=indent)(?P=body_indent).*)?\n)+)',
134133
re.MULTILINE,
135134
)
136135

@@ -159,7 +158,6 @@ def process_match(self, match: Match[str]) -> str:
159158
lang_config = {}
160159
formatter = noformat
161160

162-
min_indent = min(INDENT_RE.findall(match["code"]))
163161
trailing_ws_match = TRAILING_NL_RE.search(match["code"])
164162
assert trailing_ws_match
165163
trailing_ws = trailing_ws_match.group()
@@ -169,7 +167,7 @@ def process_match(self, match: Match[str]) -> str:
169167
with syntaxerror_for_file(self.filename):
170168
code = formatter(code, **lang_config)
171169

172-
code = textwrap.indent(code, min_indent)
170+
code = textwrap.indent(code, match["body_indent"])
173171
return f'{match["before"]}{code.rstrip()}{trailing_ws}'
174172

175173
def get_diff(self) -> str:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.