@@ -1937,29 +1937,32 @@ def indent(buffer: Buffer, from_row: int, to_row: int, count: int = 1) -> None:
19371937 Indent text of a :class:`.Buffer` object.
19381938 """
19391939 current_row = buffer .document .cursor_position_row
1940+ current_col = buffer .document .cursor_position_col
19401941 line_range = range (from_row , to_row )
19411942
19421943 # Apply transformation.
1943- new_text = buffer .transform_lines (line_range , lambda l : " " * count + l )
1944+ indent_content = " " * count
1945+ new_text = buffer .transform_lines (line_range , lambda l : indent_content + l )
19441946 buffer .document = Document (
19451947 new_text , Document (new_text ).translate_row_col_to_index (current_row , 0 )
19461948 )
19471949
1948- # Go to the start of the line.
1949- buffer .cursor_position += buffer .document .get_start_of_line_position (
1950- after_whitespace = True
1951- )
1950+ # Place cursor in the same position in text after indenting
1951+ buffer .cursor_position += current_col + len (indent_content )
19521952
19531953
19541954def unindent (buffer : Buffer , from_row : int , to_row : int , count : int = 1 ) -> None :
19551955 """
19561956 Unindent text of a :class:`.Buffer` object.
19571957 """
19581958 current_row = buffer .document .cursor_position_row
1959+ current_col = buffer .document .cursor_position_col
19591960 line_range = range (from_row , to_row )
19601961
1962+ indent_content = " " * count
1963+
19611964 def transform (text : str ) -> str :
1962- remove = " " * count
1965+ remove = indent_content
19631966 if text .startswith (remove ):
19641967 return text [len (remove ) :]
19651968 else :
@@ -1971,10 +1974,8 @@ def transform(text: str) -> str:
19711974 new_text , Document (new_text ).translate_row_col_to_index (current_row , 0 )
19721975 )
19731976
1974- # Go to the start of the line.
1975- buffer .cursor_position += buffer .document .get_start_of_line_position (
1976- after_whitespace = True
1977- )
1977+ # Place cursor in the same position in text after dedent
1978+ buffer .cursor_position += current_col - len (indent_content )
19781979
19791980
19801981def reshape_text (buffer : Buffer , from_row : int , to_row : int ) -> None :
0 commit comments