Skip to content

Commit b4b6bf2

Browse files
committed
Fix indentation level handling
1 parent f5fc2b0 commit b4b6bf2

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/cedarscript_editor/cedarscript_editor.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,12 @@ def _update_command(self, cmd: UpdateCommand):
185185
case str() | [str(), *_] | (str(), *_):
186186
pass
187187
case (region, relindent_level):
188-
dest_indent_count = search_range.indent
189188
content_range = restrict_search_range_for_marker(
190189
region, action, lines, RangeSpec.EMPTY, identifier_finder
191190
)
192191
content = content_range.read(lines)
193-
count = dest_indent_count + (relindent_level or 0)
194-
content = IndentationInfo.from_content(content).shift_indentation(
195-
content, count
192+
content = IndentationInfo.from_content(lines).update_min_indent_level(content).shift_indentation(
193+
content, search_range.indent, relindent_level
196194
)
197195
content = (region, content)
198196
case _:
@@ -201,13 +199,10 @@ def _update_command(self, cmd: UpdateCommand):
201199
# dest_range = restrict_search_range_for_marker(
202200
# region, action, lines, RangeSpec.EMPTY, identifier_finder
203201
# )
204-
# TODO Make sure 3 lines above are not needed
205-
dest_range = search_range
206-
dest_indent_count = dest_range.indent
202+
# TODO Are the 3 lines above needed?
207203
content = move_src_range.read(lines)
208-
shift_count = dest_indent_count + (relindent_level or 0) # TODO Fix
209-
content = IndentationInfo.from_content(content).shift_indentation(
210-
content, shift_count
204+
content = IndentationInfo.from_content(lines).update_min_indent_level(content).shift_indentation(
205+
content, search_range.indent, relindent_level
211206
)
212207
case DeleteClause():
213208
pass

src/text_manipulation/indentation_kit.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ def from_content(cls, content: str | Sequence[str]) -> 'IndentationInfo':
174174

175175
return cls(char_count, dominant_char, min_indent_level, consistency, message)
176176

177+
def update_min_indent_level(self, content: str | Sequence[str]) -> 'IndentationInfo':
178+
return self._replace(min_indent_level=IndentationInfo.from_content(content).min_indent_level)
179+
177180
def level_difference(self, base_indentation_count: int) -> int:
178181
"""
179182
Calculate the difference in indentation levels.
@@ -211,7 +214,9 @@ def level_to_chars(self, level: int) -> str:
211214
return level * self.char_count * self.char
212215

213216
# TODO Revise
214-
def shift_indentation(self, lines: Sequence[str], target_base_indentation_count: int) -> list[str]:
217+
def shift_indentation(
218+
self, lines: Sequence[str], target_base_indentation_count: int, relindent_level: int | None
219+
) -> list[str]:
215220
"""
216221
Shift the indentation of a sequence of lines based on a target base indentation count.
217222
@@ -222,6 +227,7 @@ def shift_indentation(self, lines: Sequence[str], target_base_indentation_count:
222227
Args:
223228
lines (Sequence[str]): A sequence of strings representing the lines to be adjusted.
224229
target_base_indentation_count (int): The target base indentation count to adjust to.
230+
relindent_level (int|None):
225231
226232
Returns:
227233
list[str]: A new list of strings with adjusted indentation.
@@ -238,6 +244,7 @@ def shift_indentation(self, lines: Sequence[str], target_base_indentation_count:
238244
>>> info.shift_indentation(lines, 8)
239245
[' def example():', ' print('Hello')']
240246
"""
247+
target_base_indentation_count += self.char_count * (relindent_level or 0)
241248
raw_line_adjuster = self._shift_indentation_fun(target_base_indentation_count)
242249
# Return the transformed lines
243250
return [raw_line_adjuster(line) for line in lines]

0 commit comments

Comments
 (0)