@@ -526,7 +526,7 @@ def __init__(self, parent=None):
526
526
self .format_on_save = False
527
527
self .format_eventloop = QEventLoop (None )
528
528
self .format_timer = QTimer (self )
529
- self .__cursor_position_before_format = 0
529
+ self .__line_number_before_format = 0
530
530
531
531
# Mouse tracking
532
532
self .setMouseTracking (True )
@@ -1804,10 +1804,6 @@ def handle_go_to_definition(self, position):
1804
1804
# -------------------------------------------------------------------------
1805
1805
def format_document_or_range (self ):
1806
1806
"""Format current document or selected text."""
1807
- # Save current cursor position to restore it after the current text has
1808
- # been replaced by the auto-formatted one.
1809
- self .__cursor_position_before_format = self .textCursor ().position ()
1810
-
1811
1807
if self .has_selected_text () and self .range_formatting_enabled :
1812
1808
self .format_document_range ()
1813
1809
else :
@@ -1816,6 +1812,8 @@ def format_document_or_range(self):
1816
1812
@schedule_request (method = CompletionRequestTypes .DOCUMENT_FORMATTING )
1817
1813
def format_document (self ):
1818
1814
"""Format current document."""
1815
+ self .__line_number_before_format = self .textCursor ().blockNumber ()
1816
+
1819
1817
if not self .formatting_enabled :
1820
1818
return
1821
1819
if self .formatting_in_progress :
@@ -1849,6 +1847,8 @@ def format_document(self):
1849
1847
@schedule_request (method = CompletionRequestTypes .DOCUMENT_RANGE_FORMATTING )
1850
1848
def format_document_range (self ):
1851
1849
"""Format selected text."""
1850
+ self .__line_number_before_format = self .textCursor ().blockNumber ()
1851
+
1852
1852
if not self .range_formatting_enabled or not self .has_selected_text ():
1853
1853
return
1854
1854
if self .formatting_in_progress :
@@ -2007,18 +2007,22 @@ def _apply_document_edits(self, edits):
2007
2007
# Insert formatted text in place of the previous one
2008
2008
cursor .insertText (merged_text )
2009
2009
2010
- # Don't run the lines below when testing because they give
2011
- # segfaults.
2012
- if not running_under_pytest ():
2013
- # Restore previous cursor position and center it.
2014
- # Fixes spyder-ide/spyder#19958
2015
- cursor .setPosition (self .__cursor_position_before_format )
2016
- self .setTextCursor (cursor )
2017
- self .centerCursor ()
2018
-
2019
2010
# End text insertion
2020
2011
cursor .endEditBlock ()
2021
2012
2013
+ # Restore previous cursor line and center it.
2014
+ # Fixes spyder-ide/spyder#19958
2015
+ if self .__line_number_before_format < self .blockCount ():
2016
+ self .moveCursor (QTextCursor .Start )
2017
+ cursor = self .textCursor ()
2018
+ cursor .movePosition (
2019
+ QTextCursor .Down ,
2020
+ QTextCursor .MoveAnchor ,
2021
+ self .__line_number_before_format
2022
+ )
2023
+ self .setTextCursor (cursor )
2024
+ self .centerCursor ()
2025
+
2022
2026
# ---- LSP: Code folding
2023
2027
# -------------------------------------------------------------------------
2024
2028
def compute_whitespace (self , line ):
0 commit comments