@@ -511,7 +511,7 @@ def __init__(self, parent=None):
511
511
self .format_on_save = False
512
512
self .format_eventloop = QEventLoop (None )
513
513
self .format_timer = QTimer (self )
514
- self .__cursor_position_before_format = 0
514
+ self .__line_number_before_format = 0
515
515
516
516
# Mouse tracking
517
517
self .setMouseTracking (True )
@@ -1781,10 +1781,6 @@ def handle_go_to_definition(self, position):
1781
1781
# -------------------------------------------------------------------------
1782
1782
def format_document_or_range (self ):
1783
1783
"""Format current document or selected text."""
1784
- # Save current cursor position to restore it after the current text has
1785
- # been replaced by the auto-formatted one.
1786
- self .__cursor_position_before_format = self .textCursor ().position ()
1787
-
1788
1784
if self .has_selected_text () and self .range_formatting_enabled :
1789
1785
self .format_document_range ()
1790
1786
else :
@@ -1793,6 +1789,8 @@ def format_document_or_range(self):
1793
1789
@schedule_request (method = CompletionRequestTypes .DOCUMENT_FORMATTING )
1794
1790
def format_document (self ):
1795
1791
"""Format current document."""
1792
+ self .__line_number_before_format = self .textCursor ().blockNumber ()
1793
+
1796
1794
if not self .formatting_enabled :
1797
1795
return
1798
1796
if self .formatting_in_progress :
@@ -1826,6 +1824,8 @@ def format_document(self):
1826
1824
@schedule_request (method = CompletionRequestTypes .DOCUMENT_RANGE_FORMATTING )
1827
1825
def format_document_range (self ):
1828
1826
"""Format selected text."""
1827
+ self .__line_number_before_format = self .textCursor ().blockNumber ()
1828
+
1829
1829
if not self .range_formatting_enabled or not self .has_selected_text ():
1830
1830
return
1831
1831
if self .formatting_in_progress :
@@ -1984,18 +1984,22 @@ def _apply_document_edits(self, edits):
1984
1984
# Insert formatted text in place of the previous one
1985
1985
cursor .insertText (merged_text )
1986
1986
1987
- # Don't run the lines below when testing because they give
1988
- # segfaults.
1989
- if not running_under_pytest ():
1990
- # Restore previous cursor position and center it.
1991
- # Fixes spyder-ide/spyder#19958
1992
- cursor .setPosition (self .__cursor_position_before_format )
1993
- self .setTextCursor (cursor )
1994
- self .centerCursor ()
1995
-
1996
1987
# End text insertion
1997
1988
cursor .endEditBlock ()
1998
1989
1990
+ # Restore previous cursor line and center it.
1991
+ # Fixes spyder-ide/spyder#19958
1992
+ if self .__line_number_before_format < self .blockCount ():
1993
+ self .moveCursor (QTextCursor .Start )
1994
+ cursor = self .textCursor ()
1995
+ cursor .movePosition (
1996
+ QTextCursor .Down ,
1997
+ QTextCursor .MoveAnchor ,
1998
+ self .__line_number_before_format
1999
+ )
2000
+ self .setTextCursor (cursor )
2001
+ self .centerCursor ()
2002
+
1999
2003
# ---- LSP: Code folding
2000
2004
# -------------------------------------------------------------------------
2001
2005
def compute_whitespace (self , line ):
0 commit comments