@@ -1937,29 +1937,32 @@ def indent(buffer: Buffer, from_row: int, to_row: int, count: int = 1) -> None:
1937
1937
Indent text of a :class:`.Buffer` object.
1938
1938
"""
1939
1939
current_row = buffer .document .cursor_position_row
1940
+ current_col = buffer .document .cursor_position_col
1940
1941
line_range = range (from_row , to_row )
1941
1942
1942
1943
# 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 )
1944
1946
buffer .document = Document (
1945
1947
new_text , Document (new_text ).translate_row_col_to_index (current_row , 0 )
1946
1948
)
1947
1949
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 )
1952
1952
1953
1953
1954
1954
def unindent (buffer : Buffer , from_row : int , to_row : int , count : int = 1 ) -> None :
1955
1955
"""
1956
1956
Unindent text of a :class:`.Buffer` object.
1957
1957
"""
1958
1958
current_row = buffer .document .cursor_position_row
1959
+ current_col = buffer .document .cursor_position_col
1959
1960
line_range = range (from_row , to_row )
1960
1961
1962
+ indent_content = " " * count
1963
+
1961
1964
def transform (text : str ) -> str :
1962
- remove = " " * count
1965
+ remove = indent_content
1963
1966
if text .startswith (remove ):
1964
1967
return text [len (remove ) :]
1965
1968
else :
@@ -1971,10 +1974,8 @@ def transform(text: str) -> str:
1971
1974
new_text , Document (new_text ).translate_row_col_to_index (current_row , 0 )
1972
1975
)
1973
1976
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 )
1978
1979
1979
1980
1980
1981
def reshape_text (buffer : Buffer , from_row : int , to_row : int ) -> None :
0 commit comments